AppTest.php 6.4 KB

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