Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FilterManagerTest.php
Go to the documentation of this file.
1 <?php
7 
8 class FilterManagerTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_filterManager;
14 
18  protected $_factoryMock;
19 
23  protected $_objectManager;
24 
28  protected $_config;
29 
30  protected function initMocks()
31  {
32  $factoryName = \Magento\Framework\Filter\Factory::class;
33  $this->_factoryMock = $this->createPartialMock($factoryName, ['canCreateFilter', 'createFilter']);
34  $this->_objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
35  $this->_objectManager->expects(
36  $this->atLeastOnce()
37  )->method(
38  'create'
39  )->with(
40  $this->equalTo($factoryName)
41  )->will(
42  $this->returnValue($this->_factoryMock)
43  );
44  $this->_config =
45  $this->createPartialMock(\Magento\Framework\Filter\FilterManager\Config::class, ['getFactories']);
46  $this->_config->expects(
47  $this->atLeastOnce()
48  )->method(
49  'getFactories'
50  )->will(
51  $this->returnValue([$factoryName])
52  );
53  $this->_filterManager = new \Magento\Framework\Filter\FilterManager($this->_objectManager, $this->_config);
54  }
55 
56  public function testGetFilterFactories()
57  {
58  $this->initMocks();
59  $method =
60  new \ReflectionMethod(\Magento\Framework\Filter\FilterManager::class, 'getFilterFactories');
61  $method->setAccessible(true);
62  $this->assertEquals([$this->_factoryMock], $method->invoke($this->_filterManager));
63  }
64 
70  {
71  $factoryName = \Magento\Framework\Filter\Factory::class;
72  $this->_factoryMock = new \stdClass();
73  $this->_objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
74  $this->_objectManager->expects(
75  $this->atLeastOnce()
76  )->method(
77  'create'
78  )->with(
79  $this->equalTo($factoryName)
80  )->will(
81  $this->returnValue($this->_factoryMock)
82  );
83  $this->_config =
84  $this->createPartialMock(\Magento\Framework\Filter\FilterManager\Config::class, ['getFactories']);
85  $this->_config->expects(
86  $this->atLeastOnce()
87  )->method(
88  'getFactories'
89  )->will(
90  $this->returnValue([$factoryName])
91  );
92  $this->_filterManager = new \Magento\Framework\Filter\FilterManager($this->_objectManager, $this->_config);
93 
94  $method = new \ReflectionMethod(\Magento\Framework\Filter\FilterManager::class, 'getFilterFactories');
95  $method->setAccessible(true);
96  $method->invoke($this->_filterManager);
97  }
98 
99  public function testCreateFilterInstance()
100  {
101  $this->initMocks();
102  $filterMock = $this->getMockBuilder('FactoryInterface')->getMock();
103  $this->configureFactoryMock($filterMock, 'alias', ['123']);
104 
105  $method = new \ReflectionMethod(\Magento\Framework\Filter\FilterManager::class, 'createFilterInstance');
106  $method->setAccessible(true);
107  $this->assertEquals($filterMock, $method->invoke($this->_filterManager, 'alias', ['123']));
108  }
109 
115  {
116  $this->initMocks();
117  $filterAlias = 'wrongAlias';
118  $this->_factoryMock->expects(
119  $this->atLeastOnce()
120  )->method(
121  'canCreateFilter'
122  )->with(
123  $this->equalTo($filterAlias)
124  )->will(
125  $this->returnValue(false)
126  );
127 
128  $method = new \ReflectionMethod(\Magento\Framework\Filter\FilterManager::class, 'createFilterInstance');
129  $method->setAccessible(true);
130  $method->invoke($this->_filterManager, $filterAlias, []);
131  }
132 
138  protected function configureFactoryMock($filter, $alias, $arguments = [])
139  {
140  $this->_factoryMock->expects(
141  $this->atLeastOnce()
142  )->method(
143  'canCreateFilter'
144  )->with(
145  $this->equalTo($alias)
146  )->will(
147  $this->returnValue(true)
148  );
149 
150  $this->_factoryMock->expects(
151  $this->atLeastOnce()
152  )->method(
153  'createFilter'
154  )->with(
155  $this->equalTo($alias),
156  $this->equalTo($arguments)
157  )->will(
158  $this->returnValue($filter)
159  );
160  }
161 
162  public function testCall()
163  {
164  $value = 'testValue';
165  $this->initMocks();
166  $filterMock = $this->getMockBuilder('FactoryInterface')->setMethods(['filter'])->getMock();
167  $filterMock->expects(
168  $this->atLeastOnce()
169  )->method(
170  'filter'
171  )->with(
172  $this->equalTo($value)
173  )->will(
174  $this->returnValue($value)
175  );
176  $this->configureFactoryMock($filterMock, 'alias', ['123']);
177  $this->assertEquals($value, $this->_filterManager->alias($value, ['123']));
178  }
179 }
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
$arguments
if(!trim($html)) $alias
Definition: details.phtml:20