public function getComment()
 {
     $msg = "Expected call to " . $this->method;
     if (count($this->args)) {
         $msg .= " with " . Util::printArray($this->args);
     } elseif ($this->anyArgs) {
         $msg .= " with any args";
     } else {
         $msg .= " with no args";
     }
     if ($this->times === 0) {
         $msg .= " never";
     } else {
         if ($this->atLeast) {
             $msg .= " at least";
         }
         $msg .= " " . $this->times . " times";
         $msg .= ", called " . $this->callCount . " times";
     }
     return $msg;
 }
Example #2
0
 /**
  * Have the exepectation set receive a method and a set of arguments, "perfoming" the matching expectation
  * or throwing an exception if no matching expectation is found.
  *
  * @param $method method name
  * @param $arguments array of argumetns passed to the method
  * @throws Exception\UnexpectedCallException
  * @return
  */
 public function receive($method, $arguments)
 {
     foreach ($this->expectations as $expectation) {
         if ($expectation->matches($method, $arguments)) {
             return $expectation->perform($method, $arguments);
         }
     }
     // Give the expectations a chance to 'comment' on the received arguments
     $message = "Unexpected call to method {$method}";
     if (count($arguments)) {
         $message .= " with " . Util::printArray($arguments);
     }
     $comments = [$message];
     foreach ($this->expectations as $expectation) {
         $comment = $expectation->commentOn($method, $arguments);
         if ($comment != null) {
             $comments[] = $comment;
         }
     }
     throw new UnexpectedCallException(implode("\n", $comments));
 }
 /**
  * The method should be called this number of times.
  *
  * @param int|null $times
  * @return MutableExpectationBuilder
  */
 public function times($times = null)
 {
     // API compat with mockery
     if ($this->atLeastWasCalled && $times !== null) {
         $this->atLeastWasCalled = false;
         $this->atLeast($times);
     } elseif ($times !== null) {
         if ($times === 0) {
             $this->never();
         } else {
             // normal operation
             $count = 0;
             $this->expectation->setPostPerformCallback(function () use(&$count, $times) {
                 $count++;
                 $this->commentBuilder->incrementCallCount();
                 if ($count === $times) {
                     $this->expectation->setSatisfied(true);
                     $this->expectation->setPerformCallback(function ($name, $args) use($times) {
                         $message = "Unexpected call to {$this->methodName}";
                         if (count($args)) {
                             $message .= " with " . Util::printArray($args);
                         }
                         $message .= " - already called {$times} times";
                         throw new UnexpectedCallException($message);
                     });
                 }
             });
             $this->commentBuilder->setTimes($times);
             $this->commentBuilder->setAtLeast(false);
         }
     }
     return $this;
 }