From 82e4838162de9ae84348bd8d6b273cd226bceb89 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Fri, 31 Mar 2017 13:24:00 -0400 Subject: [PATCH] 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 --- .../Database/TestDatabaseFolderSQLite3.php | 9 +++ .../Database/TestDatabaseUserSQLite3.php | 9 +++ tests/lib/Database/DriverSQLite3.php | 12 ++++ .../Database/SeriesUser.php} | 72 ++++--------------- .../lib/{Db/Tools.php => Database/Setup.php} | 53 ++++++++++++-- tests/phpunit.xml | 2 +- 6 files changed, 94 insertions(+), 63 deletions(-) create mode 100644 tests/Db/SQLite3/Database/TestDatabaseFolderSQLite3.php create mode 100644 tests/Db/SQLite3/Database/TestDatabaseUserSQLite3.php create mode 100644 tests/lib/Database/DriverSQLite3.php rename tests/{Db/SQLite3/Database/TestDatabaseUser.php => lib/Database/SeriesUser.php} (80%) rename tests/lib/{Db/Tools.php => Database/Setup.php} (53%) diff --git a/tests/Db/SQLite3/Database/TestDatabaseFolderSQLite3.php b/tests/Db/SQLite3/Database/TestDatabaseFolderSQLite3.php new file mode 100644 index 00000000..dea9c116 --- /dev/null +++ b/tests/Db/SQLite3/Database/TestDatabaseFolderSQLite3.php @@ -0,0 +1,9 @@ +dbSQLite3File = ":memory:"; + $this->drv = new Driver(true); + } +} \ No newline at end of file diff --git a/tests/Db/SQLite3/Database/TestDatabaseUser.php b/tests/lib/Database/SeriesUser.php similarity index 80% rename from tests/Db/SQLite3/Database/TestDatabaseUser.php rename to tests/lib/Database/SeriesUser.php index 3abaa9b9..00cfd2b5 100644 --- a/tests/Db/SQLite3/Database/TestDatabaseUser.php +++ b/tests/lib/Database/SeriesUser.php @@ -1,55 +1,11 @@ [ - '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); diff --git a/tests/lib/Db/Tools.php b/tests/lib/Database/Setup.php similarity index 53% rename from tests/lib/Db/Tools.php rename to tests/lib/Database/Setup.php index 47d61eaf..f4d71f23 100644 --- a/tests/lib/Db/Tools.php +++ b/tests/lib/Database/Setup.php @@ -1,11 +1,56 @@ [ + '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) { diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 103fbd0e..2881db88 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -39,7 +39,7 @@ - Db/SQLite3/Database/TestDatabaseUser.php + Db/SQLite3/Database/TestDatabaseUserSQLite3.php