Commit 0219e640 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

finish users tests

parent dc8046a8
Pipeline #1207 passed with stages
in 1 minute and 39 seconds
......@@ -10,7 +10,7 @@ use Source\Exceptions\EntityExistsException;
use Source\Exceptions\EntityNotFoundException;
use Illuminate\Validation\ValidationException;
use Source\UseCases\Users\GetUser\GetUserUseCase;
use UseCases\Users\DeleteUser\DeleteFailedException;
use Source\UseCases\Users\DeleteUser\DeleteFailedException;
use Source\UseCases\Users\CreateUser\CreateUserUseCase;
use Source\UseCases\Users\DeleteUser\DeleteUserUseCase;
use Source\UseCases\Users\UpdateUser\UpdateUserUseCase;
......@@ -128,7 +128,7 @@ class UsersController extends ApiController {
try {
$deleteUser->delete($userId, $presenter);
} catch (DeleteFailedException $e) {
$this->respondWithError($e->getMessage());
return $this->respondWithError($e->getMessage());
}
return $this->respondWithData($presenter->getViewModel());
......
<?php
namespace UseCases\Users\DeleteUser;
namespace Source\UseCases\Users\DeleteUser;
use Exception;
......
......@@ -3,7 +3,6 @@
namespace Source\UseCases\Users\DeleteUser;
use Source\Gateways\Users\UsersRepository;
use UseCases\Users\DeleteUser\DeleteFailedException;
class DeleteUser implements DeleteUserUseCase {
/**
......
......@@ -4,8 +4,6 @@
namespace Source\UseCases\Users\DeleteUser;
use UseCases\Users\DeleteUser\DeleteFailedException;
interface DeleteUserUseCase {
/**
* @param string $userId
......
......@@ -31,7 +31,9 @@ class UpdateUser implements UpdateUserUseCase {
throw new EntityNotFoundException();
}
$expires = $attributes['expires_at'] ? new Carbon($attributes['expires_at']) : null;
$expires = (array_key_exists('expires_at', $attributes) && $attributes['expires_at']) ? new Carbon
($attributes['expires_at']
) : null;
// updatedAt will get overwritten
$newUser = new User(
......@@ -53,7 +55,8 @@ class UpdateUser implements UpdateUserUseCase {
if (!$returnedUser) {
$response->setError('Unable to update user.');
} else {
}
else {
$response->setUser($returnedUser);
}
......
<?php
namespace Tests\Feature\Api\Users;
use Tests\TestCase;
use Source\Entities\User;
use Source\Gateways\Users\UsersRepository;
use Illuminate\Foundation\Testing\TestResponse;
use Source\Gateways\Users\InMemoryUsersRepository;
use Illuminate\Contracts\Container\BindingResolutionException;
use Tests\Unit\Source\UseCases\Users\UpdateUser\InMemoryUsersRepositoryStub;
class DeleteUserApiTest extends TestCase {
protected TestResponse $response;
protected InMemoryUsersRepository $usersRepository;
/**
* @throws BindingResolutionException
*/
public function setUp(): void {
parent::setUp();
$this->usersRepository = $this->app->make(UsersRepository::class);
}
protected function handleTest(string $userId): void {
$this->response = $this->deleteJson('/users/' . $userId);
}
/** @test */
public function it_deletes_a_non_existent_user(): void {
$this->handleTest('asdfasfd non existent user');
$this->response->assertStatus(200);
$this->response->assertJson(
[
'message' => 'User deleted.',
'status' => 'success',
'code' => 200,
]
);
}
/** @test */
public function it_deletes_a_user(): void {
$user = new User(69, '', '', '', null, '', '', '', null, null, null);
$this->usersRepository->create($user);
$this->handleTest('69');
$this->response->assertStatus(200);
$this->response->assertJson(
[
'message' => 'User deleted.',
'status' => 'success',
'code' => 200,
]
);
$this->assertCount(0, $this->usersRepository->all());
}
/** @test */
public function it_handles_a_failed_delete(): void {
$this->app->bind(UsersRepository::class, static function ($app) {
return new InMemoryUsersRepositoryStub();
});
$this->handleTest('asdf');
$this->response->assertStatus(200);
$this->response->assertJson(
[
'message' => 'Unable to delete user.',
'status' => 'error',
'code' => 200,
]
);
}
}
<?php
namespace Tests\Feature\Api\Users;
use Tests\TestCase;
use Source\Entities\User;
use Source\Gateways\Users\UsersRepository;
use Illuminate\Foundation\Testing\TestResponse;
use Source\Gateways\Users\InMemoryUsersRepository;
use Illuminate\Contracts\Container\BindingResolutionException;
class UpdateUserApiTest extends TestCase {
protected TestResponse $response;
protected InMemoryUsersRepository $usersRepository;
/**
* @throws BindingResolutionException
*/
public function setUp(): void {
parent::setUp();
$this->usersRepository = $this->app->make(UsersRepository::class);
}
/**
* @param string $userId
* @param array $data
*/
protected function handleTest(string $userId, array $data): void {
$this->response = $this->putJson('/users/' . $userId, $data);
}
public function invalidUserProvider(): array {
$user = new User(0, 'first', 'last', 'display', null, 't@t.com', '', '43252643', null, null, null);
return [
// Missing one of the required
[
[
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'email' => $user->getEmail(),
],
['first_name' => ['The first name field is required.']],
],
[
[
'first_name' => $user->getFirstName(),
'display_name' => $user->getDisplayName(),
'email' => $user->getEmail(),
],
['last_name' => ['The last name field is required.']],
],
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'email' => $user->getEmail(),
],
['display_name' => ['The display name field is required.']],
],
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
],
['email' => ['The email field is required.']],
],
// emplid is too small
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'emplid' => 'small',
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
],
['emplid' => ['The emplid must be at least 6 characters.']],
],
// emplid is too large
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'emplid' => 'waaaaaaaaaaay to large',
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
],
['emplid' => ['The emplid may not be greater than 7 characters.']],
],
// expires at isnt a date
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
'expires_at' => 'bad date',
],
['expires_at' => ['The expires at is not a valid date.']],
],
// doorcode isnt a number
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'email' => $user->getEmail(),
'doorcode' => 'not a number',
],
['doorcode' => ['The doorcode must be a number.', 'The doorcode must be between 4 and 255 digits.']],
],
// doorcode is too small
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'email' => $user->getEmail(),
'doorcode' => '000',
],
['doorcode' => ['The doorcode must be between 4 and 255 digits.']],
],
// email invalid
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'email' => 'not an email',
'doorcode' => $user->getDoorcode(),
],
['email' => ['The email must be a valid email address.']],
],
];
}
/**
* @test
* @param array $data
* @param array $message
* @dataProvider invalidUserProvider
*/
public function it_tests_the_validation_rules(array $data, array $message): void {
$this->handleTest('69', $data);
$this->response->assertJsonFragment($message);
}
/** @test */
public function it_updates_a_user(): void {
$user = new User(69, '', '', '', null, '', '', '', null, null, null);
$this->usersRepository->create($user);
$this->handleTest(
'69',
[
'first_name' => 'first name',
'last_name' => 'last name',
'display_name' => 'display name',
'email' => 'email@imsad.dumb',
'doorcode' => '1234',
]
);
$this->response->assertStatus(200);
$this->response->assertJsonFragment(
[
'first_name' => 'first name',
'last_name' => 'last name',
'display_name' => 'display name',
'emplid' => null,
'email' => 'email@imsad.dumb',
]
);
$this->assertEquals($this->usersRepository->get('69')->getFirstName(), 'first name');
}
/** @test */
public function it_cannot_update_non_existient_user(): void {
$this->handleTest(
'asdf',
[
'first_name' => 'first name',
'last_name' => 'last name',
'display_name' => 'display name',
'email' => 'email@imsad.dumb',
'doorcode' => '1234',
]
);
$this->response->assertStatus(404);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment