AppTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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), array('.','..'));
  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() {
  52. parent::setUp();
  53. $this->container = new \OC\AppFramework\DependencyInjection\DIContainer('test', array());
  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 = array('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'] = array();
  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 = array(null, array(), array(), null, new Response());
  77. $this->dispatcher->expects($this->once())
  78. ->method('dispatch')
  79. ->with($this->equalTo($this->controller),
  80. $this->equalTo($this->controllerMethod))
  81. ->will($this->returnValue($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() {
  100. rrmdir($this->appPath);
  101. parent::tearDown();
  102. }
  103. public function testOutputIsPrinted(){
  104. $return = [Http::STATUS_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. ->will($this->returnValue($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::STATUS_NO_CONTENT],
  118. [Http::STATUS_NOT_MODIFIED],
  119. ];
  120. }
  121. /**
  122. * @dataProvider dataNoOutput
  123. * @param int $statusCode
  124. */
  125. public function testNoOutput($statusCode) {
  126. $return = [$statusCode, [], [], $this->output, new Response()];
  127. $this->dispatcher->expects($this->once())
  128. ->method('dispatch')
  129. ->with($this->equalTo($this->controller),
  130. $this->equalTo($this->controllerMethod))
  131. ->will($this->returnValue($return));
  132. $this->io->expects($this->once())
  133. ->method('setHeader')
  134. ->with($this->equalTo($statusCode));
  135. $this->io->expects($this->never())
  136. ->method('setOutput');
  137. App::main($this->controllerName, $this->controllerMethod, $this->container, []);
  138. }
  139. public function testCallbackIsCalled(){
  140. $mock = $this->getMockBuilder('OCP\AppFramework\Http\ICallbackResponse')
  141. ->getMock();
  142. $return = [null, [], [], $this->output, $mock];
  143. $this->dispatcher->expects($this->once())
  144. ->method('dispatch')
  145. ->with($this->equalTo($this->controller),
  146. $this->equalTo($this->controllerMethod))
  147. ->will($this->returnValue($return));
  148. $mock->expects($this->once())
  149. ->method('callback');
  150. App::main($this->controllerName, $this->controllerMethod, $this->container, []);
  151. }
  152. public function testCoreApp() {
  153. $this->container['AppName'] = 'core';
  154. $this->container['OC\Core\Controller\Foo'] = $this->controller;
  155. $return = array(null, array(), array(), null, new Response());
  156. $this->dispatcher->expects($this->once())
  157. ->method('dispatch')
  158. ->with($this->equalTo($this->controller),
  159. $this->equalTo($this->controllerMethod))
  160. ->will($this->returnValue($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['OC\Settings\Controller\Foo'] = $this->controller;
  168. $return = array(null, array(), array(), null, new Response());
  169. $this->dispatcher->expects($this->once())
  170. ->method('dispatch')
  171. ->with($this->equalTo($this->controller),
  172. $this->equalTo($this->controllerMethod))
  173. ->will($this->returnValue($return));
  174. $this->io->expects($this->never())
  175. ->method('setOutput');
  176. App::main('Foo', $this->controllerMethod, $this->container);
  177. }
  178. public function testApp() {
  179. $this->container['AppName'] = 'bar';
  180. $this->container['OCA\Bar\Controller\Foo'] = $this->controller;
  181. $return = array(null, array(), array(), null, new Response());
  182. $this->dispatcher->expects($this->once())
  183. ->method('dispatch')
  184. ->with($this->equalTo($this->controller),
  185. $this->equalTo($this->controllerMethod))
  186. ->will($this->returnValue($return));
  187. $this->io->expects($this->never())
  188. ->method('setOutput');
  189. App::main('Foo', $this->controllerMethod, $this->container);
  190. }
  191. }