Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

zf2 phpunit scheme routes #5

Open
synatix opened this issue Oct 21, 2015 · 2 comments
Open

zf2 phpunit scheme routes #5

synatix opened this issue Oct 21, 2015 · 2 comments
Labels

Comments

@synatix
Copy link

synatix commented Oct 21, 2015

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
Copy link

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
Copy link
Member

This repository has been closed and moved to laminas/laminas-test; a new issue has been opened at laminas/laminas-test#8.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants