Skip to content

#4 Add test for Category views #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions tests/Integration/AbstractFrontendControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);

namespace IntegerNet\GlobalCustomLayout\Test\Integration;

use IntegerNet\GlobalCustomLayout\Test\Util\CategoryLayoutUpdateManager;
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\AbstractController;

/**
* @magentoAppIsolation enabled
* @magentoAppArea frontend
*/
abstract class AbstractFrontendControllerTest extends AbstractController
{
/**
* @var ObjectManagerInterface
*/
protected $objectManager;

/**
* @var LayoutInterface
*/
protected $layoutInterface;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->objectManager = Bootstrap::getObjectManager();
$this->layoutInterface = $this->objectManager->get(LayoutInterface::class);

$this->setUpPreferences();

parent::setUp();
}

private function setUpPreferences(): void
{
$this->objectManager->configure(
[
'preferences' => [
LayoutUpdateManager::class => CategoryLayoutUpdateManager::class,
]
]
);
$this->objectManager->removeSharedInstance(LayoutUpdateManager::class);
}
}
53 changes: 53 additions & 0 deletions tests/Integration/CategoryFrontendControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);

namespace IntegerNet\GlobalCustomLayout\Test\Integration;

use IntegerNet\GlobalCustomLayout\Test\Util\CategoryLayoutUpdateManager;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Tests whether global layout handles are correctly saved on categories
* and retrieved on the frontend on Category views
*/
class CategoryFrontendControllerTest extends AbstractFrontendControllerTest
{
/**
* Check that Global Custom Layout Update files work for Category views.
*
* @return void
* @throws CouldNotSaveException
* @throws NoSuchEntityException
*
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_product_ids.php
*/
public function testViewWithGlobalCustomUpdate(): void
{
//Setting a fake file for the category.
$file = 'test-file';
$categoryId = 5;

/** @var CategoryLayoutUpdateManager $layoutManager */
$layoutManager = $this->objectManager->get(CategoryLayoutUpdateManager::class);
$layoutManager->setCategoryFakeFiles(0, [$file]);

/** @var CategoryRepositoryInterface $categoryRepo */
$categoryRepo = $this->objectManager->create(CategoryRepositoryInterface::class);
$category = $categoryRepo->get($categoryId);

//Updating the custom attribute.
$category->setCustomAttribute('custom_layout_update_file', $file);
$categoryRepo->save($category);

//Viewing the category
$this->dispatch("catalog/category/view/id/$categoryId");

//Layout handles must contain the file.
$handles = $this->layoutInterface
->getUpdate()
->getHandles();
$this->assertContains("catalog_category_view_selectable_0_{$file}", $handles);
}
}
46 changes: 46 additions & 0 deletions tests/Util/CategoryLayoutUpdateManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace IntegerNet\GlobalCustomLayout\Test\Util;

use Magento\Catalog\Api\Data\CategoryInterface;

/**
* Easy way to fake available files.
*/
class CategoryLayoutUpdateManager extends \Magento\TestFramework\Catalog\Model\CategoryLayoutUpdateManager
{
/**
* @var array Keys are category IDs, values - file names.
*/
private $fakeFiles = [];

/**
* Supply fake files for a category.
*
* @param int $forCategoryId
* @param string[]|null $files Pass null to reset.
*/
public function setCategoryFakeFiles(int $forCategoryId, ?array $files): void
{
if ($files === null) {
unset($this->fakeFiles[$forCategoryId]);
} else {
$this->fakeFiles[$forCategoryId] = $files;
}
}

/**
* Fetches fake/mock files added through $this->setCategoryFakeFiles()
*
* @param CategoryInterface $category
* @return array
*/
public function fetchAvailableFiles(CategoryInterface $category): array
{
if (array_key_exists(0, $this->fakeFiles)) {
return $this->fakeFiles[0];
}
return parent::fetchAvailableFiles($category);
}
}