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

View File

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

View File

@ -3,37 +3,24 @@
namespace Slovocast\Tests\Domain\Repository; namespace Slovocast\Tests\Domain\Repository;
use Slovocast\Domain\Repository\User\UserRepository; use Slovocast\Domain\Repository\User\UserRepository;
use Slovocast\Domain\Entity\User;
use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface;
use Slovocast\Infrastructure\User\BasicUserAuthorization; use Slovocast\Infrastructure\User\BasicUserAuthorization;
use Slovocast\Tests\TestCase; use Slovocast\Tests\TestCase;
class UserRepositoryTest extends 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 public function testRegisteringAUser(): void
{ {
$user = $this->getUser(); $user = $this->getUserFromArray();
$databaseHandler = $this->prophesize(DatabaseHandlerInterface::class); $databaseHandler = $this->prophesize(DatabaseHandlerInterface::class);
$databaseHandler->insert()->willReturn(true);
$databaseHandler->getConnection()->willReturn(new class { $databaseHandler->getConnection()->willReturn(new class {
public function lastInsertId(): int public function lastInsertId(): int
{ {
return 100; return 100;
} }
}); });
$databaseHandler->insert(UserRepository::INSERT_QUERY, [
':name' => $user->getName(),
':email' => $user->getEmail(),
':password' => $user->getPassword()
])->willReturn(true);
$userRepository = new UserRepository( $userRepository = new UserRepository(
$databaseHandler->reveal(), $databaseHandler->reveal(),

View File

@ -11,6 +11,7 @@ use Slim\Psr7\Headers;
use Slim\Psr7\Request as SlimRequest; use Slim\Psr7\Request as SlimRequest;
use Slim\Psr7\Uri; use Slim\Psr7\Uri;
use Slovocast\Bootstrap; use Slovocast\Bootstrap;
use Slovocast\Domain\Entity\User;
/** /**
* This is the common test harness pattern lifted right out of the official * This is the common test harness pattern lifted right out of the official
@ -60,4 +61,23 @@ class TestCase extends PHPUnit_TestCase
$stream $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);
}
} }