Commit 5114a535 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Update user usecase tests

parent 75e4d8b6
<?php
namespace Tests\Unit\Source\UseCases\Users\UpdateUser;
use Source\Entities\User;
use Source\Gateways\Users\UsersRepository;
class InMemoryUsersRepositoryStub implements UsersRepository {
protected $user;
public function setUserToReturnOnGet(User $user): void {
$this->user = $user;
}
/**
* @param string $userId
* @return User|null
*/
public function get(string $userId): ?User {
if ($this->user) {
return $this->user;
}
return null;
}
/**
* @return array
*/
public function all(): array {
return [];
}
/**
* @param User $user
* @return User|null
*/
public function create(User $user): ?User {
return null;
}
/**
* @param string $userId
* @param User $user
* @return User|null
*/
public function update(string $userId, User $user): ?User {
return null;
}
/**
* @param string $userId
* @return bool
*/
public function delete(string $userId): bool {
return false;
}
/**
* @param string $userId
* @return bool
*/
public function exists(string $userId): bool {
return true;
}
}
<?php
namespace Tests\Unit\Source\UseCases\Users\UpdateUser;
use Source\UseCases\Users\UpdateUser\Presenter;
use Source\UseCases\Users\UpdateUser\ResponseModel;
class PresenterStub implements Presenter {
public ResponseModel $response;
protected bool $presenterCalled = false;
public function present(ResponseModel $responseModel): void {
$this->presenterCalled = true;
$this->response = $responseModel;
}
public function wasPresenterCalled(): bool {
return $this->presenterCalled;
}
public function getViewModel(): array {
return [];
}
}
<?php
namespace Unit\Source\UseCases\Users\UpdateUser;
use Carbon\Carbon;
use Source\Entities\User;
use PHPUnit\Framework\TestCase;
use Source\UseCases\Users\UpdateUser\APIPresenter;
use Source\UseCases\Users\UpdateUser\ResponseModel;
class PresenterTest extends TestCase {
protected APIPresenter $presenter;
protected ResponseModel $model;
protected array $response;
public function setUp(): void {
parent::setUp();
$this->presenter = new APIPresenter();
$this->model = new ResponseModel();
}
public function handleTest(): void {
$this->presenter->present($this->model);
$this->response = $this->presenter->getViewModel();
}
/** @test */
public function it_formats_a_user(): void {
$user = new User(
0,
'first',
'last',
'display',
'emplid',
'email',
'password',
'doorcode',
new Carbon('2020-02-02'),
null,
null
);
$this->model->setUser($user);
$this->handleTest();
$this->assertEquals(
[
'error' => false,
'user' => [
'id' => 0,
'first_name' => 'first',
'last_name' => 'last',
'display_name' => 'display',
'emplid' => 'emplid',
'email' => 'email',
'expires_at' => 'Feb 2, 2020',
'created_at' => null,
'updated_at' => null,
],
],
$this->response
);
}
/** @test */
public function it_formats_an_error(): void {
$this->model->setError('error');
$this->handleTest();
$this->assertEquals(
[
'error' => true,
'message' => 'error',
],
$this->response
);
}
}
<?php
namespace Tests\Unit\Source\UseCases\Users\UpdateUser;
use Source\Entities\User;
use PHPUnit\Framework\TestCase;
use Source\Exceptions\EntityNotFoundException;
use Source\UseCases\Users\UpdateUser\UpdateUser;
use Source\Gateways\Users\InMemoryUsersRepository;
use Source\UseCases\Users\UpdateUser\ResponseModel;
class UseCaseTest extends TestCase {
protected InMemoryUsersRepository $usersRepository;
protected UpdateUser $useCase;
protected ResponseModel $response;
protected PresenterStub $presenter;
public function setUp(): void {
parent::setUp();
$this->usersRepository = new InMemoryUsersRepository();
$this->useCase = new UpdateUser($this->usersRepository);
$this->presenter = new PresenterStub();
}
protected function createUserAttributes(User $user, $password = null, $doorcode = null): array {
$expires = $user->getExpiresAt();
if ($expires) {
$expires = $expires->toString();
}
return [
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'emplid' => $user->getEmplid(),
'email' => $user->getEmail(),
'password' => $password,
'doorcode' => $doorcode,
'expires_at' => $expires,
];
}
/**
* @param string $userId
* @param array $attributes
* @throws EntityNotFoundException
*/
public function handleTest(string $userId, array $attributes): void {
$this->useCase->update($userId, $attributes, $this->presenter);
$this->response = $this->presenter->response;
}
/**
* @test
* @throws EntityNotFoundException
*/
public function presenter_was_called(): void {
$user = new User(69, 'special name', '', '', null, '', '', '', null, null, null);
$this->usersRepository->create($user);
$this->handleTest('69', $this->createUserAttributes($user));
$this->assertTrue($this->presenter->wasPresenterCalled());
}
/**
* @test
* @throws EntityNotFoundException
*/
public function it_updates_a_user(): void {
$user = new User(69, 'camdiddle beerbong', '', '', null, '', '', '', null, null, null);
$updatedUser = new User(69, 'updated name', '', '', null, '', '', '', null, null, null);
$this->usersRepository->create($user);
$this->handleTest('69', $this->createUserAttributes($updatedUser));
$this->assertFalse($this->response->hasError());
$this->assertEquals('updated name', $this->usersRepository->get('69')->getFirstName());
$this->assertEquals($updatedUser, $this->response->getUser());
}
/**
* @test
* @throws EntityNotFoundException
*/
public function it_cannot_update_non_existent_user(): void {
$this->expectException(EntityNotFoundException::class);
$updatedUser = new User(69, 'updated name', '', '', null, '', '', '', null, null, null);
$this->handleTest('420', $this->createUserAttributes($updatedUser));
}
/**
* @test
* @throws EntityNotFoundException
*/
public function it_sets_error_on_fail(): void {
$updatedUser = new User(420, 'updated name', '', '', null, '', '', '', null, null, null);
$stub = new InMemoryUsersRepositoryStub();
$stub->setUserToReturnOnGet($updatedUser);
$this->useCase = new UpdateUser($stub);
$this->handleTest('420', $this->createUserAttributes($updatedUser));
$this->assertTrue($this->response->hasError());
$this->assertEquals('Unable to update user.', $this->response->getError());
}
}
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