diff --git a/app/src/Domain/Repository/User/UserRepository.php b/app/src/Domain/Repository/User/UserRepository.php index 0fae9bb..6a86f75 100644 --- a/app/src/Domain/Repository/User/UserRepository.php +++ b/app/src/Domain/Repository/User/UserRepository.php @@ -11,6 +11,9 @@ use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; class UserRepository implements UserRepositoryInterface { + const INSERT_QUERY = "INSERT INTO users (email, password, name) + VALUES (:email, :password, :name)"; + public function __construct( private DatabaseHandlerInterface $db, private UserAuthorizationInterface $userAuth @@ -56,9 +59,7 @@ class UserRepository implements UserRepositoryInterface public function getFromEmail(string $email): User { $query = "SELECT * FROM users WHERE email = :email LIMIT 1"; - $statement = $this->db->getConnection()->prepare($query); - $statement->execute([ ':email' => $email ]); - $results = $statement->fetch(\PDO::FETCH_ASSOC); + $results = $this->db->query($query, [ ':email' => $email ]); if (!is_array($results) || count($results)) { throw new EntityNotFoundException("Unable to find User"); @@ -69,11 +70,7 @@ class UserRepository implements UserRepositoryInterface public function create(User $user): bool { - $query = "INSERT INTO users (email, password, name) - VALUES (:email, :password, :name)"; - - $statement = $this->db->getConnection()->prepare($query); - $results = $statement->execute([ + $results = $this->db->insert(self::INSERT_QUERY, [ ':email' => $user->getEmail(), ':password' => $this->userAuth->hash($user->getPassword()), ':name' => $user->getName(), @@ -97,8 +94,7 @@ class UserRepository implements UserRepositoryInterface password = :password WHERE id = :id"; - $statement = this->db->prepare($query); - $results = $statement->execute([ + $results = $this->db->query($query, [ $user->getEmail(), $user->getName(), $this->userAuth->hash($user->getPassword()), diff --git a/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php b/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php index 494eece..594d488 100644 --- a/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php +++ b/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php @@ -7,5 +7,24 @@ use PDO; interface DatabaseHandlerInterface { public function getConnection(): PDO; + + /** + * Get the connection string required for establishing connections to the + * database + */ public function getDsn(): string; + + /** + * @param string $query + * @param array $params Either a list of params of the KV params + * @return array The results as an array, empty if no results + */ + public function query(string $query, array $params = []): array; + + /** + * @param string $query + * @param array $params Either a list of params, or the KV of params + * @return bool True if successful + */ + public function insert(string $query, array $params = []): bool; } diff --git a/app/src/Infrastructure/Database/DatabaseHandler.php b/app/src/Infrastructure/Database/DatabaseHandler.php index a1270ff..2835a0c 100644 --- a/app/src/Infrastructure/Database/DatabaseHandler.php +++ b/app/src/Infrastructure/Database/DatabaseHandler.php @@ -38,4 +38,17 @@ class DatabaseHandler implements DatabaseHandlerInterface { return $this->database; } + + public function query(string $query, array $params = []): array + { + $statement = $this->database->prepare($query); + $statement->execute($params); + return $statement->fetchAll($params); + } + + public function insert(string $query, array $params = []): bool + { + $statement = $this->database->prepare($query); + return $statement->execute($params); + } } diff --git a/app/tests/Domain/Repository/UserRepositoryTest.php b/app/tests/Domain/Repository/UserRepositoryTest.php new file mode 100644 index 0000000..2e0bf43 --- /dev/null +++ b/app/tests/Domain/Repository/UserRepositoryTest.php @@ -0,0 +1,47 @@ + 'dave@slovocast.com', + 'name' => 'Dave SH', + 'password' => 'hashed_password' + ]); + } + + public function testRegisteringAUser(): void + { + $user = $this->getUser(); + $databaseHandler = $this->prophesize(DatabaseHandlerInterface::class); + $databaseHandler->getConnection()->willReturn(new class { + public function lastInsertId(): int + { + return 100; + } + }); + $databaseHandler->insert(UserRepository::INSERT_QUERY, [ + ':name' => $user->getName(), + ':email' => $user->getEmail(), + ':password' => $user->getPassword() + ])->willReturn(true); + + $userRepository = new UserRepository( + $databaseHandler->reveal(), + new BasicUserAuthorization() + ); + + $results = $userRepository->create($user); + $this->assertTrue($results); + $this->assertEquals(100, $user->getId()); + } +}