Optimized order repository and implemented tests

This commit is contained in:
Kevin Frantz 2018-07-15 16:09:37 +02:00
parent aa18f2b18e
commit 541c472f15
3 changed files with 60 additions and 3 deletions

View File

@ -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!');
}
}
}

View File

@ -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()]);

View 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());
}
}