api: All Dingo routes return 404 when phpunit testing

Q A
Bug? yes
New Feature? no
Framework Lumen
Framework version 5.1
Package version 1.0.x
PHP version 7.x.y

Firstly, I already checked three issues regarding to routes problem under phpunit testing enviroment, and I think that this issue might requires some documentation update, for they are so common and annoying.

Here is a simplified routes definition in app\Http\routes.php:

$app->get('/', function () use ($app) {
    return "Welcome to mysite.com";
});

$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
    $api->group([
        'prefix' => 'dealer',
        'middleware' => 'checkH5ApiSign'
    ], function ($api) {
        $api->get('list', 'App\Http\Controllers\Credit\DealerController@index');
        $api->get('staff_list', 'App\Http\Controllers\Credit\DealerController@getStaffList');
    });
}

I can access both routes defined using $app or $api(dingo) in browser or via postman, they both can return a 200 response. But whenever I’m trying to access those routes in phpunit, the $app defined route like / is responding okay with 200 code, but all routes defined with $api(dingo) will response with 404 status code. Here is my test code and ran result:

class DealerTest extends TestCase
{
    public function testTest()
    {

        $this->get('/')->assertResponseOk();
        $this->get('/dealer/list')->assertResponseOk();
        $this->get('/dealer/staff_list')->assertResponseOk();
    }
}
PHPUnit 5.7.5 by Sebastian Bergmann and contributors.

F                                                                  1 / 1 (100%)

Time: 590 ms, Memory: 6.00MB

There was 1 failure:

1) DealerTest::testTest
Expected status code 200, got 404.
Failed asserting that false is true.

E:\Gitrepos\api.fin.youxinjinrong.com\vendor\laravel\lumen-framework\src\Testing\AssertionsTrait.php:19
E:\Gitrepos\api.fin.youxinjinrong.com\tests\DealerTest.php:8

FAILURES!
Tests: 1, Assertions: 2, Failures: 1.

I tried ran through Dingo package code to find the cause, but failed. All other related issue could not solve my problem either. So please help me.

update:

I followed the code flow, and see that FastRoute\DataGenerator\RegexBasedAbstract.php is doing the addRoute() operation, I dumped $this->staticRoutes) in that addRoute() method, see that it’s doing okay both inside browser and under phpunit. But weird enough, the following call of ->getData() is behaving differenctly: in browser all static routes are returned, but not in phpunit.

Hope this can somehow be helpful. I’m still digging this problem…

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (6 by maintainers)

Most upvoted comments

Finally found the culprit: The package causing all the 404 was barryvdh/laravel-cors middleware registered on the api routes