Skip to content

Commit 30f3fe1

Browse files
committed
clean up Validator Factory
- adapt unit test
1 parent 1db265d commit 30f3fe1

File tree

2 files changed

+24
-142
lines changed

2 files changed

+24
-142
lines changed

lib/internal/Magento/Framework/Validator/Factory.php

Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,49 @@
66

77
namespace Magento\Framework\Validator;
88

9-
use Magento\Framework\Cache\FrontendInterface;
9+
use Magento\Framework\Module\Dir\Reader;
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\Validator;
1012

13+
/**
14+
* Factory for \Magento\Framework\Validator and \Magento\Framework\Validator\Builder.
15+
*/
1116
class Factory
1217
{
13-
/** cache key */
14-
const CACHE_KEY = __CLASS__;
15-
1618
/**
17-
* @var \Magento\Framework\ObjectManagerInterface
19+
* @var ObjectManagerInterface
1820
*/
1921
protected $_objectManager;
2022

2123
/**
2224
* Validator config files
2325
*
24-
* @var array|null
26+
* @var iterable|null
2527
*/
26-
protected $_configFiles = null;
28+
protected $_configFiles;
2729

2830
/**
2931
* @var bool
3032
*/
3133
private $isDefaultTranslatorInitialized = false;
3234

3335
/**
34-
* @var \Magento\Framework\Module\Dir\Reader
36+
* @var Reader
3537
*/
3638
private $moduleReader;
3739

38-
/**
39-
* @var FrontendInterface
40-
*/
41-
private $cache;
42-
43-
/**
44-
* @var \Magento\Framework\Serialize\SerializerInterface
45-
*/
46-
private $serializer;
47-
48-
/**
49-
* @var \Magento\Framework\Config\FileIteratorFactory
50-
*/
51-
private $fileIteratorFactory;
52-
5340
/**
5441
* Initialize dependencies
5542
*
56-
* @param \Magento\Framework\ObjectManagerInterface $objectManager
57-
* @param \Magento\Framework\Module\Dir\Reader $moduleReader
58-
* @param FrontendInterface $cache
43+
* @param ObjectManagerInterface $objectManager
44+
* @param Reader $moduleReader
5945
*/
6046
public function __construct(
61-
\Magento\Framework\ObjectManagerInterface $objectManager,
62-
\Magento\Framework\Module\Dir\Reader $moduleReader,
63-
FrontendInterface $cache
47+
ObjectManagerInterface $objectManager,
48+
Reader $moduleReader
6449
) {
6550
$this->_objectManager = $objectManager;
6651
$this->moduleReader = $moduleReader;
67-
$this->cache = $cache;
6852
}
6953

7054
/**
@@ -83,6 +67,7 @@ protected function _initializeConfigList()
8367
* Create and set default translator to \Magento\Framework\Validator\AbstractValidator.
8468
*
8569
* @return void
70+
* @throws \Zend_Translate_Exception
8671
*/
8772
protected function _initializeDefaultTranslator()
8873
{
@@ -95,7 +80,7 @@ protected function _initializeDefaultTranslator()
9580
/** @var \Magento\Framework\Translate\Adapter $translator */
9681
$translator = $this->_objectManager->create(\Magento\Framework\Translate\Adapter::class);
9782
$translator->setOptions(['translator' => $translatorCallback]);
98-
\Magento\Framework\Validator\AbstractValidator::setDefaultTranslator($translator);
83+
AbstractValidator::setDefaultTranslator($translator);
9984
$this->isDefaultTranslatorInitialized = true;
10085
}
10186
}
@@ -105,14 +90,15 @@ protected function _initializeDefaultTranslator()
10590
*
10691
* Will instantiate \Magento\Framework\Validator\Config
10792
*
108-
* @return \Magento\Framework\Validator\Config
93+
* @return Config
94+
* @throws \Zend_Translate_Exception
10995
*/
11096
public function getValidatorConfig()
11197
{
11298
$this->_initializeConfigList();
11399
$this->_initializeDefaultTranslator();
114100
return $this->_objectManager->create(
115-
\Magento\Framework\Validator\Config::class,
101+
Config::class,
116102
['configFiles' => $this->_configFiles]
117103
);
118104
}
@@ -123,7 +109,8 @@ public function getValidatorConfig()
123109
* @param string $entityName
124110
* @param string $groupName
125111
* @param array|null $builderConfig
126-
* @return \Magento\Framework\Validator\Builder
112+
* @return Builder
113+
* @throws \Zend_Translate_Exception
127114
*/
128115
public function createValidatorBuilder($entityName, $groupName, array $builderConfig = null)
129116
{
@@ -137,43 +124,12 @@ public function createValidatorBuilder($entityName, $groupName, array $builderCo
137124
* @param string $entityName
138125
* @param string $groupName
139126
* @param array|null $builderConfig
140-
* @return \Magento\Framework\Validator
127+
* @return Validator
128+
* @throws \Zend_Translate_Exception
141129
*/
142130
public function createValidator($entityName, $groupName, array $builderConfig = null)
143131
{
144132
$this->_initializeDefaultTranslator();
145133
return $this->getValidatorConfig()->createValidator($entityName, $groupName, $builderConfig);
146134
}
147-
148-
/**
149-
* Get serializer
150-
*
151-
* @return \Magento\Framework\Serialize\SerializerInterface
152-
* @deprecated 100.2.0
153-
*/
154-
private function getSerializer()
155-
{
156-
if ($this->serializer === null) {
157-
$this->serializer = $this->_objectManager->get(
158-
\Magento\Framework\Serialize\SerializerInterface::class
159-
);
160-
}
161-
return $this->serializer;
162-
}
163-
164-
/**
165-
* Get file iterator factory
166-
*
167-
* @return \Magento\Framework\Config\FileIteratorFactory
168-
* @deprecated 100.2.0
169-
*/
170-
private function getFileIteratorFactory()
171-
{
172-
if ($this->fileIteratorFactory === null) {
173-
$this->fileIteratorFactory = $this->_objectManager->get(
174-
\Magento\Framework\Config\FileIteratorFactory::class
175-
);
176-
}
177-
return $this->fileIteratorFactory;
178-
}
179135
}

lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,6 @@ class FactoryTest extends \PHPUnit\Framework\TestCase
2525
*/
2626
private $validatorConfigMock;
2727

28-
/**
29-
* @var \Magento\Framework\Cache\FrontendInterface|\PHPUnit_Framework_MockObject_MockObject
30-
*/
31-
private $cacheMock;
32-
33-
/**
34-
* @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
35-
*/
36-
private $serializerMock;
37-
38-
/**
39-
* @var \Magento\Framework\Config\FileIteratorFactory|\PHPUnit_Framework_MockObject_MockObject
40-
*/
41-
private $fileIteratorFactoryMock;
42-
4328
/**
4429
* @var \Magento\Framework\Config\FileIterator|\PHPUnit_Framework_MockObject_MockObject
4530
*/
@@ -55,11 +40,6 @@ class FactoryTest extends \PHPUnit\Framework\TestCase
5540
*/
5641
private $factory;
5742

58-
/**
59-
* @var string
60-
*/
61-
private $jsonString = '["\/tmp\/moduleOne\/etc\/validation.xml"]';
62-
6343
/**
6444
* @var array
6545
*/
@@ -99,23 +79,9 @@ protected function setUp()
9979
\Magento\Framework\Validator\Factory::class,
10080
[
10181
'objectManager' => $this->objectManagerMock,
102-
'moduleReader' => $this->readerMock,
103-
'cache' => $this->cacheMock
82+
'moduleReader' => $this->readerMock
10483
]
10584
);
106-
107-
$this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
108-
$this->fileIteratorFactoryMock = $this->createMock(\Magento\Framework\Config\FileIteratorFactory::class);
109-
$objectManager->setBackwardCompatibleProperty(
110-
$this->factory,
111-
'serializer',
112-
$this->serializerMock
113-
);
114-
$objectManager->setBackwardCompatibleProperty(
115-
$this->factory,
116-
'fileIteratorFactory',
117-
$this->fileIteratorFactoryMock
118-
);
11985
}
12086

12187
/**
@@ -147,46 +113,6 @@ public function testGetValidatorConfig()
147113
);
148114
}
149115

150-
public function testGetValidatorConfigCacheNotExist()
151-
{
152-
$this->cacheMock->expects($this->once())
153-
->method('load')
154-
->willReturn(false);
155-
$this->readerMock->expects($this->once())
156-
->method('getConfigurationFiles')
157-
->willReturn($this->fileIteratorMock);
158-
$this->fileIteratorMock->method('toArray')
159-
->willReturn($this->data);
160-
$this->cacheMock->expects($this->once())
161-
->method('save')
162-
->with($this->jsonString);
163-
$this->serializerMock->expects($this->once())
164-
->method('serialize')
165-
->with($this->data)
166-
->willReturn($this->jsonString);
167-
$this->factory->getValidatorConfig();
168-
$this->factory->getValidatorConfig();
169-
}
170-
171-
public function testGetValidatorConfigCacheExist()
172-
{
173-
$this->cacheMock->expects($this->once())
174-
->method('load')
175-
->willReturn($this->jsonString);
176-
$this->readerMock->expects($this->never())
177-
->method('getConfigurationFiles');
178-
$this->cacheMock->expects($this->never())
179-
->method('save');
180-
$this->serializerMock->expects($this->once())
181-
->method('unserialize')
182-
->with($this->jsonString)
183-
->willReturn($this->data);
184-
$this->fileIteratorFactoryMock->method('create')
185-
->willReturn($this->fileIteratorMock);
186-
$this->factory->getValidatorConfig();
187-
$this->factory->getValidatorConfig();
188-
}
189-
190116
public function testCreateValidatorBuilder()
191117
{
192118
$this->readerMock->method('getConfigurationFiles')

0 commit comments

Comments
 (0)