Commit 51d8b106 authored by okhin's avatar okhin 🚴

Building the framework itemsfor Counterparts

parent 678e4014
Pipeline #2234 failed with stages
in 0 seconds
<?php
namespace LQDN\Command;
class CounterpartChangeStateCommand
{
private $counterpartId;
private $state;
public function __construct($counterpartId, $state)
{
$this->counterpartId = $counterpartId;
$this->state = $state;
}
public function getCounterpartId()
{
return $this->counterpartId;
}
public function getState()
{
return $this->state;
}
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace LQDN\Command; namespace LQDN\Command;
class CounterPartCreateCommand class CounterpartCreateCommand
{ {
private $counterpartId; private $counterpartId;
private $adresseId; private $adresseId;
...@@ -12,7 +12,7 @@ class CounterPartCreateCommand ...@@ -12,7 +12,7 @@ class CounterPartCreateCommand
private $status; private $status;
private $date; private $date;
public function __construct($adresseId, $userId, $quoi, $taille, $status, $date) public function __construct($addressId, $userId, $quoi, $taille, $status, $date)
{ {
$this->userId = $userId; $this->userId = $userId;
$this->addressId = $addressId; $this->addressId = $addressId;
...@@ -29,7 +29,7 @@ class CounterPartCreateCommand ...@@ -29,7 +29,7 @@ class CounterPartCreateCommand
public function getAddressId() public function getAddressId()
{ {
return $this->addressid; return $this->addressId;
} }
public function getQuoi() public function getQuoi()
......
<?php
namespace LQDN\Command;
class CounterpartDeleteCommand
{
private $counterpartId;
public function __construct($counterpartId)
{
$this->counterpartId = $counterpartId;
}
public function getCounterpartId()
{
return $this->counterpartId;
}
}
...@@ -56,4 +56,91 @@ EOQ; ...@@ -56,4 +56,91 @@ EOQ;
return $counterparts; return $counterparts;
} }
/**
* Return all counterparts in a specific status
*
* @param mixed $status
*
* @return array
*/
public function findByStatus($status)
{
$status = (int) $status;
$counterparts = [];
$stmt = $this->connection->executeQuery('SELECT * FROM contreparties WHERE status = :status', ['status' => $status]);
while ($counterpart = $stmt->fetch()) {
$counterpart['pdf_id'] = '';
$counterpart['pdf_nom'] = '';
$counterpart['pdf_url'] = '';
// Of course, piplomes are messy
// @TODO: This part is probably not working.
if ('piplome' === $counterpart['quoi']) {
$query = <<<EOQ
SELECT d.id as id, a.nom as pseudo, d.pdf as pdf
FROM dons d
JOIN adresses a ON a.id = d.adresse_id
JOIN contreparties c ON c.id = d.taille
WHERE d.taille = :size
EOQ;
$pdf = $this->connection->fetchAssoc($query, [
'size' => $counterpart['taille']
]);
if (false !== $pdf) {
$counterpart['pdf_id'] = $pdf['id'];
$counterpart['pdf_nom'] = $pdf['pseudo'];
$counterpart['pdf_url'] = $pdf['pdf'];
}
}
$counterparts[$counterpart['id']] = $counterpart;
}
return $counterparts;
}
/** Return all counterparts by quoi
*
* @param mixed $quoi
*
* @return array
*/
public function findByQuoi($quoi)
{
$quoi = (string) $quoi;
$counterparts = [];
$stmt = $this->connection->executeQuery('SELECT * FROM contreparties WHERE quoi = :quoi', ['quoi' => $quoi]);
while ($counterpart = $stmt->fetch()) {
$counterpart['pdf_id'] = '';
$counterpart['pdf_nom'] = '';
$counterpart['pdf_url'] = '';
// Of course, piplomes are messy
// @TODO: This part is probably not working.
if ('piplome' === $counterpart['quoi']) {
$query = <<<EOQ
SELECT d.id as id, a.nom as pseudo, d.pdf as pdf
FROM dons d
JOIN adresses a ON a.id = d.adresse_id
JOIN contreparties c ON c.id = d.taille
WHERE d.taille = :size
EOQ;
$pdf = $this->connection->fetchAssoc($query, [
'size' => $counterpart['taille']
]);
if (false !== $pdf) {
$counterpart['pdf_id'] = $pdf['id'];
$counterpart['pdf_nom'] = $pdf['pseudo'];
$counterpart['pdf_url'] = $pdf['pdf'];
}
}
$counterparts[$counterpart['id']] = $counterpart;
}
return $counterparts;
}
} }
...@@ -67,7 +67,6 @@ EOF; ...@@ -67,7 +67,6 @@ EOF;
* Check if an address already exists. * Check if an address already exists.
* *
* @param int $userId * @param int $userId
* @param string $alias
* *
* @return bool * @return bool
*/ */
......
...@@ -5,7 +5,7 @@ namespace LQDN\Handler; ...@@ -5,7 +5,7 @@ namespace LQDN\Handler;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use LQDN\Command\CounterpartCreateCommand; use LQDN\Command\CounterpartCreateCommand;
use LQDN\Command\CounterpartDeleteCommand; use LQDN\Command\CounterpartDeleteCommand;
use LQDN\Command\CounterpartUpdateCommand; use LQDN\Command\CounterpartChangeStateCommand;
use LQDN\Exception\CounterpartAlreadyExistsException; use LQDN\Exception\CounterpartAlreadyExistsException;
class CounterpartHandler class CounterpartHandler
...@@ -25,9 +25,10 @@ class CounterpartHandler ...@@ -25,9 +25,10 @@ class CounterpartHandler
public function handleCounterpartDeleteCommand(CounterpartDeleteCommand $command) public function handleCounterpartDeleteCommand(CounterpartDeleteCommand $command)
{ {
$counterpartId = $command->getCounterpartId(); $counterpartId = $command->getCounterpartId();
// Let's check if the counterpart exist
$this->connection->executeUpdate( $this->connection->executeUpdate(
"DELETE FROM counterparts WHERE id = :id", "DELETE FROM contreparties WHERE id = :id",
['id' => $counterpartId()] ['id' => $counterpartId]
); );
} }
...@@ -39,18 +40,75 @@ class CounterpartHandler ...@@ -39,18 +40,75 @@ class CounterpartHandler
public function handleCounterpartCreateCommand(CounterpartCreateCommand $command) public function handleCounterpartCreateCommand(CounterpartCreateCommand $command)
{ {
$userId = $command->getUserId(); $userId = $command->getUserId();
$adresseId = $command->getAdressId(); $adresseId = $command->getAddressId();
$query =<<<EOF $query =<<<EOF
REPLACE INTO counterparts(datec, user_id, addresse_id, quoi, taille, status) INSERT INTO contreparties(datec, user_id, adresse_id, quoi, taille, status)
VALUES (:datec, :user_id, :addresse_id, :quoi, :taille, :status) VALUES (:datec, :user_id, :adresse_id, :quoi, :taille, :status)
EOF; EOF;
$stmt = $this->connection->prepare($query); $stmt = $this->connection->prepare($query);
$stmt->bindValue('datec', $command->getDateCreation());
$stmt->bindValue('user_id', $command->getUserId()); $stmt->bindValue('user_id', $command->getUserId());
$stmt->bindValue('adresse_id', $command->getAddressId()); $stmt->bindValue('adresse_id', $command->getAddressId());
$stmt->bindValue('quoi', $command->getQuoi()); $stmt->bindValue('quoi', $command->getQuoi());
$stmt->bindValue('taille', $command->getTaille()); $stmt->bindValue('taille', $command->getTaille());
$stmt->bindValue('datec', $command->getDate());
$stmt->bindValue('status', $command->getStatus()); $stmt->bindValue('status', $command->getStatus());
$stmt->execute();
}
/**
* Chnge the state of a counterpart
*
* @param CounterpartChangeStateCommand $command
*/
public function handleCounterpartChangeStateCommand(CounterpartChangeStateCommand $command)
{
$counterpartId = $command->getCounterpartId();
$status = $command->getState();
$this->connection->executeUpdate(
"UPDATE contreparties SET status = :status WHERE id=:id",
[
'status' => $status,
'id' => $counterpartId,
]
);
}
/**
* Test if a counterpart exists
*
* @param int $counterpartId
*
* @return bool
*/
private function counterpartExists($counterpartId)
{
return (bool) $this->connection->fetchColumn(
"SELECT 1 FROM contreparties WHERE id = :counterpart_id",
[
'counterpart_id' => $counterpartId,
],
0
);
}
/**
* Test if the counterpart can be deleted
*
* @param int $countepartId
*
* @return bool
*/
private function counterpartUsed($counterpartId)
{
$status = (int) $this->connection->fetchColumn(
"SELECT status FROM contreparties WHERE id = :id",
[
'id' => $counterpartId,
],
0
);
return ($status == 2); // status 2 is delivered counterparts
} }
} }
...@@ -29,4 +29,50 @@ class CounterpartFinderTest extends FunctionalTest ...@@ -29,4 +29,50 @@ class CounterpartFinderTest extends FunctionalTest
]; ];
$this->assertEquals($expectedCounterpart, $firstCounterpart); $this->assertEquals($expectedCounterpart, $firstCounterpart);
} }
public function testFindByQuoi()
{
$counterparts = $this->container['counterpart_finder']->findByQuoi('piplome');
$this->assertCount(1, $counterparts);
$firstCounterpart = reset($counterparts);
// Check the first counterpart
$expectedCounterpart = [
'id' => '2',
'user_id' => '2',
'datec' => '2016-06-22 12:34:00',
'quoi' => 'piplome',
'taille' => '2',
'status' => '1',
'adresse_id' => null,
'pdf_id' => '1',
'pdf_nom' => 'Main',
'pdf_url' => 'pdf',
'commentaire' => '',
];
$this->assertEquals($expectedCounterpart, $firstCounterpart);
}
public function testFindById()
{
$counterparts = $this->container['counterpart_finder']->findByStatus(1);
$this->assertCount(2, $counterparts);
$firstCounterpart = reset($counterparts);
$expectedCounterpart = [
'id' => '1',
'datec' => '2016-06-22 12:34:00',
'user_id' => '1',
'quoi' => 'pishirt', // [piplome|pibag|pishirt|hoodie]
'taille' => '8',
'status' => '1',
'adresse_id' => '1',
'pdf_id' => '',
'pdf_nom' => '',
'pdf_url' => '',
'commentaire' => '',
];
$this->assertEquals($expectedCounterpart, $firstCounterpart);
}
} }
<?php
namespace LQDN\Tests\Functional\Handler;
use LQDN\Command\CounterpartCreateCommand;
use LQDN\Command\CounterpartDeleteCommand;
use LQDN\Command\CounterpartChangeStateCommand;
use LQDN\Handler\CounterpartHandler;
use LQDN\Tests\Functional\FunctionalTest;
class CounterpartHandlerTest extends FunctionalTest
{
public function testCounterpartDelete()
{
$this->assertTrue($this->counterpartExists(1));
$this->container['command_handler']->handle(new CounterpartDeleteCommand(1));
}
public function testCounterpartCreate()
{
$this->assertFalse($this->counterpartExists(3));
$this->container['command_handler']->handle(new CounterpartCreateCommand(1, 1, 'pishirt', 4, 1, date("Y-m-d H:i:s")));
}
public function testCounterpartChangeState()
{
$this->assertTrue($this->counterpartExists(1));
$this->container['command_handler']->handle(new CounterpartChangeStateCommand(1, 2));
}
/**
* Check if a counterpart exists in BDD
*
* @param int $id
*
* @return bool
*/
private function counterpartExists($id)
{
return (bool) $this->container['db']->fetchColumn("SELECT 1 FROM contreparties WHERE id = $id");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment