contributte / codeception

:arrow_forward: Integration of Nette Framework to Codeception.

Home Page:https://contributte.org/packages/contributte/codeception.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Nette\Application\BadRequestException] No route for HTTP request. when running tests

dakorpar opened this issue · comments

I'm getting this error when running codecept run functional

Functional Tests (1) ---------------------------------------

E Test1Cest: Verify homepage (1.16s)

Time: 14.23 seconds, Memory: 34.00MB

There was 1 error:

  1. Test1Cest: Verify homepage
    Test tests\functional\Test1Cest.php:tryToTest

[Nette\Application\BadRequestException] No route for HTTP request.

Scenario Steps:

  1. $I->amOnPage("/") at tests\functional\Test1Cest.php:18

1 C:\Development\project\web\vendor\nette\application\src\Application\Application.php:83

2 C:\Development\project\web\vendor\arachne\codeception\src\Connector\NetteConnector.php:73

3 C:\Development\project\web\vendor\symfony\browser-kit\Client.php:315

4 Codeception\Lib\InnerBrowser->amOnPage

5 C:\Development\project\web\tests_support_generated\FunctionalSuiteTesterActions.php:142

6 C:\Development\project\web\tests\functional\Test1Cest.php:18

7 Test1Cest->tryToTest

Homepage HTML is outputed in console when calling command also, and HTML is correct of homepage , and links looks good there also...
You need some more info on it or you have a clue what could it be?

My Test1Cept looks like:


class Test1Cest
{
    public function _before(FunctionalSuiteTester $I)
    {
    }

    public function _after(FunctionalSuiteTester $I)
    {
    }

    // tests
    public function tryToTest(FunctionalSuiteTester $I)
    {
        $I->wantTo('Verify homepage');
        $I->amOnPage('/');
        $I->see('Welcome');
    }
}

and _bootstrap file:

define('WWW_DIR', __DIR__ . '/../../www');
define('APP_DIR', __DIR__ . '/../../app');
define('ESHOPID', 1);
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['HTTP_HOST'] = 'project.local';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTPS'] = TRUE;
require __DIR__ . '/../../app/bootstrap.php';
return $container;

I'm just wanting this to work then I'll play with correct bootstrap, different db connection and etc...

and functional.yml

error_level: "E_ALL"
class_name: FunctionalSuiteTester
modules:
    enabled:
        - Arachne\Codeception\Module\NetteApplicationModule
        - Arachne\Codeception\Module\NetteDIModule:
            tempDir: ../../temp/cache/functional
            configFiles:
                - ../../app/config/config.neon
                - ../../app/config/config.local.neon
                - config.neon

I believe problem is in my custom router. I did little debugging
https://github.com/nette/application/blob/master/src/Application/Routers/RouteList.php#L37
First time page is loaded I have all my routes here (this load is probably directly from codeception, I don't know...), but then when it hits my code $I->amOnPage('/'); now $this does not have any routes defined...
How can I avoid this? any thoughts?

I managed to get over this by including router code in _before function, but now also looks like some other things from config are not initialized.
I have difficulites not in figuring out which part of code was run and which wasn't, because it looks like it was mess. I have to take a look between atreo implementation and this one, he did it other way, and with him I don't have this issues, any thoughts?

Ok so first a response I've written when reading your latest post on nette forum:

Oh. Ok I'l try to help you out. I'd like my repository to be compatible with most people's use cases but it's difficult to achieve without knowledge of other people's applications.

Arachne/Codeception should work with Nette 2.4 (~2.3 means >=2.3 && <3.0). Should be compatible with both 2.3 and 2.4...

With Arachne/Codeception your app/bootstrap.php file is NOT executed at all. If you included it manually somehow (which you should not) the container your bootstrap.php returns won't be used anyway.

Instead you should set up your routes in a RouterFactory service (see https://github.com/nette/sandbox/blob/master/app/router/RouterFactory.php and https://github.com/nette/sandbox/blob/master/app/config/config.neon#L23).

Oh and your tests/functional/_bootstrap.php file should be empty. I mean mine is.

That's all I can think of at the moment. Let me know if the RouterFactory service helps and how far you'll get that way.

When you manage to set it up we will need to update the docs in this repo accordingly to prevent other users running into the same problems.

Thanks for bringing this up!

Well I'm working on project that's preety old, started with nette 0.9. Project is preety big and rewriting it or clearing out deprecated code isn't easy. It's working currently with nette 2.4 but most of the stuff needs to be rewritten, to much usage of \Nette\Environment through project. I'm easily modernizing it to go up to date, to nette 2.4 way, with DI, proper way, but that's task that will span over long period of time... Some modules are in rewrite process and they're done proper nette 2.4 way, router is still not there, but this is rather easy fix. Now I'm figuring out how this all is working since I'm new with codeception, so wasn't really sure how it supposed to work, but I'm getting there. Thanks for your support, I'll resolve this my way, I'm close to it, and then I'll write here how it went, what I did and why (I'll have to bypass some stuff because of legacy code in my project), and then easily I'll take this up to correct path.
Thanks for your input :)

Glad to help. Let me know if you run into any more troubles. I'm looking forward to your report!

Just a recommendation - write the stuff down periodically before you forget the details. ;-)

I would do that but sometimes I have to go two steps behind so I can advance and then this here would be mess.
first test is running now and it's running fine with warnings :)
1050x DEPRECATION: Nette\Environment is deprecated

Those are the steps I did:
my bootstrap.php file has to be loaded because I still have some initializatione code there without which my app won't run so this is my _bootstrap.php:

define('WWW_DIR', __DIR__ . '/../../www');
define('APP_DIR', __DIR__ . '/../../app');

$_SERVER['HTTP_HOST'] = 'my.local';


require __DIR__ . '/../../app/bootstrap.php';

and in tests:

    public function _before(FunctionalSuiteTester $I)
    {
        $container = $I->getContainer();
        require (__DIR__ . '/../../app/bootstrap-router.php');
    }

I know it's bad, but I'm making it work. Now I can easily try to make this work properly...
I have to clear out bootstrap.php code which I was planning for a long time, and properly initialize router, after that all should be fine...

Yeah that _bootstrap.php seems about fine. My app is much newer so I've written it to keep my app/bootstrap.php as simple as possible. Try initializing what you can in services. Also you might need to add your own CompilerExtension to add some code into container initialization in afterCompile (like https://github.com/Kdyby/Translation/blob/master/src/Kdyby/Translation/DI/TranslationExtension.php#L448-L454).

Try to get rid of the bootstrap-router.php file if you can with a RouterFactory. That should clean your tests a bit.

I've got rid of bootstrap-router, so I've got rid of

   public function _before(FunctionalSuiteTester $I)
    {
        $container = $I->getContainer();
        require (__DIR__ . '/../../app/bootstrap-router.php');
    }

I think that's actually good enough for now, willcleanup app/bootstrap.php little bit, but that actually now doesn't have any influence on tests setup, would be nice to get rid of global constants through the code, but I don't see that comming soon...
Thank you very much for your help.