Skip to content

Commit

Permalink
failing tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Echo committed Sep 11, 2020
1 parent 2ac6e0c commit 276cb1c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 30 deletions.
57 changes: 39 additions & 18 deletions app/code/Magento/Theme/Model/PageLayout/Config/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,38 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Theme\Model\PageLayout\Config;

use Magento\Framework\App\Cache\Type\Layout;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\View\Model\PageLayout\Config\BuilderInterface;
use Magento\Framework\View\PageLayout\ConfigFactory;
use Magento\Framework\View\PageLayout\File\Collector\Aggregated;
use Magento\Theme\Model\ResourceModel\Theme\Collection;
use Magento\Theme\Model\Theme\Data;
use Magento\Framework\Serialize\SerializerInterface;

/**
* Page layout config builder
*/
class Builder implements \Magento\Framework\View\Model\PageLayout\Config\BuilderInterface
class Builder implements BuilderInterface
{
const CACHE_KEY_LAYOUTS = 'THEME_LAYOUTS_FILES_MERGED';

/**
* @var \Magento\Framework\View\PageLayout\ConfigFactory
* @var ConfigFactory
*/
protected $configFactory;

/**
* @var \Magento\Framework\View\PageLayout\File\Collector\Aggregated
* @var Aggregated
*/
protected $fileCollector;

/**
* @var \Magento\Theme\Model\ResourceModel\Theme\Collection
* @var Collection
*/
protected $themeCollection;

Expand All @@ -35,27 +46,36 @@ class Builder implements \Magento\Framework\View\Model\PageLayout\Config\Builder
private $configFiles = [];

/**
* @var \Magento\Framework\App\Cache\Type\Layout
* @var Layout|null
*/
private $cacheModel;
/**
* @var SerializerInterface|null
*/
protected $cacheModel;
private $serializer;

/**
* @param \Magento\Framework\View\PageLayout\ConfigFactory $configFactory
* @param \Magento\Framework\View\PageLayout\File\Collector\Aggregated $fileCollector
* @param \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection
* @param \Magento\Framework\App\Cache\Type\Layout $cacheModel
* @param ConfigFactory $configFactory
* @param Aggregated $fileCollector
* @param Collection $themeCollection
* @param Layout|null $cacheModel
* @param SerializerInterface|null $serializer
*/
public function __construct(
\Magento\Framework\View\PageLayout\ConfigFactory $configFactory,
\Magento\Framework\View\PageLayout\File\Collector\Aggregated $fileCollector,
\Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection,
\Magento\Framework\App\Cache\Type\Layout $cacheModel
ConfigFactory $configFactory,
Aggregated $fileCollector,
Collection $themeCollection,
?Layout $cacheModel = null,
?SerializerInterface $serializer = null
) {
$this->configFactory = $configFactory;
$this->fileCollector = $fileCollector;
$this->themeCollection = $themeCollection;
$this->cacheModel = $cacheModel;
$this->themeCollection->setItemObjectClass(\Magento\Theme\Model\Theme\Data::class);
$this->themeCollection->setItemObjectClass(Data::class);
$this->cacheModel = $cacheModel
?? ObjectManager::getInstance()->get(Layout::class);
$this->serializer = $serializer
?? ObjectManager::getInstance()->get(SerializerInterface::class);
}

/**
Expand All @@ -77,14 +97,15 @@ protected function getConfigFiles()
$configFiles = [];
$this->configFiles = $this->cacheModel->load(self::CACHE_KEY_LAYOUTS);
if (!empty($this->configFiles)) {
$this->configFiles = @unserialize($this->configFiles);//if value in cache is corrupted.
//if value in cache is corrupted.
$this->configFiles = $this->serializer->unserialize($this->configFiles);
}
if (empty($this->configFiles)) {
foreach ($this->themeCollection->loadRegisteredThemes() as $theme) {
$configFiles[] = $this->fileCollector->getFilesContent($theme, 'layouts.xml');
}
$this->configFiles = array_merge(...$configFiles);
$this->cacheModel->save(serialize($this->configFiles), self::CACHE_KEY_LAYOUTS);
$this->cacheModel->save($this->serializer->serialize($this->configFiles), self::CACHE_KEY_LAYOUTS);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@
*/
namespace Magento\Theme\Test\Unit\Model\PageLayout\Config;

use Magento\Framework\App\Cache\Type\FrontendPool;
use Magento\Framework\App\Cache\Type\Layout as LayoutCache;
use Magento\Framework\Cache\FrontendInterface;
use Magento\Framework\App\Cache\Type\Layout;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\View\PageLayout\Config;
use Magento\Framework\View\PageLayout\ConfigFactory;
use Magento\Framework\View\PageLayout\File\Collector\Aggregated;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Theme\Model\PageLayout\Config\Builder;
use Magento\Theme\Model\ResourceModel\Theme\Collection;
use Magento\Theme\Model\Theme\Data;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\Framework\App\Cache\Type\Layout;

class BuilderTest extends TestCase
{
Expand All @@ -32,7 +30,7 @@ class BuilderTest extends TestCase
protected $builder;

/**
* @var \Magento\Framework\View\PageLayout\ConfigFactory|MockObject
* @var ConfigFactory|MockObject
*/
protected $configFactory;

Expand All @@ -50,6 +48,10 @@ class BuilderTest extends TestCase
* @var Layout|MockObject
*/
protected $cacheModel;
/**
* @var SerializerInterface|MockObject
*/
protected $serializer;

/**
* SetUp method
Expand All @@ -58,28 +60,29 @@ class BuilderTest extends TestCase
*/
protected function setUp(): void
{
$this->configFactory = $this->getMockBuilder(\Magento\Framework\View\PageLayout\ConfigFactory::class)
$this->configFactory = $this->getMockBuilder(ConfigFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();

$this->fileCollector = $this->getMockBuilder(
Aggregated::class
)->disableOriginalConstructor()
$this->fileCollector = $this->getMockBuilder(Aggregated::class)
->disableOriginalConstructor()
->getMock();

$helper = new ObjectManager($this);
$this->themeCollection = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->getMock();
$this->cacheModel = $this->getMockBuilder(Layout::class)
->disableOriginalConstructor()
->getMock();

$this->serializer = $this->getMockForAbstractClass(SerializerInterface::class);

$this->themeCollection->expects($this->once())
->method('setItemObjectClass')
->with(Data::class)
->willReturnSelf();

$helper = new ObjectManager($this);
$this->builder = $helper->getObject(
Builder::class,
Expand All @@ -88,6 +91,7 @@ protected function setUp(): void
'fileCollector' => $this->fileCollector,
'themeCollection' => $this->themeCollection,
'cacheModel' => $this->cacheModel,
'serializer' => $this->serializer,
]
);
}
Expand All @@ -102,6 +106,7 @@ public function testGetPageLayoutsConfig()
$this->cacheModel->clean();
$files1 = ['content layouts_1.xml', 'content layouts_2.xml'];
$files2 = ['content layouts_3.xml', 'content layouts_4.xml'];
$configFiles = array_merge($files1, $files2);

$theme1 = $this->getMockBuilder(Data::class)
->disableOriginalConstructor()
Expand Down Expand Up @@ -129,9 +134,17 @@ public function testGetPageLayoutsConfig()

$this->configFactory->expects($this->once())
->method('create')
->with(['configFiles' => array_merge($files1, $files2)])
->with(['configFiles' => $configFiles])
->willReturn($config);

$this->serializer->expects($this->once())
->method('serialize')
->with($configFiles);

$this->cacheModel->expects($this->once())
->method('save')
->willReturnSelf();

$this->assertSame($config, $this->builder->getPageLayoutsConfig());
}
}

0 comments on commit 276cb1c

Please sign in to comment.