1 | <?php |
||
28 | abstract class UnitTestCase extends TestCase |
||
29 | { |
||
30 | use ReflectionMethods; |
||
31 | use OnlyTestLogging; |
||
32 | |||
33 | //<editor-fold desc="Public Methods"> |
||
34 | |||
35 | /** |
||
36 | * @param array|ArrayAccess $subset |
||
37 | * @param array|ArrayAccess $array |
||
38 | */ |
||
39 | public static function assertArrayIsSubset($subset, $array): void |
||
46 | |||
47 | public function tearDown() |
||
51 | //</editor-fold desc="Public Methods"> |
||
52 | |||
53 | //<editor-fold desc="Protected Final Methods"> |
||
54 | |||
55 | /** @noinspection PhpDocMissingThrowsInspection */ |
||
56 | /** |
||
57 | * Gets a mock class (with full implementation). The given arguments are used for the arguments for the constructor. |
||
58 | * If too less arguments are given mocks are created for the rest of the constructor arguments. |
||
59 | * @param string $className the class to mock |
||
60 | * @param array $arguments the arguments to use for the constructor |
||
61 | * @param string[] $mockedMethods the methods to mock in the class |
||
62 | * @return MockObject the mocked object |
||
63 | * @throws ReflectionException |
||
64 | */ |
||
65 | protected final function getMockWithMockedArguments(string $className, array $arguments = [], |
||
77 | |||
78 | /** |
||
79 | * Gets a new instance of the given class. The given arguments are used for the arguments for the constructor. |
||
80 | * If too less arguments are given mocks are created for the rest of the constructor arguments. |
||
81 | * @param string $className the class for which to create an instance |
||
82 | * @param array $arguments the arguments to use for the constructor |
||
83 | * @return mixed an instance of the given class |
||
84 | * @throws ReflectionException |
||
85 | */ |
||
86 | protected final function getObjectWithMockedArguments($className, array $arguments = []) |
||
97 | //</editor-fold desc="Protected Final Methods"> |
||
98 | |||
99 | //<editor-fold desc="Protected Methods"> |
||
100 | |||
101 | /** |
||
102 | * @param string|string[] $originalClassName |
||
103 | * @param array $methods |
||
104 | * @return MockObject |
||
105 | */ |
||
106 | protected function createPartialMock($originalClassName, array $methods): MockObject |
||
110 | |||
111 | /** |
||
112 | * Creates a stub with a given set of stubbed methods, which will return the given results |
||
113 | * @param string $class the class name |
||
114 | * @param array $methodResults a dictionary mapping method names to results of this methods |
||
115 | * @return MockObject|mixed the configured stub |
||
116 | */ |
||
117 | protected function createStub(string $class, array $methodResults = []): MockObject |
||
121 | |||
122 | /** |
||
123 | * Creates an empty mock with a getId method |
||
124 | * @param string $class the class to mock |
||
125 | * @param string $entityId the id to assign |
||
126 | * @param string $getterMethod the name of the getter method |
||
127 | * @return MockObject|mixed the mocked instance |
||
128 | */ |
||
129 | protected function createStubWithId(string $class, $entityId = "entity-id", $getterMethod = 'getId') |
||
133 | |||
134 | /** |
||
135 | * Gets a mock for an entity manager which creates a query builder which will return a query which will return the |
||
136 | * given result. |
||
137 | * @param array $results the result arrays the queries should return |
||
138 | * @param string[] $expectedQueries the expected queries if set |
||
139 | * @param string[] $otherMockedMethods list of other methods to mock |
||
140 | * @return MockObject|EntityManager the mocked entity manager |
||
141 | * @throws ReflectionException |
||
142 | */ |
||
143 | protected function getEntityManagerMockForQueries(array $results, array $expectedQueries = [], |
||
189 | |||
190 | /** |
||
191 | * Gets a mock for an entity manager which creates a query builder which will return a query which will return the |
||
192 | * given result. |
||
193 | * @param array $result the result array the query should return |
||
194 | * @param string|null $expectedQuery the expected query if set |
||
195 | * @param string[] $otherMockedMethods list of other methods to mock |
||
196 | * @param int $amount the number of times this query gets sent |
||
197 | * @return MockObject|EntityManager the mocked entity manager |
||
198 | * @throws ReflectionException |
||
199 | */ |
||
200 | protected function getEntityManagerMockForQuery(array $result, ?string $expectedQuery = null, |
||
206 | |||
207 | /** @noinspection PhpTooManyParametersInspection */ |
||
208 | |||
209 | /** |
||
210 | * @param string $className |
||
211 | * @param array $methodNames |
||
212 | * @param array $additionalInterfaces |
||
213 | * @param string|null $baseClass |
||
214 | * @param bool $callParentConstructor |
||
215 | * @param bool $hasInit |
||
216 | * @return MockObject |
||
217 | * @throws ReflectionException |
||
218 | */ |
||
219 | protected function getMockedEntity(string $className, array $methodNames = [], array $additionalInterfaces = [], |
||
250 | |||
251 | /** |
||
252 | * @param string $className |
||
253 | * @param array $methods |
||
254 | * @param array $additionalInterfaces |
||
255 | * @return MockObject |
||
256 | * @throws ReflectionException |
||
257 | */ |
||
258 | protected function getMockedTournamentHierarchyEntity(string $className, array $methods = [], |
||
263 | |||
264 | /** @noinspection PhpTooManyParametersInspection */ |
||
265 | /** |
||
266 | * @param $traitName |
||
267 | * @param array $methods |
||
268 | * @param array $arguments |
||
269 | * @param string $mockClassName |
||
270 | * @param bool $callOriginalConstructor |
||
271 | * @param bool $callOriginalClone |
||
272 | * @param bool $callAutoload |
||
273 | * @param bool $cloneArguments |
||
274 | * @return MockObject |
||
275 | * @throws ReflectionException |
||
276 | */ |
||
277 | protected function getPartialMockForTrait($traitName, array $methods, array $arguments = [], |
||
287 | |||
288 | /** @noinspection PhpTooManyParametersInspection */ |
||
289 | /** |
||
290 | * @param string $className |
||
291 | * @param array $methods |
||
292 | * @param array $additionalInterfaces |
||
293 | * @param string|null $baseClass |
||
294 | * @param bool $callParentConstructor |
||
295 | * @param bool $hasInit |
||
296 | * @return MockObject |
||
297 | * @throws ReflectionException |
||
298 | */ |
||
299 | protected function getStubbedEntity(string $className, array $methods = [], array $additionalInterfaces = [], |
||
309 | |||
310 | /** |
||
311 | * @param MockObject $o |
||
312 | * @param $methods |
||
313 | */ |
||
314 | protected function stubMethods(MockObject $o, $methods) |
||
320 | //</editor-fold desc="Protected Methods"> |
||
321 | } |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: