DispatcherTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OC\AppFramework\Http;
  23. use OC\AppFramework\Core\API;
  24. use OC\AppFramework\Middleware\MiddlewareDispatcher;
  25. use OCP\AppFramework\Http;
  26. //require_once(__DIR__ . "/../classloader.php");
  27. class DispatcherTest extends \PHPUnit_Framework_TestCase {
  28. private $middlewareDispatcher;
  29. private $dispatcher;
  30. private $controllerMethod;
  31. private $response;
  32. private $lastModified;
  33. private $etag;
  34. private $http;
  35. protected function setUp() {
  36. $this->controllerMethod = 'test';
  37. $app = $this->getMockBuilder(
  38. 'OC\AppFramework\DependencyInjection\DIContainer')
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $request = $this->getMockBuilder(
  42. '\OC\AppFramework\Http\Request')
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->http = $this->getMockBuilder(
  46. '\OC\AppFramework\Http')
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->middlewareDispatcher = $this->getMockBuilder(
  50. '\OC\AppFramework\Middleware\MiddlewareDispatcher')
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->controller = $this->getMock(
  54. '\OCP\AppFramework\Controller',
  55. array($this->controllerMethod), array($app, $request));
  56. $this->dispatcher = new Dispatcher(
  57. $this->http, $this->middlewareDispatcher);
  58. $this->response = $this->getMockBuilder(
  59. '\OCP\AppFramework\Http\Response')
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->lastModified = new \DateTime(null, new \DateTimeZone('GMT'));
  63. $this->etag = 'hi';
  64. }
  65. private function setMiddlewareExpections($out=null,
  66. $httpHeaders=null, $responseHeaders=array(),
  67. $ex=false, $catchEx=true) {
  68. if($ex) {
  69. $exception = new \Exception();
  70. $this->middlewareDispatcher->expects($this->once())
  71. ->method('beforeController')
  72. ->with($this->equalTo($this->controller),
  73. $this->equalTo($this->controllerMethod))
  74. ->will($this->throwException($exception));
  75. if($catchEx) {
  76. $this->middlewareDispatcher->expects($this->once())
  77. ->method('afterException')
  78. ->with($this->equalTo($this->controller),
  79. $this->equalTo($this->controllerMethod),
  80. $this->equalTo($exception))
  81. ->will($this->returnValue($this->response));
  82. } else {
  83. $this->middlewareDispatcher->expects($this->once())
  84. ->method('afterException')
  85. ->with($this->equalTo($this->controller),
  86. $this->equalTo($this->controllerMethod),
  87. $this->equalTo($exception))
  88. ->will($this->returnValue(null));
  89. return;
  90. }
  91. } else {
  92. $this->middlewareDispatcher->expects($this->once())
  93. ->method('beforeController')
  94. ->with($this->equalTo($this->controller),
  95. $this->equalTo($this->controllerMethod));
  96. $this->controller->expects($this->once())
  97. ->method($this->controllerMethod)
  98. ->will($this->returnValue($this->response));
  99. }
  100. $this->response->expects($this->once())
  101. ->method('render')
  102. ->will($this->returnValue($out));
  103. $this->response->expects($this->once())
  104. ->method('getStatus')
  105. ->will($this->returnValue(Http::STATUS_OK));
  106. $this->response->expects($this->once())
  107. ->method('getLastModified')
  108. ->will($this->returnValue($this->lastModified));
  109. $this->response->expects($this->once())
  110. ->method('getETag')
  111. ->will($this->returnValue($this->etag));
  112. $this->response->expects($this->once())
  113. ->method('getHeaders')
  114. ->will($this->returnValue($responseHeaders));
  115. $this->http->expects($this->once())
  116. ->method('getStatusHeader')
  117. ->with($this->equalTo(Http::STATUS_OK),
  118. $this->equalTo($this->lastModified),
  119. $this->equalTo($this->etag))
  120. ->will($this->returnValue($httpHeaders));
  121. $this->middlewareDispatcher->expects($this->once())
  122. ->method('afterController')
  123. ->with($this->equalTo($this->controller),
  124. $this->equalTo($this->controllerMethod),
  125. $this->equalTo($this->response))
  126. ->will($this->returnValue($this->response));
  127. $this->middlewareDispatcher->expects($this->once())
  128. ->method('afterController')
  129. ->with($this->equalTo($this->controller),
  130. $this->equalTo($this->controllerMethod),
  131. $this->equalTo($this->response))
  132. ->will($this->returnValue($this->response));
  133. $this->middlewareDispatcher->expects($this->once())
  134. ->method('beforeOutput')
  135. ->with($this->equalTo($this->controller),
  136. $this->equalTo($this->controllerMethod),
  137. $this->equalTo($out))
  138. ->will($this->returnValue($out));
  139. }
  140. public function testDispatcherReturnsArrayWith2Entries() {
  141. $this->setMiddlewareExpections();
  142. $response = $this->dispatcher->dispatch($this->controller,
  143. $this->controllerMethod);
  144. $this->assertNull($response[0]);
  145. $this->assertEquals(array(), $response[1]);
  146. $this->assertNull($response[2]);
  147. }
  148. public function testHeadersAndOutputAreReturned(){
  149. $out = 'yo';
  150. $httpHeaders = 'Http';
  151. $responseHeaders = array('hell' => 'yeah');
  152. $this->setMiddlewareExpections($out, $httpHeaders, $responseHeaders);
  153. $response = $this->dispatcher->dispatch($this->controller,
  154. $this->controllerMethod);
  155. $this->assertEquals($httpHeaders, $response[0]);
  156. $this->assertEquals($responseHeaders, $response[1]);
  157. $this->assertEquals($out, $response[2]);
  158. }
  159. public function testExceptionCallsAfterException() {
  160. $out = 'yo';
  161. $httpHeaders = 'Http';
  162. $responseHeaders = array('hell' => 'yeah');
  163. $this->setMiddlewareExpections($out, $httpHeaders, $responseHeaders, true);
  164. $response = $this->dispatcher->dispatch($this->controller,
  165. $this->controllerMethod);
  166. $this->assertEquals($httpHeaders, $response[0]);
  167. $this->assertEquals($responseHeaders, $response[1]);
  168. $this->assertEquals($out, $response[2]);
  169. }
  170. public function testExceptionThrowsIfCanNotBeHandledByAfterException() {
  171. $out = 'yo';
  172. $httpHeaders = 'Http';
  173. $responseHeaders = array('hell' => 'yeah');
  174. $this->setMiddlewareExpections($out, $httpHeaders, $responseHeaders, true, false);
  175. $this->setExpectedException('\Exception');
  176. $response = $this->dispatcher->dispatch($this->controller,
  177. $this->controllerMethod);
  178. }
  179. }