Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

phpunit does not trigger Application::routes() #13317

Closed
1 of 3 tasks
asgraf opened this issue Jun 3, 2019 · 10 comments
Closed
1 of 3 tasks

phpunit does not trigger Application::routes() #13317

asgraf opened this issue Jun 3, 2019 · 10 comments

Comments

@asgraf
Copy link

asgraf commented Jun 3, 2019

This is a (multiple allowed):

  • bug

  • enhancement

  • feature-discussion (RFC)

  • CakePHP Version: 3.7.8

  • Platform and Target: PHP 5.6.40 on Mageia Linux 6.1 (x64)

What you did

in src/Application.php

public function routes($routes)
{
	// Register scoped middleware for use in routes.php
	$routes->registerMiddleware('csrf', new CsrfProtectionMiddleware([
		'httpOnly' => true,
		'secure' => !Configure::read('debug'),
	]));
	parent::routes($routes);
}

in config/routes.php

$routes->scope('/pl', ['language' => 'pl'], function (RouteBuilder $routes) {
    $routes->applyMiddleware('csrf');
   //some routes here
});

Then called from console:
./vendor/bin/phpunit

What happened

Every test fails with following error:

RuntimeException: Cannot apply 'csrf' middleware or middleware group. Use registerMiddleware() to register middleware.

I have added xdebug_print_function_stack() to my routes.php to trace how this occurs.
Here is interesting part:

0.8846   11347576  15. Cake\TestSuite\MiddlewareDispatcher->resolveUrl(string(28)) ./vendor/cakephp/cakephp/src/TestSuite/IntegrationTestTrait.php:513
0.8846   11348000  16. Cake\Routing\Router::url(string(28), ???) ./vendor/cakephp/cakephp/src/TestSuite/MiddlewareDispatcher.php:105
0.8846   11348096  17. Cake\Routing\Router::_loadRoutes() ./vendor/cakephp/cakephp/src/Routing/Router.php:662
0.8861   11714744  18. include('./config/routes.php') ./vendor/cakephp/cakephp/src/Routing/Router.php:1222
0.8861   11714792  19. xdebug_print_function_stack() ./config/routes.php:49

Looks like routes.php is included internally via Router::_loadRoutes() and Application::routes() is never called

Moving registerMiddleware call from Application::routes() method into routes.php file seem to fix problem for me

What you expected to happen

Tests should use Application::routes()

@saeideng
Copy link
Member

saeideng commented Jun 3, 2019

use like this

    public function setUp()
    {
        parent::setUp();
        $this->useHttpServer(true); <-- add this line 
    }

this requires in cake 3.x series ,cake 4 uses the application by default

@saeideng saeideng closed this as completed Jun 3, 2019
@saeideng saeideng added this to the 3.7.9 milestone Jun 3, 2019
@asgraf
Copy link
Author

asgraf commented Jun 3, 2019

I have added setUp() method with $this->useHttpServer(true); to my TestCase class as you suggested, but this did not help in any way.
Exception RuntimeException: Cannot apply 'csrf' middleware or middleware group. Use registerMiddleware() to register middleware is still throwed and Application::routes() is still not being called

@saeideng saeideng reopened this Jun 3, 2019
@asgraf
Copy link
Author

asgraf commented Jun 3, 2019

I also just noticed that all my Router::addUrlFilter() filters defined in my Application::bootstrap() (as manual suggests) are also not called.

@markstory
Copy link
Member

How can someone reproduce this problem? My applications get their bootstrap() and routes() hooks called when the test case enables useHttpServer().

@markstory markstory modified the milestones: 3.7.9, 3.7.10 Jun 20, 2019
@saeideng saeideng modified the milestones: 3.7.10, 3.8.1 Jun 27, 2019
@markstory markstory modified the milestones: 3.8.1, 3.8.2 Jul 13, 2019
@markstory markstory modified the milestones: 3.8.2, 3.8.3 Aug 9, 2019
@markstory markstory modified the milestones: 3.8.3, 3.8.4 Aug 31, 2019
@markstory markstory modified the milestones: 3.8.4, 3.8.5 Sep 14, 2019
@markstory markstory modified the milestones: 3.8.5, 3.8.6 Oct 7, 2019
@markstory markstory modified the milestones: 3.8.6, 3.8.7 Nov 7, 2019
@othercorey
Copy link
Member

@asgraf Can you provide more information on your application setup?

@markstory markstory modified the milestones: 3.8.7, 3.8.8 Dec 8, 2019
@ADmad
Copy link
Member

ADmad commented Dec 14, 2019

Closing as the issue has gone stale and there hasn't been any follow up in a long time.

@ADmad ADmad closed this as completed Dec 14, 2019
@vladbanyai-spearline
Copy link

How can someone reproduce this problem? My applications get their bootstrap() and routes() hooks called when the test case enables useHttpServer().

The hooks are called but it will first try to run the applyMiddleware method from config/routes.php and this will cause an error (if as in the example the middleware is registered in Application.php).
If this applyMiddleware call is removed from config/routes.php the hooks are indeed called.

@markstory
Copy link
Member

@vladbanyai-spearline Thanks for the extra context, but I don't know how that helps me understand how to reproduce this issue. Do you have an example application that contains the error you're facing? I'm looking for some code that exhibits the problem so that I can debug what is going on and why the applications I work on behave differently and don't suffer from this problem.

@othercorey othercorey reopened this Aug 26, 2021
@othercorey
Copy link
Member

@vladbanyai-spearline Can you help us reproduce it?

@othercorey
Copy link
Member

Closing again due to no response.

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

No branches or pull requests

6 participants