Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DirectoryTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class DirectoryTest extends \PHPUnit\Framework\TestCase
11 {
15  private $classesScanner;
16 
20  private $classReaderMock;
21 
25  private $model;
26 
30  private $validatorMock;
31 
35  private $logMock;
36 
40  protected function setUp()
41  {
42  $this->logMock = $this->getMockBuilder(\Magento\Setup\Module\Di\Compiler\Log\Log::class)
43  ->disableOriginalConstructor()
44  ->setMethods(['add'])
45  ->getMock();
46 
47  $this->classesScanner = $this->getMockBuilder(\Magento\Setup\Module\Di\Code\Reader\ClassesScanner::class)
48  ->disableOriginalConstructor()
49  ->setMethods(['getList'])
50  ->getMock();
51 
52  $this->classReaderMock = $this->getMockBuilder(\Magento\Framework\Code\Reader\ClassReader::class)
53  ->disableOriginalConstructor()
54  ->setMethods(['getParents'])
55  ->getMock();
56 
57  $this->validatorMock = $this->getMockBuilder(\Magento\Framework\Code\Validator::class)
58  ->disableOriginalConstructor()
59  ->setMethods(['validate'])
60  ->getMock();
61 
62  $this->model = new \Magento\Setup\Module\Di\Code\Reader\Decorator\Directory(
63  $this->logMock,
64  $this->classReaderMock,
65  $this->classesScanner,
66  $this->validatorMock,
67  '/generated/code'
68  );
69  }
70 
71  public function testGetList()
72  {
73  $path = '/tmp/test';
74 
75  $classes = ['NameSpace1\ClassName1', 'NameSpace1\ClassName2'];
76 
77  $this->classesScanner->expects($this->once())
78  ->method('getList')
79  ->with($path)
80  ->willReturn($classes);
81 
82  $parents = [
83  ['NameSpace1\ClassName1', ['Parent_Class_Name', 'Interface_1', 'Interface_2']],
84  ['NameSpace1\ClassName2', ['Parent_Class_Name', 'Interface_1', 'Interface_2']]
85  ];
86 
87  $this->classReaderMock->expects($this->exactly(count($classes)))
88  ->method('getParents')
89  ->will($this->returnValueMap(
90  $parents
91  ));
92 
93  $this->logMock->expects($this->never())
94  ->method('add');
95 
96  $this->validatorMock->expects($this->exactly(count($classes)))
97  ->method('validate');
98 
99  $this->model->getList($path);
100  $result = $this->model->getRelations();
101 
102  $expected = [
103  $classes[0] => $parents[0][1],
104  $classes[1] => $parents[1][1]
105  ];
106 
107  $this->assertEquals($result, $expected);
108  }
109 
110  public function testGetListNoValidation()
111  {
112  $path = '/generated/code';
113 
114  $classes = ['NameSpace1\ClassName1', 'NameSpace1\ClassName2'];
115 
116  $this->classesScanner->expects($this->once())
117  ->method('getList')
118  ->with($path)
119  ->willReturn($classes);
120 
121  $parents = [
122  ['NameSpace1\ClassName1', ['Parent_Class_Name', 'Interface_1', 'Interface_2']],
123  ['NameSpace1\ClassName2', ['Parent_Class_Name', 'Interface_1', 'Interface_2']]
124  ];
125 
126  $this->classReaderMock->expects($this->exactly(count($classes)))
127  ->method('getParents')
128  ->will($this->returnValueMap(
129  $parents
130  ));
131 
132  $this->logMock->expects($this->never())
133  ->method('add');
134 
135  $this->validatorMock->expects($this->never())
136  ->method('validate');
137 
138  $this->model->getList($path);
139  $result = $this->model->getRelations();
140 
141  $expected = [
142  $classes[0] => $parents[0][1],
143  $classes[1] => $parents[1][1]
144  ];
145 
146  $this->assertEquals($result, $expected);
147  }
148 
154  public function testGetListException(\Exception $exception)
155  {
156  $path = '/tmp/test';
157 
158  $classes = ['NameSpace1\ClassName1'];
159 
160  $this->classesScanner->expects($this->once())
161  ->method('getList')
162  ->with($path)
163  ->willReturn($classes);
164 
165  $this->logMock->expects($this->once())
166  ->method('add')
167  ->with(Log::COMPILATION_ERROR, $classes[0], $exception->getMessage());
168 
169  $this->validatorMock->expects($this->exactly(count($classes)))
170  ->method('validate')
171  ->will(
172  $this->throwException($exception)
173  );
174 
175  $this->model->getList($path);
176 
177  $result = $this->model->getRelations();
178 
179  $this->assertEquals($result, []);
180  }
181 
188  {
189  return [
190  [new \Magento\Framework\Exception\ValidatorException(new \Magento\Framework\Phrase('Not Valid!'))],
191  [new \ReflectionException('Not Valid!')]
192  ];
193  }
194 }