Skip to content

Commit

Permalink
Merge pull request #41 from netresearch/CI-239
Browse files Browse the repository at this point in the history
CI-239: fix placing multiple orders
  • Loading branch information
schmengler committed Apr 20, 2020
2 parents 511710b + 7ea4ad5 commit 7a7db57
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 23 deletions.
10 changes: 10 additions & 0 deletions src/Customer/CustomerFixture.php
Expand Up @@ -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();
}
}
6 changes: 5 additions & 1 deletion src/Sales/OrderBuilder.php
Expand Up @@ -138,6 +138,10 @@ static function (ProductBuilder $productBuilder) {
$checkout = $checkout->withPaymentMethodCode($builder->paymentMethod);
}

return $checkout->placeOrder();
$order = $checkout->placeOrder();

$customerFixture->logout();

return $order;
}
}
104 changes: 82 additions & 22 deletions tests/Sales/OrderBuilderTest.php
Expand Up @@ -18,9 +18,9 @@
class OrderBuilderTest extends TestCase
{
/**
* @var OrderFixture
* @var OrderFixture[]
*/
private $orderFixture;
private $orderFixtures;

/**
* @var OrderRepositoryInterface
Expand All @@ -40,7 +40,7 @@ protected function setUp()
*/
protected function tearDown()
{
OrderFixtureRollback::create()->execute($this->orderFixture);
OrderFixtureRollback::create()->execute(...$this->orderFixtures);

parent::tearDown();
}
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -160,19 +164,75 @@ public function createOrderWithCart()
$cartBuilder = $cartBuilder->withSimpleProduct($sku, $qty);
}

$this->orderFixture = new OrderFixture(
$orderFixture = new OrderFixture(
OrderBuilder::anOrder()
->withProducts(...$productBuilders)
->withCustomer($customerBuilder)
->withCart($cartBuilder)
->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);
}
}

0 comments on commit 7a7db57

Please sign in to comment.