2017-02-14 06:53:02 +00:00
|
|
|
<?php
|
|
|
|
|
2024-01-09 18:34:40 +00:00
|
|
|
namespace Tests\Traits;
|
2017-02-14 06:53:02 +00:00
|
|
|
|
|
|
|
use App\Models\User;
|
2020-09-06 18:21:39 +00:00
|
|
|
use Illuminate\Testing\TestResponse;
|
2017-02-14 06:53:02 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_user;
|
|
|
|
|
2024-01-09 18:34:40 +00:00
|
|
|
trait MakesHttpRequests
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
2024-01-09 18:34:40 +00:00
|
|
|
/**
|
|
|
|
* @param string $method
|
|
|
|
* @param string $uri
|
|
|
|
* @return TestResponse
|
|
|
|
*/
|
|
|
|
abstract public function json($method, $uri, array $data = [], array $headers = []); // @phpcs:ignore
|
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
private function jsonAs(?User $user, string $method, $uri, array $data = [], array $headers = []): TestResponse
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user ??= create_user();
|
2022-07-26 20:08:31 +00:00
|
|
|
$this->withToken($user->createToken('koel')->plainTextToken);
|
2020-09-06 18:21:39 +00:00
|
|
|
|
2024-01-09 18:34:40 +00:00
|
|
|
return $this->json($method, $uri, $data, $headers);
|
2017-02-14 06:53:02 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
protected function getAs(string $url, ?User $user = null): TestResponse
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
2022-07-27 08:49:33 +00:00
|
|
|
return $this->jsonAs($user, 'get', $url);
|
2017-02-14 06:53:02 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
protected function deleteAs(string $url, array $data = [], ?User $user = null): TestResponse
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
2022-07-27 08:49:33 +00:00
|
|
|
return $this->jsonAs($user, 'delete', $url, $data);
|
2017-02-14 06:53:02 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
protected function postAs(string $url, array $data, ?User $user = null): TestResponse
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
2022-07-27 08:49:33 +00:00
|
|
|
return $this->jsonAs($user, 'post', $url, $data);
|
2017-02-14 06:53:02 +00:00
|
|
|
}
|
2018-08-19 11:08:16 +00:00
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
protected function putAs(string $url, array $data, ?User $user = null): TestResponse
|
2018-08-31 13:47:15 +00:00
|
|
|
{
|
2022-07-27 08:49:33 +00:00
|
|
|
return $this->jsonAs($user, 'put', $url, $data);
|
2018-08-31 13:47:15 +00:00
|
|
|
}
|
2022-09-08 05:06:49 +00:00
|
|
|
|
|
|
|
protected function patchAs(string $url, array $data, ?User $user = null): TestResponse
|
|
|
|
{
|
|
|
|
return $this->jsonAs($user, 'patch', $url, $data);
|
|
|
|
}
|
2017-02-14 06:53:02 +00:00
|
|
|
}
|