ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ContainerMock.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ReflectionClass;
24
25trait ContainerMock
26{
27 private function mock(string $class)
28 {
29 return $this->getMockBuilder($class)->disableOriginalConstructor()->getMock();
30 }
31
32 private function mockMethod(string $class, string $method, array $args, $return, $times = null)
33 {
34 $times ??= self::once();
35 $mock = $this->mock($class);
36 $mock->expects($times)->method($method)->with(...$args)->willReturn($return);
37
38 return $mock;
39 }
40
45 private function assertGetter($class_or_instance, array $methods_and_values): void
46 {
47 $instance = is_string($class_or_instance) ? new $class_or_instance(...array_values($methods_and_values)) : $class_or_instance;
48 foreach ($methods_and_values as $method => $value) {
49 $this->assertSame($value, $instance->$method($value));
50 }
51 }
52
56 private function mockTree(string $class, array $tree)
57 {
58 $r = new ReflectionClass($class);
59 $mock = $this->mock($class);
60 foreach ($tree as $name => $value) {
61 $type = (string) $r->getMethod($name)->getReturnType();
62 $type = $type === 'self' ? $class : $type;
63 $type = preg_replace('/^\?/', '', $type);
64 $mock->method($name)->willReturn(
65 class_exists($type) || interface_exists($type) ?
66 (is_array($value) ? $this->mockTree($type, $value) : $value) :
67 $value
68 );
69 }
70
71 return $mock;
72 }
73}