From c5feb240c3654a3f711b1139fa42f94366884789 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Sun, 2 Jun 2024 21:26:10 -0400 Subject: [PATCH] Fix the warnings on the test case. --- app/composer.json | 2 +- .../Controller/User/RegisterUserAction.php | 10 +++++----- app/src/Domain/Entity/User.php | 6 +++--- .../User/RegisterUserActionTest.php | 20 +++++++++++++++++++ 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/app/composer.json b/app/composer.json index f26f1ab..611ed93 100644 --- a/app/composer.json +++ b/app/composer.json @@ -30,6 +30,6 @@ } }, "scripts": { - "test": "./vendor/bin/phpunit tests" + "test": "./vendor/bin/phpunit tests --display-warnings" } } diff --git a/app/src/Controller/User/RegisterUserAction.php b/app/src/Controller/User/RegisterUserAction.php index 324eaa1..8c9d0b5 100644 --- a/app/src/Controller/User/RegisterUserAction.php +++ b/app/src/Controller/User/RegisterUserAction.php @@ -29,12 +29,12 @@ class RegisterUserAction extends Controller 'password' => $requestData['password'], ]); - try { - $this->userRepository->save($user); + $result = $this->userRepository->save($user); + + if ($result) { return $this->render('user/success.twig'); - } catch (\Exception $e) { - $response = $this->render('user/register.twig'); - return $response->withStatus(400); + } else { + return $this->render('user/register.twig')->withStatus(400); } } diff --git a/app/src/Domain/Entity/User.php b/app/src/Domain/Entity/User.php index b34f5fa..b8e2b09 100644 --- a/app/src/Domain/Entity/User.php +++ b/app/src/Domain/Entity/User.php @@ -55,15 +55,15 @@ class User { $user = new self($props['email'], $props['password'], $props['name']); - if ($props['id']) { + if (array_key_exists('id', $props)) { $user->setId($props['id']); } - if ($props['createdAt']) { + if (array_key_exists('createdAt', $props)) { $user->setCreatedAt($props['createdAt']); } - if ($props['updatedAt']) { + if (array_key_exists('updatedAt', $props)) { $user->setUpdatedAt($props['updatedAt']); } diff --git a/app/tests/Controller/User/RegisterUserActionTest.php b/app/tests/Controller/User/RegisterUserActionTest.php index 8a9fff2..7434005 100644 --- a/app/tests/Controller/User/RegisterUserActionTest.php +++ b/app/tests/Controller/User/RegisterUserActionTest.php @@ -44,6 +44,26 @@ class RegisterUserActionTest extends TestCase public function testFailingRegistration(): void { + $user = $this->getUser(); + $prophecy = $this->getProphet() + ->prophesize(UserRepositoryInterface::class); + $prophecy->save($user)->willReturn(false); + $app = $this->getAppInstance(); + + /** @var $container \DI\Container */ + $container = $app->getContainer(); + $container->set(UserRepositoryInterface::class, $prophecy->reveal()); + + $request = $this->createRequest('POST', '/users/register') + ->withParsedBody([ + 'email' => 'dave@slovocast.com', + 'name' => 'Dave SH', + 'password' => 'hashed_password', + 'checked_password' => 'hashed_password' + ]); + + $response = $app->handle($request); + $this->assertEquals(400, $response->getStatusCode()); } }