Issues

ZF-11680: Zend_Test_PHPUnit_ControllerTestCase :: dispatch() doesn't accept absolute URL's

Description

Please check the example, I think it self-explainable.


$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
    ':username.domain.com',
    array(
        'controller' => 'user',
        'action'     => 'info'
    )
);
 
$plainPathRoute = new Zend_Controller_Router_Route_Static('');
 
$router->addRoute('user', $hostnameRoute->chain($plainPathRoute));

// IndexControllerTest.php 
<?php

class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{

    public function setUp()
    {
        $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
        parent::setUp();
    }

    public function testIndexAction()
    {
        $this->dispatch('http://myname.domain.com');
        $this->assertController('user'); // Fail
    }
}

$this->dispatch($url) is actually call Zend_Controller_Request_HttpTestCase->setRequestUri($url) which is based on Zend_Controller_Request_Http which use parse_str function to parse $url argument. parse_str usually used to parse query string, but not full URL.
I think it's better to use some sort of regexp's and/or parse_url() method.

Thanks, and sorry for my bad english :)

Comments

I found a workaround, which, I think is not good idea but it works:

define $_SERVER['HTTP_HOST'] before ->dispatch() call.



Index: Controller/Request/HttpTestCase.php
===================================================================
--- Controller/Request/HttpTestCase.php (revision 24388)
+++ Controller/Request/HttpTestCase.php (working copy)
@@ -65,8 +65,60 @@
         'POST',
         'PUT',
     );
+    
+    /**
+     * Constructor
+     *
+     * If a $uri is passed, the object will attempt to populate itself using
+     * that information.
+     *
+     * @param string|Zend_Uri $uri
+     * @return void
+     * @throws Zend_Controller_Request_Exception when invalid URI passed
+     */
+    public function __construct($uri = null)
+    {
+        if (null !== $uri) {
+            if (!$uri instanceof Zend_Uri) {
+                $uri = Zend_Uri::factory($uri);
+            }
+            if ($uri->valid()) {
+                $path  = $uri->getPath();
+                $query = $uri->getQuery();
+                if (!empty($query)) {
+                    $path .= '?' . $query;
+                }
 
+                $this->setServer('HTTP_HOST', $uri->getHost());
+                $this->setRequestUri($path);
+            } else {
+                require_once 'Zend/Controller/Request/Exception.php';
+                throw new Zend_Controller_Request_Exception('Invalid URI provided to constructor');
+            }
+        } else {
+            $this->setRequestUri();
+        }
+    }
+
     /**
+     * Set stub for a the $_SERVER superglobal
+     *
+     * @param string|array $key
+     * @param mixed $value
+     * @return Zend_Controller_Request_HttpTestCase
+     */
+    public function setServer($key, $value = null)
+    {
+        if (is_array($key)) {
+            $_SERVER = array_merge($_SERVER, $key);
+        } else {
+            $_SERVER[$key] = $value;
+        }
+
+        return $this;
+    }
+
+    /**
      * Clear GET values
      *
      * @return Zend_Controller_Request_HttpTestCase
Index: Test/PHPUnit/ControllerTestCase.php
===================================================================
--- Test/PHPUnit/ControllerTestCase.php (revision 24388)
+++ Test/PHPUnit/ControllerTestCase.php (working copy)
@@ -191,7 +191,11 @@
 
         $request    = $this->getRequest();
         if (null !== $url) {
-            $request->setRequestUri($url);
+            if (0 === strpos($uri, 'http')) {
+                $request = new Zend_Controller_Request_HttpTestCase($uri);
+            }  else {
+                $request->setRequestUri($url);
+            }
         }
         $request->setPathInfo(null);

Maks -- before your code contribution can be used in Zend Framework, you need to sign a CLA:

<a rel="nofollow" href="http://framework.zend.com/cla">http://framework.zend.com/cla</a>

Once you've signed and returned it (email: cla@zend.com), comment on this issue and we'll have a look at your suggestion.

Thanks!

I've signed it and sent it by fax couple weeks ago.