CreateClassCacheCommandTest.php 4.1 KB

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