koel/tests/Feature/TestCase.php

40 lines
1.2 KiB
PHP
Raw Normal View History

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
{
$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
}
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
}