Skip to content

Commit

Permalink
fixed integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Charlie committed Feb 11, 2021
1 parent b12a58e commit a958272
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
use Magento\Catalog\Model\Indexer\Category\Product\Processor;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Cms\Api\GetBlockByIdentifierInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Indexer\StateInterface;
use Magento\Framework\Indexer\StateInterfaceFactory;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Api\StoreManagementInterface;
use Magento\Store\Model\StoreManagerInterface;
Expand All @@ -33,62 +28,45 @@
*/
class CategoryRepositoryTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
/** @var ObjectManagerInterface */
private $objectManager;

/**
* @var CategoryLayoutUpdateManager
*/
/** @var CategoryLayoutUpdateManager */
private $layoutManager;

/**
* @var CategoryRepositoryInterface
*/
/** @var CategoryRepositoryInterface */
private $categoryRepository;

/**
* @var CollectionFactory
*/
/** @var CollectionFactory */
private $productCollectionFactory;

/**
* @var CategoryCollectionFactory
*/
/** @var CategoryCollectionFactory */
private $categoryCollectionFactory;

/**
* @var StoreManagementInterface
*/
/** @var StoreManagementInterface */
private $storeManager;

/**
* @var GetBlockByIdentifierInterface
*/
/** @var GetBlockByIdentifierInterface */
private $getBlockByIdentifier;

/**
* @var IndexerRegistry
*/
private $indexerRegistry;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->objectManager->configure([
'preferences' => [LayoutUpdateManager::class => CategoryLayoutUpdateManager::class]
'preferences' => [
\Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager::class
=> \Magento\TestFramework\Catalog\Model\CategoryLayoutUpdateManager::class
]
]);
$this->layoutManager = $this->objectManager->get(CategoryLayoutUpdateManager::class);
$this->productCollectionFactory = $this->objectManager->get(CollectionFactory::class);
$this->categoryCollectionFactory = $this->objectManager->get(CategoryCollectionFactory::class);
$this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
$this->getBlockByIdentifier = $this->objectManager->get(GetBlockByIdentifierInterface::class);
$this->indexerRegistry = $this->objectManager->get(IndexerRegistry::class);
}

/**
Expand Down Expand Up @@ -226,51 +204,4 @@ private function assertCategoryData(array $expectedData, CategoryInterface $cate
$this->assertEquals($value, $category->getData($key));
}
}

/**
* Verify that indexer still valid after deleting inactive category
*
* @magentoAppArea adminhtml
* @magentoDataFixture Magento/Catalog/_files/categories_disabled.php
* @magentoDbIsolation disabled
*
* @return void
*/
public function testDeleteInactiveCategory(): void
{
$indexerProcessor = $this->indexerRegistry->get(Processor::INDEXER_ID);
$indexerProcessor->reindexAll();
$indexerProcessor->setScheduled(true);

/** @var CategoryRepositoryInterface $categoryRepository */
$categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
$category = $categoryRepository->get(4);
$categoryRepository->delete($category);

$state = $this->getIndexerState(Processor::INDEXER_ID);
$this->assertEquals(StateInterface::STATUS_VALID, $state->getStatus());
}

/**
* Get Indexer state
*
* @param string $indexerId
* @return StateInterface
*/
private function getIndexerState(string $indexerId): StateInterface
{
$state = $this->objectManager->get(StateInterfaceFactory::class)
->create();

return $state->loadByIndexer($indexerId);
}

/**
* Return indexer to previous state
*/
protected function tearDown(): void
{
$indexerProcessor = $this->indexerRegistry->get(Processor::INDEXER_ID);
$indexerProcessor->setScheduled(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,36 @@ public function testCategoryDelete()
}
}

public function testCategoryCreate()

/**
* Verify that indexer still valid after deleting inactive category
*
* @magentoAppArea adminhtml
* @magentoDataFixture Magento/Catalog/_files/categories_disabled.php
*
* @return void
*/
public function testDeleteInactiveCategory(): void
{
$this->indexer->reindexAll();
$indexerShouldBeValid = $this->indexer->isInvalid();

$this->categoryRepository->deleteByIdentifier(4);

$state = $this->indexer->getState();
$state->loadByIndexer($this->indexer->getId());
$status = $state->getStatus();

$this->assertFalse($indexerShouldBeValid);
$this->assertEquals(StateInterface::STATUS_VALID, $status);
}

/**
* Create category
*
* @return void
*/
public function testCategoryCreate(): void
{
$this->testReindexAll();
$categories = $this->getCategories(4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\CatalogSearch\Model\Indexer\Fulltext\Processor;
use Magento\Framework\Indexer\StateInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class CategoryTest extends \PHPUnit\Framework\TestCase
/**
* Test for category repository plugin
*/
class CategoryTest extends TestCase
{
/**
* @var Processor
Expand All @@ -24,10 +30,19 @@ class CategoryTest extends \PHPUnit\Framework\TestCase
*/
private $categoryRepository;

/**
* @var CategoryCollectionFactory
*/
private $categoryCollectionFactory;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->indexerProcessor = Bootstrap::getObjectManager()->create(Processor::class);
$this->categoryRepository = Bootstrap::getObjectManager()->create(CategoryRepositoryInterface::class);
$this->categoryCollectionFactory = Bootstrap::getObjectManager()->create(CategoryCollectionFactory::class);
}

/**
Expand All @@ -47,22 +62,22 @@ public function testIndexerInvalidatedAfterCategoryDelete()
$status = $state->getStatus();

$this->assertTrue($isIndexerValid);
$this->assertEquals(\Magento\Framework\Indexer\StateInterface::STATUS_INVALID, $status);
$this->assertEquals(StateInterface::STATUS_INVALID, $status);
}

/**
* Returns categories
*
* @param int $count
* @return Category[]
*/
private function getCategories($count)
private function getCategories(int $count): array
{
/** @var Category $category */
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\Category::class
);

$result = $category->getCollection()->addAttributeToSelect('name')->getItems();
$result = array_slice($result, 2);
$collection = $this->categoryCollectionFactory->create()
->addAttributeToSelect('name')
->addAttributeToSelect('is_active')
->getItems();
$result = array_slice($collection, 2);

return array_slice($result, 0, $count);
}
Expand Down

0 comments on commit a958272

Please sign in to comment.