Skip to content

Commit

Permalink
Additional cart-based integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Oct 22, 2024
1 parent f222cb2 commit 87bb801
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 7 deletions.
67 changes: 67 additions & 0 deletions Test/Integration/DataLayer/Event/PurchaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types=1);

namespace Yireo\GoogleTagManager2\Test\Integration\DataLayer\Event;

use Magento\Catalog\Model\Product;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\ObjectManager;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use PHPUnit\Framework\TestCase;
use Yireo\GoogleTagManager2\DataLayer\Event\Purchase;
use Yireo\GoogleTagManager2\Test\Integration\FixtureTrait\CreateProduct;

/**
* @magentoAppArea frontend
*/
class PurchaseTest extends TestCase
{
use CreateProduct;

/**
* @magentoConfigFixture current_store googletagmanager2/settings/enabled 1
* @magentoConfigFixture current_store googletagmanager2/settings/method 1
* @magentoConfigFixture current_store googletagmanager2/settings/id test
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
* @magentoDataFixture Magento/Sales/_files/order.php
*/
public function testValidDataLayerWithCart()
{
$purchaseEvent = ObjectManager::getInstance()->get(Purchase::class);
$data = $purchaseEvent->setOrder($this->getOrder())->get();

$this->assertNotEmpty($data);
$this->assertCount(1, $data['ecommerce']['items']);
}

/**
* @magentoConfigFixture current_store googletagmanager2/settings/enabled 1
* @magentoConfigFixture current_store googletagmanager2/settings/method 1
* @magentoConfigFixture current_store googletagmanager2/settings/id test
* @magentoConfigFixture current_store googletagmanager2/settings/order_states_for_purchase_event payment_review,pending_payment,holded,processing,complete
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
* @magentoDataFixture Magento/Sales/_files/order.php
*/
public function testValidDataLayerWithCartWithSpecificOrderStates()
{
$purchaseEvent = ObjectManager::getInstance()->get(Purchase::class);
$data = $purchaseEvent->setOrder($this->getOrder())->get();

$this->assertNotEmpty($data);
$this->assertCount(1, $data['ecommerce']['items']);
}

private function getOrder(): OrderInterface
{
$orderRepository = ObjectManager::getInstance()->get(OrderRepositoryInterface::class);
$searchCriteriaBuilder = ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
$searchCriteriaBuilder->setPageSize(1);
$searchResults = $orderRepository->getList($searchCriteriaBuilder->create());
$items = $searchResults->getItems();
return array_shift($items);
}
}
29 changes: 24 additions & 5 deletions Test/Integration/FixtureTrait/CreateProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\Product\Visibility;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\CatalogSearch\Model\Indexer\Fulltext;
use Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\App\State;
use Magento\Framework\Event\Config\Data;
use Magento\Framework\Event\ConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Registry;
use Magento\Store\Api\WebsiteRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Yireo\GoogleTagManager2\Test\Integration\Stub\FulltextStub;

trait CreateProduct
{
Expand All @@ -33,6 +31,18 @@ public function createProduct(
array $data = []
): ProductInterface {
$objectManager = ObjectManager::getInstance();
/*$objectManager->configure([
'preferences' => [
Fulltext::class => FulltextStub::class
]
]);*/

/*
$indexerFactory = $objectManager->get(\Magento\Indexer\Model\IndexerFactory::class);
/** @var \Magento\Indexer\Model\Indexer $indexer */
//$indexer = $indexerFactory->create()->load('catalogsearch_fulltext');
//$indexer->reindexAll();

$productFactory = $objectManager->get(ProductInterfaceFactory::class);
$defaultCategory = $objectManager->get(DefaultCategory::class);
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
Expand All @@ -58,14 +68,21 @@ public function createProduct(
->setWebsiteIds([$this->getDefaultWebsiteId()])
->setAttributeSetId($this->getDefaultAttributeSetId())
->setVisibility(Visibility::VISIBILITY_BOTH)
->setStockData(['use_config_manage_stock' => 0])
//->setCanSaveCustomOptions(true)
//->setHasOptions(true)
->addData($data);

$product->isObjectNew(true);
$productRepository->save($product);

$stockRegistry = $objectManager->get(StockRegistryInterface::class);
$stockItem = $stockRegistry->getStockItemBySku($product->getSku());
$stockItem->setUseConfigManageStock(1);
$stockItem->setIsInStock(1);
$stockItem->setProductId($product->getId());
$stockItem->setQty(9999);
$stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);

if (!empty($product->getCategoryIds())) {
$categoryLinkManagement = $objectManager->get(CategoryLinkManagementInterface::class);
$categoryLinkManagement->assignProductToCategories(
Expand All @@ -74,6 +91,8 @@ public function createProduct(
);
}



return $product;
}

Expand Down
2 changes: 0 additions & 2 deletions Test/Integration/Page/AddToWishlistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class AddToWishlistTest extends PageTestCase
use CreateCustomer;

/**
* @magentoDbIsolation disabled
* @magentoAppIsolation disabled
* @magentoAppArea frontend
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoConfigFixture current_store cataloginventory/options/show_out_of_stock 1
Expand Down
21 changes: 21 additions & 0 deletions Test/Integration/Stub/FulltextStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace Yireo\GoogleTagManager2\Test\Integration\Stub;

use Magento\CatalogSearch\Model\Indexer\Fulltext;

class FulltextStub extends Fulltext
{
public function execute($entityIds)
{
}

public function executeFull()
{
}

public function executeRow($id)
{
}
}

0 comments on commit 87bb801

Please sign in to comment.