2015-02-17 18:49:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test\Command;
|
|
|
|
|
2015-02-23 17:26:12 +03:00
|
|
|
use OC\Command\FileAccess;
|
2015-02-23 17:42:18 +03:00
|
|
|
use OCP\Command\IBus;
|
2015-02-17 18:49:14 +03:00
|
|
|
use OCP\Command\ICommand;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class SimpleCommand implements ICommand {
|
|
|
|
public function handle() {
|
2016-05-20 16:38:20 +03:00
|
|
|
AsyncBusTest::$lastCommand = 'SimpleCommand';
|
2015-02-17 18:49:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class StateFullCommand implements ICommand {
|
|
|
|
private $state;
|
|
|
|
|
|
|
|
function __construct($state) {
|
|
|
|
$this->state = $state;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle() {
|
2016-05-20 16:38:20 +03:00
|
|
|
AsyncBusTest::$lastCommand = $this->state;
|
2015-02-17 18:49:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 17:26:12 +03:00
|
|
|
class FilesystemCommand implements ICommand {
|
|
|
|
use FileAccess;
|
|
|
|
|
|
|
|
public function handle() {
|
2016-05-20 16:38:20 +03:00
|
|
|
AsyncBusTest::$lastCommand = 'FileAccess';
|
2015-02-23 17:26:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 18:49:14 +03:00
|
|
|
function basicFunction() {
|
2016-05-20 16:38:20 +03:00
|
|
|
AsyncBusTest::$lastCommand = 'function';
|
2015-02-17 18:49:14 +03:00
|
|
|
}
|
|
|
|
|
2015-02-23 17:42:18 +03:00
|
|
|
// clean class to prevent phpunit putting closure in $this
|
|
|
|
class ThisClosureTest {
|
|
|
|
private function privateMethod() {
|
2016-05-20 16:38:20 +03:00
|
|
|
AsyncBusTest::$lastCommand = 'closure-this';
|
2015-02-23 17:42:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test(IBus $bus) {
|
|
|
|
$bus->push(function () {
|
|
|
|
$this->privateMethod();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-24 17:06:37 +03:00
|
|
|
abstract class AsyncBusTest extends TestCase {
|
2015-02-17 18:49:14 +03:00
|
|
|
/**
|
|
|
|
* Basic way to check output from a command
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $lastCommand;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\Command\IBus
|
|
|
|
*/
|
|
|
|
private $bus;
|
|
|
|
|
|
|
|
public static function DummyCommand() {
|
|
|
|
self::$lastCommand = 'static';
|
|
|
|
}
|
|
|
|
|
2017-08-24 17:06:37 +03:00
|
|
|
/**
|
|
|
|
* @return IBus
|
|
|
|
*/
|
|
|
|
protected function getBus() {
|
|
|
|
if (!$this->bus instanceof IBus) {
|
|
|
|
$this->bus = $this->createBus();
|
|
|
|
}
|
|
|
|
return $this->bus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return IBus
|
|
|
|
*/
|
|
|
|
abstract protected function createBus();
|
|
|
|
|
2015-02-17 18:49:14 +03:00
|
|
|
public function setUp() {
|
|
|
|
self::$lastCommand = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleCommand() {
|
|
|
|
$command = new SimpleCommand();
|
2017-08-24 17:06:37 +03:00
|
|
|
$this->getBus()->push($command);
|
2015-02-17 18:49:14 +03:00
|
|
|
$this->runJobs();
|
|
|
|
$this->assertEquals('SimpleCommand', self::$lastCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStateFullCommand() {
|
|
|
|
$command = new StateFullCommand('foo');
|
2017-08-24 17:06:37 +03:00
|
|
|
$this->getBus()->push($command);
|
2015-02-17 18:49:14 +03:00
|
|
|
$this->runJobs();
|
|
|
|
$this->assertEquals('foo', self::$lastCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStaticCallable() {
|
2017-08-24 17:06:37 +03:00
|
|
|
$this->getBus()->push(['\Test\Command\AsyncBusTest', 'DummyCommand']);
|
2015-02-17 18:49:14 +03:00
|
|
|
$this->runJobs();
|
|
|
|
$this->assertEquals('static', self::$lastCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMemberCallable() {
|
|
|
|
$command = new StateFullCommand('bar');
|
2017-08-24 17:06:37 +03:00
|
|
|
$this->getBus()->push([$command, 'handle']);
|
2015-02-17 18:49:14 +03:00
|
|
|
$this->runJobs();
|
|
|
|
$this->assertEquals('bar', self::$lastCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFunctionCallable() {
|
2017-08-24 17:06:37 +03:00
|
|
|
$this->getBus()->push('\Test\Command\BasicFunction');
|
2015-02-17 18:49:14 +03:00
|
|
|
$this->runJobs();
|
|
|
|
$this->assertEquals('function', self::$lastCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClosure() {
|
2017-08-24 17:06:37 +03:00
|
|
|
$this->getBus()->push(function () {
|
2016-05-20 16:38:20 +03:00
|
|
|
AsyncBusTest::$lastCommand = 'closure';
|
2015-02-17 18:49:14 +03:00
|
|
|
});
|
|
|
|
$this->runJobs();
|
|
|
|
$this->assertEquals('closure', self::$lastCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClosureSelf() {
|
2017-08-24 17:06:37 +03:00
|
|
|
$this->getBus()->push(function () {
|
2019-02-14 21:36:19 +03:00
|
|
|
AsyncBusTest::$lastCommand = 'closure-self';
|
2015-02-17 18:49:14 +03:00
|
|
|
});
|
|
|
|
$this->runJobs();
|
|
|
|
$this->assertEquals('closure-self', self::$lastCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testClosureThis() {
|
2015-02-23 17:42:18 +03:00
|
|
|
// clean class to prevent phpunit putting closure in $this
|
|
|
|
$test = new ThisClosureTest();
|
2017-08-24 17:06:37 +03:00
|
|
|
$test->test($this->getBus());
|
2015-02-17 18:49:14 +03:00
|
|
|
$this->runJobs();
|
|
|
|
$this->assertEquals('closure-this', self::$lastCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClosureBind() {
|
|
|
|
$state = 'bar';
|
2017-08-24 17:06:37 +03:00
|
|
|
$this->getBus()->push(function () use ($state) {
|
2019-02-14 21:36:19 +03:00
|
|
|
AsyncBusTest::$lastCommand = 'closure-' . $state;
|
2015-02-17 18:49:14 +03:00
|
|
|
});
|
|
|
|
$this->runJobs();
|
|
|
|
$this->assertEquals('closure-bar', self::$lastCommand);
|
|
|
|
}
|
|
|
|
|
2015-02-23 17:26:12 +03:00
|
|
|
public function testFileFileAccessCommand() {
|
2017-08-24 17:06:37 +03:00
|
|
|
$this->getBus()->push(new FilesystemCommand());
|
2015-02-23 17:26:12 +03:00
|
|
|
$this->assertEquals('', self::$lastCommand);
|
|
|
|
$this->runJobs();
|
|
|
|
$this->assertEquals('FileAccess', self::$lastCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFileFileAccessCommandSync() {
|
2017-08-24 17:06:37 +03:00
|
|
|
$this->getBus()->requireSync('\OC\Command\FileAccess');
|
|
|
|
$this->getBus()->push(new FilesystemCommand());
|
2015-02-23 17:26:12 +03:00
|
|
|
$this->assertEquals('FileAccess', self::$lastCommand);
|
|
|
|
self::$lastCommand = '';
|
|
|
|
$this->runJobs();
|
|
|
|
$this->assertEquals('', self::$lastCommand);
|
|
|
|
}
|
|
|
|
|
2015-02-17 18:49:14 +03:00
|
|
|
|
2017-08-24 17:06:37 +03:00
|
|
|
abstract protected function runJobs();
|
2015-02-17 18:49:14 +03:00
|
|
|
}
|