From 74ae7b8929a7fd3f539fd15efb9533424114a480 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 17 Feb 2015 16:49:14 +0100 Subject: Add async command system to handle asynchronous operations --- tests/lib/backgroundjob/dummyjoblist.php | 7 +- tests/lib/command/asyncbus.php | 143 +++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 tests/lib/command/asyncbus.php (limited to 'tests') diff --git a/tests/lib/backgroundjob/dummyjoblist.php b/tests/lib/backgroundjob/dummyjoblist.php index 7801269b27e..6cc690fd553 100644 --- a/tests/lib/backgroundjob/dummyjoblist.php +++ b/tests/lib/backgroundjob/dummyjoblist.php @@ -21,13 +21,18 @@ class DummyJobList extends \OC\BackgroundJob\JobList { private $last = 0; - public function __construct(){} + public function __construct() { + } /** * @param \OC\BackgroundJob\Job|string $job * @param mixed $argument */ public function add($job, $argument = null) { + if (is_string($job)) { + /** @var \OC\BackgroundJob\Job $job */ + $job = new $job; + } $job->setArgument($argument); if (!$this->has($job, null)) { $this->jobs[] = $job; diff --git a/tests/lib/command/asyncbus.php b/tests/lib/command/asyncbus.php new file mode 100644 index 00000000000..030c416953d --- /dev/null +++ b/tests/lib/command/asyncbus.php @@ -0,0 +1,143 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Command; + +use OCP\Command\ICommand; +use Test\BackgroundJob\DummyJobList; +use Test\TestCase; + +class SimpleCommand implements ICommand { + public function handle() { + AsyncBus::$lastCommand = 'SimpleCommand'; + } +} + +class StateFullCommand implements ICommand { + private $state; + + function __construct($state) { + $this->state = $state; + } + + public function handle() { + AsyncBus::$lastCommand = $this->state; + } +} + +function basicFunction() { + AsyncBus::$lastCommand = 'function'; +} + +class AsyncBus extends TestCase { + /** + * Basic way to check output from a command + * + * @var string + */ + public static $lastCommand; + + /** + * @var \OCP\BackgroundJob\IJobList + */ + private $jobList; + + /** + * @var \OCP\Command\IBus + */ + private $bus; + + public static function DummyCommand() { + self::$lastCommand = 'static'; + } + + public function setUp() { + $this->jobList = new DummyJobList(); + $this->bus = new \OC\Command\AsyncBus($this->jobList); + self::$lastCommand = ''; + } + + public function testSimpleCommand() { + $command = new SimpleCommand(); + $this->bus->push($command); + $this->runJobs(); + $this->assertEquals('SimpleCommand', self::$lastCommand); + } + + public function testStateFullCommand() { + $command = new StateFullCommand('foo'); + $this->bus->push($command); + $this->runJobs(); + $this->assertEquals('foo', self::$lastCommand); + } + + public function testStaticCallable() { + $this->bus->push(['\Test\Command\AsyncBus', 'DummyCommand']); + $this->runJobs(); + $this->assertEquals('static', self::$lastCommand); + } + + public function testMemberCallable() { + $command = new StateFullCommand('bar'); + $this->bus->push([$command, 'handle']); + $this->runJobs(); + $this->assertEquals('bar', self::$lastCommand); + } + + public function testFunctionCallable() { + $this->bus->push('\Test\Command\BasicFunction'); + $this->runJobs(); + $this->assertEquals('function', self::$lastCommand); + } + + public function testClosure() { + $this->bus->push(function () { + AsyncBus::$lastCommand = 'closure'; + }); + $this->runJobs(); + $this->assertEquals('closure', self::$lastCommand); + } + + public function testClosureSelf() { + $this->bus->push(function () { + self::$lastCommand = 'closure-self'; + }); + $this->runJobs(); + $this->assertEquals('closure-self', self::$lastCommand); + } + + private function privateMethod() { + self::$lastCommand = 'closure-this'; + } + + public function testClosureThis() { + $this->bus->push(function () { + $this->privateMethod(); + }); + $this->runJobs(); + $this->assertEquals('closure-this', self::$lastCommand); + } + + public function testClosureBind() { + $state = 'bar'; + $this->bus->push(function () use ($state) { + self::$lastCommand = 'closure-' . $state; + }); + $this->runJobs(); + $this->assertEquals('closure-bar', self::$lastCommand); + } + + + private function runJobs() { + $jobs = $this->jobList->getAll(); + foreach ($jobs as $job) { + $job->execute($this->jobList); + } + } +} -- cgit v1.2.3 From a39c7bf4643fdaa17e62ea503fede1c2f4a30d35 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 23 Feb 2015 15:26:12 +0100 Subject: Add FileAccess trait for commands --- lib/private/command/fileaccess.php | 18 ++++++++++++++++++ tests/lib/command/asyncbus.php | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 lib/private/command/fileaccess.php (limited to 'tests') diff --git a/lib/private/command/fileaccess.php b/lib/private/command/fileaccess.php new file mode 100644 index 00000000000..5de00862fac --- /dev/null +++ b/lib/private/command/fileaccess.php @@ -0,0 +1,18 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Command; + +use OCP\IUser; + +trait FileAccess { + protected function getUserFolder(IUser $user) { + \OC_Util::setupFS($user->getUID()); + return \OC::$server->getUserFolder($user->getUID()); + } +} diff --git a/tests/lib/command/asyncbus.php b/tests/lib/command/asyncbus.php index 030c416953d..d22ce34d839 100644 --- a/tests/lib/command/asyncbus.php +++ b/tests/lib/command/asyncbus.php @@ -9,6 +9,7 @@ namespace Test\Command; +use OC\Command\FileAccess; use OCP\Command\ICommand; use Test\BackgroundJob\DummyJobList; use Test\TestCase; @@ -31,6 +32,14 @@ class StateFullCommand implements ICommand { } } +class FilesystemCommand implements ICommand { + use FileAccess; + + public function handle() { + AsyncBus::$lastCommand = 'FileAccess'; + } +} + function basicFunction() { AsyncBus::$lastCommand = 'function'; } @@ -133,6 +142,22 @@ class AsyncBus extends TestCase { $this->assertEquals('closure-bar', self::$lastCommand); } + public function testFileFileAccessCommand() { + $this->bus->push(new FilesystemCommand()); + $this->assertEquals('', self::$lastCommand); + $this->runJobs(); + $this->assertEquals('FileAccess', self::$lastCommand); + } + + public function testFileFileAccessCommandSync() { + $this->bus->requireSync('\OC\Command\FileAccess'); + $this->bus->push(new FilesystemCommand()); + $this->assertEquals('FileAccess', self::$lastCommand); + self::$lastCommand = ''; + $this->runJobs(); + $this->assertEquals('', self::$lastCommand); + } + private function runJobs() { $jobs = $this->jobList->getAll(); -- cgit v1.2.3 From 9873ab20afde7def037dcee2e00921d01e52ae82 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 23 Feb 2015 15:42:18 +0100 Subject: prevent phpunit from messing with the bound $this --- tests/lib/command/asyncbus.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/lib/command/asyncbus.php b/tests/lib/command/asyncbus.php index d22ce34d839..183eaa29c37 100644 --- a/tests/lib/command/asyncbus.php +++ b/tests/lib/command/asyncbus.php @@ -10,6 +10,7 @@ namespace Test\Command; use OC\Command\FileAccess; +use OCP\Command\IBus; use OCP\Command\ICommand; use Test\BackgroundJob\DummyJobList; use Test\TestCase; @@ -44,6 +45,19 @@ function basicFunction() { AsyncBus::$lastCommand = 'function'; } +// clean class to prevent phpunit putting closure in $this +class ThisClosureTest { + private function privateMethod() { + AsyncBus::$lastCommand = 'closure-this'; + } + + public function test(IBus $bus) { + $bus->push(function () { + $this->privateMethod(); + }); + } +} + class AsyncBus extends TestCase { /** * Basic way to check output from a command @@ -121,14 +135,11 @@ class AsyncBus extends TestCase { $this->assertEquals('closure-self', self::$lastCommand); } - private function privateMethod() { - self::$lastCommand = 'closure-this'; - } public function testClosureThis() { - $this->bus->push(function () { - $this->privateMethod(); - }); + // clean class to prevent phpunit putting closure in $this + $test = new ThisClosureTest(); + $test->test($this->bus); $this->runJobs(); $this->assertEquals('closure-this', self::$lastCommand); } -- cgit v1.2.3