Skip to content

Instantly share code, notes, and snippets.

@acfatah
Last active March 17, 2019 13:28
Show Gist options
  • Save acfatah/783db6ed32001577ad0308075b46224f to your computer and use it in GitHub Desktop.
Save acfatah/783db6ed32001577ad0308075b46224f to your computer and use it in GitHub Desktop.
Laravel 5.8 unit testing with laravel/passport
<?php
// origin: https://gist.github.com/archy-bold/bd696e8ec84a7657b724523e07fd7a6c
namespace Tests;
use DB;
use DateTime;
use App\User;
use Laravel\Passport\ClientRepository;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
abstract class PassportTestCase extends TestCase
{
use DatabaseTransactions, DatabaseMigrations;
protected $headers = [];
protected $scopes = [];
protected $user;
protected function setUp(): void
{
parent::setUp();
$clientRepository = new ClientRepository();
$client = $clientRepository->createPersonalAccessClient(
null,
'Test Personal Access Client',
'/'
);
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));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment