浏览代码

Added CreateClassCacheCommandTest

Andrej Hudec 11 年之前
父节点
当前提交
aef6d2ba89

+ 126 - 0
Tests/Command/CreateClassCacheCommandTest.php

@@ -0,0 +1,126 @@
+<?php
+
+/*
+ * This file is part of the Sonata 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 Symfony\Component\Console\Application;
+use Symfony\Component\Console\Tester\CommandTester;
+use Sonata\AdminBundle\Command\CreateClassCacheCommand;
+use Symfony\Component\HttpKernel\KernelInterface;
+
+/**
+ * @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->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
+        $kernel = $this->getMock('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 null;
+                }));
+
+        $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->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->assertEquals(sprintf('The file %s/classes.map does not exist', $this->tempDirectory), $e->getMessage());
+
+            return;
+        }
+
+        $this->fail('An expected exception has not been raised.');
+    }
+}

+ 29 - 0
Tests/Fixtures/Command/classes.php

@@ -0,0 +1,29 @@
+<?php 
+namespace Sonata\AdminBundle\Tests\Fixtures\Controller
+{
+abstract class AbstractFooAdminController
+{
+public function bazAction()
+{
+}
+}
+}
+namespace Sonata\AdminBundle\Tests\Fixtures\Controller
+{
+use Sonata\AdminBundle\Tests\Fixtures\Controller\AbstractFooAdminController;
+class FooAdminController extends AbstractFooAdminController
+{
+public function fooAction($baz)
+{
+}
+}
+}
+namespace Sonata\AdminBundle\Tests\Fixtures\Controller
+{
+class BarAdminController
+{
+public function barAction()
+{
+}
+}
+}

+ 16 - 0
Tests/Fixtures/Controller/AbstractFooAdminController.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace Sonata\AdminBundle\Tests\Fixtures\Controller;
+
+/**
+ * Some documentation about abstract controller
+ */
+abstract class AbstractFooAdminController
+{
+    /**
+     * Bar action
+     */
+    public function bazAction()
+    {
+    }
+}

+ 16 - 0
Tests/Fixtures/Controller/BarAdminController.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace Sonata\AdminBundle\Tests\Fixtures\Controller;
+
+/**
+ * Some documentation about Bar controller
+ */
+class BarAdminController
+{
+    /**
+     * Bar action
+     */
+    public function barAction()
+    {
+    }
+}

+ 18 - 0
Tests/Fixtures/Controller/FooAdminController.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace Sonata\AdminBundle\Tests\Fixtures\Controller;
+
+use Sonata\AdminBundle\Tests\Fixtures\Controller\AbstractFooAdminController;
+
+/**
+ * Some documentation about Foo controller
+ */
+class FooAdminController extends AbstractFooAdminController
+{
+    /**
+     * Foo action
+     */
+    public function fooAction($baz)
+    {
+    }
+}

+ 1 - 0
composer.json

@@ -25,6 +25,7 @@
         "symfony/config": "~2.2",
         "symfony/console": "~2.2",
         "symfony/twig-bridge": "~2.2",
+        "symfony/class-loader":"~2.2",
         "sensio/generator-bundle": "~2.2",
         "twig/twig": "~1.12",
         "knplabs/knp-menu-bundle": "~1.1",