Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ActionListTest.php
Go to the documentation of this file.
1 <?php
7 
8 class ActionListTest extends \PHPUnit\Framework\TestCase
9 {
13  private $objectManager;
14 
18  private $cacheMock;
19 
23  private $readerMock;
24 
28  private $actionList;
29 
33  private $serializerMock;
34 
35  protected function setUp()
36  {
37  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
38  $this->cacheMock = $this->createMock(\Magento\Framework\Config\CacheInterface::class);
39  $this->readerMock = $this->createMock(\Magento\Framework\Module\Dir\Reader::class);
40  $this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
41  }
42 
43  public function testConstructActionsCached()
44  {
45  $this->cacheMock->expects($this->once())
46  ->method('load')
47  ->willReturn('"data"');
48  $this->serializerMock->expects($this->once())
49  ->method('unserialize');
50  $this->cacheMock->expects($this->never())
51  ->method('save');
52  $this->readerMock->expects($this->never())
53  ->method('getActionFiles');
54  $this->createActionListInstance();
55  }
56 
57  public function testConstructActionsNoCached()
58  {
59  $this->cacheMock->expects($this->once())
60  ->method('load')
61  ->willReturn(false);
62  $this->serializerMock->expects($this->once())
63  ->method('serialize');
64  $this->cacheMock->expects($this->once())
65  ->method('save');
66  $this->readerMock->expects($this->once())
67  ->method('getActionFiles')
68  ->willReturn('data')
69  ;
70  $this->createActionListInstance();
71  }
72 
82  public function testGet($module, $area, $namespace, $action, $data, $expected)
83  {
84  $this->cacheMock->expects($this->once())
85  ->method('load')
86  ->will($this->returnValue(false));
87  $this->cacheMock->expects($this->once())
88  ->method('save');
89  $this->readerMock->expects($this->once())
90  ->method('getActionFiles')
91  ->willReturn($data);
92  $this->createActionListInstance();
93  $this->assertEquals($expected, $this->actionList->get($module, $area, $namespace, $action));
94  }
95 
99  public function getDataProvider()
100  {
101  $mockClassName = 'Mock_Action_Class';
102  $actionClass = $this->getMockClass(
103  \Magento\Framework\App\ActionInterface::class,
104  ['execute', 'getResponse'],
105  [],
106  $mockClassName
107  );
108 
109  return [
110  [
111  'Magento_Module',
112  'Area',
113  'Namespace',
114  'Index',
115  ['magento\module\controller\area\namespace\index' => $mockClassName],
116  $actionClass
117  ],
118  [
119  'Magento_Module',
120  '',
121  'Namespace',
122  'Index',
123  ['magento\module\controller\namespace\index' => $mockClassName],
124  $actionClass
125  ],
126  [
127  'Magento_Module',
128  'Area',
129  'Namespace',
130  'Catch',
131  ['magento\module\controller\area\namespace\catchaction' => $mockClassName],
132  $actionClass
133  ],
134  [
135  'Magento_Module',
136  'Area',
137  'Namespace',
138  'Index',
139  ['magento\module\controller\area\namespace\index' => 'Not_Exist_Class'],
140  null
141  ],
142  [
143  'Magento_Module',
144  'Area',
145  'Namespace',
146  'Index',
147  [],
148  null
149  ],
150  [
151  'Magento_Module',
152  null,
153  'adminhtml_product',
154  'index',
155  'magento\module\controller\adminhtml\product\index' => '$mockClassName',
156  null
157  ],
158  ];
159  }
160 
161  private function createActionListInstance()
162  {
163  $this->actionList = $this->objectManager->getObject(
164  \Magento\Framework\App\Router\ActionList::class,
165  [
166  'cache' => $this->cacheMock,
167  'moduleReader' => $this->readerMock,
168  'serializer' => $this->serializerMock,
169  ]
170  );
171  }
172 }
testGet($module, $area, $namespace, $action, $data, $expected)