diff options
author | Olivier Paroz <github@oparoz.com> | 2015-09-19 14:34:36 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-09-19 14:34:36 +0300 |
commit | 44632508b21fbac0778dddae3788b0de49825d02 (patch) | |
tree | e50f3d3a79757a806f3a9a2c63847334b6e349d2 /tests | |
parent | 7cd60d548ac863b23fb8b12a3274c77784f0617a (diff) |
Test for HttpError::htmlError
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/controller/HttpErrorTest.php | 106 |
1 files changed, 90 insertions, 16 deletions
diff --git a/tests/unit/controller/HttpErrorTest.php b/tests/unit/controller/HttpErrorTest.php index c598ae06..f3476f27 100644 --- a/tests/unit/controller/HttpErrorTest.php +++ b/tests/unit/controller/HttpErrorTest.php @@ -13,6 +13,8 @@ namespace OCA\Gallery\Controller; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\RedirectResponse; use OCA\Gallery\Environment\NotFoundEnvException; use OCA\Gallery\Service\NotFoundServiceException; @@ -26,22 +28,8 @@ use OCA\Gallery\Service\InternalServerErrorServiceException; */ class HttpErrorTest extends \Test\TestCase { - /** - * @dataProvider providesExceptionData - * - * @param \Exception $exception - * @param String $message - * @param String $status - */ - public function testError($exception, $message, $status) { - $httpError = $this->getMockForTrait('\OCA\Gallery\Controller\HttpError'); - $response = $httpError->jsonError($exception); - - $this->assertEquals( - ['message' => $message . ' (' . $status . ')', 'success' => false], $response->getData() - ); - $this->assertEquals($status, $response->getStatus()); - } + /** @var string */ + private $appName = 'gallery'; /** * @return array @@ -75,4 +63,90 @@ class HttpErrorTest extends \Test\TestCase { [$coreServiceException, $coreServiceMessage, $coreServiceStatus] ]; } + + /** + * @dataProvider providesExceptionData + * + * @param \Exception $exception + * @param String $message + * @param String $status + */ + public function testJsonError($exception, $message, $status) { + $httpError = $this->getMockForTrait('\OCA\Gallery\Controller\HttpError'); + /** @type JSONResponse $response */ + $response = $httpError->jsonError($exception); + + $this->assertEquals( + ['message' => $message . ' (' . $status . ')', 'success' => false], $response->getData() + ); + $this->assertEquals($status, $response->getStatus()); + } + + /** + * @dataProvider providesExceptionData + * + * @param \Exception $exception + * @param String $message + * @param String $status + */ + public function testHtmlError($exception, $message, $status) { + $session = $this->mockISession(); + $urlGenerator = $this->mockIURLGenerator(); + $this->mockSession($session, 'galleryErrorMessage', $message); + $redirectUrl = '/index.php/app/error'; + $this->mockUrlToErrorPage($urlGenerator, $status, $redirectUrl); + + $httpError = $this->getMockForTrait('\OCA\Gallery\Controller\HttpError'); + + /** @type RedirectResponse $response */ + $response = $httpError->htmlError($session, $urlGenerator, $this->appName, $exception); + $this->assertEquals($redirectUrl, $response->getRedirectURL()); + $this->assertEquals(Http::STATUS_TEMPORARY_REDIRECT, $response->getStatus()); + $this->assertEquals($message, $session->get('galleryErrorMessage')); + } + + private function mockISession() { + return $this->getMockBuilder('\OCP\ISession') + ->disableOriginalConstructor() + ->getMock(); + } + + private function mockIURLGenerator() { + return $this->getMockBuilder('\OCP\IURLGenerator') + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * Needs to be called at least once by testDownloadWithWrongId() or the tests will fail + * + * @param $session + * @param $key + * @param $value + */ + private function mockSession($session, $key, $value) { + $session->expects($this->once()) + ->method('set') + ->with($key) + ->willReturn($value); + + $session->expects($this->once()) + ->method('get') + ->with($key) + ->willReturn($value); + } + + /** + * Mocks IURLGenerator->linkToRoute() + * + * @param int $code + * @param string $url + */ + private function mockUrlToErrorPage($urlGenerator, $code, $url) { + $urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with($this->appName . '.page.error_page', ['code' => $code]) + ->willReturn($url); + } + } |