/** * Evaluates negative match. * * @param string $name * @param mixed $subject * @param array $arguments * * @throws \PhpSpec\Exception\Example\FailureException * @return boolean */ public function negativeMatch($name, $subject, array $arguments) { $checker = $this->getCheckerName($name); if (call_user_func($checker, $subject)) { throw new FailureException(sprintf('%s not expected to return %s, but it did.', $this->presenter->presentString(sprintf('%s(%s)', $checker, $this->presenter->presentValue($subject))), $this->presenter->presentValue(true))); } }
/** * {@inheritdoc} */ public function negativeMatch($name, $subject, array $arguments) { $count = $this->countDifference($subject, (int) $arguments[0]); if (self::EQUAL === $count) { throw new FailureException(sprintf('Expected %s not to have %s items, but got it.', $this->presenter->presentValue($subject), $this->presenter->presentString((int) $arguments[0]))); } return $subject; }
/** * @param string $keyword * @param mixed $subject * @param array $arguments * * @return Matcher * * @throws \PhpSpec\Exception\Wrapper\MatcherNotFoundException */ public function find($keyword, $subject, array $arguments) { foreach ($this->matchers as $matcher) { if (true === $matcher->supports($keyword, $subject, $arguments)) { return $matcher; } } throw new MatcherNotFoundException(sprintf('No %s(%s) matcher found for %s.', $this->presenter->presentString($keyword), $this->presenter->presentValue($arguments), $this->presenter->presentValue($subject)), $keyword, $subject, $arguments); }
/** * @param array $args * * @throws \PhpSpec\Exception\Wrapper\SubjectException */ public function beConstructedWith($args) { if (null === $this->classname) { throw new SubjectException(sprintf('You can not set object arguments. Behavior subject is %s.', $this->presenter->presentValue(null))); } if ($this->isInstantiated()) { throw new SubjectException('You can not change object construction method when it is already instantiated'); } $this->beAnInstanceOf($this->classname, $args); }
/** * @param mixed $value * @return string */ public function present($value) { if (is_array($value)) { $type = is_object($value[0]) ? $this->presenter->presentValue($value[0]) : $value[0]; return sprintf('%s::%s()', $type, $value[1]); } if ($value instanceof \Closure) { return '[closure]'; } if (is_object($value)) { return sprintf('[obj:%s]', get_class($value)); } return sprintf('[%s()]', $value); }
function it_throws_a_failure_exception_with_the_thrown_exceptions_message_if_a_positive_match_failed(Presenter $presenter) { if (!class_exists('\\Error')) { throw new SkippingException('The class Error, introduced in PHP 7, does not exist'); } $actually_thrown_error = new \Error('This is a test Error'); $callable = function () use($actually_thrown_error) { throw $actually_thrown_error; }; $expected_error = new \PhpSpec\Exception\Example\FailureException('Expected exception of class Exception, but got Error with the message: "This is a test Error"'); $incorrectly_matched_exception = new \Exception('This is the exception I expect to be thrown.'); $presenter->presentValue($actually_thrown_error)->willReturn('Error'); $presenter->presentValue($incorrectly_matched_exception)->willReturn('Exception'); $this->shouldThrow($expected_error)->during('verifyPositive', [$callable, [], $incorrectly_matched_exception]); }
function let(Presenter $presenter) { $presenter->presentValue(Argument::any())->will(function ($subject) { return '"' . $subject[0] . '"'; }); $this->beConstructedWith($presenter); }
function let(Presenter $presenter) { $presenter->presentValue(Argument::any())->willReturn('val'); $presenter->presentString(Argument::any())->willReturnArgument(); $this->beConstructedWith('custom', function () { }, $presenter); }
function let(Presenter $presenter) { $presenter->presentValue(Argument::any())->will(function ($subject) { if (is_array($subject[0])) { return 'array'; } if (is_object($subject[0])) { return 'object'; } return $subject[0]; }); $presenter->presentString(Argument::any())->willReturnArgument(); $this->beConstructedWith($presenter); }
/** * @param array|\Traversable $subject * @param array|\Traversable $expected * * @throws \InvalidArgumentException * @throws SubjectElementDoesNotMatchException * @throws SubjectHasFewerElementsException * @throws SubjectHasMoreElementsException */ public function match($subject, $expected) { if (!$this->isIterable($subject)) { throw new \InvalidArgumentException('Subject value should be an array or implement \\Traversable.'); } if (!$this->isIterable($expected)) { throw new \InvalidArgumentException('Expected value should be an array or implement \\Traversable.'); } $expectedIterator = $this->createIteratorFromIterable($expected); $count = 0; foreach ($subject as $subjectKey => $subjectValue) { if (!$expectedIterator->valid()) { throw new SubjectHasMoreElementsException(); } if ($subjectKey !== $expectedIterator->key() || $subjectValue !== $expectedIterator->current()) { throw new SubjectElementDoesNotMatchException($count, $this->presenter->presentValue($subjectKey), $this->presenter->presentValue($subjectValue), $this->presenter->presentValue($expectedIterator->key()), $this->presenter->presentValue($expectedIterator->current())); } $expectedIterator->next(); ++$count; } if ($expectedIterator->valid()) { throw new SubjectHasFewerElementsException(); } }
/** * @param array $arguments * * @return null|string * @throws \PhpSpec\Exception\Example\MatcherException */ private function getException(array $arguments) { if (0 === count($arguments)) { return null; } if (is_string($arguments[0])) { return $arguments[0]; } if (is_object($arguments[0])) { if ($arguments[0] instanceof \Throwable) { return $arguments[0]; } elseif ($arguments[0] instanceof \Exception) { return $arguments[0]; } } throw new MatcherException(sprintf("Wrong argument provided in throw matcher.\n" . "Fully qualified classname or exception instance expected,\n" . "Got %s.", $this->presenter->presentValue($arguments[0]))); }
function let(Presenter $presenter) { $presenter->presentValue(Argument::any())->willReturn('val1', 'val2'); $presenter->presentString(Argument::any())->willReturnArgument(); $this->beConstructedWith($presenter); }
function let(Presenter $presenter) { $presenter->presentValue(Argument::any())->willReturn(41.889240346184, 0.1); $this->beConstructedWith($presenter); }
function it_should_present_a_magic_method_as_string(WithMagicCall $object, Presenter $presenter) { $className = get_class($object->getWrappedObject()); $presenter->presentValue($object->getWrappedObject())->willReturn(sprintf('[obj:%s]', $className)); $this->present(array($object, 'undefinedMethod'))->shouldReturn(sprintf('[obj:%s]::undefinedMethod()', $className)); }
function let(Presenter $presenter) { $presenter->presentValue(Argument::any())->willReturn('traversable'); $this->beConstructedWith($presenter); }
/** * @param string $name * @param mixed $subject * @param array $arguments * * @return FailureException */ protected function getNegativeFailureException($name, $subject, array $arguments) { return new FailureException(sprintf('Expected %s not to have %s key, but it does.', $this->presenter->presentValue($subject), $this->presenter->presentString($arguments[0]))); }
/** * @param callable $callable * @param Boolean $expectedBool * @param Boolean $result * * @return FailureException */ private function getFailureExceptionFor($callable, $expectedBool, $result) { return new FailureException(sprintf("Expected %s to return %s, but got %s.", $this->presenter->presentValue($callable), $this->presenter->presentValue($expectedBool), $this->presenter->presentValue($result))); }
/** * @param string $name * @param mixed $subject * @param array $arguments * * @return FailureException */ protected function getNegativeFailureException($name, $subject, array $arguments) { return new FailureException(sprintf('Did not expect instance of %s, but got %s.', $this->presenter->presentString($arguments[0]), $this->presenter->presentValue($subject))); }
protected function getNegativeFailureException($name, $subject, array $arguments) { return new FailureException(sprintf('Did Not expect an approximated value of %s, but got %s', $this->presenter->presentValue($arguments[0]), $this->presenter->presentValue($subject))); }
/** * @param mixed $subject * * @return SubjectException */ private function cantUseAsArray($subject) { return new SubjectException(sprintf('Can not use %s as array.', $this->presenter->presentValue($subject))); }
/** * @param string $name * @param mixed $subject * @param array $arguments * * @return FailureException */ protected function getNegativeFailureException($name, $subject, array $arguments) { return new FailureException(sprintf('%s not expected to %s(%s), but it did.', $this->presenter->presentValue($subject), $this->presenter->presentString($name), implode(', ', array_map(array($this->presenter, 'presentValue'), $arguments)))); }
function it_should_tag_values_from_the_decorated_presenter(Presenter $presenter) { $presenter->presentValue('foo')->willReturn('zfooz'); $this->presentValue('foo')->shouldReturn('<value>zfooz</value>'); }