Skip to content

Commit

Permalink
CU-8694eqmtm Updated integration test with logic which temporarily di…
Browse files Browse the repository at this point in the history
…sables observer event, so it will not be triggered before proper data is mocked
  • Loading branch information
Telepatrick committed Jul 10, 2024
1 parent 4fb49d3 commit bdf4a73
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
13 changes: 13 additions & 0 deletions Test/Integration/Mock/InvoiceObserverMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace StoreKeeper\StoreKeeper\Test\Integration\Mock;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class InvoiceObserverMock implements ObserverInterface
{
public function execute(Observer $observer)
{
// Dummy data for disabling original observer
}
}
34 changes: 31 additions & 3 deletions Test/Integration/OrderWithInvoiceCreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class OrderWithInvoiceCreationTest extends AbstractTestCase
protected $invoice;
protected $orderResource;
protected $invoiceObserver;
protected $eventManager;

protected function setUp(): void
{
Expand All @@ -22,12 +23,16 @@ protected function setUp(): void
$this->invoice = Bootstrap::getObjectManager()->create(\Magento\Sales\Model\Service\InvoiceService::class);
$this->orderResource = Bootstrap::getObjectManager()->create(\Magento\Sales\Model\ResourceModel\Order::class);
$this->paymentApiClientMock->method('isStorekeeperPayment')->willReturn(false);

// Disable the observer during setup in order not to trigger observer before data is mocked
$this->disableObserver();

$this->invoiceObserver = $objectManager->getObject(
\StoreKeeper\StoreKeeper\Observers\SalesOrderInvoicePayObserver::class,
[
'orderResource' => $this->orderResource,
'paymentApiClient' => $this->paymentApiClientMock,
'authHelper' => $this->authHelper,
'authHelper' => $this->authHelper
]
);
}
Expand All @@ -46,6 +51,10 @@ public function testOrderCreation()
$invoice->save();
$order->setData('storekeeper_id', '17')->save();

// Re-enable the observer after data setup
$this->enableObserver();

// Manually trigger the observer event
//Mock observer run with freshly created invoice
$observer = $this->getMockBuilder(Event\Observer::class)
->disableOriginalConstructor()
Expand All @@ -54,9 +63,28 @@ public function testOrderCreation()
$observer->getEvent()->setData('invoice', $invoice);
$this->invoiceObserver->execute($observer);

//Reload order with fresh data
// Reload order with fresh data
$existingOrder = $this->orderFactory->create()->loadByIncrementId('100000001');
//Assert that storekeeper_payment_id was populated during 'storekeeper_sales_order_invoice_pay' event

// Assert that storekeeper_payment_id was populated during 'storekeeper_sales_order_invoice_pay' event
$this->assertNotEmpty($existingOrder->getStorekeeperPaymentId());
}

protected function disableObserver()
{
Bootstrap::getObjectManager()->configure([
'preferences' => [
\StoreKeeper\StoreKeeper\Observers\SalesOrderInvoicePayObserver::class => \StoreKeeper\StoreKeeper\Test\Integration\Mock\InvoiceObserverMock::class,
],
]);
}

protected function enableObserver()
{
Bootstrap::getObjectManager()->configure([
'preferences' => [
\StoreKeeper\StoreKeeper\Test\Integration\Mock\InvoiceObserverMock::class => \StoreKeeper\StoreKeeper\Observers\SalesOrderInvoicePayObserver::class,
],
]);
}
}

0 comments on commit bdf4a73

Please sign in to comment.