Refactor some of the testing, try and understand why the mocking for the DB Handler is returning null and not true

This commit is contained in:
Dave Smith-Hayes 2024-11-18 01:47:18 +00:00
parent 811912784b
commit 1b14f1523b
4 changed files with 31 additions and 24 deletions

12
app/composer.lock generated
View File

@ -3928,16 +3928,16 @@
},
{
"name": "twig/twig",
"version": "v3.14.2",
"version": "v3.15.0",
"source": {
"type": "git",
"url": "https://github.com/twigphp/Twig.git",
"reference": "0b6f9d8370bb3b7f1ce5313ed8feb0fafd6e399a"
"reference": "2d5b3964cc21d0188633d7ddce732dc8e874db02"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twigphp/Twig/zipball/0b6f9d8370bb3b7f1ce5313ed8feb0fafd6e399a",
"reference": "0b6f9d8370bb3b7f1ce5313ed8feb0fafd6e399a",
"url": "https://api.github.com/repos/twigphp/Twig/zipball/2d5b3964cc21d0188633d7ddce732dc8e874db02",
"reference": "2d5b3964cc21d0188633d7ddce732dc8e874db02",
"shasum": ""
},
"require": {
@ -3991,7 +3991,7 @@
],
"support": {
"issues": "https://github.com/twigphp/Twig/issues",
"source": "https://github.com/twigphp/Twig/tree/v3.14.2"
"source": "https://github.com/twigphp/Twig/tree/v3.15.0"
},
"funding": [
{
@ -4003,7 +4003,7 @@
"type": "tidelift"
}
],
"time": "2024-11-07T12:36:22+00:00"
"time": "2024-11-17T15:59:19+00:00"
},
{
"name": "vlucas/phpdotenv",

View File

@ -11,7 +11,7 @@ use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface;
class UserRepository implements UserRepositoryInterface
{
const INSERT_QUERY = "INSERT INTO users (email, password, name)
const CREATE_QUERY = "INSERT INTO users (email, password, name)
VALUES (:email, :password, :name)";
public function __construct(
@ -70,7 +70,7 @@ class UserRepository implements UserRepositoryInterface
public function create(User $user): bool
{
$results = $this->db->insert(self::INSERT_QUERY, [
$results = $this->db->insert(self::CREATE_QUERY, [
':email' => $user->getEmail(),
':password' => $this->userAuth->hash($user->getPassword()),
':name' => $user->getName(),

View File

@ -3,37 +3,24 @@
namespace Slovocast\Tests\Domain\Repository;
use Slovocast\Domain\Repository\User\UserRepository;
use Slovocast\Domain\Entity\User;
use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface;
use Slovocast\Infrastructure\User\BasicUserAuthorization;
use Slovocast\Tests\TestCase;
class UserRepositoryTest extends TestCase
{
protected function getUser(): User
{
return User::fromArray([
'email' => 'dave@slovocast.com',
'name' => 'Dave SH',
'password' => 'hashed_password'
]);
}
public function testRegisteringAUser(): void
{
$user = $this->getUser();
$user = $this->getUserFromArray();
$databaseHandler = $this->prophesize(DatabaseHandlerInterface::class);
$databaseHandler->insert()->willReturn(true);
$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(),

View File

@ -11,6 +11,7 @@ use Slim\Psr7\Headers;
use Slim\Psr7\Request as SlimRequest;
use Slim\Psr7\Uri;
use Slovocast\Bootstrap;
use Slovocast\Domain\Entity\User;
/**
* This is the common test harness pattern lifted right out of the official
@ -60,4 +61,23 @@ class TestCase extends PHPUnit_TestCase
$stream
);
}
/**
* Quickly generate a User new User entity for testing.
*
* @param array $user The User properties to call User:fromArray on
* @return User a new User entity
*/
protected function getUserFromArray(array $user = []): User
{
if (empty($user)) {
$user = [
'email' => 'dave@slovocast.com',
'name' => 'Dave SH',
'password' => 'hashed_password'
];
}
return User::fromArray($user);
}
}