Example #1
0
 public function testExpectedExceptionCode()
 {
     $method = new \ReflectionMethod($this->meta, 'setExpectedException');
     $method->setAccessible(true);
     $method->invoke($this->meta, ['expectedExceptionCode' => 5]);
     $this->assertNull($this->meta->getExpectedExceptionCode(), 'Code must be NULL if exception does not set');
     $method->invoke($this->meta, ['expectedException' => 'Exception', 'expectedExceptionCode' => 5]);
     $this->assertSame(5, $this->meta->getExpectedExceptionCode());
     $method->invoke($this->meta, ['expectedException' => 'Exception', 'expectedExceptionCode' => '76']);
     $this->assertSame(76, $this->meta->getExpectedExceptionCode());
 }
Example #2
0
 /**
  * Try to resolve situation with exception.
  *
  * @param TestMeta $test
  * @param MethodEvent $event
  * @param \Exception $exception
  *
  * @return int Status code
  */
 private function exceptionControl(TestMeta $test, MethodEvent $event, \Exception $exception)
 {
     if (is_a($exception, $test->getExpectedException())) {
         $code = $test->getExpectedExceptionCode();
         if ($code !== null && $code !== $exception->getCode()) {
             $error = new TestFailException(sprintf('Failed asserting that expected exception code %d is equal to %d', $code, $exception->getCode()), 0, $exception);
             $status = $this->context->onFailure($error);
             $this->finish($test, $event, MethodEvent::METHOD_FAILED, $exception);
             return $status;
         }
         $message = $test->getExpectedExceptionMessage();
         if ($message !== null && strpos($exception->getMessage(), $message) === false) {
             $error = new TestFailException(sprintf('Failed asserting that exception message "%s" contains "%s"', $exception->getMessage(), $message), 0, $exception);
             $status = $this->context->onFailure($error);
             $this->finish($test, $event, MethodEvent::METHOD_FAILED, $exception);
             return $status;
         }
         return 0;
     } else {
         $this->context->onUnexpectedException($exception);
         return 1;
     }
 }