zendframework / zend-test

Test component from Zend Framework
BSD 3-Clause "New" or "Revised" License
18 stars 38 forks source link

zf2 phpunit scheme routes #5

Open synatix opened 8 years ago

synatix commented 8 years ago

We're using ZF2 for an web-application and want to test it with phpunit (v4.8.9). Within this application we've got a scheme-route, to be able to switch between http/https-context (Doesnt matter why...). The route looks like this:

'http' => array(
    'type' => 'Scheme',
    'options' => array(
        'scheme' => 'http',
        'defaults' => array(
            'http' => true
        )
    ),
    'child_routes' => array(
        'search' => array(
            'type'    => 'segment',
            'options' => array(
                'route' => '/search[/:keyword[/:page]]',
                'constraints' => array(
                    'page' => '[1-9]+[0-9]*'
                ),
                'defaults' => array(
                    'controller'    => SearchController::class,
                    'action'        => 'search',
                ),
            ),
        ),
    ),
),
'https' => array(
    'type' => 'Scheme',
    'options' => array(
        'scheme' => 'https',
        'defaults' => array(
            'https' => true
        )
    ),
    'child_routes' => array(
        'search' => array(
            'type'    => 'segment',
            'options' => array(
                'route' => '/search[/:keyword[/:page]]',
                'constraints' => array(
                    'page' => '[1-9]+[0-9]*'
                ),
                'defaults' => array(
                    'controller'    => SearchController::class,
                    'action'        => 'search',
                ),
            ),
        ),
    ),
),

The class of the test looks like this:

class SearchControllerTest extends SynHttpControllerTestCase
{
public function setUp()
{
    $this->setApplicationConfig($this->getCurrentBootstrap()->getApplicationConfig());
    parent::setUp();
    $this->getApplicationServiceLocator()->setAllowOverride(true);
}

public function testSearchActionCanBeAccessed()
{
    $this->dispatch('/search');
    $this->assertResponseStatusCode(200);
    $this->assertControllerName(SearchController::class);
    $this->assertControllerClass('SearchController');
    $this->assertActionName('search');
    $this->assertMatchedRouteName('search');
}
}

FYI: The "SynHttpControllerTestCase" is an extension from the original AbstractHttpControllerTestCase which comes with Zend-Test. It's modified to get the right bootstrap-file in our tests.

If we're running the tests, this error appears:

Fatal error: Call to a member function getParam() on null in C:\xampp\htdocs\git\xxx\vendor\zendframework\zend-test\src\PHPUnit\Controller\AbstractControllerTestCase.php on line 563

We looked into the AbstractControllerTestCase and this line is throwing the error:

$routeMatch = $this->getApplication()->getMvcEvent()->getRouteMatch();

Because the $routeMatch-Object is empty. We've some other controllers and tests within our application, they're all fine and not affected from this problem, cause the routes to these controllers arent scheme-routes.

Do you have any ideas how to solve this? In advance: we're not able to use static https-routes in this case.

monkeyphp commented 8 years ago

I appear to be getting the same problem

PHP Fatal error: Call to a member function getParam() on a non-object in /mnt/project/farmison/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php on line 472

I'm able to pin it down to any route (and child routes) of any hostname type route I have in my application - which is essentially most of the application.

'public' => [
            'type' => 'hostname',
            'options' => [
                'route' => 'www.my-domain.com',
            ],
            'child_routes' => [ ... etc

Update:

If I call $this->dispatch('http://www.my-domain.com/'); I appear to get a little bit further until I hit

1) FcoSiteTest\Controller\CheckoutControllerTest::testTheIndexActionCanBeAccessed
Undefined index: REQUEST_URI

/mnt/project/farmison/vendor/zendframework/zendframework/library/Zend/View/Helper/ServerUrl.php:59
/mnt/project/farmison/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php:393
weierophinney commented 4 years ago

This repository has been closed and moved to laminas/laminas-test; a new issue has been opened at https://github.com/laminas/laminas-test/issues/8.