From 1b14f1523b86736cbee1dcd2fa170b74b77db03d Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Mon, 18 Nov 2024 01:47:18 +0000 Subject: [PATCH] Refactor some of the testing, try and understand why the mocking for the DB Handler is returning null and not true --- app/composer.lock | 12 +++++------ .../Domain/Repository/User/UserRepository.php | 4 ++-- .../Domain/Repository/UserRepositoryTest.php | 19 +++--------------- app/tests/TestCase.php | 20 +++++++++++++++++++ 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/app/composer.lock b/app/composer.lock index a665e11..e41bde4 100644 --- a/app/composer.lock +++ b/app/composer.lock @@ -3928,16 +3928,16 @@ }, { "name": "twig/twig", - "version": "v3.14.2", + "version": "v3.15.0", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "0b6f9d8370bb3b7f1ce5313ed8feb0fafd6e399a" + "reference": "2d5b3964cc21d0188633d7ddce732dc8e874db02" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/0b6f9d8370bb3b7f1ce5313ed8feb0fafd6e399a", - "reference": "0b6f9d8370bb3b7f1ce5313ed8feb0fafd6e399a", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/2d5b3964cc21d0188633d7ddce732dc8e874db02", + "reference": "2d5b3964cc21d0188633d7ddce732dc8e874db02", "shasum": "" }, "require": { @@ -3991,7 +3991,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.14.2" + "source": "https://github.com/twigphp/Twig/tree/v3.15.0" }, "funding": [ { @@ -4003,7 +4003,7 @@ "type": "tidelift" } ], - "time": "2024-11-07T12:36:22+00:00" + "time": "2024-11-17T15:59:19+00:00" }, { "name": "vlucas/phpdotenv", diff --git a/app/src/Domain/Repository/User/UserRepository.php b/app/src/Domain/Repository/User/UserRepository.php index 6a86f75..3d7690f 100644 --- a/app/src/Domain/Repository/User/UserRepository.php +++ b/app/src/Domain/Repository/User/UserRepository.php @@ -11,7 +11,7 @@ use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; class UserRepository implements UserRepositoryInterface { - const INSERT_QUERY = "INSERT INTO users (email, password, name) + const CREATE_QUERY = "INSERT INTO users (email, password, name) VALUES (:email, :password, :name)"; public function __construct( @@ -70,7 +70,7 @@ class UserRepository implements UserRepositoryInterface public function create(User $user): bool { - $results = $this->db->insert(self::INSERT_QUERY, [ + $results = $this->db->insert(self::CREATE_QUERY, [ ':email' => $user->getEmail(), ':password' => $this->userAuth->hash($user->getPassword()), ':name' => $user->getName(), diff --git a/app/tests/Domain/Repository/UserRepositoryTest.php b/app/tests/Domain/Repository/UserRepositoryTest.php index 2e0bf43..80e3fd1 100644 --- a/app/tests/Domain/Repository/UserRepositoryTest.php +++ b/app/tests/Domain/Repository/UserRepositoryTest.php @@ -3,37 +3,24 @@ namespace Slovocast\Tests\Domain\Repository; use Slovocast\Domain\Repository\User\UserRepository; -use Slovocast\Domain\Entity\User; use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; use Slovocast\Infrastructure\User\BasicUserAuthorization; use Slovocast\Tests\TestCase; class UserRepositoryTest extends TestCase { - protected function getUser(): User - { - return User::fromArray([ - 'email' => 'dave@slovocast.com', - 'name' => 'Dave SH', - 'password' => 'hashed_password' - ]); - } - public function testRegisteringAUser(): void { - $user = $this->getUser(); + $user = $this->getUserFromArray(); $databaseHandler = $this->prophesize(DatabaseHandlerInterface::class); + + $databaseHandler->insert()->willReturn(true); $databaseHandler->getConnection()->willReturn(new class { public function lastInsertId(): int { return 100; } }); - $databaseHandler->insert(UserRepository::INSERT_QUERY, [ - ':name' => $user->getName(), - ':email' => $user->getEmail(), - ':password' => $user->getPassword() - ])->willReturn(true); $userRepository = new UserRepository( $databaseHandler->reveal(), diff --git a/app/tests/TestCase.php b/app/tests/TestCase.php index 87a076b..69f1a85 100644 --- a/app/tests/TestCase.php +++ b/app/tests/TestCase.php @@ -11,6 +11,7 @@ use Slim\Psr7\Headers; use Slim\Psr7\Request as SlimRequest; use Slim\Psr7\Uri; use Slovocast\Bootstrap; +use Slovocast\Domain\Entity\User; /** * This is the common test harness pattern lifted right out of the official @@ -60,4 +61,23 @@ class TestCase extends PHPUnit_TestCase $stream ); } + + /** + * Quickly generate a User new User entity for testing. + * + * @param array $user The User properties to call User:fromArray on + * @return User a new User entity + */ + protected function getUserFromArray(array $user = []): User + { + if (empty($user)) { + $user = [ + 'email' => 'dave@slovocast.com', + 'name' => 'Dave SH', + 'password' => 'hashed_password' + ]; + } + + return User::fromArray($user); + } }