Skip to content

Commit

Permalink
Improve product creation in integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Nov 21, 2024
1 parent e628bdd commit 29de16e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Test/Integration/DataLayer/Event/AddToCartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AddToCartTest extends TestCase
public function testValidDataLayerWithCart()
{
/** @var Product $product */
$product = $this->createProduct(1);
$product = $this->getProduct(1);
$addToCartEvent = ObjectManager::getInstance()->get(AddToCart::class);
$data = $addToCartEvent->setProduct($product)->get();
$this->assertCount(1, $data['ecommerce']['items']);
Expand Down
2 changes: 1 addition & 1 deletion Test/Integration/DataLayer/Event/ViewCartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testValidViewCartEvent()
$om = ObjectManager::getInstance();
$cart = $om->create(CartInterface::class);

$product = $this->createProduct(1);
$product = $this->getProduct(1);
$item = $cart->addProduct($product);
$item->setQty(2);
$cart->setItems([$item]);
Expand Down
5 changes: 2 additions & 3 deletions Test/Integration/DataLayer/Mapper/ProductDataMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ class ProductDataMapperTest extends TestCase

public function testMapByProduct()
{
$product = $this->createProducts()[0];
$product = $this->getProducts()[0];
$productDataMapper = ObjectManager::getInstance()->get(ProductDataMapper::class);
$productData = $productDataMapper->mapByProduct($product);
$this->assertEquals('Product 1', $productData['item_name']);
$this->assertEquals('product1', $productData['item_id']);
$this->assertEquals('Simple Product 1', $productData['item_name']);
}
}
124 changes: 57 additions & 67 deletions Test/Integration/FixtureTrait/CreateProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,73 @@
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Helper\DefaultCategory;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product as ProductModel;
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\Event\Config\Data;
use Magento\Framework\Event\ConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Magento\Store\Api\WebsiteRepositoryInterface;
use Yireo\GoogleTagManager2\Test\Integration\Stub\FulltextStub;

trait CreateProduct
{
public function getProduct($productId = 0, array $data = []): ProductInterface
{
$objectManager = ObjectManager::getInstance();
$productRepository = $objectManager->get(ProductRepositoryInterface::class);

try {
$product = $productRepository->getById($productId);
} catch (NoSuchEntityException $e) {
$product = $this->createProduct($productId, $data);
}

$this->assertTrue($product->getId() > 0, 'No product ID given');
$this->assertTrue($product->isSalable(), 'Product is not salable');

return $product;
}

public function createProduct(
int $id,
int $productId,
array $data = []
): ProductInterface {
$objectManager = ObjectManager::getInstance();
/*$objectManager->configure([
$productRepository = $objectManager->get(ProductRepositoryInterface::class);

$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();
//$this->disableEventObserver('catalog_product_save_after', ProductProcessUrlRewriteSavingObserver::class);

$productFactory = $objectManager->get(ProductInterfaceFactory::class);
$defaultCategory = $objectManager->get(DefaultCategory::class);
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
$productSku = 'simple-product-'.rand(1,1000000);
$product = $objectManager->create(ProductInterface::class);

$this->disableEventObserver('catalog_product_save_after', ProductProcessUrlRewriteSavingObserver::class);
$this->deleteProduct($id);
if ($productId > 0) {
$product->setId($productId);
}

/** @var $product ProductModel */
$product = $productFactory->create();
$product->addData([
'type_id' => Type::TYPE_SIMPLE,
'attribute_set_id' => 4,
'name' => 'Simple Product '.rand(1,1000000),
'sku' => $productSku,
'price' => 10,
'weight' => 1,
'visibility' => Visibility::VISIBILITY_BOTH,
'status' => Status::STATUS_ENABLED,
]);

$product->addData($data);

$product->setCustomAttribute('tax_class_id', '2');
$product->getExtensionAttributes()->setWebsiteIds([1]);
$product->isObjectNew(true);
$product
->setId($id)
->setName('Product '.$id)
->setSku('product'.$id)
->setUrlKey('product'.$id)
->setUrlPath('product'.$id)
->setWeight(10)
->setCategoryIds([$defaultCategory->getId()])
->setTypeId(Type::TYPE_SIMPLE)
->setPrice(10)
->setStatus(Status::STATUS_ENABLED)
->setStoreId(0)
->setWebsiteIds([$this->getDefaultWebsiteId()])
->setAttributeSetId($this->getDefaultAttributeSetId())
->setVisibility(Visibility::VISIBILITY_BOTH)
//->setCanSaveCustomOptions(true)
//->setHasOptions(true)
->addData($data);

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

$stockRegistry = $objectManager->get(StockRegistryInterface::class);
Expand All @@ -91,21 +91,28 @@ public function createProduct(
);
}

$product = $productRepository->get($productSku);

$productHelper = $objectManager->get(\Magento\Catalog\Helper\Product::class);
$productHelper->setSkipSaleableCheck(true);

$this->assertTrue($product->getId() > 0, 'No product ID given');
$this->assertTrue($product->isSalable(), 'Product is not salable');

return $product;
}

public function createProducts($numberOfProducts = 1, array $data = []): array
public function getProducts($numberOfProducts = 1, array $data = []): array
{
$products = [];
for ($i = 1; $i < $numberOfProducts + 1; $i++) {
$products[] = $this->createProduct($i, $data);
$products[] = $this->getProduct($i, $data);
}

return $products;
}

/*
private function deleteProduct(int $id)
{
$objectManager = ObjectManager::getInstance();
Expand All @@ -132,28 +139,6 @@ private function deleteProduct(int $id)
}
}
/**
* @return int
*/
private function getDefaultAttributeSetId(): int
{
$productFactory = ObjectManager::getInstance()->get(Product::class);

return (int)$productFactory->getDefaultAttributeSetId();
}

/**
* @return int
* @throws NoSuchEntityException
*/
private function getDefaultWebsiteId(): int
{
$objectManager = ObjectManager::getInstance();
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);

return (int)$websiteRepository->get('base')->getId();
}

private function disableEventObserver(string $eventName, string $observerClass): void
{
$objectManager = ObjectManager::getInstance();
Expand All @@ -172,4 +157,9 @@ private function disableEventObserver(string $eventName, string $observerClass):
}
}
}
private function getProductRepository(): ProductRepositoryInterface
{
return ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
}*/
}
2 changes: 1 addition & 1 deletion Test/Integration/Page/CategoryPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testValidDataLayerWithOneCategory()
/** @var CategoryInterface $category */
$category = $this->createCategory(3);
$this->assertTrue($category->getId() > 0);
$this->createProducts(3, ['category_ids' => [$category->getId()]]);
$this->getProducts(3, ['category_ids' => [$category->getId()]]);

$products = $category->getProductCollection();
$this->assertTrue($products->count() >= 3, 'Product count is '.$products->count());
Expand Down
2 changes: 1 addition & 1 deletion Test/Integration/Page/ProductPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testValidDataLayerWithOneCategory()

/** @var CategoryInterface $category */
$category = $this->createCategory(3);
$product = $this->createProducts(1, ['category_ids' => [$category->getId()]])[0];
$product = $this->getProducts(1, ['category_ids' => [$category->getId()]])[0];

$this->dispatch('catalog/product/view/id/' . $product->getId());
$this->assertRequestActionName('view');
Expand Down

0 comments on commit 29de16e

Please sign in to comment.