*/ class ExceptionInterpreterTest extends TestCase { public function interpretErrorProvider() { $action = 'test'; $module = 'test'; return [ [new Exception('test message'), 500, 'test message', 'MainResponse'], [new ResolverException('test message'), 403, 'test message', 'RedirectResponse'], [new tao_models_classes_UserException('test message'), 403, 'test message', 'MainResponse'], [new ActionEnforcingException('test message', $module, $action), 404, 'test message', 'MainResponse'], [new tao_models_classes_FileNotFoundException('test message'), 404, 'File test message not found', 'MainResponse'], [new LoginFailedException([new Exception('test message')]), 500, '', 'MainResponse'], ]; } /** * Test the interpreter exception process and getTrace() method * * @param $exception * @param $expectedHttpStatus * @param $expectedTrace * @param $expectedResponseClassName * @dataProvider interpretErrorProvider * @throws \ReflectionException */ public function testInterpretError($exception, $expectedHttpStatus, $expectedTrace, $expectedResponseClassName) { $exceptionInterpreter = new ExceptionInterpretor(); $this->setInaccessibleProperty($exceptionInterpreter, 'exception', $exception); $this->assertSame($exceptionInterpreter, $this->invokeProtectedMethod($exceptionInterpreter, 'interpretError')); $this->assertSame($expectedHttpStatus, $this->getInaccessibleProperty($exceptionInterpreter, 'returnHttpCode')); $this->assertSame($expectedTrace, $exceptionInterpreter->getTrace()); $this->assertSame($expectedResponseClassName, $this->getInaccessibleProperty($exceptionInterpreter, 'responseClassName')); } /** * */ public function testSetException() { $ExceptionInterpretor = new ExceptionInterpretor(); $exception = new Exception(); $this->assertSame($ExceptionInterpretor, $ExceptionInterpretor->setException($exception)); $this->assertSame($exception, $this->getInaccessibleProperty($ExceptionInterpretor, 'exception')); } public function testGetHttpCode() { $fixtureHttpCode = 407; $ExceptionInterpretor = new ExceptionInterpretor(); $this->setInaccessibleProperty($ExceptionInterpretor, 'returnHttpCode', $fixtureHttpCode); $this->assertSame($fixtureHttpCode, $ExceptionInterpretor->getHttpCode()); } public function testGetResponseClassName() { $fixtureClassName = 'MainResponse'; $expected = 'oat\\tao\\model\\mvc\\error\\' . $fixtureClassName; $ExceptionInterpretor = new ExceptionInterpretor(); $this->setInaccessibleProperty($ExceptionInterpretor, 'responseClassName', $fixtureClassName); $this->assertEquals($expected, $ExceptionInterpretor->getResponseClassName()); } /** * Returns private or protected property value. * * @param \Object $object * @param string $propertyName * * @return mixed property value * @throws \ReflectionException if the class or property does not exist. */ protected function getInaccessibleProperty($object, $propertyName) { $property = new \ReflectionProperty(get_class($object), $propertyName); $property->setAccessible(true); $value = $property->getValue($object); $property->setAccessible(false); return $value; } /** * Sets inaccessible property value. * * @param \Object $object * @param string $propertyName * @param mixed $value * * @return $this * @throws \ReflectionException if the class or property does not exist. */ protected function setInaccessibleProperty($object, $propertyName, $value) { $property = new \ReflectionProperty(get_class($object), $propertyName); $property->setAccessible(true); $property->setValue($object, $value); $property->setAccessible(false); return $this; } /** * Calls protected/private method of a class. * * @param \Object $object Instantiated object that we will run method on. * @param string $methodName Method name to call * @param array $parameters Array of parameters to pass into method. * * @return mixed Method return * @throws \ReflectionException if the class or method does not exist. */ public function invokeProtectedMethod($object, $methodName, array $parameters = []) { $reflection = new \ReflectionClass(get_class($object)); $method = $reflection->getMethod($methodName); $method->setAccessible(true); return $method->invokeArgs($object, $parameters); } }