-
-
Save archy-bold/bd696e8ec84a7657b724523e07fd7a6c to your computer and use it in GitHub Desktop.
{ | |
... | |
"autoload-dev": { | |
"classmap": [ | |
"tests/TestCase.php", | |
"tests/PassportTestCase.php" | |
] | |
}, | |
... | |
} |
<?php | |
use Illuminate\Foundation\Testing\WithoutMiddleware; | |
use Illuminate\Foundation\Testing\DatabaseMigrations; | |
use Illuminate\Foundation\Testing\DatabaseTransactions; | |
class ExamplePassportTest extends \PassportTestCase | |
{ | |
use DatabaseTransactions; | |
protected $scopes = ['restricted-scope']; | |
public function testRestrictedRoute() | |
{ | |
$this->get('/api/user') | |
->assertResponseStatus(401); | |
} | |
public function testUnrestrictedRoute() | |
{ | |
$this->get('/api/restricted') | |
->assertResponseOk(); | |
} | |
} |
<?php | |
use App\User; | |
use Laravel\Passport\ClientRepository; | |
use Illuminate\Foundation\Testing\WithoutMiddleware; | |
use Illuminate\Foundation\Testing\DatabaseMigrations; | |
use Illuminate\Foundation\Testing\DatabaseTransactions; | |
class PassportTestCase extends TestCase | |
{ | |
use DatabaseTransactions; | |
protected $headers = []; | |
protected $scopes = []; | |
protected $user; | |
public function setUp() | |
{ | |
parent::setUp(); | |
$clientRepository = new ClientRepository(); | |
$client = $clientRepository->createPersonalAccessClient( | |
null, 'Test Personal Access Client', $this->baseUrl | |
); | |
DB::table('oauth_personal_access_clients')->insert([ | |
'client_id' => $client->id, | |
'created_at' => new DateTime, | |
'updated_at' => new DateTime, | |
]); | |
$this->user = factory(User::class)->create(); | |
$token = $this->user->createToken('TestToken', $this->scopes)->accessToken; | |
$this->headers['Accept'] = 'application/json'; | |
$this->headers['Authorization'] = 'Bearer '.$token; | |
} | |
public function get($uri, array $headers = []) | |
{ | |
return parent::get($uri, array_merge($this->headers, $headers)); | |
} | |
public function getJson($uri, array $headers = []) | |
{ | |
return parent::getJson($uri, array_merge($this->headers, $headers)); | |
} | |
public function post($uri, array $data = [], array $headers = []) | |
{ | |
return parent::post($uri, $data, array_merge($this->headers, $headers)); | |
} | |
public function postJson($uri, array $data = [], array $headers = []) | |
{ | |
return parent::postJson($uri, $data, array_merge($this->headers, $headers)); | |
} | |
public function put($uri, array $data = [], array $headers = []) | |
{ | |
return parent::put($uri, $data, array_merge($this->headers, $headers)); | |
} | |
public function putJson($uri, array $data = [], array $headers = []) | |
{ | |
return parent::putJson($uri, $data, array_merge($this->headers, $headers)); | |
} | |
public function patch($uri, array $data = [], array $headers = []) | |
{ | |
return parent::patch($uri, $data, array_merge($this->headers, $headers)); | |
} | |
public function patchJson($uri, array $data = [], array $headers = []) | |
{ | |
return parent::patchJson($uri, $data, array_merge($this->headers, $headers)); | |
} | |
public function delete($uri, array $data = [], array $headers = []) | |
{ | |
return parent::delete($uri, $data, array_merge($this->headers, $headers)); | |
} | |
public function deleteJson($uri, array $data = [], array $headers = []) | |
{ | |
return parent::deleteJson($uri, $data, array_merge($this->headers, $headers)); | |
} | |
} |
Thanks for writing this up! It helped me :D
Hi please can i do a test without using ClientRepository
How about
- RestrictionPassportTest::testRestrictedRoute
ErrorException: Undefined property: RestrictionPassportTest::$baseUrl
??
How about
- RestrictionPassportTest::testRestrictedRoute
ErrorException: Undefined property: RestrictionPassportTest::$baseUrl??
Ya same issue here.
Instead of using $this->baseUrl
, just use an empty string ''
.
With Laravel 5.4+ you can use this very simple method to test passport routes: https://laravel.com/docs/5.7/passport#testing
@archy-bold Is this any different than simply calling Passport::actingAs()
?
searching for this .you saved my life sir . thank you 👍
Thanks @hemantachhami19, as has been said; you can just use Passport::actingAs($user)
now instead of all this scaffolding.
Same for this @hfatahi @hindermyertim @AhmedHelalAhmed (only just got notifications)
Crack of cracks 👑
@archy-bold do you have an example please ? I could never get actingAs to work in my tests... keep getting all sorts of weird errors like
BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::withAccessToken does not exist.
Hi @cAstraea. There's really not much to it, there's an example in the Passport documentation. If you're still experiencing errors, I imagine it's one of two things:
- You've not installed Passport correctly or not run the migrations on your test database. The documentation on installing should help you get it all set up. It will be worth checking the traits are on your model too.
- You might not be passing a single
User
model to theactingAs()
function. Maybe it's worthdd()
ing what you're passing to the function. It might be aQueryBuilder
, orCollection
object rather than aUser
model.
Hope that helps.
Thanks yea that was it thank you 🤣 Dunno what I was thinking calling it on collection
@cAstraea I usually blame tiredness!
Sorry, I didn't get any notifications for this gist for some reason. You need to ensure Passport is installed and running on your Laravel app. You can do that as follows:
Install with composer:
Then add the following to the
providers
array of yourapp/config.php
file:Finally, run the setup for Passport:
Source: https://laravel.com/docs/5.4/passport#installation