2017-02-14 06:53:02 +00:00
|
|
|
<?php
|
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
namespace Tests\Feature;
|
2017-02-14 06:53:02 +00:00
|
|
|
|
|
|
|
use App\Models\User;
|
2020-09-06 18:21:39 +00:00
|
|
|
use Illuminate\Testing\TestResponse;
|
2020-09-09 16:18:53 +00:00
|
|
|
use Tests\TestCase as BaseTestCase;
|
2017-02-14 06:53:02 +00:00
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
abstract class TestCase extends BaseTestCase
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
2020-09-06 18:21:39 +00:00
|
|
|
private function jsonAsUser(?User $user, string $method, $uri, array $data = [], array $headers = []): TestResponse
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
2021-07-26 21:21:36 +00:00
|
|
|
/** @var User $user */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = $user ?: User::factory()->create();
|
2020-09-06 18:21:39 +00:00
|
|
|
$headers['X-Requested-With'] = 'XMLHttpRequest';
|
2020-12-22 20:11:22 +00:00
|
|
|
$headers['Authorization'] = 'Bearer ' . $user->createToken('koel')->plainTextToken;
|
2020-09-06 18:21:39 +00:00
|
|
|
|
|
|
|
return parent::json($method, $uri, $data, $headers);
|
2017-02-14 06:53:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
protected function getAsUser(string $url, ?User $user = null): TestResponse
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
2020-09-06 18:21:39 +00:00
|
|
|
return $this->jsonAsUser($user, 'get', $url);
|
2017-02-14 06:53:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
protected function deleteAsUser(string $url, array $data = [], ?User $user = null): TestResponse
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
2020-09-06 18:21:39 +00:00
|
|
|
return $this->jsonAsUser($user, 'delete', $url, $data);
|
2017-02-14 06:53:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
protected function postAsUser(string $url, array $data, ?User $user = null): TestResponse
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
2020-09-06 18:21:39 +00:00
|
|
|
return $this->jsonAsUser($user, 'post', $url, $data);
|
2017-02-14 06:53:02 +00:00
|
|
|
}
|
2018-08-19 11:08:16 +00:00
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
protected function putAsUser(string $url, array $data, ?User $user = null): TestResponse
|
2018-08-31 13:47:15 +00:00
|
|
|
{
|
2020-09-06 18:21:39 +00:00
|
|
|
return $this->jsonAsUser($user, 'put', $url, $data);
|
2018-08-31 13:47:15 +00:00
|
|
|
}
|
2017-02-14 06:53:02 +00:00
|
|
|
}
|