mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 21:22:40 +00:00
Re-organize Database tests
- Test files now consist entirely of traits: - General setup trait mocking User class and cleaning up - Driver trait to set up the database connection - Series trait containing tests for the series, independent of driver used
This commit is contained in:
parent
bc863ae935
commit
82e4838162
6 changed files with 94 additions and 63 deletions
9
tests/Db/SQLite3/Database/TestDatabaseFolderSQLite3.php
Normal file
9
tests/Db/SQLite3/Database/TestDatabaseFolderSQLite3.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace JKingWeb\Arsse;
|
||||
|
||||
class TestDatabaseFolderSQLite3 extends \PHPUnit\Framework\TestCase {
|
||||
use Test\Tools, Test\Database\Setup;
|
||||
use Test\Database\DriverSQLite3;
|
||||
use Test\Database\SeriesFolder;
|
||||
}
|
9
tests/Db/SQLite3/Database/TestDatabaseUserSQLite3.php
Normal file
9
tests/Db/SQLite3/Database/TestDatabaseUserSQLite3.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace JKingWeb\Arsse;
|
||||
|
||||
class TestDatabaseUserSQLite3 extends \PHPUnit\Framework\TestCase {
|
||||
use Test\Tools, Test\Database\Setup;
|
||||
use Test\Database\DriverSQLite3;
|
||||
use Test\Database\SeriesUser;
|
||||
}
|
12
tests/lib/Database/DriverSQLite3.php
Normal file
12
tests/lib/Database/DriverSQLite3.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace JKingWeb\Arsse\Test\Database;
|
||||
use JKingWeb\Arsse\Data;
|
||||
use JKingWeb\Arsse\Db\SQLite3\Driver;
|
||||
|
||||
trait DriverSQLite3 {
|
||||
function setUpDriver() {
|
||||
Data::$conf->dbSQLite3File = ":memory:";
|
||||
$this->drv = new Driver(true);
|
||||
}
|
||||
}
|
|
@ -1,55 +1,11 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace JKingWeb\Arsse;
|
||||
namespace JKingWeb\Arsse\Test\Database;
|
||||
use JKingWeb\Arsse\Data;
|
||||
use JKingWeb\Arsse\User\Driver as UserDriver;
|
||||
use Phake;
|
||||
|
||||
|
||||
class TestDatabaseUser extends \PHPUnit\Framework\TestCase {
|
||||
use Test\Tools, Test\Db\Tools;
|
||||
|
||||
protected $drv;
|
||||
protected $data = [
|
||||
'arsse_users' => [
|
||||
'columns' => [
|
||||
'id' => 'str',
|
||||
'password' => 'str',
|
||||
'name' => 'str',
|
||||
'rights' => 'int',
|
||||
],
|
||||
'rows' => [
|
||||
["admin@example.net", '$2y$10$PbcG2ZR3Z8TuPzM7aHTF8.v61dtCjzjK78gdZJcp4UePE8T9jEgBW', "Hard Lip Herbert", User\Driver::RIGHTS_GLOBAL_ADMIN], // password is hash of "secret"
|
||||
["jane.doe@example.com", "", "Jane Doe", User\Driver::RIGHTS_NONE],
|
||||
["john.doe@example.com", "", "John Doe", User\Driver::RIGHTS_NONE],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
function setUp() {
|
||||
// establish a clean baseline
|
||||
$this->clearData();
|
||||
// create a default configuration
|
||||
Data::$conf = new Conf();
|
||||
// configure and create the relevant database driver
|
||||
Data::$conf->dbSQLite3File = ":memory:";
|
||||
$this->drv = new Db\SQLite3\Driver(true);
|
||||
// create the database interface with the suitable driver
|
||||
Data::$db = new Database($this->drv);
|
||||
Data::$db->schemaUpdate();
|
||||
// create a mock user manager
|
||||
Data::$user = Phake::mock(User::class);
|
||||
Phake::when(Data::$user)->authorize->thenReturn(true);
|
||||
// call the additional setup method if it exists
|
||||
if(method_exists($this, "setUpSeries")) $this->setUpSeries();
|
||||
}
|
||||
|
||||
function tearDown() {
|
||||
// call the additional teardiwn method if it exists
|
||||
if(method_exists($this, "tearDownSeries")) $this->tearDownSeries();
|
||||
// clean up
|
||||
$this->drv = null;
|
||||
$this->clearData();
|
||||
}
|
||||
|
||||
trait SeriesUser {
|
||||
function setUpSeries() {
|
||||
$this->primeDatabase($this->data);
|
||||
}
|
||||
|
@ -85,7 +41,7 @@ class TestDatabaseUser extends \PHPUnit\Framework\TestCase {
|
|||
$this->assertSame("", Data::$db->userAdd("john.doe@example.org", ""));
|
||||
Phake::verify(Data::$user)->authorize("john.doe@example.org", "userAdd");
|
||||
$state = $this->primeExpectations($this->data, ['arsse_users' => ['id','name','rights']]);
|
||||
$state['arsse_users']['rows'][] = ["john.doe@example.org", null, User\Driver::RIGHTS_NONE];
|
||||
$state['arsse_users']['rows'][] = ["john.doe@example.org", null, UserDriver::RIGHTS_NONE];
|
||||
$this->compareExpectations($state);
|
||||
}
|
||||
|
||||
|
@ -192,7 +148,7 @@ class TestDatabaseUser extends \PHPUnit\Framework\TestCase {
|
|||
function testGetUserProperties() {
|
||||
$exp = [
|
||||
'name' => 'Hard Lip Herbert',
|
||||
'rights' => User\Driver::RIGHTS_GLOBAL_ADMIN,
|
||||
'rights' => UserDriver::RIGHTS_GLOBAL_ADMIN,
|
||||
];
|
||||
$props = Data::$db->userPropertiesGet("admin@example.net");
|
||||
Phake::verify(Data::$user)->authorize("admin@example.net", "userPropertiesGet");
|
||||
|
@ -215,12 +171,12 @@ class TestDatabaseUser extends \PHPUnit\Framework\TestCase {
|
|||
$try = [
|
||||
'name' => 'James Kirk', // only this should actually change
|
||||
'password' => '000destruct0',
|
||||
'rights' => User\Driver::RIGHTS_NONE,
|
||||
'rights' => UserDriver::RIGHTS_NONE,
|
||||
'lifeform' => 'tribble',
|
||||
];
|
||||
$exp = [
|
||||
'name' => 'James Kirk',
|
||||
'rights' => User\Driver::RIGHTS_GLOBAL_ADMIN,
|
||||
'rights' => UserDriver::RIGHTS_GLOBAL_ADMIN,
|
||||
];
|
||||
$props = Data::$db->userPropertiesSet("admin@example.net", $try);
|
||||
Phake::verify(Data::$user)->authorize("admin@example.net", "userPropertiesSet");
|
||||
|
@ -247,14 +203,14 @@ class TestDatabaseUser extends \PHPUnit\Framework\TestCase {
|
|||
function testGetUserRights() {
|
||||
$user1 = "john.doe@example.com";
|
||||
$user2 = "admin@example.net";
|
||||
$this->assertSame(User\Driver::RIGHTS_NONE, Data::$db->userRightsGet($user1));
|
||||
$this->assertSame(User\Driver::RIGHTS_GLOBAL_ADMIN, Data::$db->userRightsGet($user2));
|
||||
$this->assertSame(UserDriver::RIGHTS_NONE, Data::$db->userRightsGet($user1));
|
||||
$this->assertSame(UserDriver::RIGHTS_GLOBAL_ADMIN, Data::$db->userRightsGet($user2));
|
||||
Phake::verify(Data::$user)->authorize($user1, "userRightsGet");
|
||||
Phake::verify(Data::$user)->authorize($user2, "userRightsGet");
|
||||
}
|
||||
|
||||
function testGetTheRightsOfAMissingUser() {
|
||||
$this->assertSame(User\Driver::RIGHTS_NONE, Data::$db->userRightsGet("john.doe@example.org"));
|
||||
$this->assertSame(UserDriver::RIGHTS_NONE, Data::$db->userRightsGet("john.doe@example.org"));
|
||||
Phake::verify(Data::$user)->authorize("john.doe@example.org", "userRightsGet");
|
||||
}
|
||||
|
||||
|
@ -266,7 +222,7 @@ class TestDatabaseUser extends \PHPUnit\Framework\TestCase {
|
|||
|
||||
function testSetUserRights() {
|
||||
$user = "john.doe@example.com";
|
||||
$rights = User\Driver::RIGHTS_GLOBAL_ADMIN;
|
||||
$rights = UserDriver::RIGHTS_GLOBAL_ADMIN;
|
||||
$this->assertTrue(Data::$db->userRightsSet($user, $rights));
|
||||
Phake::verify(Data::$user)->authorize($user, "userRightsSet", $rights);
|
||||
$state = $this->primeExpectations($this->data, ['arsse_users' => ['id','rights']]);
|
||||
|
@ -275,13 +231,13 @@ class TestDatabaseUser extends \PHPUnit\Framework\TestCase {
|
|||
}
|
||||
|
||||
function testSetTheRightsOfAMissingUser() {
|
||||
$rights = User\Driver::RIGHTS_GLOBAL_ADMIN;
|
||||
$rights = UserDriver::RIGHTS_GLOBAL_ADMIN;
|
||||
$this->assertException("doesNotExist", "User");
|
||||
Data::$db->userRightsSet("john.doe@example.org", $rights);
|
||||
}
|
||||
|
||||
function testSetUserRightsWithoutAuthority() {
|
||||
$rights = User\Driver::RIGHTS_GLOBAL_ADMIN;
|
||||
$rights = UserDriver::RIGHTS_GLOBAL_ADMIN;
|
||||
Phake::when(Data::$user)->authorize->thenReturn(false);
|
||||
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
|
||||
Data::$db->userRightsSet("john.doe@example.com", $rights);
|
|
@ -1,11 +1,56 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace JKingWeb\Arsse\Test\Db;
|
||||
namespace JKingWeb\Arsse\Test\Database;
|
||||
use JKingWeb\Arsse\User\Driver as UserDriver;
|
||||
use JKingWeb\Arsse\Data;
|
||||
use JKingWeb\Arsse\Conf;
|
||||
use JKingWeb\Arsse\User;
|
||||
use JKingWeb\Arsse\Database;
|
||||
use Phake;
|
||||
|
||||
trait Tools {
|
||||
trait Setup {
|
||||
protected $drv;
|
||||
|
||||
|
||||
protected $data = [
|
||||
'arsse_users' => [
|
||||
'columns' => [
|
||||
'id' => 'str',
|
||||
'password' => 'str',
|
||||
'name' => 'str',
|
||||
'rights' => 'int',
|
||||
],
|
||||
'rows' => [
|
||||
["admin@example.net", '$2y$10$PbcG2ZR3Z8TuPzM7aHTF8.v61dtCjzjK78gdZJcp4UePE8T9jEgBW', "Hard Lip Herbert", UserDriver::RIGHTS_GLOBAL_ADMIN], // password is hash of "secret"
|
||||
["jane.doe@example.com", "", "Jane Doe", UserDriver::RIGHTS_NONE],
|
||||
["john.doe@example.com", "", "John Doe", UserDriver::RIGHTS_NONE],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
function setUp() {
|
||||
// establish a clean baseline
|
||||
$this->clearData();
|
||||
// create a default configuration
|
||||
Data::$conf = new Conf();
|
||||
// configure and create the relevant database driver
|
||||
$this->setUpDriver();
|
||||
// create the database interface with the suitable driver
|
||||
Data::$db = new Database($this->drv);
|
||||
Data::$db->schemaUpdate();
|
||||
// create a mock user manager
|
||||
Data::$user = Phake::mock(User::class);
|
||||
Phake::when(Data::$user)->authorize->thenReturn(true);
|
||||
// call the additional setup method if it exists
|
||||
if(method_exists($this, "setUpSeries")) $this->setUpSeries();
|
||||
}
|
||||
|
||||
function tearDown() {
|
||||
// call the additional teardiwn method if it exists
|
||||
if(method_exists($this, "tearDownSeries")) $this->tearDownSeries();
|
||||
// clean up
|
||||
$this->drv = null;
|
||||
$this->clearData();
|
||||
}
|
||||
|
||||
function primeDatabase(array $data): bool {
|
||||
$this->drv->begin();
|
||||
foreach($data as $table => $info) {
|
|
@ -39,7 +39,7 @@
|
|||
</testsuite>
|
||||
|
||||
<testsuite name="SQLite3 database functions">
|
||||
<file>Db/SQLite3/Database/TestDatabaseUser.php</file>
|
||||
<file>Db/SQLite3/Database/TestDatabaseUserSQLite3.php</file>
|
||||
</testsuite>
|
||||
|
||||
<testsuite name="NextCloud News API">
|
||||
|
|
Loading…
Reference in a new issue