diff --git a/.gitignore b/.gitignore index f4686f8..553b88d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.idea/ /vendor/ /composer.lock +/phpunit.xml diff --git a/src/CoreShop2VueStorefrontBundle/Bridge/Response/ResponseBodyCreator.php b/src/CoreShop2VueStorefrontBundle/Bridge/Response/ResponseBodyCreator.php index 8576f15..bb57a9d 100644 --- a/src/CoreShop2VueStorefrontBundle/Bridge/Response/ResponseBodyCreator.php +++ b/src/CoreShop2VueStorefrontBundle/Bridge/Response/ResponseBodyCreator.php @@ -5,9 +5,8 @@ use Carbon\Carbon; use CoreShop\Component\Address\Model\AddressInterface; use CoreShop\Component\Core\Model\CustomerInterface; -use CoreShop\Component\Core\Model\ProductInterface; -use CoreShop\Component\Resource\Repository\RepositoryInterface; use CoreShop\Component\Index\Model\IndexInterface; +use CoreShop\Component\Resource\Repository\RepositoryInterface; class ResponseBodyCreator { @@ -109,18 +108,6 @@ private function getAddress(AddressInterface $defaultAddress, int $customerId): return $address; } - public function stockResponse(ProductInterface $product): array - { - $stock = []; - $stock['item_id'] = $product->getId(); - $stock['product_id'] = $product->getId(); - $stock['stock_id'] = 1; - $stock['qty'] = $product->getOnHand() ?: 0; - $stock['is_in_stock'] = $product->getOnHand() > 0 ? true : false; - - return $stock; - } - protected function formatDate(string $dateTime): string { return Carbon::createFromTimestamp($dateTime)->format('Y-m-d H:i:s'); diff --git a/src/CoreShop2VueStorefrontBundle/Controller/StockController.php b/src/CoreShop2VueStorefrontBundle/Controller/StockController.php index 37839bc..b8fa007 100644 --- a/src/CoreShop2VueStorefrontBundle/Controller/StockController.php +++ b/src/CoreShop2VueStorefrontBundle/Controller/StockController.php @@ -4,37 +4,33 @@ use CoreShop\Component\Core\Model\ProductInterface; use CoreShop\Component\Core\Repository\ProductRepositoryInterface; -use CoreShop2VueStorefrontBundle\Bridge\Response\ResponseBodyCreator; use Exception; -use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; -use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Routing\Annotation\Route; class StockController extends AbstractController { /** - * @Route("/vsbridge/stock/check") - * @Method("GET") + * @Route("/vsbridge/stock/check", methods={"GET"}) * * @param Request $request * @param ProductRepositoryInterface $productRepository - * @param ResponseBodyCreator $responseBodyCreator * * @return JsonResponse */ public function checkStockForSku( Request $request, - ProductRepositoryInterface $productRepository, - ResponseBodyCreator $responseBodyCreator - ) { + ProductRepositoryInterface $productRepository + ): JsonResponse + { try { /** @var ProductInterface $product */ $product = $productRepository->findOneBy(['sku' => $request->get('sku')]); return $this->json([ 'code' => 200, - 'result' => $responseBodyCreator->stockResponse($product) + 'result' => $this->stockResponse($product) ]); } catch (Exception $exception) { return $this->json([ @@ -43,4 +39,18 @@ public function checkStockForSku( ]); } } + + private function stockResponse(ProductInterface $product): array + { + $stock = []; + $stock['item_id'] = $product->getId(); + $stock['product_id'] = $product->getId(); + $stock['stock_id'] = 1; + $stock['qty'] = $product->getOnHand() ?: 0; + $stock['is_in_stock'] = $product->getOnHand() > 0 ? true : false; + $stock['manage_stock'] = true; + + return $stock; + } + } diff --git a/src/CoreShop2VueStorefrontBundle/Tests/Controller/StockControllerTest.php b/src/CoreShop2VueStorefrontBundle/Tests/Controller/StockControllerTest.php new file mode 100644 index 0000000..789b2c4 --- /dev/null +++ b/src/CoreShop2VueStorefrontBundle/Tests/Controller/StockControllerTest.php @@ -0,0 +1,60 @@ +mockRequest = $this->prophesize(Request::class); + $this->mockRepository = $this->prophesize(ProductRepositoryInterface::class); + + $this->mockContainer = $this->prophesize(ContainerInterface::class); + $this->mockContainer->has('serializer')->willReturn(false); + + $this->controller = new StockController(); + $this->controller->setContainer($this->mockContainer->reveal()); + } + + /** + * @test + */ + public function checkStockForSku(): void + { + $this->mockRequest->get('sku')->willReturn('1234'); + + $mockProduct = $this->prophesize(Product::class); + $mockProduct->getId()->willReturn('5678'); + $mockProduct->getOnHand()->willReturn(true); + + $this->mockRepository->findOneBy(['sku' => '1234'])->willReturn($mockProduct->reveal()); + + $response = $this->controller->checkStockForSku($this->mockRequest->reveal(), $this->mockRepository->reveal()); + + self::assertSame(<<getContent() + ); + } +}