mirror of
https://github.com/kevinveenbirkenbach/coding-challenge-online-shop.git
synced 2024-12-22 23:25:03 +01:00
Optimized order repository and implemented tests
This commit is contained in:
parent
aa18f2b18e
commit
541c472f15
@ -65,6 +65,8 @@ final class Order extends AbstractDefaultController implements OrderInterface
|
||||
$this->core->getBasket()->setCustomer($this->core->getUser());
|
||||
if($this->orderRepository->saveOrder($this->core->getBasket())){
|
||||
$this->core->setBasket(new OrderEntity());
|
||||
}else{
|
||||
throw new \Exception('Order could not be saved!');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ namespace repository\order;
|
||||
|
||||
use repository\AbstractRepository;
|
||||
use entity\order\OrderInterface as OrderEntityInterface;
|
||||
use core\CoreInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -20,13 +19,21 @@ final class Order extends AbstractRepository implements OrderInterface
|
||||
return $this->database->rollBack();
|
||||
}
|
||||
|
||||
private function saveOrderEntity(OrderEntityInterface $order):void{
|
||||
/**
|
||||
* Public for testing reasons
|
||||
* @param OrderEntityInterface $order
|
||||
*/
|
||||
public function saveOrderEntity(OrderEntityInterface $order):void{
|
||||
$statement = $this->database->prepare("INSERT INTO `order` (`customer`) VALUES (?);");
|
||||
$statement->execute([$order->getCustomer()->getId()]);
|
||||
$order->setId($this->database->lastInsertId());
|
||||
}
|
||||
|
||||
private function saveConnection(OrderEntityInterface $order):void{
|
||||
/**
|
||||
* Public for testing reasons
|
||||
* @param OrderEntityInterface $order
|
||||
*/
|
||||
public function saveConnection(OrderEntityInterface $order):void{
|
||||
foreach ($order->getProducts()->toArray() as $product){
|
||||
$statement = $this->database->prepare("INSERT INTO `order_product` (`product_id`,`order_id`) VALUES (?,?);");
|
||||
$statement->execute([$product->getId(),$order->getId()]);
|
||||
|
48
src/repository/order/OrderTest.php
Normal file
48
src/repository/order/OrderTest.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
namespace repository\order;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use core\Core;
|
||||
use entity\user\User;
|
||||
use repository\order\Order as OrderRepository;
|
||||
use entity\order\Order as OrderEntity;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*
|
||||
*/
|
||||
class OrderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var OrderRepository
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var Core
|
||||
*/
|
||||
protected $core;
|
||||
|
||||
/**
|
||||
* @var OrderEntity
|
||||
*/
|
||||
protected $order;
|
||||
|
||||
protected function setUp():void{
|
||||
$this->core = new Core();
|
||||
$user = new User();
|
||||
$user->setId(1);
|
||||
$this->core->setUser($user);
|
||||
$this->repository = new OrderRepository($this->core);
|
||||
$this->order = new OrderEntity();
|
||||
$this->order->setCustomer($user);
|
||||
}
|
||||
|
||||
public function testEntitySaver():void{
|
||||
$this->repository->saveOrder($this->order);
|
||||
$this->assertGreaterThan(0,$this->order->getId());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user