Test and update UserController endpoint
This commit is contained in:
31
app/DataTransferObjects/UserData.php
Normal file
31
app/DataTransferObjects/UserData.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\DataTransferObjects;
|
||||
|
||||
use Illuminate\Support\Carbon;
|
||||
use App\DataTransferObjects\Support\Data;
|
||||
use Spatie\LaravelData\Attributes\Validation\Max;
|
||||
use Spatie\LaravelData\Attributes\Validation\Email;
|
||||
use Spatie\LaravelData\Attributes\Validation\Unique;
|
||||
use Spatie\LaravelData\Attributes\Validation\Required;
|
||||
use Spatie\LaravelData\Attributes\Validation\StringType;
|
||||
|
||||
class UserData extends Data
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $id = null,
|
||||
public ?string $avatar = null,
|
||||
#[Required,
|
||||
StringType,
|
||||
Max(255)]
|
||||
public string $name,
|
||||
#[Required,
|
||||
StringType,
|
||||
Email,
|
||||
Max(255),
|
||||
Unique('users')]
|
||||
public string $email,
|
||||
public ?Carbon $created_at = null,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Api;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UserRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return $this->bearerToken() && $this->bearerToken() === setting('api_token');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'name' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255'
|
||||
],
|
||||
'email' => [
|
||||
'required',
|
||||
'string',
|
||||
'email',
|
||||
'max:255',
|
||||
'unique:users'
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Closure;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use App\Models\Package;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
class UserFactory extends Factory
|
||||
{
|
||||
@@ -28,4 +30,20 @@ class UserFactory extends Factory
|
||||
$model->enableTwoFactorAuth();
|
||||
});
|
||||
}
|
||||
|
||||
public function withPackage(Closure $modifyFactory = null): static
|
||||
{
|
||||
$factory = Package::factory();
|
||||
|
||||
if ($modifyFactory) {
|
||||
$factory = $modifyFactory($factory);
|
||||
}
|
||||
|
||||
return $this->set('package_id', $factory);
|
||||
}
|
||||
|
||||
public function admin(): static
|
||||
{
|
||||
return $this->set('role', User::ADMIN);
|
||||
}
|
||||
}
|
||||
|
||||
112
tests/Unit/Http/Controllers/Api/UserControllerTest.php
Normal file
112
tests/Unit/Http/Controllers/Api/UserControllerTest.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
it('can list users', function () {
|
||||
$user = User::factory(20)
|
||||
->create();
|
||||
|
||||
$response = api()
|
||||
->get(route('api.user.index'))
|
||||
->assertOk()
|
||||
->collect()
|
||||
->all();
|
||||
|
||||
expect($response)->toBe([
|
||||
'data' => User::take(15)
|
||||
->get()
|
||||
->map(fn (User $user) => [
|
||||
'id' => $user->id,
|
||||
'avatar' => $user->avatar,
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'created_at' => $user->created_at->toISOString(),
|
||||
])
|
||||
->all(),
|
||||
'links' => [
|
||||
'first' => route('api.user.index', ['page' => 1]),
|
||||
'last' => route('api.user.index', ['page' => 2]),
|
||||
'prev' => null,
|
||||
'next' => route('api.user.index', ['page' => 2]),
|
||||
],
|
||||
'meta' => [
|
||||
'current_page' => 1,
|
||||
'from' => 1,
|
||||
'last_page' => 2,
|
||||
'links' => [
|
||||
[
|
||||
'url' => null,
|
||||
'label' => '«',
|
||||
'active' => false,
|
||||
],
|
||||
[
|
||||
'url' => route('api.user.index', ['page' => 1]),
|
||||
'label' => '1',
|
||||
'active' => true,
|
||||
],
|
||||
[
|
||||
'url' => route('api.user.index', ['page' => 2]),
|
||||
'label' => '2',
|
||||
'active' => false,
|
||||
],
|
||||
[
|
||||
'url' => route('api.user.index', ['page' => 2]),
|
||||
'label' => '»',
|
||||
'active' => false,
|
||||
],
|
||||
],
|
||||
'path' => route('api.user.index'),
|
||||
'per_page' => 15,
|
||||
'to' => 15,
|
||||
'total' => 20,
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('can create and get a user', function () {
|
||||
$response = api()
|
||||
->post(route('api.user.store'), [
|
||||
'id' => 8, // Not fillable.
|
||||
'name' => 'John Doe',
|
||||
'email' => 'john@example.com',
|
||||
])
|
||||
->assertCreated()
|
||||
->collect()
|
||||
->all();
|
||||
|
||||
$user = User::sole();
|
||||
|
||||
expect($response)->toBe([
|
||||
'data' => [
|
||||
'id' => $user->id,
|
||||
'avatar' => $user->avatar,
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'created_at' => $user->created_at->toISOString(),
|
||||
],
|
||||
]);
|
||||
|
||||
$getResponse = api()
|
||||
->get(route('api.user.show', $user))
|
||||
->assertOk()
|
||||
->collect()
|
||||
->all();
|
||||
|
||||
expect($getResponse)->toBe([
|
||||
'data' => [
|
||||
'id' => $user->id,
|
||||
'avatar' => $user->avatar,
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'created_at' => $user->created_at->toISOString(),
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('can validate a request', function () {
|
||||
api()
|
||||
->post(route('api.user.store'), [
|
||||
'email' => 'john@example.com',
|
||||
])
|
||||
->assertStatus(302);
|
||||
});
|
||||
Reference in New Issue
Block a user