Пример #1
0
 /**
  * @param string $name
  *
  * @return Collaborator
  *
  * @throws \PhpSpec\Exception\Wrapper\CollaboratorException
  */
 public function get($name)
 {
     if (!$this->has($name)) {
         throw new CollaboratorException(sprintf('Collaborator %s not found.', $this->presenter->presentString($name)));
     }
     return $this->collaborators[$name];
 }
 function let(Presenter $presenter)
 {
     $presenter->presentValue(Argument::any())->will(function ($subject) {
         return '"' . $subject[0] . '"';
     });
     $this->beConstructedWith($presenter);
 }
Пример #3
0
 /**
  * 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)));
     }
 }
Пример #4
0
 function let(Presenter $presenter)
 {
     $presenter->presentValue(Argument::any())->willReturn('val');
     $presenter->presentString(Argument::any())->willReturnArgument();
     $this->beConstructedWith('custom', function () {
     }, $presenter);
 }
Пример #5
0
 function it_creates_an_accessing_property_on_non_object_exception(Presenter $presenter)
 {
     $presenter->presentString("{$this->fixture->property}")->shouldBeCalled()->willReturn("\"{$this->fixture->property}\"");
     $fixtureMessage = "Getting property \"{$this->fixture->property}\" on a non-object.";
     $exception = $this->gettingPropertyOnNonObject($this->fixture->property);
     $exception->shouldHaveType('PhpSpec\\Exception\\Wrapper\\SubjectException');
     $exception->getMessage()->shouldBe($fixtureMessage);
 }
Пример #6
0
 /**
  * {@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;
 }
Пример #7
0
 /**
  * @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);
 }
Пример #8
0
 /**
  * @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);
 }
Пример #9
0
 function it_writes_a_fail_message_for_a_failing_example(Template $template, ExampleEvent $event, Presenter $presenter)
 {
     $event->getTitle()->willReturn(self::EVENT_TITLE);
     $event->getMessage()->willReturn(self::EVENT_MESSAGE);
     $event->getBacktrace()->willReturn(self::$BACKTRACE);
     $event->getException()->willReturn(new \Exception());
     $template->render(HtmlTemplate::DIR . '/Template/ReportFailed.html', array('title' => self::EVENT_TITLE, 'message' => self::EVENT_MESSAGE, 'backtrace' => self::BACKTRACE, 'code' => self::CODE, 'index' => 1, 'specification' => 1))->shouldBeCalled();
     $presenter->presentException(Argument::cetera())->willReturn(self::CODE);
     $this->write(1);
 }
Пример #10
0
 /**
  * @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);
 }
Пример #11
0
 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);
 }
Пример #12
0
 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]);
 }
Пример #13
0
 /**
  * @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();
     }
 }
Пример #14
0
 /**
  * @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])));
 }
Пример #15
0
 function let(Presenter $presenter)
 {
     $presenter->presentValue(Argument::any())->willReturn('val1', 'val2');
     $presenter->presentString(Argument::any())->willReturnArgument();
     $this->beConstructedWith($presenter);
 }
Пример #16
0
 /**
  * @param string $property
  *
  * @return SubjectException
  */
 public function gettingPropertyOnNonObject($property)
 {
     return new SubjectException(sprintf('Getting property %s on a non-object.', $this->presenter->presentString($property)));
 }
Пример #17
0
 function let(Presenter $presenter)
 {
     $presenter->presentValue(Argument::any())->willReturn(41.889240346184, 0.1);
     $this->beConstructedWith($presenter);
 }
 function let(Presenter $presenter)
 {
     $presenter->presentValue(Argument::any())->willReturn('traversable');
     $this->beConstructedWith($presenter);
 }
Пример #19
0
 /**
  * @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 start with %s, but it does.', $this->presenter->presentString($subject), $this->presenter->presentString($arguments[0])));
 }
Пример #20
0
 /**
  * @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 items, but got it.', $this->presenter->presentValue($subject), $this->presenter->presentString(intval($arguments[0]))));
 }
Пример #21
0
 function let(Presenter $presenter)
 {
     $presenter->presentString(Argument::type('string'))->willReturnArgument();
     $this->beConstructedWith($presenter);
 }
Пример #22
0
 /**
  * @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))));
 }
Пример #23
0
 /**
  * @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)));
 }
Пример #24
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)));
 }
Пример #25
0
 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));
 }
Пример #26
0
 /**
  * @param int $index
  */
 public function write($index)
 {
     $code = $this->presenter->presentException($this->event->getException(), true);
     $this->template->render(Template::DIR . '/Template/ReportFailed.html', array('title' => htmlentities(strip_tags($this->event->getTitle())), 'message' => htmlentities(strip_tags($this->event->getMessage())), 'backtrace' => $this->formatBacktrace(), 'code' => $code, 'index' => self::$failingExamplesCount++, 'specification' => $index));
 }
Пример #27
0
 /**
  * {@inheritdoc}
  */
 protected function getNegativeFailureException($name, $subject, array $arguments)
 {
     return new FailureException(sprintf('Expected %s not to have an element with %s key and %s value, but it does.', $this->presenter->presentValue($subject), $this->presenter->presentValue($arguments[0]), $this->presenter->presentValue($arguments[1])));
 }
Пример #28
0
 function it_should_return_presented_exceptions_from_the_decorated_presenter_unchanged(Presenter $presenter, \Exception $exception)
 {
     $presenter->presentException($exception, true)->willReturn('exc');
     $this->presentException($exception, true)->shouldReturn('exc');
 }
Пример #29
0
 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)));
 }
Пример #30
0
 /**
  * @param mixed $subject
  *
  * @return SubjectException
  */
 private function cantUseAsArray($subject)
 {
     return new SubjectException(sprintf('Can not use %s as array.', $this->presenter->presentValue($subject)));
 }