1
0

AppTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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 Test\AppFramework;
  23. use OC\AppFramework\App;
  24. use OCP\AppFramework\Http;
  25. use OCP\AppFramework\Http\Response;
  26. function rrmdir($directory) {
  27. $files = array_diff(scandir($directory), array('.','..'));
  28. foreach ($files as $file) {
  29. if (is_dir($directory . '/' . $file)) {
  30. rrmdir($directory . '/' . $file);
  31. } else {
  32. unlink($directory . '/' . $file);
  33. }
  34. }
  35. return rmdir($directory);
  36. }
  37. class AppTest extends \Test\TestCase {
  38. private $container;
  39. private $io;
  40. private $api;
  41. private $controller;
  42. private $dispatcher;
  43. private $params;
  44. private $headers;
  45. private $output;
  46. private $controllerName;
  47. private $controllerMethod;
  48. private $appPath;
  49. protected function setUp() {
  50. parent::setUp();
  51. $this->container = new \OC\AppFramework\DependencyInjection\DIContainer('test', array());
  52. $this->controller = $this->getMockBuilder(
  53. 'OCP\AppFramework\Controller')
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->dispatcher = $this->getMockBuilder(
  57. 'OC\AppFramework\Http\Dispatcher')
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->io = $this->getMockBuilder('OCP\\AppFramework\\Http\\IOutput')->getMock();
  61. $this->headers = array('key' => 'value');
  62. $this->output = 'hi';
  63. $this->controllerName = 'Controller';
  64. $this->controllerMethod = 'method';
  65. $this->container[$this->controllerName] = $this->controller;
  66. $this->container['Dispatcher'] = $this->dispatcher;
  67. $this->container['OCP\\AppFramework\\Http\\IOutput'] = $this->io;
  68. $this->container['urlParams'] = array();
  69. $this->appPath = __DIR__ . '/../../../apps/namespacetestapp';
  70. $infoXmlPath = $this->appPath . '/appinfo/info.xml';
  71. mkdir($this->appPath . '/appinfo', 0777, true);
  72. $xml = '<?xml version="1.0" encoding="UTF-8"?>' .
  73. '<info>' .
  74. '<id>namespacetestapp</id>' .
  75. '<namespace>NameSpaceTestApp</namespace>' .
  76. '</info>';
  77. file_put_contents($infoXmlPath, $xml);
  78. }
  79. public function testControllerNameAndMethodAreBeingPassed(){
  80. $return = array(null, array(), array(), null, new Response());
  81. $this->dispatcher->expects($this->once())
  82. ->method('dispatch')
  83. ->with($this->equalTo($this->controller),
  84. $this->equalTo($this->controllerMethod))
  85. ->will($this->returnValue($return));
  86. $this->io->expects($this->never())
  87. ->method('setOutput');
  88. App::main($this->controllerName, $this->controllerMethod,
  89. $this->container);
  90. }
  91. public function testBuildAppNamespace() {
  92. $ns = App::buildAppNamespace('someapp');
  93. $this->assertEquals('OCA\Someapp', $ns);
  94. }
  95. public function testBuildAppNamespaceCore() {
  96. $ns = App::buildAppNamespace('someapp', 'OC\\');
  97. $this->assertEquals('OC\Someapp', $ns);
  98. }
  99. public function testBuildAppNamespaceInfoXml() {
  100. $ns = App::buildAppNamespace('namespacetestapp', 'OCA\\');
  101. $this->assertEquals('OCA\NameSpaceTestApp', $ns);
  102. }
  103. protected function tearDown() {
  104. rrmdir($this->appPath);
  105. parent::tearDown();
  106. }
  107. public function testOutputIsPrinted(){
  108. $return = [Http::STATUS_OK, [], [], $this->output, new Response()];
  109. $this->dispatcher->expects($this->once())
  110. ->method('dispatch')
  111. ->with($this->equalTo($this->controller),
  112. $this->equalTo($this->controllerMethod))
  113. ->will($this->returnValue($return));
  114. $this->io->expects($this->once())
  115. ->method('setOutput')
  116. ->with($this->equalTo($this->output));
  117. App::main($this->controllerName, $this->controllerMethod, $this->container, []);
  118. }
  119. public function dataNoOutput() {
  120. return [
  121. [Http::STATUS_NO_CONTENT],
  122. [Http::STATUS_NOT_MODIFIED],
  123. ];
  124. }
  125. /**
  126. * @dataProvider dataNoOutput
  127. * @param int $statusCode
  128. */
  129. public function testNoOutput($statusCode) {
  130. $return = [$statusCode, [], [], $this->output, new Response()];
  131. $this->dispatcher->expects($this->once())
  132. ->method('dispatch')
  133. ->with($this->equalTo($this->controller),
  134. $this->equalTo($this->controllerMethod))
  135. ->will($this->returnValue($return));
  136. $this->io->expects($this->once())
  137. ->method('setHeader')
  138. ->with($this->equalTo($statusCode));
  139. $this->io->expects($this->never())
  140. ->method('setOutput');
  141. App::main($this->controllerName, $this->controllerMethod, $this->container, []);
  142. }
  143. public function testCallbackIsCalled(){
  144. $mock = $this->getMockBuilder('OCP\AppFramework\Http\ICallbackResponse')
  145. ->getMock();
  146. $return = [null, [], [], $this->output, $mock];
  147. $this->dispatcher->expects($this->once())
  148. ->method('dispatch')
  149. ->with($this->equalTo($this->controller),
  150. $this->equalTo($this->controllerMethod))
  151. ->will($this->returnValue($return));
  152. $mock->expects($this->once())
  153. ->method('callback');
  154. App::main($this->controllerName, $this->controllerMethod, $this->container, []);
  155. }
  156. public function testCoreApp() {
  157. $this->container['AppName'] = 'core';
  158. $this->container['OC\Core\Controller\Foo'] = $this->controller;
  159. $return = array(null, array(), array(), null, new Response());
  160. $this->dispatcher->expects($this->once())
  161. ->method('dispatch')
  162. ->with($this->equalTo($this->controller),
  163. $this->equalTo($this->controllerMethod))
  164. ->will($this->returnValue($return));
  165. $this->io->expects($this->never())
  166. ->method('setOutput');
  167. App::main('Foo', $this->controllerMethod, $this->container);
  168. }
  169. public function testSettingsApp() {
  170. $this->container['AppName'] = 'settings';
  171. $this->container['OC\Settings\Controller\Foo'] = $this->controller;
  172. $return = array(null, array(), array(), null, new Response());
  173. $this->dispatcher->expects($this->once())
  174. ->method('dispatch')
  175. ->with($this->equalTo($this->controller),
  176. $this->equalTo($this->controllerMethod))
  177. ->will($this->returnValue($return));
  178. $this->io->expects($this->never())
  179. ->method('setOutput');
  180. App::main('Foo', $this->controllerMethod, $this->container);
  181. }
  182. public function testApp() {
  183. $this->container['AppName'] = 'bar';
  184. $this->container['OCA\Bar\Controller\Foo'] = $this->controller;
  185. $return = array(null, array(), array(), null, new Response());
  186. $this->dispatcher->expects($this->once())
  187. ->method('dispatch')
  188. ->with($this->equalTo($this->controller),
  189. $this->equalTo($this->controllerMethod))
  190. ->will($this->returnValue($return));
  191. $this->io->expects($this->never())
  192. ->method('setOutput');
  193. App::main('Foo', $this->controllerMethod, $this->container);
  194. }
  195. }