Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InjectableTest.php
Go to the documentation of this file.
1 <?php
7 
9 
12 class InjectableTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $injectable;
18 
22  protected $fileReflection;
23 
28 
32  protected $declaredClass;
33 
37  public function setUp()
38  {
39  $this->injectable = new Injectable();
40  $this->fileReflection = $this->getMockBuilder(
41  \Zend\Code\Reflection\FileReflection::class
42  )->disableOriginalConstructor()->getMock();
43 
44  $classReflection = $this->getMockBuilder(
45  \Zend\Code\Reflection\ClassReflection::class
46  )->disableOriginalConstructor()->getMock();
47 
48  $methodReflection = $this->getMockBuilder(
49  \Zend\Code\Reflection\MethodReflection::class
50  )->disableOriginalConstructor()->getMock();
51 
52  $this->parameterReflection = $this->getMockBuilder(
53  \Zend\Code\Reflection\ParameterReflection::class
54  )->disableOriginalConstructor()->getMock();
55 
56  $this->declaredClass = $this->getMockBuilder(
57  \Zend\Code\Reflection\ClassReflection::class
58  )->disableOriginalConstructor()->getMock();
59 
60  $methodReflection->expects(
61  $this->once()
62  )->method(
63  'getDeclaringClass'
64  )->will(
65  $this->returnValue($this->declaredClass)
66  );
67 
68  $methodReflection->expects(
69  $this->any()
70  )->method(
71  'getParameters'
72  )->will(
73  $this->returnValue([$this->parameterReflection])
74  );
75 
76  $classReflection->expects(
77  $this->once()
78  )->method(
79  'getMethods'
80  )->will(
81  $this->returnValue([$methodReflection])
82  );
83 
84  $this->fileReflection->expects(
85  $this->once()
86  )->method(
87  'getClasses'
88  )->will(
89  $this->returnValue([$classReflection])
90  );
91  }
92 
98  public function testGetDependencies()
99  {
100  $classReflection = $this->getMockBuilder(
101  \Zend\Code\Reflection\ClassReflection::class
102  )->disableOriginalConstructor()->getMock();
103 
104  $classReflection->expects(
105  $this->once()
106  )->method(
107  'getName'
108  )->will(
109  $this->returnValue(\Magento\Core\Model\Object::class)
110  );
111 
112  $this->parameterReflection->expects(
113  $this->once()
114  )->method(
115  'getClass'
116  )->will(
117  $this->returnValue($classReflection)
118  );
119 
120  $this->assertEquals(
121  [\Magento\Core\Model\Object::class],
122  $this->injectable->getDependencies($this->fileReflection)
123  );
124  }
125 
132  {
133  $this->parameterReflection->expects($this->once())->method('getClass')->will(
134  $this->returnCallback(
135  function () {
136  throw new \ReflectionException('Class Magento\Core\Model\Object does not exist');
137  }
138  )
139  );
140 
141  $this->assertEquals(
142  [\Magento\Core\Model\Object::class],
143  $this->injectable->getDependencies($this->fileReflection)
144  );
145  }
146 
154  {
155  $this->parameterReflection->expects($this->once())->method('getClass')->will(
156  $this->returnCallback(
157  function () {
158  throw new \ReflectionException('Some message');
159  }
160  )
161  );
162 
163  $this->injectable->getDependencies($this->fileReflection);
164  }
165 
172  {
173  $this->declaredClass->expects($this->once())->method('getName')->will($this->returnValue('ParentClass'));
174 
175  $this->injectable->getDependencies($this->fileReflection);
176  }
177 }