Prophet.php (3865B)
1 <?php 2 3 /* 4 * This file is part of the Prophecy. 5 * (c) Konstantin Kudryashov <ever.zet@gmail.com> 6 * Marcello Duarte <marcello.duarte@gmail.com> 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\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; 24 25 /** 26 * Prophet creates prophecies. 27 * 28 * @author Konstantin Kudryashov <ever.zet@gmail.com> 29 */ 30 class Prophet 31 { 32 private $doubler; 33 private $revealer; 34 private $util; 35 36 /** 37 * @var ObjectProphecy[] 38 */ 39 private $prophecies = array(); 40 41 /** 42 * Initializes Prophet. 43 * 44 * @param null|Doubler $doubler 45 * @param null|RevealerInterface $revealer 46 * @param null|StringUtil $util 47 */ 48 public function __construct(Doubler $doubler = null, RevealerInterface $revealer = null, 49 StringUtil $util = null) 50 { 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); 61 } 62 63 $this->doubler = $doubler; 64 $this->revealer = $revealer ?: new Revealer; 65 $this->util = $util ?: new StringUtil; 66 } 67 68 /** 69 * Creates new object prophecy. 70 * 71 * @param null|string $classOrInterface Class or interface name 72 * 73 * @return ObjectProphecy 74 */ 75 public function prophesize($classOrInterface = null) 76 { 77 $this->prophecies[] = $prophecy = new ObjectProphecy( 78 new LazyDouble($this->doubler), 79 new CallCenter($this->util), 80 $this->revealer 81 ); 82 83 if ($classOrInterface && class_exists($classOrInterface)) { 84 return $prophecy->willExtend($classOrInterface); 85 } 86 87 if ($classOrInterface && interface_exists($classOrInterface)) { 88 return $prophecy->willImplement($classOrInterface); 89 } 90 91 return $prophecy; 92 } 93 94 /** 95 * Returns all created object prophecies. 96 * 97 * @return ObjectProphecy[] 98 */ 99 public function getProphecies() 100 { 101 return $this->prophecies; 102 } 103 104 /** 105 * Returns Doubler instance assigned to this Prophet. 106 * 107 * @return Doubler 108 */ 109 public function getDoubler() 110 { 111 return $this->doubler; 112 } 113 114 /** 115 * Checks all predictions defined by prophecies of this Prophet. 116 * 117 * @throws Exception\Prediction\AggregateException If any prediction fails 118 */ 119 public function checkPredictions() 120 { 121 $exception = new AggregateException("Some predictions failed:\n"); 122 foreach ($this->prophecies as $prophecy) { 123 try { 124 $prophecy->checkProphecyMethodsPredictions(); 125 } catch (PredictionException $e) { 126 $exception->append($e); 127 } 128 } 129 130 if (count($exception->getExceptions())) { 131 throw $exception; 132 } 133 } 134 }