0

dingo/api 0.10.0我使用和构建Laravel 5.1了一个 API lucadegasperi/oauth2-server-laravel": "^5.1"

我所有的路线都在 Postman/Paw 中运行良好!

当我尝试使用PHPUnit.

这是我route-api.php文件的一部分

<?php

$api = app('Dingo\Api\Routing\Router');

$api->version(['v1'], function ($api) {

$api->post('oauth/access_token', function () {
    return response(
        \LucaDegasperi\OAuth2Server\Facades\Authorizer::issueAccessToken()
    )->header('Content-Type', 'application/json');
});

$api->group(['middleware' => ['oauth', 'api.auth']], function ($api) {
    $api->post('/register', 'YPS\Http\Controllers\Api\UserController@register');
});

这是我的测试文件 UserRegistrationTest.php

class UserRegistrationTest extends ApiTestCase
{

public function setUp()
{
    parent::setUp();
    parent::afterApplicationCreated();
}

public function testRegisterSuccess()
{
    $data = factory(YPS\User::class)->make()->toArray();
    $data['password'] = 'password123';

    $this->post('api/register', $data, $this->headers)
        ->seeStatusCode(201)
        ->seeJson([
            'email' => $data['email'],
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
        ]);
}

public function testRegisterMissingParams()
{
    $this->post('api/register', [], $this->headers, $this->headers, $this->headers)->seeStatusCode(422);
}
}

ApiTestCase 只是检索一个令牌并设置标头。

private function setHeaders()
{
    $this->headers = [
        'Accept' => 'application/vnd.yps.v1+json',
        'Authorization' => 'Bearer ' . $this->OAuthAccessToken,
    ];
}

现在,奇怪的部分是第一个测试testRegisterSuccess运行完美并返回了我期望的响应。但是第二个testRegisterMissingParams,即使它是相同的路线,也会返回这个,

array:2 [
      "message" => "The version given was unknown or has no registered routes."
      "status_code" => 400
 ] 

我跟踪了错误,它在Laravel adapter这里:

public function dispatch(Request $request, $version)
{
    // it seems that the second time around can't find any routes with the key 'v1'
    if (! isset($this->routes[$version])) {
        throw new UnknownVersionException;
    }

    $routes = $this->mergeExistingRoutes($this->routes[$version]);

    $this->router->setRoutes($routes);

    return $this->router->dispatch($request);
}

此外,如果我一次运行一个测试(例如,注释掉一个,运行测试,然后注释另一个并运行测试),我会在两个测试中看到预期的结果。问题是当我运行多个测试时。

对此有什么想法吗?

谢谢!

4

2 回答 2

0

我在使用 Dingo & Lumen 进行测试时遇到了同样的问题。这对我有用 -bootstrap="bootstrap/app.php"从 phpunit.xml 文件中删除并将行更改processIsolation="false"processIsolation="true".

于 2016-07-26T20:56:06.010 回答
0

运行 php artisan api:routes 以查看完整路径,您可能错过了 URL 的某些内容,如果您手动请求 URL,这是否有效?

于 2016-07-20T10:29:20.567 回答