1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Prophecy. |
5
|
|
|
* (c) Konstantin Kudryashov <[email protected]> |
6
|
|
|
* Marcello Duarte <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Prophecy; |
13
|
|
|
|
14
|
|
|
use Prophecy\Doubler\CachedDoubler; |
15
|
|
|
use Prophecy\Doubler\Doubler; |
16
|
|
|
use Prophecy\Doubler\LazyDouble; |
17
|
|
|
use Prophecy\Doubler\ClassPatch; |
18
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
19
|
|
|
use Prophecy\Prophecy\RevealerInterface; |
20
|
|
|
use Prophecy\Prophecy\Revealer; |
21
|
|
|
use Prophecy\Call\CallCenter; |
22
|
|
|
use Prophecy\Util\StringUtil; |
23
|
|
|
use Prophecy\Exception\Prediction\PredictionException; |
24
|
|
|
use Prophecy\Exception\Prediction\AggregateException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Prophet creates prophecies. |
28
|
|
|
* |
29
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class Prophet |
32
|
|
|
{ |
33
|
|
|
private $doubler; |
34
|
|
|
private $revealer; |
35
|
|
|
private $util; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ObjectProphecy[] |
39
|
|
|
*/ |
40
|
|
|
private $prophecies = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Initializes Prophet. |
44
|
|
|
* |
45
|
|
|
* @param null|Doubler $doubler |
46
|
|
|
* @param null|RevealerInterface $revealer |
47
|
|
|
* @param null|StringUtil $util |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
Doubler $doubler = null, |
51
|
|
|
RevealerInterface $revealer = null, |
52
|
|
|
StringUtil $util = null |
53
|
|
|
) { |
54
|
|
|
if (null === $doubler) { |
55
|
|
|
$doubler = new CachedDoubler(); |
56
|
|
|
$doubler->registerClassPatch(new ClassPatch\SplFileInfoPatch); |
57
|
|
|
$doubler->registerClassPatch(new ClassPatch\TraversablePatch); |
58
|
|
|
$doubler->registerClassPatch(new ClassPatch\ThrowablePatch); |
59
|
|
|
$doubler->registerClassPatch(new ClassPatch\DisableConstructorPatch); |
60
|
|
|
$doubler->registerClassPatch(new ClassPatch\ProphecySubjectPatch); |
61
|
|
|
$doubler->registerClassPatch(new ClassPatch\ReflectionClassNewInstancePatch); |
62
|
|
|
$doubler->registerClassPatch(new ClassPatch\HhvmExceptionPatch()); |
63
|
|
|
$doubler->registerClassPatch(new ClassPatch\MagicCallPatch); |
64
|
|
|
$doubler->registerClassPatch(new ClassPatch\KeywordPatch); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->doubler = $doubler; |
68
|
|
|
$this->revealer = $revealer ?: new Revealer; |
69
|
|
|
$this->util = $util ?: new StringUtil; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Creates new object prophecy. |
74
|
|
|
* |
75
|
|
|
* @param null|string $classOrInterface Class or interface name |
76
|
|
|
* |
77
|
|
|
* @return ObjectProphecy |
78
|
|
|
*/ |
79
|
|
|
public function prophesize($classOrInterface = null) |
80
|
|
|
{ |
81
|
|
|
$this->prophecies[] = $prophecy = new ObjectProphecy( |
82
|
|
|
new LazyDouble($this->doubler), |
83
|
|
|
new CallCenter($this->util), |
84
|
|
|
$this->revealer |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
if ($classOrInterface && class_exists($classOrInterface)) { |
|
|
|
|
88
|
|
|
return $prophecy->willExtend($classOrInterface); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($classOrInterface && interface_exists($classOrInterface)) { |
|
|
|
|
92
|
|
|
return $prophecy->willImplement($classOrInterface); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $prophecy; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns all created object prophecies. |
100
|
|
|
* |
101
|
|
|
* @return ObjectProphecy[] |
102
|
|
|
*/ |
103
|
|
|
public function getProphecies() |
104
|
|
|
{ |
105
|
|
|
return $this->prophecies; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns Doubler instance assigned to this Prophet. |
110
|
|
|
* |
111
|
|
|
* @return Doubler |
112
|
|
|
*/ |
113
|
|
|
public function getDoubler() |
114
|
|
|
{ |
115
|
|
|
return $this->doubler; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Checks all predictions defined by prophecies of this Prophet. |
120
|
|
|
* |
121
|
|
|
* @throws Exception\Prediction\AggregateException If any prediction fails |
122
|
|
|
*/ |
123
|
|
|
public function checkPredictions() |
124
|
|
|
{ |
125
|
|
|
$exception = new AggregateException("Some predictions failed:\n"); |
126
|
|
|
foreach ($this->prophecies as $prophecy) { |
127
|
|
|
try { |
128
|
|
|
$prophecy->checkProphecyMethodsPredictions(); |
129
|
|
|
} catch (PredictionException $e) { |
130
|
|
|
$exception->append($e); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (count($exception->getExceptions())) { |
135
|
|
|
throw $exception; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: