AppTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 OC\AppFramework;
  23. use OCP\AppFramework\Http\Response;
  24. function rrmdir($directory) {
  25. $files = array_diff(scandir($directory), array('.','..'));
  26. foreach ($files as $file) {
  27. if (is_dir($directory . '/' . $file)) {
  28. rrmdir($directory . '/' . $file);
  29. } else {
  30. unlink($directory . '/' . $file);
  31. }
  32. }
  33. return rmdir($directory);
  34. }
  35. class AppTest extends \Test\TestCase {
  36. private $container;
  37. private $io;
  38. private $api;
  39. private $controller;
  40. private $dispatcher;
  41. private $params;
  42. private $headers;
  43. private $output;
  44. private $controllerName;
  45. private $controllerMethod;
  46. private $appPath;
  47. protected function setUp() {
  48. parent::setUp();
  49. $this->container = new \OC\AppFramework\DependencyInjection\DIContainer('test', array());
  50. $this->controller = $this->getMockBuilder(
  51. 'OCP\AppFramework\Controller')
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->dispatcher = $this->getMockBuilder(
  55. 'OC\AppFramework\Http\Dispatcher')
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->io = $this->getMockBuilder('OCP\\AppFramework\\Http\\IOutput')->getMock();
  59. $this->headers = array('key' => 'value');
  60. $this->output = 'hi';
  61. $this->controllerName = 'Controller';
  62. $this->controllerMethod = 'method';
  63. $this->container[$this->controllerName] = $this->controller;
  64. $this->container['Dispatcher'] = $this->dispatcher;
  65. $this->container['OCP\\AppFramework\\Http\\IOutput'] = $this->io;
  66. $this->container['urlParams'] = array();
  67. $this->appPath = __DIR__ . '/../../../apps/namespacetestapp/appinfo';
  68. $infoXmlPath = $this->appPath . '/info.xml';
  69. mkdir($this->appPath, 0777, true);
  70. $xml = '<?xml version="1.0" encoding="UTF-8"?>' .
  71. '<info>' .
  72. '<id>namespacetestapp</id>' .
  73. '<namespace>NameSpaceTestApp</namespace>' .
  74. '</info>';
  75. file_put_contents($infoXmlPath, $xml);
  76. }
  77. public function testControllerNameAndMethodAreBeingPassed(){
  78. $return = array(null, array(), array(), null, new Response());
  79. $this->dispatcher->expects($this->once())
  80. ->method('dispatch')
  81. ->with($this->equalTo($this->controller),
  82. $this->equalTo($this->controllerMethod))
  83. ->will($this->returnValue($return));
  84. $this->io->expects($this->never())
  85. ->method('setOutput');
  86. App::main($this->controllerName, $this->controllerMethod,
  87. $this->container);
  88. }
  89. public function testBuildAppNamespace() {
  90. $ns = App::buildAppNamespace('someapp');
  91. $this->assertEquals('OCA\Someapp', $ns);
  92. }
  93. public function testBuildAppNamespaceCore() {
  94. $ns = App::buildAppNamespace('someapp', 'OC\\');
  95. $this->assertEquals('OC\Someapp', $ns);
  96. }
  97. public function testBuildAppNamespaceInfoXml() {
  98. $ns = App::buildAppNamespace('namespacetestapp', 'OCA\\');
  99. $this->assertEquals('OCA\NameSpaceTestApp', $ns);
  100. }
  101. protected function tearDown() {
  102. rrmdir($this->appPath);
  103. }
  104. public function testOutputIsPrinted(){
  105. $return = [null, [], [], $this->output, new Response()];
  106. $this->dispatcher->expects($this->once())
  107. ->method('dispatch')
  108. ->with($this->equalTo($this->controller),
  109. $this->equalTo($this->controllerMethod))
  110. ->will($this->returnValue($return));
  111. $this->io->expects($this->once())
  112. ->method('setOutput')
  113. ->with($this->equalTo($this->output));
  114. App::main($this->controllerName, $this->controllerMethod, $this->container, []);
  115. }
  116. public function testCallbackIsCalled(){
  117. $mock = $this->getMockBuilder('OCP\AppFramework\Http\ICallbackResponse')
  118. ->getMock();
  119. $return = [null, [], [], $this->output, $mock];
  120. $this->dispatcher->expects($this->once())
  121. ->method('dispatch')
  122. ->with($this->equalTo($this->controller),
  123. $this->equalTo($this->controllerMethod))
  124. ->will($this->returnValue($return));
  125. $mock->expects($this->once())
  126. ->method('callback');
  127. App::main($this->controllerName, $this->controllerMethod, $this->container, []);
  128. }
  129. }