-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from AmpersandHQ/test-cancelling-order-with-m…
…issing-order-sources Fix cancelling an order with missing `order_sources` data
- Loading branch information
Showing
3 changed files
with
222 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Ampersand\DisableStockReservation\Test\Integration\Cancel; | ||
|
||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use TddWizard\Fixtures\Catalog\ProductBuilder; | ||
use TddWizard\Fixtures\Catalog\ProductFixture; | ||
use TddWizard\Fixtures\Checkout\CustomerCheckout; | ||
use TddWizard\Fixtures\Checkout\CartBuilder; | ||
use TddWizard\Fixtures\Customer\CustomerFixture; | ||
use TddWizard\Fixtures\Customer\CustomerBuilder; | ||
use TddWizard\Fixtures\Customer\AddressBuilder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CancelOrderTest extends TestCase | ||
{ | ||
/** @var ObjectManagerInterface */ | ||
private $objectManager; | ||
|
||
/** @var ProductRepositoryInterface */ | ||
private $productRepository; | ||
|
||
/** @var ProductFixture */ | ||
private $productFixture; | ||
|
||
/** @var CustomerFixture */ | ||
private $customerFixture; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->objectManager = Bootstrap::getObjectManager(); | ||
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); | ||
$this->productRepository->cleanCache(); | ||
} | ||
|
||
public function testCancelOrderBaseline() | ||
{ | ||
$sku = uniqid('cancel_order_baseline'); | ||
|
||
/** | ||
* Create a product with 5 qty | ||
*/ | ||
$this->productFixture = new ProductFixture( | ||
ProductBuilder::aSimpleProduct() | ||
->withSku($sku) | ||
->withPrice(10) | ||
->withStockQty(5) | ||
->withIsInStock(true) | ||
->build() | ||
); | ||
|
||
/** | ||
* Create a customer and login | ||
*/ | ||
$this->customerFixture = new CustomerFixture(CustomerBuilder::aCustomer()->withAddresses( | ||
AddressBuilder::anAddress()->asDefaultBilling(), | ||
AddressBuilder::anAddress()->asDefaultShipping() | ||
)->build()); | ||
$this->customerFixture->login(); | ||
|
||
/** | ||
* Order 1 qty | ||
*/ | ||
$checkout = CustomerCheckout::fromCart( | ||
CartBuilder::forCurrentSession() | ||
->withSimpleProduct( | ||
$this->productFixture->getSku(), | ||
1 | ||
) | ||
->build() | ||
); | ||
|
||
$order = $checkout | ||
->withPaymentMethodCode('checkmo') | ||
->placeOrder(); | ||
$this->assertGreaterThan(0, strlen($order->getIncrementId()), 'the order does not have a valid increment_id'); | ||
$this->assertIsNumeric($order->getId(), 'the order does not have an entity_id'); | ||
|
||
/** | ||
* Cancel the order | ||
*/ | ||
$this->assertTrue($order->canCancel(), 'Cannot cancel the order'); | ||
$order->cancel(); | ||
$this->assertEquals('canceled', $order->getStatus(), 'The order was not cancelled'); | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
dev/MagentoTests/Integration/Cancel/CancelOrderWithMissingOrderSourcesTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Ampersand\DisableStockReservation\Test\Integration\Cancel; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use TddWizard\Fixtures\Catalog\ProductBuilder; | ||
use TddWizard\Fixtures\Catalog\ProductFixture; | ||
use TddWizard\Fixtures\Checkout\CustomerCheckout; | ||
use TddWizard\Fixtures\Checkout\CartBuilder; | ||
use TddWizard\Fixtures\Customer\CustomerFixture; | ||
use TddWizard\Fixtures\Customer\CustomerBuilder; | ||
use TddWizard\Fixtures\Customer\AddressBuilder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CancelOrderWithMissingOrderSourcesTest extends TestCase | ||
{ | ||
/** @var ObjectManagerInterface */ | ||
private $objectManager; | ||
|
||
/** @var ProductRepositoryInterface */ | ||
private $productRepository; | ||
|
||
/** @var ProductFixture */ | ||
private $productFixture; | ||
|
||
/** @var CustomerFixture */ | ||
private $customerFixture; | ||
|
||
/** @var ResourceConnection\ */ | ||
private $connection; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->objectManager = Bootstrap::getObjectManager(); | ||
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); | ||
$this->productRepository->cleanCache(); | ||
$this->connection = $this->objectManager->get(ResourceConnection::class); | ||
} | ||
|
||
public function testCancelOrderWithMissingOrderSources() | ||
{ | ||
$sku = uniqid('some_sku'); | ||
|
||
/** | ||
* Create a product with 5 qty | ||
*/ | ||
$this->productFixture = new ProductFixture( | ||
ProductBuilder::aSimpleProduct() | ||
->withSku($sku) | ||
->withPrice(10) | ||
->withStockQty(5) | ||
->withIsInStock(true) | ||
->build() | ||
); | ||
|
||
/** | ||
* Create a customer and login | ||
*/ | ||
$this->customerFixture = new CustomerFixture(CustomerBuilder::aCustomer()->withAddresses( | ||
AddressBuilder::anAddress()->asDefaultBilling(), | ||
AddressBuilder::anAddress()->asDefaultShipping() | ||
)->build()); | ||
$this->customerFixture->login(); | ||
|
||
/** | ||
* Order 1 qty | ||
*/ | ||
$checkout = CustomerCheckout::fromCart( | ||
CartBuilder::forCurrentSession() | ||
->withSimpleProduct( | ||
$this->productFixture->getSku(), | ||
1 | ||
) | ||
->build() | ||
); | ||
|
||
$order = $checkout | ||
->withPaymentMethodCode('checkmo') | ||
->placeOrder(); | ||
$this->assertGreaterThan(0, strlen($order->getIncrementId()), 'the order does not have a valid increment_id'); | ||
$this->assertIsNumeric($order->getId(), 'the order does not have an entity_id'); | ||
|
||
/** | ||
* Delete the order_sources information, can occur when cancelling an old order created on an older version | ||
* of this module before the order_sources table was introduced | ||
*/ | ||
$this->connection->getConnection()->query('delete from trv_order_sources'); | ||
|
||
/** | ||
* Cancel the order | ||
*/ | ||
$this->assertTrue($order->canCancel(), 'Cannot cancel the order'); | ||
$order->cancel(); | ||
$this->assertEquals('canceled', $order->getStatus(), 'The order was not cancelled'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters