Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractFactoryTest.php
Go to the documentation of this file.
1 <?php
7 
8 class AbstractFactoryTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_factory;
14 
18  protected $_invokableList = [
19  'sprintf' => \Magento\Framework\Filter\Sprintf::class,
20  'template' => \Magento\Framework\Filter\Template::class,
21  'arrayFilter' => \Magento\Framework\Filter\ArrayFilter::class,
22  ];
23 
27  protected $_sharedList = [
28  \Magento\Framework\Filter\Template::class => true,
29  \Magento\Framework\Filter\ArrayFilter::class => false,
30  ];
31 
35  protected $_objectManager;
36 
37  protected function setUp()
38  {
39  $this->_objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
40 
41  $this->_factory = $this->getMockForAbstractClass(
42  \Magento\Framework\Filter\AbstractFactory::class,
43  ['objectManger' => $this->_objectManager]
44  );
45  $property = new \ReflectionProperty(\Magento\Framework\Filter\AbstractFactory::class, 'invokableClasses');
46  $property->setAccessible(true);
47  $property->setValue($this->_factory, $this->_invokableList);
48 
49  $property = new \ReflectionProperty(\Magento\Framework\Filter\AbstractFactory::class, 'shared');
50  $property->setAccessible(true);
51  $property->setValue($this->_factory, $this->_sharedList);
52  }
53 
59  public function testCanCreateFilter($alias, $expectedResult)
60  {
61  $this->assertEquals($expectedResult, $this->_factory->canCreateFilter($alias));
62  }
63 
67  public function canCreateFilterDataProvider()
68  {
69  return [['arrayFilter', true], ['notExist', false]];
70  }
71 
77  public function testIsShared($alias, $expectedResult)
78  {
79  $this->assertEquals($expectedResult, $this->_factory->isShared($alias));
80  }
81 
85  public function isSharedDataProvider()
86  {
87  return [
88  'shared' => [\Magento\Framework\Filter\Template::class, true],
89  'not shared' => [\Magento\Framework\Filter\ArrayFilter::class, false],
90  'default value' => [\Magento\Framework\Filter\Sprintf::class, true]
91  ];
92  }
93 
100  public function testCreateFilter($alias, $arguments, $isShared)
101  {
102  $property = new \ReflectionProperty(\Magento\Framework\Filter\AbstractFactory::class, 'sharedInstances');
103  $property->setAccessible(true);
104 
105  $filterMock = $this->getMockBuilder('FactoryInterface')->setMethods(['filter'])->getMock();
106  $this->_objectManager->expects(
107  $this->atLeastOnce()
108  )->method(
109  'create'
110  )->with(
111  $this->equalTo($this->_invokableList[$alias]),
112  $this->equalTo($arguments)
113  )->will(
114  $this->returnValue($filterMock)
115  );
116 
117  $this->assertEquals($filterMock, $this->_factory->createFilter($alias, $arguments));
118  if ($isShared) {
119  $sharedList = $property->getValue($this->_factory);
120  $this->assertTrue(array_key_exists($alias, $sharedList));
121  $this->assertEquals($filterMock, $sharedList[$alias]);
122  } else {
123  $this->assertEmpty($property->getValue($this->_factory));
124  }
125  }
126 
130  public function createFilterDataProvider()
131  {
132  return [
133  'not shared with args' => ['arrayFilter', ['123', '231'], false],
134  'not shared without args' => ['arrayFilter', [], true],
135  'shared' => ['template', [], true],
136  'default shared' => ['sprintf', [], true]
137  ];
138  }
139 }
$arguments
if(!trim($html)) $alias
Definition: details.phtml:20