Test throwing EntityNotFoundException when trying to get nothing from the repository.

This commit is contained in:
Dave Smith-Hayes 2024-11-18 20:31:44 -05:00
parent 4d25d3039a
commit 8dce6a1603
3 changed files with 28 additions and 15 deletions

View File

@ -2,6 +2,8 @@
namespace Slovocast\Domain\Entity;
use DateTimeImmutable;
use DateTime;
use Slovocast\Domain\Entity;
class User
@ -60,10 +62,18 @@ class User
}
if (isset($props['createdAt'])) {
if (is_string($props['createdAt'])) {
$props['createdAt'] = new DateTimeImmutable($props['createdAt']);
}
$user->setCreatedAt($props['createdAt']);
}
if (isset($props['updatedAt'])) {
if (is_string($props['updatedAt'])) {
$props['updatedAt'] = new DateTime($props['updatedAt']);
}
$user->setUpdatedAt($props['updatedAt']);
}

View File

@ -52,9 +52,11 @@ class UserRepository implements UserRepositoryInterface
{
$query = "SELECT * FROM users WHERE id = :id LIMIT 1";
$statement = $this->db->getConnection()->prepare($query);
$statement->execute([ ':id' => $id ]);
$results = $statement->fetch(\PDO::FETCH_ASSOC);
$results = $this->db->query($query, [ ':id' => $id ]);
if (!is_array($results) || empty($results)) {
throw new EntityNotFoundException("Unable to find User");
}
return $this->userFromQueryResults($results);
}
@ -70,7 +72,7 @@ class UserRepository implements UserRepositoryInterface
$query = "SELECT * FROM users WHERE email = :email LIMIT 1";
$results = $this->db->query($query, [ ':email' => $email ]);
if (!is_array($results) || count($results)) {
if (!is_array($results) || empty($results)) {
throw new EntityNotFoundException("Unable to find User");
}

View File

@ -3,6 +3,7 @@
namespace Slovocast\Tests\Domain\Repository;
use Slovocast\Domain\Repository\User\UserRepository;
use Slovocast\Exception\EntityNotFoundException;
use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface;
use Slovocast\Infrastructure\User\BasicUserAuthorization;
use Slovocast\Tests\TestCase;
@ -51,7 +52,7 @@ class UserRepositoryTest extends TestCase
);
}
public function getGettingUsers(): void
public function testGettingUsers(): void
{
$hasher = new BasicUserAuthorization();
$password = "password";
@ -61,22 +62,22 @@ class UserRepositoryTest extends TestCase
$databaseHandler
->query(Argument::type('string'), Argument::type('array'))
->willReturn([
[
'id' => 1,
'name' => 'Dave Test',
'email' => 'dave.test@email',
'password' => $hashedPassword,
'created_at' => '2024-11-17T00:00:00',
'updated_at' => '2024-11-17T00:00:00'
]
]);
'id' => 1,
'name' => 'Dave Test',
'email' => 'dave.test@email',
'password' => $hashedPassword,
'created_at' => '2024-11-17T00:00:00',
'updated_at' => '2024-11-17T00:00:00'
], []);
$userRepository = new UserRepository(
$databaseHandler->reveal(),
$hasher
);
$user = $user->get(1);
$user = $userRepository->get(1);
$this->assertEquals("Dave Test", $user->getName());
$this->expectException(EntityNotFoundException::class);
$userRepository->get(2);
}
}