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; namespace Slovocast\Domain\Entity;
use DateTimeImmutable;
use DateTime;
use Slovocast\Domain\Entity; use Slovocast\Domain\Entity;
class User class User
@ -60,10 +62,18 @@ class User
} }
if (isset($props['createdAt'])) { if (isset($props['createdAt'])) {
if (is_string($props['createdAt'])) {
$props['createdAt'] = new DateTimeImmutable($props['createdAt']);
}
$user->setCreatedAt($props['createdAt']); $user->setCreatedAt($props['createdAt']);
} }
if (isset($props['updatedAt'])) { if (isset($props['updatedAt'])) {
if (is_string($props['updatedAt'])) {
$props['updatedAt'] = new DateTime($props['updatedAt']);
}
$user->setUpdatedAt($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"; $query = "SELECT * FROM users WHERE id = :id LIMIT 1";
$statement = $this->db->getConnection()->prepare($query); $results = $this->db->query($query, [ ':id' => $id ]);
$statement->execute([ ':id' => $id ]);
$results = $statement->fetch(\PDO::FETCH_ASSOC); if (!is_array($results) || empty($results)) {
throw new EntityNotFoundException("Unable to find User");
}
return $this->userFromQueryResults($results); return $this->userFromQueryResults($results);
} }
@ -70,7 +72,7 @@ class UserRepository implements UserRepositoryInterface
$query = "SELECT * FROM users WHERE email = :email LIMIT 1"; $query = "SELECT * FROM users WHERE email = :email LIMIT 1";
$results = $this->db->query($query, [ ':email' => $email ]); $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"); throw new EntityNotFoundException("Unable to find User");
} }

View File

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