Merge pull request #18457 from owncloud/test-traits
Move common test logic to re-usable traits
This commit is contained in:
commit
5f5c3520c9
|
@ -11,22 +11,18 @@ namespace Test\Connector\Sabre\RequestTest;
|
||||||
use OC\Connector\Sabre\Server;
|
use OC\Connector\Sabre\Server;
|
||||||
use OC\Connector\Sabre\ServerFactory;
|
use OC\Connector\Sabre\ServerFactory;
|
||||||
use OC\Files\Mount\MountPoint;
|
use OC\Files\Mount\MountPoint;
|
||||||
|
use OC\Files\Storage\StorageFactory;
|
||||||
use OC\Files\Storage\Temporary;
|
use OC\Files\Storage\Temporary;
|
||||||
use OC\Files\View;
|
use OC\Files\View;
|
||||||
use OCP\IUser;
|
use OCP\IUser;
|
||||||
use Sabre\HTTP\Request;
|
use Sabre\HTTP\Request;
|
||||||
use Test\TestCase;
|
use Test\TestCase;
|
||||||
|
use Test\Traits\MountProviderTrait;
|
||||||
|
use Test\Traits\UserTrait;
|
||||||
|
|
||||||
abstract class RequestTest extends TestCase {
|
abstract class RequestTest extends TestCase {
|
||||||
/**
|
use UserTrait;
|
||||||
* @var \OC_User_Dummy
|
use MountProviderTrait;
|
||||||
*/
|
|
||||||
protected $userBackend;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \OCP\Files\Config\IMountProvider[]
|
|
||||||
*/
|
|
||||||
protected $mountProviders;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \OC\Connector\Sabre\ServerFactory
|
* @var \OC\Connector\Sabre\ServerFactory
|
||||||
|
@ -40,33 +36,8 @@ abstract class RequestTest extends TestCase {
|
||||||
return $stream;
|
return $stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $userId
|
|
||||||
* @param $storages
|
|
||||||
* @return \OCP\Files\Config\IMountProvider
|
|
||||||
*/
|
|
||||||
protected function getMountProvider($userId, $storages) {
|
|
||||||
$mounts = [];
|
|
||||||
foreach ($storages as $mountPoint => $storage) {
|
|
||||||
$mounts[] = new MountPoint($storage, $mountPoint);
|
|
||||||
}
|
|
||||||
$provider = $this->getMock('\OCP\Files\Config\IMountProvider');
|
|
||||||
$provider->expects($this->any())
|
|
||||||
->method('getMountsForUser')
|
|
||||||
->will($this->returnCallback(function (IUser $user) use ($userId, $mounts) {
|
|
||||||
if ($user->getUID() === $userId) {
|
|
||||||
return $mounts;
|
|
||||||
} else {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
return $provider;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function setUp() {
|
protected function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->userBackend = new \OC_User_Dummy();
|
|
||||||
\OC::$server->getUserManager()->registerBackend($this->userBackend);
|
|
||||||
|
|
||||||
$this->serverFactory = new ServerFactory(
|
$this->serverFactory = new ServerFactory(
|
||||||
\OC::$server->getConfig(),
|
\OC::$server->getConfig(),
|
||||||
|
@ -78,16 +49,9 @@ abstract class RequestTest extends TestCase {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function tearDown() {
|
|
||||||
parent::tearDown();
|
|
||||||
\OC::$server->getUserManager()->removeBackend($this->userBackend);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function setupUser($name, $password) {
|
protected function setupUser($name, $password) {
|
||||||
$this->userBackend->createUser($name, $password);
|
$this->createUser($name, $password);
|
||||||
\OC::$server->getMountProviderCollection()->registerProvider($this->getMountProvider($name, [
|
$this->registerMount($name, '\OC\Files\Storage\Temporary', '/' . $name);
|
||||||
'/' . $name => new Temporary()
|
|
||||||
]));
|
|
||||||
$this->loginAsUser($name);
|
$this->loginAsUser($name);
|
||||||
return new View('/' . $name . '/files');
|
return new View('/' . $name . '/files');
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,21 +32,52 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
|
||||||
*/
|
*/
|
||||||
private $commandBus;
|
private $commandBus;
|
||||||
|
|
||||||
|
protected function getTestTraits() {
|
||||||
|
$traits = [];
|
||||||
|
$class = $this;
|
||||||
|
do {
|
||||||
|
$traits = array_merge(class_uses($class), $traits);
|
||||||
|
} while ($class = get_parent_class($class));
|
||||||
|
foreach ($traits as $trait => $same) {
|
||||||
|
$traits = array_merge(class_uses($trait), $traits);
|
||||||
|
}
|
||||||
|
$traits = array_unique($traits);
|
||||||
|
return array_filter($traits, function ($trait) {
|
||||||
|
return substr($trait, 0, 5) === 'Test\\';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
protected function setUp() {
|
protected function setUp() {
|
||||||
// overwrite the command bus with one we can run ourselves
|
// overwrite the command bus with one we can run ourselves
|
||||||
$this->commandBus = new QueueBus();
|
$this->commandBus = new QueueBus();
|
||||||
\OC::$server->registerService('AsyncCommandBus', function () {
|
\OC::$server->registerService('AsyncCommandBus', function () {
|
||||||
return $this->commandBus;
|
return $this->commandBus;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$traits = $this->getTestTraits();
|
||||||
|
foreach ($traits as $trait) {
|
||||||
|
$methodName = 'setUp' . basename(str_replace('\\', '/', $trait));
|
||||||
|
if (method_exists($this, $methodName)) {
|
||||||
|
call_user_func([$this, $methodName]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function tearDown() {
|
protected function tearDown() {
|
||||||
$hookExceptions = \OC_Hook::$thrownExceptions;
|
$hookExceptions = \OC_Hook::$thrownExceptions;
|
||||||
\OC_Hook::$thrownExceptions = [];
|
\OC_Hook::$thrownExceptions = [];
|
||||||
\OC::$server->getLockingProvider()->releaseAll();
|
\OC::$server->getLockingProvider()->releaseAll();
|
||||||
if(!empty($hookExceptions)) {
|
if (!empty($hookExceptions)) {
|
||||||
throw $hookExceptions[0];
|
throw $hookExceptions[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$traits = $this->getTestTraits();
|
||||||
|
foreach ($traits as $trait) {
|
||||||
|
$methodName = 'tearDown' . basename(str_replace('\\', '/', $trait));
|
||||||
|
if (method_exists($this, $methodName)) {
|
||||||
|
call_user_func([$this, $methodName]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
<?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\Traits;
|
||||||
|
|
||||||
|
use OC\Files\Mount\MountPoint;
|
||||||
|
use OC\Files\Storage\StorageFactory;
|
||||||
|
use OCP\IUser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allow setting mounts for users
|
||||||
|
*/
|
||||||
|
trait MountProviderTrait {
|
||||||
|
/**
|
||||||
|
* @var \OCP\Files\Config\IMountProvider
|
||||||
|
*/
|
||||||
|
protected $mountProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \OC\Files\Storage\StorageFactory
|
||||||
|
*/
|
||||||
|
protected $storageFactory;
|
||||||
|
|
||||||
|
protected $mounts = [];
|
||||||
|
|
||||||
|
protected function registerMount($userId, $storage, $mountPoint, $arguments = null) {
|
||||||
|
if (!isset($this->mounts[$userId])) {
|
||||||
|
$this->mounts[$userId] = [];
|
||||||
|
}
|
||||||
|
$this->mounts[$userId][] = new MountPoint($storage, $mountPoint, $arguments, $this->storageFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function registerStorageWrapper($name, $wrapper) {
|
||||||
|
$this->storageFactory->addStorageWrapper($name, $wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setUpMountProviderTrait() {
|
||||||
|
$this->storageFactory = new StorageFactory();
|
||||||
|
$this->mountProvider = $this->getMock('\OCP\Files\Config\IMountProvider');
|
||||||
|
$this->mountProvider->expects($this->any())
|
||||||
|
->method('getMountsForUser')
|
||||||
|
->will($this->returnCallback(function (IUser $user) {
|
||||||
|
if (isset($this->mounts[$user->getUID()])) {
|
||||||
|
return $this->mounts[$user->getUID()];
|
||||||
|
} else {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
\OC::$server->getMountProviderCollection()->registerProvider($this->mountProvider);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?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\Traits;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allow creating users in a temporary backend
|
||||||
|
*/
|
||||||
|
trait UserTrait {
|
||||||
|
/**
|
||||||
|
* @var \OC_User_Dummy|\OCP\UserInterface
|
||||||
|
*/
|
||||||
|
protected $userBackend;
|
||||||
|
|
||||||
|
protected function createUser($name, $password) {
|
||||||
|
$this->userBackend->createUser($name, $password);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setUpUserTrait() {
|
||||||
|
$this->userBackend = new \OC_User_Dummy();
|
||||||
|
\OC::$server->getUserManager()->registerBackend($this->userBackend);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDownUserTrait() {
|
||||||
|
\OC::$server->getUserManager()->removeBackend($this->userBackend);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue