From cf41e30a58c37f33e13e0ee2fa6592a83784967c Mon Sep 17 00:00:00 2001 From: Okhin <okhin@okhin.fr> Date: Thu, 15 Nov 2018 17:48:20 +0100 Subject: [PATCH] Add a DonationCreate command --- src/LQDN/Command/DonationCreateCommand.php | 53 +++++++++++++++++++ src/LQDN/Handler/DonationHandler.php | 22 ++++++++ .../Handler/CounterpartHandlerTest.php | 2 +- .../Handler/DonationHandlerTest.php | 8 +++ tests/functional/Handler/UserHandlerTest.php | 18 +++++++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/LQDN/Command/DonationCreateCommand.php diff --git a/src/LQDN/Command/DonationCreateCommand.php b/src/LQDN/Command/DonationCreateCommand.php new file mode 100644 index 0000000..8e713c7 --- /dev/null +++ b/src/LQDN/Command/DonationCreateCommand.php @@ -0,0 +1,53 @@ +<?php + +namespace LQDN\Command; + +class DonationCreateCommand +{ + private $status; + private $datec; + private $somme; + private $userId; + private $public; + private $cumul; + + public function __construct($userId, $status, $datec, $somme, $public, $cumul) + { + $this->status = $status; + $this->userId = $userId; + $this->datec = $datec; + $this->somme = $somme; + $this->public = $public; + $this->cumul = $cumul; + } + + public function getUserId() + { + return $this->userId; + } + + public function getStatus() + { + return $this->status; + } + + public function getDateC() + { + return $this->datec; + } + + public function getSomme() + { + return $this->somme; + } + + public function getPublic() + { + return $this->public; + } + + public function getCumul() + { + return $this->cumul; + } +} diff --git a/src/LQDN/Handler/DonationHandler.php b/src/LQDN/Handler/DonationHandler.php index 94dad2e..34b17e8 100644 --- a/src/LQDN/Handler/DonationHandler.php +++ b/src/LQDN/Handler/DonationHandler.php @@ -3,6 +3,7 @@ namespace LQDN\Handler; use Doctrine\DBAL\Connection; +use LQDN\Command\DonationCreateCommand; use LQDN\Command\DonationInvalidateCommand; use LQDN\Command\DonationResetPdfCommand; use LQDN\Command\DonationValidateCommand; @@ -16,6 +17,27 @@ class DonationHandler $this->connection = $connection; } + /** + * Create a donation + * + * @param DonationCreateCommand $command + */ + public function handleDonationCreateCommand(DonationCreateCommand $command) + { + $query =<<<EOF +INSERT INTO dons(status, datec, somme, user_id, public, cumul) +VALUES(:status, :datec, :somme, :user_id, :public, :cumul) +EOF; + $stmt = $this->connection->prepare($query); + $stmt->bindvalue('status', $command->getStatus()); + $stmt->bindvalue('datec', $command->getDateC()); + $stmt->bindvalue('somme', $command->getSomme()); + $stmt->bindvalue('user_id', $command->getUserId()); + $stmt->bindvalue('public', $command->getPublic()); + $stmt->bindvalue('cumul', $command->getCumul()); + $stmt->execute(); + } + /** * Validate a donation. * diff --git a/tests/functional/Handler/CounterpartHandlerTest.php b/tests/functional/Handler/CounterpartHandlerTest.php index 6df472f..ecce1a6 100644 --- a/tests/functional/Handler/CounterpartHandlerTest.php +++ b/tests/functional/Handler/CounterpartHandlerTest.php @@ -21,7 +21,7 @@ class CounterpartHandlerTest extends FunctionalTest { $this->assertFalse($this->counterpartExists(3)); - $this->container['command_handler']->handle(new CounterpartCreateCommand(1, 1, 'pishirt', 4, 1, date("Y-m-d H:i:s"))); + $this->container['command_handler']->handle(new CounterpartCreateCommand(1, 1, 'pishirt', 4, 1, date("Y-m-d H:i:s"), '')); } public function testCounterpartChangeState() diff --git a/tests/functional/Handler/DonationHandlerTest.php b/tests/functional/Handler/DonationHandlerTest.php index e54a486..dec818a 100644 --- a/tests/functional/Handler/DonationHandlerTest.php +++ b/tests/functional/Handler/DonationHandlerTest.php @@ -5,6 +5,7 @@ namespace LQDN\Tests\Functional\Handler; use LQDN\Command\DonationInvalidateCommand; use LQDN\Command\DonationResetPdfCommand; use LQDN\Command\DonationValidateCommand; +use LQDN\Command\DonationCreateCommand; use LQDN\Tests\Functional\FunctionalTest; class DonationHandlerTest extends FunctionalTest @@ -32,6 +33,13 @@ class DonationHandlerTest extends FunctionalTest $this->assertEquals(2, $this->getDonationByUser(2)[0]['user_id']); } + public function testDonationCreateCommand() + { + $this->container['command_handler']->handle(new DonationCreateCommand(1, 1, date("Y-m-d H:M:s"), 50, 0, 0)); + + } + + /** * Retrieve a donation * diff --git a/tests/functional/Handler/UserHandlerTest.php b/tests/functional/Handler/UserHandlerTest.php index 3e0f652..a083917 100644 --- a/tests/functional/Handler/UserHandlerTest.php +++ b/tests/functional/Handler/UserHandlerTest.php @@ -3,6 +3,8 @@ namespace LQDN\Tests\Functional\Handler; use LQDN\Command\UserUpdateByAdminCommand; +use LQDN\Command\UserUpdateTotalCommand; +use LQDN\Command\UserUpdateCumulCommand; use LQDN\Tests\Functional\FunctionalTest; class UserHandlerTest extends FunctionalTest @@ -22,6 +24,22 @@ class UserHandlerTest extends FunctionalTest $this->assertSame('This is foobar avé dé accênts !', $user['commentaire']); } + public function testUserUpdateTotal() + { + $this->container['command_handler']->handle(new UserUpdateTotalCommand(1, 600)); + $user = $this->getUser(1); + + $this->assertSame(600, (int) $user['total']); + } + + public function testUserUpdateCumul() + { + $this->container['command_handler']->handle(new UserUpdateCumulCommand(1, 600)); + $user = $this->getUser(1); + + $this->assertSame(600, (int) $user['cumul']); + } + /** * Retrieve a given user. * -- GitLab