From 8dce6a1603c0d93de9f8cb1d776307845af9d415 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Mon, 18 Nov 2024 20:31:44 -0500 Subject: [PATCH] Test throwing EntityNotFoundException when trying to get nothing from the repository. --- app/src/Domain/Entity/User.php | 10 ++++++++ .../Domain/Repository/User/UserRepository.php | 10 ++++---- .../Domain/Repository/UserRepositoryTest.php | 23 ++++++++++--------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/app/src/Domain/Entity/User.php b/app/src/Domain/Entity/User.php index 4c9f8ee..efdeab4 100644 --- a/app/src/Domain/Entity/User.php +++ b/app/src/Domain/Entity/User.php @@ -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']); } diff --git a/app/src/Domain/Repository/User/UserRepository.php b/app/src/Domain/Repository/User/UserRepository.php index 713f1c7..72e50ec 100644 --- a/app/src/Domain/Repository/User/UserRepository.php +++ b/app/src/Domain/Repository/User/UserRepository.php @@ -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"); } diff --git a/app/tests/Domain/Repository/UserRepositoryTest.php b/app/tests/Domain/Repository/UserRepositoryTest.php index 36044d6..91e68f2 100644 --- a/app/tests/Domain/Repository/UserRepositoryTest.php +++ b/app/tests/Domain/Repository/UserRepositoryTest.php @@ -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); } }