diff --git a/tests/lib/connector/sabre/requesttest/requesttest.php b/tests/lib/connector/sabre/requesttest/requesttest.php index c7739aefcd..7b19282441 100644 --- a/tests/lib/connector/sabre/requesttest/requesttest.php +++ b/tests/lib/connector/sabre/requesttest/requesttest.php @@ -16,12 +16,10 @@ use OC\Files\View; use OCP\IUser; use Sabre\HTTP\Request; use Test\TestCase; +use Test\Traits\UserTrait; abstract class RequestTest extends TestCase { - /** - * @var \OC_User_Dummy - */ - protected $userBackend; + use UserTrait; /** * @var \OCP\Files\Config\IMountProvider[] @@ -65,8 +63,6 @@ abstract class RequestTest extends TestCase { protected function setUp() { parent::setUp(); - $this->userBackend = new \OC_User_Dummy(); - \OC::$server->getUserManager()->registerBackend($this->userBackend); $this->serverFactory = new ServerFactory( \OC::$server->getConfig(), @@ -78,15 +74,10 @@ abstract class RequestTest extends TestCase { ); } - protected function tearDown() { - parent::tearDown(); - \OC::$server->getUserManager()->removeBackend($this->userBackend); - } - protected function setupUser($name, $password) { - $this->userBackend->createUser($name, $password); + $this->createUser($name, $password); \OC::$server->getMountProviderCollection()->registerProvider($this->getMountProvider($name, [ - '/' . $name => new Temporary() + '/' . $name => '\OC\Files\Storage\Temporary' ])); $this->loginAsUser($name); return new View('/' . $name . '/files'); diff --git a/tests/lib/testcase.php b/tests/lib/testcase.php index fd0b8d5f2d..854d5f4f65 100644 --- a/tests/lib/testcase.php +++ b/tests/lib/testcase.php @@ -32,21 +32,52 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { */ 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() { // overwrite the command bus with one we can run ourselves $this->commandBus = new QueueBus(); \OC::$server->registerService('AsyncCommandBus', function () { 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() { $hookExceptions = \OC_Hook::$thrownExceptions; \OC_Hook::$thrownExceptions = []; \OC::$server->getLockingProvider()->releaseAll(); - if(!empty($hookExceptions)) { + if (!empty($hookExceptions)) { 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]); + } + } } /** diff --git a/tests/lib/traits/usertrait.php b/tests/lib/traits/usertrait.php new file mode 100644 index 0000000000..401d8b8a83 --- /dev/null +++ b/tests/lib/traits/usertrait.php @@ -0,0 +1,32 @@ + + * 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); + } +}