You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AsyncBusTest.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Command;
  9. use OC\Command\FileAccess;
  10. use OCP\Command\IBus;
  11. use OCP\Command\ICommand;
  12. use Test\TestCase;
  13. class SimpleCommand implements ICommand {
  14. public function handle() {
  15. AsyncBusTest::$lastCommand = 'SimpleCommand';
  16. }
  17. }
  18. class StateFullCommand implements ICommand {
  19. private $state;
  20. function __construct($state) {
  21. $this->state = $state;
  22. }
  23. public function handle() {
  24. AsyncBusTest::$lastCommand = $this->state;
  25. }
  26. }
  27. class FilesystemCommand implements ICommand {
  28. use FileAccess;
  29. public function handle() {
  30. AsyncBusTest::$lastCommand = 'FileAccess';
  31. }
  32. }
  33. function basicFunction() {
  34. AsyncBusTest::$lastCommand = 'function';
  35. }
  36. // clean class to prevent phpunit putting closure in $this
  37. class ThisClosureTest {
  38. private function privateMethod() {
  39. AsyncBusTest::$lastCommand = 'closure-this';
  40. }
  41. public function test(IBus $bus) {
  42. $bus->push(function () {
  43. $this->privateMethod();
  44. });
  45. }
  46. }
  47. abstract class AsyncBusTest extends TestCase {
  48. /**
  49. * Basic way to check output from a command
  50. *
  51. * @var string
  52. */
  53. public static $lastCommand;
  54. /**
  55. * @var \OCP\Command\IBus
  56. */
  57. private $bus;
  58. public static function DummyCommand() {
  59. self::$lastCommand = 'static';
  60. }
  61. /**
  62. * @return IBus
  63. */
  64. protected function getBus() {
  65. if (!$this->bus instanceof IBus) {
  66. $this->bus = $this->createBus();
  67. }
  68. return $this->bus;
  69. }
  70. /**
  71. * @return IBus
  72. */
  73. abstract protected function createBus();
  74. public function setUp() {
  75. self::$lastCommand = '';
  76. }
  77. public function testSimpleCommand() {
  78. $command = new SimpleCommand();
  79. $this->getBus()->push($command);
  80. $this->runJobs();
  81. $this->assertEquals('SimpleCommand', self::$lastCommand);
  82. }
  83. public function testStateFullCommand() {
  84. $command = new StateFullCommand('foo');
  85. $this->getBus()->push($command);
  86. $this->runJobs();
  87. $this->assertEquals('foo', self::$lastCommand);
  88. }
  89. public function testStaticCallable() {
  90. $this->getBus()->push(['\Test\Command\AsyncBusTest', 'DummyCommand']);
  91. $this->runJobs();
  92. $this->assertEquals('static', self::$lastCommand);
  93. }
  94. public function testMemberCallable() {
  95. $command = new StateFullCommand('bar');
  96. $this->getBus()->push([$command, 'handle']);
  97. $this->runJobs();
  98. $this->assertEquals('bar', self::$lastCommand);
  99. }
  100. public function testFunctionCallable() {
  101. $this->getBus()->push('\Test\Command\BasicFunction');
  102. $this->runJobs();
  103. $this->assertEquals('function', self::$lastCommand);
  104. }
  105. public function testClosure() {
  106. $this->getBus()->push(function () {
  107. AsyncBusTest::$lastCommand = 'closure';
  108. });
  109. $this->runJobs();
  110. $this->assertEquals('closure', self::$lastCommand);
  111. }
  112. public function testClosureSelf() {
  113. $this->getBus()->push(function () {
  114. AsyncBusTest::$lastCommand = 'closure-self';
  115. });
  116. $this->runJobs();
  117. $this->assertEquals('closure-self', self::$lastCommand);
  118. }
  119. public function testClosureThis() {
  120. // clean class to prevent phpunit putting closure in $this
  121. $test = new ThisClosureTest();
  122. $test->test($this->getBus());
  123. $this->runJobs();
  124. $this->assertEquals('closure-this', self::$lastCommand);
  125. }
  126. public function testClosureBind() {
  127. $state = 'bar';
  128. $this->getBus()->push(function () use ($state) {
  129. AsyncBusTest::$lastCommand = 'closure-' . $state;
  130. });
  131. $this->runJobs();
  132. $this->assertEquals('closure-bar', self::$lastCommand);
  133. }
  134. public function testFileFileAccessCommand() {
  135. $this->getBus()->push(new FilesystemCommand());
  136. $this->assertEquals('', self::$lastCommand);
  137. $this->runJobs();
  138. $this->assertEquals('FileAccess', self::$lastCommand);
  139. }
  140. public function testFileFileAccessCommandSync() {
  141. $this->getBus()->requireSync('\OC\Command\FileAccess');
  142. $this->getBus()->push(new FilesystemCommand());
  143. $this->assertEquals('FileAccess', self::$lastCommand);
  144. self::$lastCommand = '';
  145. $this->runJobs();
  146. $this->assertEquals('', self::$lastCommand);
  147. }
  148. abstract protected function runJobs();
  149. }