|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Zalas\PHPUnit\Doubles\TestCase; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
7
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
8
|
|
|
use Zalas\PHPUnit\Doubles\Extractor\Property; |
|
9
|
|
|
use Zalas\PHPUnit\Doubles\PhpDocumentor\ReflectionExtractor; |
|
10
|
|
|
|
|
11
|
|
|
trait TestDoubles |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @before |
|
15
|
|
|
*/ |
|
16
|
4 |
|
protected function initialiseTestDoubles(): void |
|
17
|
|
|
{ |
|
18
|
4 |
|
foreach ($this->getTestDoubleProperties() as $property) { |
|
19
|
4 |
|
$this->{$property->getName()} = $this->createTestDouble($property); |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
4 |
|
private function createTestDouble(Property $property) |
|
24
|
|
|
{ |
|
25
|
4 |
|
if ($property->hasType(ObjectProphecy::class)) { |
|
26
|
|
|
return $this->createTestDoubleWithProphecy($property->getTypesFiltered(function (string $type) { |
|
27
|
2 |
|
return ObjectProphecy::class !== $type; |
|
28
|
2 |
|
})); |
|
29
|
|
|
} |
|
30
|
2 |
|
if ($property->hasType(MockObject::class)) { |
|
31
|
|
|
return $this->createTestDoubleWithPhpunit($property->getTypesFiltered(function (string $type) { |
|
32
|
2 |
|
return MockObject::class !== $type; |
|
33
|
2 |
|
})); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
private function createTestDoubleWithProphecy(array $types): ObjectProphecy |
|
38
|
|
|
{ |
|
39
|
2 |
|
$prophecy = $this->prophesize(\array_shift($types)); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
2 |
|
foreach ($types as $type) { |
|
42
|
2 |
|
if (\interface_exists($type)) { |
|
43
|
2 |
|
$prophecy->willImplement($type); |
|
44
|
|
|
} else { |
|
45
|
2 |
|
$prophecy->willExtend($type); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
return $prophecy; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
private function createTestDoubleWithPhpunit(array $types): MockObject |
|
53
|
|
|
{ |
|
54
|
2 |
|
return $this->getMockBuilder(1 === \count($types) ? \array_pop($types) : $types) |
|
|
|
|
|
|
55
|
2 |
|
->disableOriginalConstructor() |
|
56
|
2 |
|
->disableOriginalClone() |
|
57
|
2 |
|
->disableArgumentCloning() |
|
58
|
2 |
|
->disableProxyingToOriginalMethods() |
|
59
|
2 |
|
->disallowMockingUnknownTypes() |
|
60
|
2 |
|
->getMock(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
private function getTestDoubleProperties(): array |
|
64
|
|
|
{ |
|
65
|
|
|
return (new ReflectionExtractor())->extract($this, function (Property $property) { |
|
66
|
4 |
|
return $property->hasType(ObjectProphecy::class) || $property->hasType(MockObject::class); |
|
67
|
4 |
|
}); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|