diff --git a/src/Definition/DropUser.php b/src/Definition/DropUser.php new file mode 100644 index 0000000..ac7f53f --- /dev/null +++ b/src/Definition/DropUser.php @@ -0,0 +1,53 @@ +user = $user; + $this->host = $host; + } + + public function ifExists() + { + $this->ifExists = true; + return $this; + } + + public function getValues(): array + { + return []; + } + + public function __toString(): string + { + $sql = 'DROP USER'; + + if ($this->ifExists) { + $sql = "{$sql} IF EXISTS {$this->user}@{$this->host}"; + } else { + $sql = "{$sql} {$this->user}@{$this->host}"; + } + + return $sql; + } +} + diff --git a/src/Definition/IndexInterface.php b/src/Definition/IndexInterface.php new file mode 100644 index 0000000..d0506b8 --- /dev/null +++ b/src/Definition/IndexInterface.php @@ -0,0 +1,8 @@ +host = $host; + $this->user = $user; + $this->password = $password; + } + + public function getValues(): array + { + return []; + } + + /** + * + */ + public function orReplace() + { + $this->orReplace = true; + return $this; + } + + /** + * + */ + public function ifNotExists() + { + $this->ifNotExists = true; + return $this; + } + + public function __toString(): string + { + + /** + * Exception: CREATE 'OR REPLACE' USER 'IF NOT EXISTS' ... + */ + if ($this->orReplace && $this->ifNotExists) + { + throw new \Exception('The clause "OR REPLACE" and "IF NOT EXISTS" cannot be used together'); + } + + + $sql = 'CREATE'; + if ($this->orReplace) { + $sql = "{$sql} OR REPLACE USER {$this->user}@{$this->host}"; + } + + if ($this->ifNotExists) { + $sql = "{$sql} USER IF NOT EXISTS {$this->user}@{$this->host}"; + } + + if (!$this->ifNotExists && !$this->orReplace) { + $sql = "{$sql} USER {$this->user}@{$this->host}"; + } + + if ($this->password) { + $sql = "{$sql} IDENTIFIED BY '{$this->password}'"; + } + + return $sql; + } +} \ No newline at end of file diff --git a/src/Definition/UserInterface.php b/src/Definition/UserInterface.php new file mode 100644 index 0000000..d9a3177 --- /dev/null +++ b/src/Definition/UserInterface.php @@ -0,0 +1,8 @@ +expectException(\Exception::class); + + $user = 'bob'; + $host = 'localhost'; + $password = ''; + + $subject = new User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->orReplace()->ifNotExists()->__toString(); + + } + + public function testCreateUser() + { + $user = 'bob'; + $host = 'localhost'; + $password = ''; + + $subject = new User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->__toString(); + + $expect = "CREATE USER {$user}@{$host}"; + + $this->assertEquals($expect, $sql); + } + + public function testCreateUserWithPassword() + { + $user = 'bob'; + $host = 'localhost'; + $password = '123'; + + $subject = new \FaaPz\PDO\Definition\User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->__toString(); + + $expect = "CREATE USER {$user}@{$host} IDENTIFIED BY '{$password}'"; + + $this->assertEquals($expect, $sql); + } + + public function testCreateUserWithOrReplace() + { + $user = 'bob'; + $host = 'localhost'; + $password = ''; + + $subject = new \FaaPz\PDO\Definition\User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->orReplace()->__toString(); + + $expect = "CREATE OR REPLACE USER {$user}@{$host}"; + + $this->assertEquals($expect, $sql); + } + + public function testCreateUserWithIfNotExists() + { + $user = 'bob'; + $host = 'localhost'; + $password = ''; + + $subject = new \FaaPz\PDO\Definition\User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->ifNotExists()->__toString(); + + $expect = "CREATE USER IF NOT EXISTS {$user}@{$host}"; + + $this->assertEquals($expect, $sql); + } + + public function testCreateUserWithOrReplaceAndPassword() + { + $user = 'bob'; + $host = 'localhost'; + $password = '123'; + + $subject = new \FaaPz\PDO\Definition\User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->orReplace()->__toString(); + + $expect = "CREATE OR REPLACE USER {$user}@{$host} IDENTIFIED BY '{$password}'"; + + $this->assertEquals($expect, $sql); + } + + public function testCreateUserWithIfNotExistsAndPassword() + { + $user = 'bob'; + $host = 'localhost'; + $password = '123'; + + $subject = new \FaaPz\PDO\Definition\User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->ifNotExists()->__toString(); + + $expect = "CREATE USER IF NOT EXISTS {$user}@{$host} IDENTIFIED BY '{$password}'"; + + $this->assertEquals($expect, $sql); + } +} \ No newline at end of file diff --git a/tests/UserManager/MariaDB/DropUserTest.php b/tests/UserManager/MariaDB/DropUserTest.php new file mode 100644 index 0000000..2341084 --- /dev/null +++ b/tests/UserManager/MariaDB/DropUserTest.php @@ -0,0 +1,46 @@ +user = 'bob'; + $this->host = 'localhost'; + + $this->subject = new DropUser( + + $this->createMock(PDO::class), + $this->host, + $this->user + ); + } + + public function testDropUser() + { + $sql = $this->subject->__toString(); + + $expect = "DROP USER {$this->user}@{$this->host}"; + + $this->assertEquals($expect, $sql); + } + + public function testDropUserIfExists() + { + $sql = $this->subject->ifExists()->__toString(); + + $expect = "DROP USER IF EXISTS {$this->user}@{$this->host}"; + + $this->assertEquals($expect, $sql); + } +} \ No newline at end of file diff --git a/tests/UserManager/MariaDB/UserManagerMariaDBTest.php b/tests/UserManager/MariaDB/UserManagerMariaDBTest.php new file mode 100644 index 0000000..26d36f5 --- /dev/null +++ b/tests/UserManager/MariaDB/UserManagerMariaDBTest.php @@ -0,0 +1,59 @@ +subject = $this->createMock(PDO::class); + + $this->userManager = new UserManagerMariaDB(); + } + + public function testIfInstanceIsFromCreateUser() + { + $createUser = $this->userManager->createUser($this->subject, $this->host, $this->user, $this->password); + + $this->assertInstanceOf(User::class, $createUser); + } + + public function testIfInstanceIsFromDropUser() + { + $dropUser = $this->userManager->dropUser($this->subject, $this->host, $this->user); + + $this->assertInstanceOf(DropUser::class, $dropUser); + } + + public function testIfInstanceIsFromRovokePrivileges() + { + $revokeUser = $this->userManager->revokePrivileges($this->subject, $this->host, $this->user, $this->database, $this->table); + + $this->assertInstanceOf(RevokePrivileges::class, $revokeUser); + + } + + public function testIfInstanceIsFromGrantPrivileges() + { + $grantUser = $this->userManager->grantPrivileges($this->subject, $this->host, $this->user, $this->password, $this->database, $this->table); + + $this->assertInstanceOf(GrantPrivileges::class, $grantUser); + } +} \ No newline at end of file