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\DisableConstructorPatch);
56 $doubler->registerClassPatch(new ClassPatch\ProphecySubjectPatch);
57 $doubler->registerClassPatch(new ClassPatch\ReflectionClassNewInstancePatch);
58 $doubler->registerClassPatch(new ClassPatch\HhvmExceptionPatch());
59 $doubler->registerClassPatch(new ClassPatch\MagicCallPatch);
60 $doubler->registerClassPatch(new ClassPatch\KeywordPatch);
63 $this->doubler = $doubler;
64 $this->revealer = $revealer ?: new Revealer;
65 $this->util = $util ?: new StringUtil;
69 * Creates new object prophecy.
71 * @param null|string $classOrInterface Class or interface name
73 * @return ObjectProphecy
75 public function prophesize($classOrInterface = null)
77 $this->prophecies[] = $prophecy = new ObjectProphecy(
78 new LazyDouble($this->doubler),
79 new CallCenter($this->util),
83 if ($classOrInterface && class_exists($classOrInterface)) {
84 return $prophecy->willExtend($classOrInterface);
87 if ($classOrInterface && interface_exists($classOrInterface)) {
88 return $prophecy->willImplement($classOrInterface);
95 * Returns all created object prophecies.
97 * @return ObjectProphecy[]
99 public function getProphecies()
101 return $this->prophecies;
105 * Returns Doubler instance assigned to this Prophet.
109 public function getDoubler()
111 return $this->doubler;
115 * Checks all predictions defined by prophecies of this Prophet.
117 * @throws Exception\Prediction\AggregateException If any prediction fails
119 public function checkPredictions()
121 $exception = new AggregateException("Some predictions failed:\n");
122 foreach ($this->prophecies as $prophecy) {
124 $prophecy->checkProphecyMethodsPredictions();
125 } catch (PredictionException $e) {
126 $exception->append($e);
130 if (count($exception->getExceptions())) {