4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 * Marcello Duarte <marcello.duarte@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
14 use Prophecy\Doubler\Doubler;
15 use Prophecy\Doubler\LazyDouble;
16 use Prophecy\Doubler\ClassPatch;
17 use Prophecy\Prophecy\ObjectProphecy;
18 use Prophecy\Prophecy\RevealerInterface;
19 use Prophecy\Prophecy\Revealer;
20 use Prophecy\Call\CallCenter;
21 use Prophecy\Util\StringUtil;
22 use Prophecy\Exception\Prediction\PredictionException;
23 use Prophecy\Exception\Prediction\AggregateException;
26 * Prophet creates prophecies.
28 * @author Konstantin Kudryashov <ever.zet@gmail.com>
37 * @var ObjectProphecy[]
39 private $prophecies = array();
42 * Initializes Prophet.
44 * @param null|Doubler $doubler
45 * @param null|RevealerInterface $revealer
46 * @param null|StringUtil $util
48 public function __construct(Doubler $doubler = null, RevealerInterface $revealer = null,
49 StringUtil $util = null)
51 if (null === $doubler) {
52 $doubler = new Doubler;
53 $doubler->registerClassPatch(new ClassPatch\SplFileInfoPatch);
54 $doubler->registerClassPatch(new ClassPatch\TraversablePatch);
55 $doubler->registerClassPatch(new ClassPatch\ThrowablePatch);
56 $doubler->registerClassPatch(new ClassPatch\DisableConstructorPatch);
57 $doubler->registerClassPatch(new ClassPatch\ProphecySubjectPatch);
58 $doubler->registerClassPatch(new ClassPatch\ReflectionClassNewInstancePatch);
59 $doubler->registerClassPatch(new ClassPatch\HhvmExceptionPatch());
60 $doubler->registerClassPatch(new ClassPatch\MagicCallPatch);
61 $doubler->registerClassPatch(new ClassPatch\KeywordPatch);
64 $this->doubler = $doubler;
65 $this->revealer = $revealer ?: new Revealer;
66 $this->util = $util ?: new StringUtil;
70 * Creates new object prophecy.
72 * @param null|string $classOrInterface Class or interface name
74 * @return ObjectProphecy
76 public function prophesize($classOrInterface = null)
78 $this->prophecies[] = $prophecy = new ObjectProphecy(
79 new LazyDouble($this->doubler),
80 new CallCenter($this->util),
84 if ($classOrInterface && class_exists($classOrInterface)) {
85 return $prophecy->willExtend($classOrInterface);
88 if ($classOrInterface && interface_exists($classOrInterface)) {
89 return $prophecy->willImplement($classOrInterface);
96 * Returns all created object prophecies.
98 * @return ObjectProphecy[]
100 public function getProphecies()
102 return $this->prophecies;
106 * Returns Doubler instance assigned to this Prophet.
110 public function getDoubler()
112 return $this->doubler;
116 * Checks all predictions defined by prophecies of this Prophet.
118 * @throws Exception\Prediction\AggregateException If any prediction fails
120 public function checkPredictions()
122 $exception = new AggregateException("Some predictions failed:\n");
123 foreach ($this->prophecies as $prophecy) {
125 $prophecy->checkProphecyMethodsPredictions();
126 } catch (PredictionException $e) {
127 $exception->append($e);
131 if (count($exception->getExceptions())) {