From 541c472f153316b289b0cf9f1f9bf43717ba785f Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sun, 15 Jul 2018 16:09:37 +0200 Subject: [PATCH] Optimized order repository and implemented tests --- src/controller/order/Order.php | 2 ++ src/repository/order/Order.php | 13 ++++++-- src/repository/order/OrderTest.php | 48 ++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/repository/order/OrderTest.php diff --git a/src/controller/order/Order.php b/src/controller/order/Order.php index 3c3158f..6c81133 100644 --- a/src/controller/order/Order.php +++ b/src/controller/order/Order.php @@ -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!'); } } } diff --git a/src/repository/order/Order.php b/src/repository/order/Order.php index acfc797..9214c05 100644 --- a/src/repository/order/Order.php +++ b/src/repository/order/Order.php @@ -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()]); diff --git a/src/repository/order/OrderTest.php b/src/repository/order/OrderTest.php new file mode 100644 index 0000000..1ab2f8c --- /dev/null +++ b/src/repository/order/OrderTest.php @@ -0,0 +1,48 @@ +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()); + } +} +