1

original posted at https://github.com/dingo/api/issues/1472

I'm using Lumen 5.1 and DingoApi 1.0.x to do my api development, and now I'm trying to do some acceptance testing. Following the documentation of Lumen, here is how I do it:

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:

class DealerTest extends TestCase
{
    public function testTest()
    {
        $this->get('/')->assertResponseOk();
        $this->get('/dealer/list')->assertResponseOk();
        $this->get('/dealer/staff_list')->assertResponseOk();
    }
}

and ran result:

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...

unifreak
  • 773
  • 6
  • 17

1 Answers1

2

So I got mine to work by doing this; Using the example in the example used in creating the issue:

class DealerTest extends TestCase
{
    public function testTest()
    {
        $this->get('/')->assertResponseOk();
        $this->get('/dealer/list')->assertResponseOk();
        $this->get('/dealer/staff_list')->assertResponseOk();
    }
}

becomes

class DealerTest extends TestCase
{
    public function testTest()
    {
        $this->get(getenv('API_DOMAIN') . '/v1/')->assertResponseOk();
        $this->get(getenv('API_DOMAIN') . '/v1/dealer/list')->assertResponseOk();
        $this->get(getenv('API_DOMAIN') . '/v1/dealer/staff_list')->assertResponseOk();
    }
}

I hope this helps

petersowah
  • 728
  • 8
  • 20