Skip to content

Commit

Permalink
clean up Validator Factory
Browse files Browse the repository at this point in the history
- adapt unit test
  • Loading branch information
david-fuehr committed Mar 20, 2019
1 parent 7d0d01e commit e266d9a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 142 deletions.
90 changes: 23 additions & 67 deletions lib/internal/Magento/Framework/Validator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,49 @@

namespace Magento\Framework\Validator;

use Magento\Framework\Cache\FrontendInterface;
use Magento\Framework\Module\Dir\Reader;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Validator;

/**
* Factory for \Magento\Framework\Validator and \Magento\Framework\Validator\Builder.
*/
class Factory
{
/** cache key */
const CACHE_KEY = __CLASS__;

/**
* @var \Magento\Framework\ObjectManagerInterface
* @var ObjectManagerInterface
*/
protected $_objectManager;

/**
* Validator config files
*
* @var array|null
* @var iterable|null
*/
protected $_configFiles = null;
protected $_configFiles;

/**
* @var bool
*/
private $isDefaultTranslatorInitialized = false;

/**
* @var \Magento\Framework\Module\Dir\Reader
* @var Reader
*/
private $moduleReader;

/**
* @var FrontendInterface
*/
private $cache;

/**
* @var \Magento\Framework\Serialize\SerializerInterface
*/
private $serializer;

/**
* @var \Magento\Framework\Config\FileIteratorFactory
*/
private $fileIteratorFactory;

/**
* Initialize dependencies
*
* @param \Magento\Framework\ObjectManagerInterface $objectManager
* @param \Magento\Framework\Module\Dir\Reader $moduleReader
* @param FrontendInterface $cache
* @param ObjectManagerInterface $objectManager
* @param Reader $moduleReader
*/
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Framework\Module\Dir\Reader $moduleReader,
FrontendInterface $cache
ObjectManagerInterface $objectManager,
Reader $moduleReader
) {
$this->_objectManager = $objectManager;
$this->moduleReader = $moduleReader;
$this->cache = $cache;
}

/**
Expand All @@ -83,6 +67,7 @@ protected function _initializeConfigList()
* Create and set default translator to \Magento\Framework\Validator\AbstractValidator.
*
* @return void
* @throws \Zend_Translate_Exception
*/
protected function _initializeDefaultTranslator()
{
Expand All @@ -95,7 +80,7 @@ protected function _initializeDefaultTranslator()
/** @var \Magento\Framework\Translate\Adapter $translator */
$translator = $this->_objectManager->create(\Magento\Framework\Translate\Adapter::class);
$translator->setOptions(['translator' => $translatorCallback]);
\Magento\Framework\Validator\AbstractValidator::setDefaultTranslator($translator);
AbstractValidator::setDefaultTranslator($translator);
$this->isDefaultTranslatorInitialized = true;
}
}
Expand All @@ -105,14 +90,15 @@ protected function _initializeDefaultTranslator()
*
* Will instantiate \Magento\Framework\Validator\Config
*
* @return \Magento\Framework\Validator\Config
* @return Config
* @throws \Zend_Translate_Exception
*/
public function getValidatorConfig()
{
$this->_initializeConfigList();
$this->_initializeDefaultTranslator();
return $this->_objectManager->create(
\Magento\Framework\Validator\Config::class,
Config::class,
['configFiles' => $this->_configFiles]
);
}
Expand All @@ -123,7 +109,8 @@ public function getValidatorConfig()
* @param string $entityName
* @param string $groupName
* @param array|null $builderConfig
* @return \Magento\Framework\Validator\Builder
* @return Builder
* @throws \Zend_Translate_Exception
*/
public function createValidatorBuilder($entityName, $groupName, array $builderConfig = null)
{
Expand All @@ -137,43 +124,12 @@ public function createValidatorBuilder($entityName, $groupName, array $builderCo
* @param string $entityName
* @param string $groupName
* @param array|null $builderConfig
* @return \Magento\Framework\Validator
* @return Validator
* @throws \Zend_Translate_Exception
*/
public function createValidator($entityName, $groupName, array $builderConfig = null)
{
$this->_initializeDefaultTranslator();
return $this->getValidatorConfig()->createValidator($entityName, $groupName, $builderConfig);
}

/**
* Get serializer
*
* @return \Magento\Framework\Serialize\SerializerInterface
* @deprecated 100.2.0
*/
private function getSerializer()
{
if ($this->serializer === null) {
$this->serializer = $this->_objectManager->get(
\Magento\Framework\Serialize\SerializerInterface::class
);
}
return $this->serializer;
}

/**
* Get file iterator factory
*
* @return \Magento\Framework\Config\FileIteratorFactory
* @deprecated 100.2.0
*/
private function getFileIteratorFactory()
{
if ($this->fileIteratorFactory === null) {
$this->fileIteratorFactory = $this->_objectManager->get(
\Magento\Framework\Config\FileIteratorFactory::class
);
}
return $this->fileIteratorFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,6 @@ class FactoryTest extends \PHPUnit\Framework\TestCase
*/
private $validatorConfigMock;

/**
* @var \Magento\Framework\Cache\FrontendInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $cacheMock;

/**
* @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $serializerMock;

/**
* @var \Magento\Framework\Config\FileIteratorFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $fileIteratorFactoryMock;

/**
* @var \Magento\Framework\Config\FileIterator|\PHPUnit_Framework_MockObject_MockObject
*/
Expand All @@ -55,11 +40,6 @@ class FactoryTest extends \PHPUnit\Framework\TestCase
*/
private $factory;

/**
* @var string
*/
private $jsonString = '["\/tmp\/moduleOne\/etc\/validation.xml"]';

/**
* @var array
*/
Expand Down Expand Up @@ -99,23 +79,9 @@ protected function setUp()
\Magento\Framework\Validator\Factory::class,
[
'objectManager' => $this->objectManagerMock,
'moduleReader' => $this->readerMock,
'cache' => $this->cacheMock
'moduleReader' => $this->readerMock
]
);

$this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
$this->fileIteratorFactoryMock = $this->createMock(\Magento\Framework\Config\FileIteratorFactory::class);
$objectManager->setBackwardCompatibleProperty(
$this->factory,
'serializer',
$this->serializerMock
);
$objectManager->setBackwardCompatibleProperty(
$this->factory,
'fileIteratorFactory',
$this->fileIteratorFactoryMock
);
}

/**
Expand Down Expand Up @@ -147,46 +113,6 @@ public function testGetValidatorConfig()
);
}

public function testGetValidatorConfigCacheNotExist()
{
$this->cacheMock->expects($this->once())
->method('load')
->willReturn(false);
$this->readerMock->expects($this->once())
->method('getConfigurationFiles')
->willReturn($this->fileIteratorMock);
$this->fileIteratorMock->method('toArray')
->willReturn($this->data);
$this->cacheMock->expects($this->once())
->method('save')
->with($this->jsonString);
$this->serializerMock->expects($this->once())
->method('serialize')
->with($this->data)
->willReturn($this->jsonString);
$this->factory->getValidatorConfig();
$this->factory->getValidatorConfig();
}

public function testGetValidatorConfigCacheExist()
{
$this->cacheMock->expects($this->once())
->method('load')
->willReturn($this->jsonString);
$this->readerMock->expects($this->never())
->method('getConfigurationFiles');
$this->cacheMock->expects($this->never())
->method('save');
$this->serializerMock->expects($this->once())
->method('unserialize')
->with($this->jsonString)
->willReturn($this->data);
$this->fileIteratorFactoryMock->method('create')
->willReturn($this->fileIteratorMock);
$this->factory->getValidatorConfig();
$this->factory->getValidatorConfig();
}

public function testCreateValidatorBuilder()
{
$this->readerMock->method('getConfigurationFiles')
Expand Down

0 comments on commit e266d9a

Please sign in to comment.