Best Prophecy code snippet using CallPrediction.check
MethodProphecy.php
Source:MethodProphecy.php
...25 private $methodName;26 private $argumentsWildcard;27 private $promise;28 private $prediction;29 private $checkedPredictions = array();30 private $bound = false;31 /**32 * Initializes method prophecy.33 *34 * @param ObjectProphecy $objectProphecy35 * @param string $methodName36 * @param null|Argument\ArgumentsWildcard|array $arguments37 *38 * @throws \Prophecy\Exception\Doubler\MethodNotFoundException If method not found39 */40 public function __construct(ObjectProphecy $objectProphecy, $methodName, $arguments = null)41 {42 $double = $objectProphecy->reveal();43 if (!method_exists($double, $methodName)) {44 throw new MethodNotFoundException(sprintf(45 'Method `%s::%s()` is not defined.', get_class($double), $methodName46 ), get_class($double), $methodName);47 }48 $this->objectProphecy = $objectProphecy;49 $this->methodName = $methodName;50 $reflectedMethod = new \ReflectionMethod($double, $methodName);51 if ($reflectedMethod->isFinal()) {52 throw new MethodProphecyException(sprintf(53 "Can not add prophecy for a method `%s::%s()`\n".54 "as it is a final method.",55 get_class($double),56 $methodName57 ), $this);58 }59 if (null !== $arguments) {60 $this->withArguments($arguments);61 }62 }63 /**64 * Sets argument wildcard.65 *66 * @param array|Argument\ArgumentsWildcard $arguments67 *68 * @return $this69 *70 * @throws \Prophecy\Exception\InvalidArgumentException71 */72 public function withArguments($arguments)73 {74 if (is_array($arguments)) {75 $arguments = new Argument\ArgumentsWildcard($arguments);76 }77 if (!$arguments instanceof Argument\ArgumentsWildcard) {78 throw new InvalidArgumentException(sprintf(79 "Either an array or an instance of ArgumentsWildcard expected as\n".80 'a `MethodProphecy::withArguments()` argument, but got %s.',81 gettype($arguments)82 ));83 }84 $this->argumentsWildcard = $arguments;85 return $this;86 }87 /**88 * Sets custom promise to the prophecy.89 *90 * @param callable|Promise\PromiseInterface $promise91 *92 * @return $this93 *94 * @throws \Prophecy\Exception\InvalidArgumentException95 */96 public function will($promise)97 {98 if (is_callable($promise)) {99 $promise = new Promise\CallbackPromise($promise);100 }101 if (!$promise instanceof Promise\PromiseInterface) {102 throw new InvalidArgumentException(sprintf(103 'Expected callable or instance of PromiseInterface, but got %s.',104 gettype($promise)105 ));106 }107 $this->bindToObjectProphecy();108 $this->promise = $promise;109 return $this;110 }111 /**112 * Sets return promise to the prophecy.113 *114 * @see Prophecy\Promise\ReturnPromise115 *116 * @return $this117 */118 public function willReturn()119 {120 return $this->will(new Promise\ReturnPromise(func_get_args()));121 }122 /**123 * Sets return argument promise to the prophecy.124 *125 * @see Prophecy\Promise\ReturnArgumentPromise126 *127 * @return $this128 */129 public function willReturnArgument()130 {131 return $this->will(new Promise\ReturnArgumentPromise);132 }133 /**134 * Sets throw promise to the prophecy.135 *136 * @see Prophecy\Promise\ThrowPromise137 *138 * @param string|\Exception $exception Exception class or instance139 *140 * @return $this141 */142 public function willThrow($exception)143 {144 return $this->will(new Promise\ThrowPromise($exception));145 }146 /**147 * Sets custom prediction to the prophecy.148 *149 * @param callable|Prediction\PredictionInterface $prediction150 *151 * @return $this152 *153 * @throws \Prophecy\Exception\InvalidArgumentException154 */155 public function should($prediction)156 {157 if (is_callable($prediction)) {158 $prediction = new Prediction\CallbackPrediction($prediction);159 }160 if (!$prediction instanceof Prediction\PredictionInterface) {161 throw new InvalidArgumentException(sprintf(162 'Expected callable or instance of PredictionInterface, but got %s.',163 gettype($prediction)164 ));165 }166 $this->bindToObjectProphecy();167 $this->prediction = $prediction;168 return $this;169 }170 /**171 * Sets call prediction to the prophecy.172 *173 * @see Prophecy\Prediction\CallPrediction174 *175 * @return $this176 */177 public function shouldBeCalled()178 {179 return $this->should(new Prediction\CallPrediction);180 }181 /**182 * Sets no calls prediction to the prophecy.183 *184 * @see Prophecy\Prediction\NoCallsPrediction185 *186 * @return $this187 */188 public function shouldNotBeCalled()189 {190 return $this->should(new Prediction\NoCallsPrediction);191 }192 /**193 * Sets call times prediction to the prophecy.194 *195 * @see Prophecy\Prediction\CallTimesPrediction196 *197 * @param $count198 *199 * @return $this200 */201 public function shouldBeCalledTimes($count)202 {203 return $this->should(new Prediction\CallTimesPrediction($count));204 }205 /**206 * Checks provided prediction immediately.207 *208 * @param callable|Prediction\PredictionInterface $prediction209 *210 * @return $this211 *212 * @throws \Prophecy\Exception\InvalidArgumentException213 */214 public function shouldHave($prediction)215 {216 if (is_callable($prediction)) {217 $prediction = new Prediction\CallbackPrediction($prediction);218 }219 if (!$prediction instanceof Prediction\PredictionInterface) {220 throw new InvalidArgumentException(sprintf(221 'Expected callable or instance of PredictionInterface, but got %s.',222 gettype($prediction)223 ));224 }225 if (null === $this->promise) {226 $this->willReturn();227 }228 $calls = $this->getObjectProphecy()->findProphecyMethodCalls(229 $this->getMethodName(),230 $this->getArgumentsWildcard()231 );232 try {233 $prediction->check($calls, $this->getObjectProphecy(), $this);234 $this->checkedPredictions[] = $prediction;235 } catch (\Exception $e) {236 $this->checkedPredictions[] = $prediction;237 throw $e;238 }239 return $this;240 }241 /**242 * Checks call prediction.243 *244 * @see Prophecy\Prediction\CallPrediction245 *246 * @return $this247 */248 public function shouldHaveBeenCalled()249 {250 return $this->shouldHave(new Prediction\CallPrediction);251 }252 /**253 * Checks no calls prediction.254 *255 * @see Prophecy\Prediction\NoCallsPrediction256 *257 * @return $this258 */259 public function shouldNotHaveBeenCalled()260 {261 return $this->shouldHave(new Prediction\NoCallsPrediction);262 }263 /**264 * Checks no calls prediction.265 *266 * @see Prophecy\Prediction\NoCallsPrediction267 * @deprecated268 *269 * @return $this270 */271 public function shouldNotBeenCalled()272 {273 return $this->shouldNotHaveBeenCalled();274 }275 /**276 * Checks call times prediction.277 *278 * @see Prophecy\Prediction\CallTimesPrediction279 *280 * @param int $count281 *282 * @return $this283 */284 public function shouldHaveBeenCalledTimes($count)285 {286 return $this->shouldHave(new Prediction\CallTimesPrediction($count));287 }288 /**289 * Checks currently registered [with should(...)] prediction.290 */291 public function checkPrediction()292 {293 if (null === $this->prediction) {294 return;295 }296 $this->shouldHave($this->prediction);297 }298 /**299 * Returns currently registered promise.300 *301 * @return null|Promise\PromiseInterface302 */303 public function getPromise()304 {305 return $this->promise;306 }307 /**308 * Returns currently registered prediction.309 *310 * @return null|Prediction\PredictionInterface311 */312 public function getPrediction()313 {314 return $this->prediction;315 }316 /**317 * Returns predictions that were checked on this object.318 *319 * @return PredictionInterface[]320 */321 public function getCheckedPredictions()322 {323 return $this->checkedPredictions;324 }325 /**326 * Returns object prophecy this method prophecy is tied to.327 *328 * @return ObjectProphecy329 */330 public function getObjectProphecy()331 {332 return $this->objectProphecy;333 }334 /**335 * Returns method name.336 *337 * @return string...
Uses.php
Source:Uses.php
1 here assigns `CallPrediction` to our method prophecy.2Predictions are a delayed behavior check for your prophecies. You see, during the entire lifetime3of your doubles, Prophecy records every single call you're making against it inside your4code. After that, Prophecy can use this collected information to check if it matches defined5predictions. You can assign predictions to method prophecies using the6`MethodProphecy::should(PredictionInterface $prediction)` method. As a matter of fact,7the `shouldBeCalled()` method we used earlier is just a shortcut to:8```php9$entityManager->flush()->should(new Prophecy\Prediction\CallPrediction());10```11It checks if your method of interest (that matches both the method name and the arguments wildcard)12was called 1 or more times. If the prediction failed then it throws an exception. When does this13check happen? Whenever you call `checkPredictions()` on the main Prophet object:14```php15$prophet->checkPredictions();16```17In PHPUnit, you would want to put this call into the `tearDown()` method. If no predictions18are defined, it would do nothing. So it won't harm to call it after every test.19There are plenty more predictions you can play with:20- `CallPrediction` or `shouldBeCalled()` - checks that the method has been called 1 or more times21- `NoCallsPrediction` or `shouldNotBeCalled()` - checks that the method has not been called22- `CallTimesPrediction` or `shouldBeCalledTimes($count)` - checks that the method has been called23 `$count` times24- `CallbackPrediction` or `should($callback)` - checks the method against your own custom callback25Of course, you can always create your own custom prediction any time by implementing26`PredictionInterface`.27### Spies28The last bit of awesomeness in Prophecy is out-of-the-box spies support. As I said in the previous29section, Prophecy records every call made during the double's entire lifetime. This means30you don't need to record predictions in order to check them. You can also do it31manually by using the `MethodProphecy::shouldHave(PredictionInterface $prediction)` method:32```php33$em = $prophet->prophesize('Doctrine...
check
Using AI Code Generation
1$callPrediction = new CallPrediction();2$callPrediction->check();3$callPrediction = new CallPrediction();4$callPrediction->check();5$callPrediction = new CallPrediction();6$callPrediction->check();7$callPrediction = new CallPrediction();8$callPrediction->check();9$callPrediction = new CallPrediction();10$callPrediction->check();11$callPrediction = new CallPrediction();12$callPrediction->check();13$callPrediction = new CallPrediction();14$callPrediction->check();15$callPrediction = new CallPrediction();16$callPrediction->check();17$callPrediction = new CallPrediction();18$callPrediction->check();19$callPrediction = new CallPrediction();20$callPrediction->check();21$callPrediction = new CallPrediction();22$callPrediction->check();23$callPrediction = new CallPrediction();24$callPrediction->check();25$callPrediction = new CallPrediction();26$callPrediction->check();27$callPrediction = new CallPrediction();28$callPrediction->check();29$callPrediction = new CallPrediction();30$callPrediction->check();31$callPrediction = new CallPrediction();32$callPrediction->check();
check
Using AI Code Generation
1$callpred = new CallPrediction();2if($callpred->check('9999999999'))3{4 echo "valid";5}6{7 echo "invalid";8}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with check on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!