iteo/tests/Behat/ClientsContext.php
2024-04-14 21:49:23 +02:00

100 lines
2.6 KiB
PHP

<?php
namespace App\Tests\Behat;
use App\Entity\Client;
use App\Repository\ClientRepository;
use Behat\Behat\Context\Context;
use Behat\Behat\Tester\Exception\PendingException;
use Behat\Gherkin\Node\TableNode;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Assert;
use Symfony\Component\Uid\Uuid;
class ClientsContext implements Context
{
private array $clients;
public function __construct(
protected readonly ClientRepository $clientRepository,
) {}
/**
* @BeforeScenario
*/
public function clean()
{
$this->clientRepository
->createQueryBuilder('c')
->delete()
->getQuery()
->execute();
$this->clients = [];
}
/**
* @Given /^there exist following clients:$/
*/
public function thereExistFollowingClients(TableNode $table)
{
foreach ($table as $row) {
$client = $this->getClient($row['clientId']);
if (!$client) {
$client = new Client(
clientId: Uuid::fromRfc4122($row['clientId']),
name: $row['name'],
initialBalance: intval($row['initialBalance']),
currentBalance: intval($row['currentBalance']),
lockedAt: $row['locked'] === 'yes' ? new \DateTimeImmutable() : null,
);
$this->clientRepository->save($client);
}
$this->clients[$client->getClientId()->toRfc4122()] = $client;
}
}
public function getClient(string $id): ?Client
{
return $this->clients[$id] ??= $this->clientRepository->find(Uuid::fromRfc4122($id));
}
/**
* @Given /^client with id "([^"]+)" should exist$/
*/
public function clientWithIdShouldExist(string $clientId)
{
Assert::assertNotNull($this->getClient($clientId));
}
/**
* @Given /^client with id "([^"]+)" should be locked$/
*/
public function clientWithIdIsLocked(string $clientId)
{
Assert::assertTrue($this->getClient($clientId)->isLocked());
}
/**
* @Given /^client with id "([^"]+)" should not be locked$/
*/
public function clientWithIdIsNotLocked(string $clientId)
{
Assert::assertFalse($this->getClient($clientId)->isLocked());
}
/**
* @Given /^client with id "([^"]+)" should have balance of (\d+)$/
*/
public function clientWithIdShouldHaveBalanceOf(string $clientId, int $balance)
{
$client = $this->getClient($clientId);
Assert::assertEquals($balance, $client->getBalance());
}
}