slovocast/app/tests/Domain/Repository/UserRepositoryTest.php

84 lines
2.6 KiB
PHP

<?php
namespace Slovocast\Tests\Domain\Repository;
use Slovocast\Domain\Repository\User\UserRepository;
use Slovocast\Exception\EntityNotFoundException;
use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface;
use Slovocast\Infrastructure\User\BasicUserAuthorization;
use Slovocast\Tests\TestCase;
use Prophecy\Argument;
class UserRepositoryTest extends TestCase
{
public function testRegisteringAUser(): void
{
$user = $this->getUserFromArray();
$databaseHandler = $this->prophesize(DatabaseHandlerInterface::class);
$databaseHandler
->execute(Argument::type('string'), Argument::type('array'))
->willReturn(true);
$databaseHandler
->getConnection()
->willReturn(new class {
public function lastInsertId(): int
{
return 100;
}
});
$userRepository = new UserRepository(
$databaseHandler->reveal(),
new BasicUserAuthorization()
);
/**
* We are testing if the User entity has been mutated by the `create`
* method in the UserRepository.
*/
$results = $userRepository->create($user);
$this->assertTrue($results);
$this->assertEquals(100, $user->getId());
$user->setName("Dave Smith-Hayes");
$results = $userRepository->update($user);
$this->assertTrue($results);
$this->assertEquals(
$user->getUpdatedAt()->format("dmY"),
(new \DateTime())->format("dmY")
);
}
public function testGettingUsers(): void
{
$hasher = new BasicUserAuthorization();
$password = "password";
$hashedPassword = $hasher->hash($password);
$databaseHandler = $this->prophesize(DatabaseHandlerInterface::class);
$databaseHandler
->query(Argument::type('string'), Argument::type('array'))
->willReturn([
'id' => 1,
'name' => 'Dave Test',
'email' => 'dave.test@email',
'password' => $hashedPassword,
'created_at' => '2024-11-17T00:00:00',
'updated_at' => '2024-11-17T00:00:00'
], []);
$userRepository = new UserRepository(
$databaseHandler->reveal(),
$hasher
);
$user = $userRepository->get(1);
$this->assertEquals("Dave Test", $user->getName());
$this->expectException(EntityNotFoundException::class);
$userRepository->get(2);
}
}