AsyncBusTest.php 4.1 KB

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