123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Tests\Command;
- use Sonata\AdminBundle\Command\CreateClassCacheCommand;
- use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
- use Symfony\Component\Console\Application;
- use Symfony\Component\Console\Tester\CommandTester;
- /**
- * @author Andrej Hudec <pulzarraider@gmail.com>
- */
- class CreateClassCacheCommandTest extends PHPUnit_Framework_TestCase
- {
- /**
- * @var string
- */
- private $tempDirectory;
- /**
- * @var Application
- */
- private $application;
- protected function setUp()
- {
- $tempFile = tempnam(sys_get_temp_dir(), 'sonata_');
- if (file_exists($tempFile)) {
- unlink($tempFile);
- }
- if (mkdir($tempFile)) {
- $this->tempDirectory = $tempFile;
- file_put_contents($this->tempDirectory.'/classes.map', '<?php return array(\'Sonata\\AdminBundle\\Tests\\Fixtures\\Controller\\FooAdminController\', \'Sonata\\AdminBundle\\Tests\\Fixtures\\Controller\\BarAdminController\',);');
- } else {
- $this->markTestSkipped(sprintf('Temp directory "%s" creation error.', $tempFile));
- }
- $this->application = new Application();
- $command = new CreateClassCacheCommand();
- $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $kernel = $this->createMock('Symfony\Component\HttpKernel\KernelInterface');
- $kernel->expects($this->any())
- ->method('getCacheDir')
- ->will($this->returnValue($this->tempDirectory));
- $kernel->expects($this->any())
- ->method('isDebug')
- ->will($this->returnValue(false));
- $container->expects($this->any())
- ->method('get')
- ->will($this->returnCallback(function ($id) use ($kernel) {
- if ($id == 'kernel') {
- return $kernel;
- }
- return;
- }));
- $command->setContainer($container);
- $this->application->add($command);
- }
- protected function tearDown()
- {
- if ($this->tempDirectory) {
- if (file_exists($this->tempDirectory.'/classes.map')) {
- unlink($this->tempDirectory.'/classes.map');
- }
- if (file_exists($this->tempDirectory.'/classes.php')) {
- unlink($this->tempDirectory.'/classes.php');
- }
- if (file_exists($this->tempDirectory) && is_dir($this->tempDirectory)) {
- rmdir($this->tempDirectory);
- }
- }
- }
- public function testExecute()
- {
- $this->markTestSkipped();
- $this->assertFileExists($this->tempDirectory.'/classes.map');
- $this->assertFileNotExists($this->tempDirectory.'/classes.php');
- $command = $this->application->find('cache:create-cache-class');
- $commandTester = new CommandTester($command);
- $commandTester->execute(array('command' => $command->getName()));
- $this->assertRegExp('@Writing cache file ...\s+done!@', $commandTester->getDisplay());
- $this->assertFileExists($this->tempDirectory.'/classes.php');
- $this->assertFileEquals(__DIR__.'/../Fixtures/Command/classes.php', $this->tempDirectory.'/classes.php');
- }
- public function testExecuteWithException()
- {
- $this->assertFileExists($this->tempDirectory.'/classes.map');
- unlink($this->tempDirectory.'/classes.map');
- try {
- $command = $this->application->find('cache:create-cache-class');
- $commandTester = new CommandTester($command);
- $commandTester->execute(array('command' => $command->getName()));
- } catch (\RuntimeException $e) {
- $this->assertSame(sprintf('The file %s/classes.map does not exist', $this->tempDirectory), $e->getMessage());
- return;
- }
- $this->fail('An expected exception has not been raised.');
- }
- }
|