Hey guys.
First off some code:
$api->version('v1', ['middleware' => 'jwt.auth', 'protected' => true], function ($api) {
$api->post('users', ['as' => 'api.users.create', 'uses' => 'Webbish\Http\Controllers\Api\UserController@store']);
$api->put('users/{id}', ['as' => 'api.users.update', 'uses' => 'Webbish\Http\Controllers\Api\UserController@update']);
});
Note: I've set the routes to be both protected and not protected to see if there was any difference.
use Illuminate\Foundation\Testing\WithoutMiddleware;
//use Illuminate\Foundation\Testing\DatabaseTransactions;
class UserTest extends TestCase
{
use WithoutMiddleware;
protected $faker;
/**
* @return void
*/
public function testSuccessfullyCreateNewUser()
{
$this->faker = Faker\Factory::create();
$this->pwd = $this->faker->word;
$response = $this->call('POST', 'api/users/', [
'email' => $this->faker->email,
'password' => $this->pwd,
'password_confirmation' => $this->pwd,
'confirmation_code' => 1,
'firstname' => $this->faker->firstName,
'lastname' => $this->faker->lastName,
])->header('Accept', 'application/vnd.webbish.v1');
$this->assertEquals('201', $response->status());
}
So.
When I try the exact same setup in postman everything works just fine and dandy.
But when I run my phpUnit tests I get a 404 on every routes which makes my tests b0rk totally.
The response I get is a 404 instead of a 201, which it should be.
I'm using Laravel 5.1 and Dingo/Api @ version 0.9.0 and dev-master
[13:21:24] Starting 'phpunit'...
[13:21:24]
*** Debug Cmd: ./vendor/bin/phpunit --colors --debug ***
[13:21:26] PHPUnit 4.7.7 by Sebastian Bergmann and contributors.
Starting test 'UserTest::testSuccessfullyCreateNewUser'.
F
Starting test 'UserTest::testFaultyCreateUserWithCorrectErrorMessage'.
F
Starting test 'UserTest::testThatPasswordHasToBeConfirmed'.
F
Time: 1.35 seconds, Memory: 15.75Mb
There were 3 failures:
1) UserTest::testSuccessfullyCreateNewUser
Failed asserting that 404 matches expected '201'.
/Users/rm/www/gar.dev/tests/Api/User/UserTest.php:33
2) UserTest::testFaultyCreateUserWithCorrectErrorMessage
Failed asserting that 404 matches expected '422'.
/Users/rm/www/gar.dev/tests/Api/User/UserTest.php:57
3) UserTest::testThatPasswordHasToBeConfirmed
Failed asserting that 404 matches expected '422'.
/Users/rm/www/gar.dev/tests/Api/User/UserTest.php:83
FAILURES!
Tests: 3, Assertions: 3, Failures: 3.
Is there anyone else who've had this problem?
I have of course checked the URL so that it's the correct URL. So you don't have to ask that ^^
I've tried both localhost/api/users and my dev-domain which is gar.dev/api/users.
I have also set the Authorization and Access headers but even if I strip that out I shouldn't really get a 404 - right, rather a 422.
I might have found what makes it break.
The withoutMiddleware-trait doesn't exclude the middleware from the test so because that I don't have the JWT in my header it returns a 404.
I don't know if that is a bug in Laravel or Dingo/Api though.
It's probably too late but this is why: #571
You're a lifesaver, @jadjoubran!! :+1: (I was about to quit on TDD because of this... but once again, thanks to you, I'm in it to win it! :)
Hey Guys!
I still could not make it work. I need to set all middleware on the router?
@shiruken1 I'm ab out to quit TDD in Laravel 5 'cause of this too rs
Hi @leandrodma
as mentioned in #571 you just need to remove the withoutMiddleware trait
add this method in your test case that all tests extends and don't worry about middleware
protected function headers($user = null)
{
$headers = ['Accept' => 'application/json'];
if (!is_null($user)) {
$token = JWTAuth::fromUser($user);
JWTAuth::setToken($token);
$headers['Authorization'] = 'Bearer' .$token;
}return $headers;
}
in your tests add $this->headers($user);
Most helpful comment
It's probably too late but this is why: #571