Procházet zdrojové kódy

ZF-7839: Allow using Zend_Application with Zend_Test_PHPUnit_ControllerTestCase

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18604 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew před 16 roky
rodič
revize
74a02b2a5b

+ 25 - 2
documentation/manual/en/module_specs/Zend_Test-PHPUnit-Bootstrapping.xml

@@ -20,7 +20,30 @@
     </para>
 
     <para>
-        First, you can set this property to point to a file. If you do
+        First, and probably most straight-forward, simply create a
+        <classname>Zend_Application</classname> instance as you would in your
+        <filename>index.php</filename>, and assign it to the <varname>$bootstrap</varname> property.
+        Typically, you will do this in your <methodname>setUp()</methodname> method; you will need
+        to call <methodname>parent::setUp()</methodname> when done:
+    </para>
+
+    <programlisting language="php"><![CDATA[
+class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
+{
+    public function setUp()
+    {
+        // Assign and instantiate in one step:
+        $this->bootstrap = new Zend_Application(
+            'testing', 
+            APPLICATION_PATH . '/configs/application.ini'
+        );
+        parent::setUp();
+    }
+}
+]]></programlisting>
+
+    <para>
+        Second, you can set this property to point to a file. If you do
         this, the file should <emphasis>not</emphasis> dispatch the front
         controller, but merely setup the front controller and any application
         specific needs.
@@ -36,7 +59,7 @@ class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
 ]]></programlisting>
 
     <para>
-        Second, you can provide a <acronym>PHP</acronym> callback to execute in order to bootstrap
+        Third, you can provide a <acronym>PHP</acronym> callback to execute in order to bootstrap
         your application. This method is seen in the <link
             linkend="zend.test.phpunit.loginexample">Login example</link>. If
         the callback is a function or static method, this could be set at the

+ 4 - 1
library/Zend/Test/PHPUnit/ControllerTestCase.php

@@ -142,7 +142,10 @@ abstract class Zend_Test_PHPUnit_ControllerTestCase extends PHPUnit_Framework_Te
     {
         $this->reset();
         if (null !== $this->bootstrap) {
-            if (is_callable($this->bootstrap)) {
+            if ($this->bootstrap instanceof Zend_Application) {
+                $this->bootstrap->bootstrap();
+                $this->_frontController = $this->bootstrap->getBootstrap()->getResource('frontcontroller');
+            } elseif (is_callable($this->bootstrap)) {
                 call_user_func($this->bootstrap);
             } elseif (is_string($this->bootstrap)) {
                 require_once 'Zend/Loader.php';

+ 21 - 0
tests/Zend/Test/PHPUnit/ControllerTestCaseTest.php

@@ -736,6 +736,27 @@ class Zend_Test_PHPUnit_ControllerTestCaseTest extends PHPUnit_Framework_TestCas
         $this->assertTrue(empty($_POST));
         $this->assertTrue(empty($_GET));
     }
+
+    /**
+     * @group ZF-7839
+     */
+    public function testTestCaseShouldAllowUsingApplicationObjectAsBootstrap()
+    {
+        require_once 'Zend/Application.php';
+        $application = new Zend_Application('testing', array(
+            'resources' => array(
+                'frontcontroller' => array(
+                    'controllerDirectory' => dirname(__FILE__) . '/_files/application/controllers',
+                ),
+            ),
+        ));
+        $this->testCase->bootstrap = $application;
+        $this->testCase->bootstrap();
+        $this->assertEquals(
+            $application->getBootstrap()->getResource('frontcontroller'),
+            $this->testCase->getFrontController()
+        );
+    }
 }
 
 // Concrete test case class for testing purposes