AppTest.php 6.9 KB

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