function it_accepts_a_method_during_which_an_error_specified_by_instance_should_be_thrown(ArrayObject $arr, ReflectionFactory $factory) { if (!class_exists('\\Error')) { throw new SkippingException('The class Error, introduced in PHP 7, does not exist'); } $error = new \Error(); $arr->ksort()->will(function () use($error) { throw $error; }); $factory->create(Argument::any())->willReturn(new \ReflectionClass($error)); $this->positiveMatch('throw', $arr, array(new \Error()))->during('ksort', array()); }
/** * * @param callable $callable * @param array $arguments * @param string|null $exception * * @throws \PhpSpec\Exception\Example\FailureException */ public function verifyNegative($callable, array $arguments, $exception = null) { try { call_user_func_array($callable, $arguments); } catch (\Exception $e) { if (null === $exception) { throw new FailureException(sprintf('Expected to not throw any exceptions, but got %s.', $this->presenter->presentValue($e))); } if ($e instanceof $exception) { $invalidProperties = array(); if (is_object($exception)) { $exceptionRefl = $this->factory->create($exception); foreach ($exceptionRefl->getProperties() as $property) { if (in_array($property->getName(), self::$ignoredProperties)) { continue; } $property->setAccessible(true); $expected = $property->getValue($exception); $actual = $property->getValue($e); if (null !== $expected && $actual === $expected) { $invalidProperties[] = sprintf(' `%s`=%s', $property->getName(), $this->presenter->presentValue($expected)); } } } $withProperties = ''; if (count($invalidProperties) > 0) { $withProperties = sprintf(' with' . PHP_EOL . '%s,' . PHP_EOL, implode(",\n", $invalidProperties)); } throw new FailureException(sprintf('Expected to not throw %s exception%s but got it.', $this->presenter->presentValue($exception), $withProperties)); } } }