koel/tests/Feature/TestCase.php

40 lines
1.2 KiB
PHP
Raw Normal View History

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