Prechádzať zdrojové kódy

[WebBundle] added some black magic to remove the boiler plate code needed in end user functional tests

Fabien Potencier 15 rokov pred
rodič
commit
defa307d40

+ 1 - 1
src/Symfony/Foundation/Test/WebTestCase.php

@@ -40,7 +40,7 @@ abstract class WebTestCase extends \PHPUnit_Framework_TestCase
     /**
      * Creates a Kernel.
      *
-     * @return Symfony\Foundation\Kernel A Kernel instance
+     * @return Symfony\Components\HttpKernel\HttpKernelInterface A HttpKernelInterface instance
      */
     abstract protected function createKernel();
 }

+ 62 - 0
src/Symfony/Framework/WebBundle/Test/WebTestCase.php

@@ -0,0 +1,62 @@
+<?php
+
+namespace Symfony\Framework\WebBundle\Test;
+
+use Symfony\Foundation\Test\WebTestCase as BaseWebTestCase;
+use Symfony\Components\Finder\Finder;
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * WebTestCase is the base class for functional tests.
+ *
+ * @package    Symfony
+ * @subpackage Framework_WebBundle
+ * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+abstract class WebTestCase extends BaseWebTestCase
+{
+    /**
+     * Creates a Kernel.
+     *
+     * If you run tests with the PHPUnit CLI tool, everything will work as expected.
+     * If not, override this method in your test classes.
+     *
+     * @return Symfony\Components\HttpKernel\HttpKernelInterface A HttpKernelInterface instance
+     */
+    protected function createKernel()
+    {
+        // black magic below, you have been warned!
+        $dir = getcwd();
+        if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
+            throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
+        }
+
+        // find the --configuration flag from PHPUnit
+        $cli = implode(' ', $_SERVER['argv']);
+        if (preg_match('/\-\-configuration[= ]+([^ ]+)/', $cli, $matches)) {
+            $dir = $dir.'/'.dirname($matches[1]);
+        }
+
+        $finder = new Finder();
+        $finder->name('*Kernel.php')->in($dir);
+        if (!count($finder)) {
+            throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
+        }
+
+        $file = current(iterator_to_array($finder));
+        $class = $file->getBasename('.php');
+        unset($finder);
+
+        require_once $file;
+
+        return new $class('test', true);
+    }
+}