CreateClassCacheCommandTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Command;
  11. use Sonata\AdminBundle\Command\CreateClassCacheCommand;
  12. use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  13. use Symfony\Component\Console\Application;
  14. use Symfony\Component\Console\Tester\CommandTester;
  15. /**
  16. * @author Andrej Hudec <pulzarraider@gmail.com>
  17. */
  18. class CreateClassCacheCommandTest extends PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var string
  22. */
  23. private $tempDirectory;
  24. /**
  25. * @var Application
  26. */
  27. private $application;
  28. protected function setUp()
  29. {
  30. $tempFile = tempnam(sys_get_temp_dir(), 'sonata_');
  31. if (file_exists($tempFile)) {
  32. unlink($tempFile);
  33. }
  34. if (mkdir($tempFile)) {
  35. $this->tempDirectory = $tempFile;
  36. file_put_contents($this->tempDirectory.'/classes.map', '<?php return array(\'Sonata\\AdminBundle\\Tests\\Fixtures\\Controller\\FooAdminController\', \'Sonata\\AdminBundle\\Tests\\Fixtures\\Controller\\BarAdminController\',);');
  37. } else {
  38. $this->markTestSkipped(sprintf('Temp directory "%s" creation error.', $tempFile));
  39. }
  40. $this->application = new Application();
  41. $command = new CreateClassCacheCommand();
  42. $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
  43. $kernel = $this->createMock('Symfony\Component\HttpKernel\KernelInterface');
  44. $kernel->expects($this->any())
  45. ->method('getCacheDir')
  46. ->will($this->returnValue($this->tempDirectory));
  47. $kernel->expects($this->any())
  48. ->method('isDebug')
  49. ->will($this->returnValue(false));
  50. $container->expects($this->any())
  51. ->method('get')
  52. ->will($this->returnCallback(function ($id) use ($kernel) {
  53. if ($id == 'kernel') {
  54. return $kernel;
  55. }
  56. return;
  57. }));
  58. $command->setContainer($container);
  59. $this->application->add($command);
  60. }
  61. protected function tearDown()
  62. {
  63. if ($this->tempDirectory) {
  64. if (file_exists($this->tempDirectory.'/classes.map')) {
  65. unlink($this->tempDirectory.'/classes.map');
  66. }
  67. if (file_exists($this->tempDirectory.'/classes.php')) {
  68. unlink($this->tempDirectory.'/classes.php');
  69. }
  70. if (file_exists($this->tempDirectory) && is_dir($this->tempDirectory)) {
  71. rmdir($this->tempDirectory);
  72. }
  73. }
  74. }
  75. public function testExecute()
  76. {
  77. $this->markTestSkipped();
  78. $this->assertFileExists($this->tempDirectory.'/classes.map');
  79. $this->assertFileNotExists($this->tempDirectory.'/classes.php');
  80. $command = $this->application->find('cache:create-cache-class');
  81. $commandTester = new CommandTester($command);
  82. $commandTester->execute(array('command' => $command->getName()));
  83. $this->assertRegExp('@Writing cache file ...\s+done!@', $commandTester->getDisplay());
  84. $this->assertFileExists($this->tempDirectory.'/classes.php');
  85. $this->assertFileEquals(__DIR__.'/../Fixtures/Command/classes.php', $this->tempDirectory.'/classes.php');
  86. }
  87. public function testExecuteWithException()
  88. {
  89. $this->assertFileExists($this->tempDirectory.'/classes.map');
  90. unlink($this->tempDirectory.'/classes.map');
  91. try {
  92. $command = $this->application->find('cache:create-cache-class');
  93. $commandTester = new CommandTester($command);
  94. $commandTester->execute(array('command' => $command->getName()));
  95. } catch (\RuntimeException $e) {
  96. $this->assertSame(sprintf('The file %s/classes.map does not exist', $this->tempDirectory), $e->getMessage());
  97. return;
  98. }
  99. $this->fail('An expected exception has not been raised.');
  100. }
  101. }