AsyncBusTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\BackgroundJob\DummyJobList;
  13. use Test\TestCase;
  14. class SimpleCommand implements ICommand {
  15. public function handle() {
  16. AsyncBusTest::$lastCommand = 'SimpleCommand';
  17. }
  18. }
  19. class StateFullCommand implements ICommand {
  20. private $state;
  21. function __construct($state) {
  22. $this->state = $state;
  23. }
  24. public function handle() {
  25. AsyncBusTest::$lastCommand = $this->state;
  26. }
  27. }
  28. class FilesystemCommand implements ICommand {
  29. use FileAccess;
  30. public function handle() {
  31. AsyncBusTest::$lastCommand = 'FileAccess';
  32. }
  33. }
  34. function basicFunction() {
  35. AsyncBusTest::$lastCommand = 'function';
  36. }
  37. // clean class to prevent phpunit putting closure in $this
  38. class ThisClosureTest {
  39. private function privateMethod() {
  40. AsyncBusTest::$lastCommand = 'closure-this';
  41. }
  42. public function test(IBus $bus) {
  43. $bus->push(function () {
  44. $this->privateMethod();
  45. });
  46. }
  47. }
  48. class AsyncBusTest extends TestCase {
  49. /**
  50. * Basic way to check output from a command
  51. *
  52. * @var string
  53. */
  54. public static $lastCommand;
  55. /**
  56. * @var \OCP\BackgroundJob\IJobList
  57. */
  58. private $jobList;
  59. /**
  60. * @var \OCP\Command\IBus
  61. */
  62. private $bus;
  63. public static function DummyCommand() {
  64. self::$lastCommand = 'static';
  65. }
  66. public function setUp() {
  67. $this->jobList = new DummyJobList();
  68. $this->bus = new \OC\Command\AsyncBus($this->jobList);
  69. self::$lastCommand = '';
  70. }
  71. public function testSimpleCommand() {
  72. $command = new SimpleCommand();
  73. $this->bus->push($command);
  74. $this->runJobs();
  75. $this->assertEquals('SimpleCommand', self::$lastCommand);
  76. }
  77. public function testStateFullCommand() {
  78. $command = new StateFullCommand('foo');
  79. $this->bus->push($command);
  80. $this->runJobs();
  81. $this->assertEquals('foo', self::$lastCommand);
  82. }
  83. public function testStaticCallable() {
  84. $this->bus->push(['\Test\Command\AsyncBusTest', 'DummyCommand']);
  85. $this->runJobs();
  86. $this->assertEquals('static', self::$lastCommand);
  87. }
  88. public function testMemberCallable() {
  89. $command = new StateFullCommand('bar');
  90. $this->bus->push([$command, 'handle']);
  91. $this->runJobs();
  92. $this->assertEquals('bar', self::$lastCommand);
  93. }
  94. public function testFunctionCallable() {
  95. $this->bus->push('\Test\Command\BasicFunction');
  96. $this->runJobs();
  97. $this->assertEquals('function', self::$lastCommand);
  98. }
  99. public function testClosure() {
  100. $this->bus->push(function () {
  101. AsyncBusTest::$lastCommand = 'closure';
  102. });
  103. $this->runJobs();
  104. $this->assertEquals('closure', self::$lastCommand);
  105. }
  106. public function testClosureSelf() {
  107. $this->bus->push(function () {
  108. self::$lastCommand = 'closure-self';
  109. });
  110. $this->runJobs();
  111. $this->assertEquals('closure-self', self::$lastCommand);
  112. }
  113. public function testClosureThis() {
  114. // clean class to prevent phpunit putting closure in $this
  115. $test = new ThisClosureTest();
  116. $test->test($this->bus);
  117. $this->runJobs();
  118. $this->assertEquals('closure-this', self::$lastCommand);
  119. }
  120. public function testClosureBind() {
  121. $state = 'bar';
  122. $this->bus->push(function () use ($state) {
  123. self::$lastCommand = 'closure-' . $state;
  124. });
  125. $this->runJobs();
  126. $this->assertEquals('closure-bar', self::$lastCommand);
  127. }
  128. public function testFileFileAccessCommand() {
  129. $this->bus->push(new FilesystemCommand());
  130. $this->assertEquals('', self::$lastCommand);
  131. $this->runJobs();
  132. $this->assertEquals('FileAccess', self::$lastCommand);
  133. }
  134. public function testFileFileAccessCommandSync() {
  135. $this->bus->requireSync('\OC\Command\FileAccess');
  136. $this->bus->push(new FilesystemCommand());
  137. $this->assertEquals('FileAccess', self::$lastCommand);
  138. self::$lastCommand = '';
  139. $this->runJobs();
  140. $this->assertEquals('', self::$lastCommand);
  141. }
  142. private function runJobs() {
  143. $jobs = $this->jobList->getAll();
  144. foreach ($jobs as $job) {
  145. $job->execute($this->jobList);
  146. }
  147. }
  148. }