-
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathProphecyTrait.php
More file actions
104 lines (90 loc) · 2.52 KB
/
ProphecyTrait.php
File metadata and controls
104 lines (90 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace PhpBench\Tests;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use Prophecy\Exception\Prediction\PredictionException;
use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use Prophecy\Prophet;
/**
* This stub is to permit compatibility with both PHPUnit 8.0 and 9.0 and is
* mostly lifted from phpspec/prophecy-phpunit.
*/
trait ProphecyTrait
{
/**
* @internal
*/
private ?Prophet $prophet = null;
/**
* @var bool
*
* @internal
*/
private $prophecyAssertionsCounted = false;
/**
* @template T of object
*
* @param class-string<T> $classOrInterface
*
* @return ObjectProphecy<T>
*/
protected function prophesize(string $classOrInterface): ObjectProphecy
{
/** @var ObjectProphecy<T> */
return $this->getProphet()->prophesize($classOrInterface);
}
/**
* @postCondition
*/
protected function verifyProphecyDoubles(): void
{
if ($this->prophet === null) {
return;
}
try {
$this->prophet->checkPredictions();
} catch (PredictionException $e) {
throw new AssertionFailedError($e->getMessage());
} finally {
$this->countProphecyAssertions();
}
}
/**
* @after
*/
protected function tearDownProphecy(): void
{
if (null !== $this->prophet && !$this->prophecyAssertionsCounted) {
// Some Prophecy assertions may have been done in tests themselves even when a failure happened before checking mock objects.
$this->countProphecyAssertions();
}
$this->prophet = null;
}
/**
* @internal
*/
private function countProphecyAssertions(): void
{
\assert($this instanceof TestCase);
$this->prophecyAssertionsCounted = true;
foreach ($this->prophet->getProphecies() as $objectProphecy) {
foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) {
foreach ($methodProphecies as $methodProphecy) {
\assert($methodProphecy instanceof MethodProphecy);
$this->addToAssertionCount(\count($methodProphecy->getCheckedPredictions()));
}
}
}
}
/**
* @internal
*/
private function getProphet(): Prophet
{
if ($this->prophet === null) {
$this->prophet = new Prophet();
}
return $this->prophet;
}
}