From 7ea4ad51f5c2704f3d12af605179eee1d1c2d427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20A=C3=9Fmann?= Date: Wed, 15 Apr 2020 18:22:50 +0200 Subject: [PATCH] CI-239: fix placing multiple orders --- src/Customer/CustomerFixture.php | 10 +++ src/Sales/OrderBuilder.php | 6 +- tests/Sales/OrderBuilderTest.php | 104 ++++++++++++++++++++++++------- 3 files changed, 97 insertions(+), 23 deletions(-) diff --git a/src/Customer/CustomerFixture.php b/src/Customer/CustomerFixture.php index b3dcd1c..7c62b1f 100644 --- a/src/Customer/CustomerFixture.php +++ b/src/Customer/CustomerFixture.php @@ -82,4 +82,14 @@ public function login(Session $session = null) } $session->setCustomerId($this->getId()); } + + public function logout(Session $session = null) + { + if ($session === null) { + $objectManager = Bootstrap::getObjectManager(); + $session = $objectManager->get(Session::class); + } + + $session->logout(); + } } diff --git a/src/Sales/OrderBuilder.php b/src/Sales/OrderBuilder.php index bd700df..cd99fe7 100644 --- a/src/Sales/OrderBuilder.php +++ b/src/Sales/OrderBuilder.php @@ -138,6 +138,10 @@ static function (ProductBuilder $productBuilder) { $checkout = $checkout->withPaymentMethodCode($builder->paymentMethod); } - return $checkout->placeOrder(); + $order = $checkout->placeOrder(); + + $customerFixture->logout(); + + return $order; } } diff --git a/tests/Sales/OrderBuilderTest.php b/tests/Sales/OrderBuilderTest.php index 37dc747..5be1e77 100644 --- a/tests/Sales/OrderBuilderTest.php +++ b/tests/Sales/OrderBuilderTest.php @@ -18,9 +18,9 @@ class OrderBuilderTest extends TestCase { /** - * @var OrderFixture + * @var OrderFixture[] */ - private $orderFixture; + private $orderFixtures; /** * @var OrderRepositoryInterface @@ -40,7 +40,7 @@ protected function setUp() */ protected function tearDown() { - OrderFixtureRollback::create()->execute($this->orderFixture); + OrderFixtureRollback::create()->execute(...$this->orderFixtures); parent::tearDown(); } @@ -55,12 +55,13 @@ protected function tearDown() */ public function createOrder() { - $this->orderFixture = new OrderFixture( + $orderFixture = new OrderFixture( OrderBuilder::anOrder()->build() ); + $this->orderFixtures[] = $orderFixture; - self::assertInstanceOf(OrderInterface::class, $this->orderRepository->get($this->orderFixture->getId())); - self::assertNotEmpty($this->orderFixture->getOrderItemQtys()); + self::assertInstanceOf(OrderInterface::class, $this->orderRepository->get($orderFixture->getId())); + self::assertNotEmpty($orderFixture->getOrderItemQtys()); } /** @@ -73,12 +74,13 @@ public function createOrder() */ public function createOrderWithProduct() { - $this->orderFixture = new OrderFixture( + $orderFixture = new OrderFixture( OrderBuilder::anOrder()->withProducts(ProductBuilder::aSimpleProduct())->build() ); + $this->orderFixtures[] = $orderFixture; - self::assertInstanceOf(OrderInterface::class, $this->orderRepository->get($this->orderFixture->getId())); - self::assertCount(1, $this->orderFixture->getOrderItemQtys()); + self::assertInstanceOf(OrderInterface::class, $this->orderRepository->get($orderFixture->getId())); + self::assertCount(1, $orderFixture->getOrderItemQtys()); } /** @@ -91,15 +93,16 @@ public function createOrderWithProduct() */ public function createOrderWithProducts() { - $this->orderFixture = new OrderFixture( + $orderFixture = new OrderFixture( OrderBuilder::anOrder()->withProducts( ProductBuilder::aSimpleProduct()->withSku('foo'), ProductBuilder::aSimpleProduct()->withSku('bar') )->build() ); + $this->orderFixtures[] = $orderFixture; - self::assertInstanceOf(OrderInterface::class, $this->orderRepository->get($this->orderFixture->getId())); - self::assertCount(2, $this->orderFixture->getOrderItemQtys()); + self::assertInstanceOf(OrderInterface::class, $this->orderRepository->get($orderFixture->getId())); + self::assertCount(2, $orderFixture->getOrderItemQtys()); } /** @@ -117,13 +120,14 @@ public function createOrderWithCustomer() ->withEmail($customerEmail) ->withAddresses(AddressBuilder::anAddress()->asDefaultBilling()->asDefaultShipping()); - $this->orderFixture = new OrderFixture( + $orderFixture = new OrderFixture( OrderBuilder::anOrder()->withCustomer($customerBuilder)->build() ); + $this->orderFixtures[] = $orderFixture; - self::assertInstanceOf(OrderInterface::class, $this->orderRepository->get($this->orderFixture->getId())); - self::assertSame($customerEmail, $this->orderFixture->getCustomerEmail()); - self::assertNotEmpty($this->orderFixture->getOrderItemQtys()); + self::assertInstanceOf(OrderInterface::class, $this->orderRepository->get($orderFixture->getId())); + self::assertSame($customerEmail, $orderFixture->getCustomerEmail()); + self::assertNotEmpty($orderFixture->getOrderItemQtys()); } /** @@ -160,7 +164,7 @@ public function createOrderWithCart() $cartBuilder = $cartBuilder->withSimpleProduct($sku, $qty); } - $this->orderFixture = new OrderFixture( + $orderFixture = new OrderFixture( OrderBuilder::anOrder() ->withProducts(...$productBuilders) ->withCustomer($customerBuilder) @@ -168,11 +172,67 @@ public function createOrderWithCart() ->withPaymentMethod($paymentMethod)->withShippingMethod($shippingMethod) ->build() ); + $this->orderFixtures[] = $orderFixture; - self::assertInstanceOf(OrderInterface::class, $this->orderRepository->get($this->orderFixture->getId())); - self::assertSame($customerEmail, $this->orderFixture->getCustomerEmail()); - self::assertEmpty(array_diff($cartItems, $this->orderFixture->getOrderItemQtys())); - self::assertSame($paymentMethod, $this->orderFixture->getPaymentMethod()); - self::assertSame($shippingMethod, $this->orderFixture->getShippingMethod()); + self::assertInstanceOf(OrderInterface::class, $this->orderRepository->get($orderFixture->getId())); + self::assertSame($customerEmail, $orderFixture->getCustomerEmail()); + self::assertEmpty(array_diff($cartItems, $orderFixture->getOrderItemQtys())); + self::assertSame($paymentMethod, $orderFixture->getPaymentMethod()); + self::assertSame($shippingMethod, $orderFixture->getShippingMethod()); + } + + /** + * Create multiple orders. Assert all of them were successfully built. + * + * @test + * @throws \Exception + */ + public function createMultipleOrders() + { + $shippingMethod = 'flatrate_flatrate'; + + // first order, simple + $orderFixture = new OrderFixture( + OrderBuilder::anOrder() + ->withShippingMethod($shippingMethod) + ->build() + ); + $this->orderFixtures[] = $orderFixture; + + // second order, with specified cart + $cartBuilder = CartBuilder::forCurrentSession(); + $orderWithCartFixture = new OrderFixture( + OrderBuilder::anOrder() + ->withShippingMethod($shippingMethod) + ->withProducts(ProductBuilder::aSimpleProduct()->withSku('bar')) + ->withCart($cartBuilder->withSimpleProduct('bar', 3)) + ->build() + ); + $this->orderFixtures[] = $orderWithCartFixture; + + // third order, with specified customer + $orderWithCustomerFixture = new OrderFixture( + OrderBuilder::anOrder() + ->withShippingMethod($shippingMethod) + ->withCustomer( + CustomerBuilder::aCustomer() + ->withAddresses( + AddressBuilder::anAddress(null, 'de_AT') + ->asDefaultBilling() + ->asDefaultShipping() + ) + ) + ->build() + ); + $this->orderFixtures[] = $orderWithCustomerFixture; + + // assert all fixtures were created with separate customers. + self::assertCount(3, $this->orderFixtures); + self::assertContainsOnlyInstancesOf(OrderFixture::class, $this->orderFixtures); + + $customerIds[$orderFixture->getCustomerId()] = 1; + $customerIds[$orderWithCartFixture->getCustomerId()] = 1; + $customerIds[$orderWithCustomerFixture->getCustomerId()] = 1; + self::assertCount(3, $customerIds); } }