Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI-239: fix placing multiple orders #41

Merged
merged 1 commit into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Customer/CustomerFixture.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}