Guest User

Untitled

a guest
Dec 10th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Tests\Traits;
  4.  
  5. use Laravel\Passport\ClientRepository;
  6. use Illuminate\Support\Facades\DB;
  7. use DateTime;
  8. use App\User;
  9.  
  10. trait InteractsWithPassport
  11. {
  12.  
  13. protected $headers = [];
  14. protected $scopes = [];
  15. protected $user;
  16.  
  17. public function createUserWithToken()
  18. {
  19. $clientRepository = new ClientRepository();
  20. $client = $clientRepository->createPersonalAccessClient(
  21. null, 'Test Personal Access Client', $this->tenantUrl
  22. );
  23.  
  24. DB::table('oauth_personal_access_clients')->insert([
  25. 'client_id' => $client->id,
  26. 'created_at' => new DateTime,
  27. 'updated_at' => new DateTime,
  28. ]);
  29.  
  30. $this->user = factory(User::class)->create();
  31. $token = $this->user->createToken('TestToken', $this->scopes)->accessToken;
  32. $this->headers['Accept'] = 'application/json';
  33. $this->headers['Authorization'] = 'Bearer '.$token;
  34. }
  35.  
  36. public function get($uri, array $headers = [])
  37. {
  38. return parent::get($this->addBaseUrl($uri), array_merge($this->headers, $headers));
  39. }
  40.  
  41. public function getJson($uri, array $headers = [])
  42. {
  43. return parent::getJson($this->addBaseUrl($uri), array_merge($this->headers, $headers));
  44. }
  45.  
  46. public function postJson($uri, array $data = [], array $headers = [])
  47. {
  48. return parent::postJson($this->addBaseUrl($uri), $data, array_merge($this->headers, $headers));
  49. }
  50.  
  51. public function putJson($uri, array $data = [], array $headers = [])
  52. {
  53. return parent::putJson($this->addBaseUrl($uri), $data, array_merge($this->headers, $headers));
  54. }
  55.  
  56. public function deleteJson($uri, array $data = [], array $headers = [])
  57. {
  58. return parent::deleteJson($this->addBaseUrl($uri), $data, array_merge($this->headers, $headers));
  59. }
  60.  
  61. protected function addBaseUrl($uri)
  62. {
  63. return $this->tenantUrl . $uri;
  64. }
  65. }
Add Comment
Please, Sign In to add comment