diff --git a/tests/Services/WikiUserEmailCheckerTest.php b/tests/Services/WikiUserEmailCheckerTest.php new file mode 100644 index 00000000..759ca38a --- /dev/null +++ b/tests/Services/WikiUserEmailCheckerTest.php @@ -0,0 +1,65 @@ + 'db_1', 'name' => 'mwdb_1', 'emails' => ['user1@email.localhost', 'user2@email.localhost']], + ['prefix' => 'db_2', 'name' => 'mwdb_2', 'emails' => ['user1@email.localhost']], + ['prefix' => 'db_3', 'name' => 'mwdb_3', 'emails' => []], + ]; + + protected function setUp(): void { + parent::setUp(); + $this->db = $this->app->make('db'); + $this->deleteDatabases(); + $pdo = $this->db->connection('mw')->getPdo(); + foreach ($this->databases as $database) { + $pdo->exec("CREATE DATABASE {$database['name']}"); + $userTable = "{$database['name']}.{$database['prefix']}_user"; + $pdo->exec("CREATE TABLE {$userTable} (user_email TINYBLOB)"); + if ($database['emails']) { + $users = implode(',', array_map(fn ($email) => "('$email')", $database['emails'])); + $pdo->exec("INSERT INTO {$userTable} VALUES {$users}"); + } + } + } + + protected function tearDown(): void { + $this->deleteDatabases(); + WikiDb::query()->delete(); + parent::tearDown(); + } + + private function deleteDatabases(): void { + $pdo = $this->db->connection('mw')->getPdo(); + foreach ($this->databases as $database) { + $pdo->exec("DROP DATABASE IF EXISTS {$database['name']};"); + } + } + + public function testCorrectDatabaseFound(): void { + $checker = new WikiUserEmailChecker($this->db); + $this->assertEquals( + ['mwdb_1.db_1_user'], + $checker->findEmail('user2@email.localhost') + ); + } + + public function testEmailFoundInMultipleDatabases(): void { + $checker = new WikiUserEmailChecker($this->db); + $this->assertEquals( + ['mwdb_1.db_1_user', 'mwdb_2.db_2_user'], + $checker->findEmail('user1@email.localhost') + ); + } +}