views:

299

answers:

1

I want to test my controller which works on subdomain www.username.domain.com

The problem is when I dispatch in ControllerTestCase it throws Zend_Controller_Dispatcher_Exception


routes.php:

$userRouter = new Zend_Controller_Router_Route_Hostname(':user.domain.com'));

$router->addRoute('user', $userRouter->chain(new Zend_Controller_Router_Route('',
       array('controller' => 'user'))));


UserControllerTest:

require_once 'AbstarctControllerTestCase.php';

class UserControllerTest extends AbstarctControllerTestCase
{
    public function setUp()
    {
     $this->cleardb();
        parent::setUp();
    }

    public function testRoute()
    {
     $this->dispatch('www.username.domain.com');
  $this->assertController('user');
    }
}


AbstarctControllerTestCase:

abstract class AbstarctControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    public function setUp()
    {
     $this->bootstrap = array($this, 'appBootstrap');
     parent::setUp();
    }

    public function appBootstrap()
    {
     chdir(dirname(dirname(dirname(dirname(__FILE__)))));
     require 'application/test/controllerunit/routes.php';
     Zend_Session::start();
    }
(...)
}

Result:

PHPUnit 3.3.17 by Sebastian Bergmann.

F

Time: 1 second

There was 1 failure:

1) testRoute(UserControllerTest)
Failed asserting last controller used was "user"

When I dispatch normal URI like /login it work well but the problem is dispatching URLs with hostnames.

Any ideas? Thank you all.

A: 

Did you try setting the $_SERVER variable in setup?

e.g.

$_SERVER['SERVER_NAME'] = 'www.username.domain.com';

and then call dispatch as per usual.

See - http://php.net/manual/en/reserved.variables.server.php

Nomolos