AppTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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'] = [];
  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. $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 testSettingsApp() {
  165. $this->container['AppName'] = 'settings';
  166. $this->container['OCA\Settings\Controller\Foo'] = $this->controller;
  167. $return = ['HTTP/2.0 200 OK', [], [], null, new Response()];
  168. $this->dispatcher->expects($this->once())
  169. ->method('dispatch')
  170. ->with($this->equalTo($this->controller),
  171. $this->equalTo($this->controllerMethod))
  172. ->willReturn($return);
  173. $this->io->expects($this->never())
  174. ->method('setOutput');
  175. App::main('Foo', $this->controllerMethod, $this->container);
  176. }
  177. public function testApp() {
  178. $this->container['AppName'] = 'bar';
  179. $this->container['OCA\Bar\Controller\Foo'] = $this->controller;
  180. $return = ['HTTP/2.0 200 OK', [], [], null, new Response()];
  181. $this->dispatcher->expects($this->once())
  182. ->method('dispatch')
  183. ->with($this->equalTo($this->controller),
  184. $this->equalTo($this->controllerMethod))
  185. ->willReturn($return);
  186. $this->io->expects($this->never())
  187. ->method('setOutput');
  188. App::main('Foo', $this->controllerMethod, $this->container);
  189. }
  190. }