Skip to content

Commit

Permalink
Added unit tests for generators.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jun 10, 2024
1 parent 728c516 commit 3fbd362
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Service/CalculationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* user_margin: float,
* groups: ServiceGroupType[]}
*/
final class CalculationService implements ServiceSubscriberInterface
class CalculationService implements ServiceSubscriberInterface
{
use MathTrait;
use ServiceMethodsSubscriberTrait;
Expand Down
8 changes: 3 additions & 5 deletions tests/DatabaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ protected function deleteEntity(?EntityInterface $entity): null
*/
protected function getManager(): EntityManager
{
/** @var ManagerRegistry $registry */
/** @psalm-var ManagerRegistry $registry */
$registry = static::getContainer()->get('doctrine');

/** @var EntityManager $manager */
$manager = $registry->getManager();

return $manager;
/** @psalm-var EntityManager */
return $registry->getManager();
}
}
99 changes: 99 additions & 0 deletions tests/Generator/CalculationGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/*
* This file is part of the Calculation package.
*
* (c) bibi.nu <bibi@bibi.nu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Tests\Generator;

use App\Entity\CalculationState;
use App\Generator\AbstractEntityGenerator;
use App\Generator\CalculationGenerator;
use App\Service\CalculationService;
use App\Tests\EntityTrait\CalculationStateTrait;
use App\Tests\EntityTrait\ProductTrait;
use Doctrine\ORM\Exception\ORMException;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* @extends GeneratorTestCase<CalculationGenerator>
*/
#[CoversClass(AbstractEntityGenerator::class)]
#[CoversClass(CalculationGenerator::class)]
class CalculationGeneratorTest extends GeneratorTestCase
{
use CalculationStateTrait;
use ProductTrait;

private CalculationService $service;

protected function setUp(): void
{
parent::setUp();
$this->service = $this->getService(CalculationService::class);
$this->service->setTranslator($this->translator);
}

public function testNegativeCount(): void
{
$generator = $this->createGenerator();
$actual = $generator->generate(-1, true);
self::assertValidateResults($actual, false, 0);
}

/**
* @throws ORMException
*/
public function testNotSimulate(): void
{
$this->getCalculationState();
$product = $this->getProduct();
$product->setPrice(0.0);
$this->manager->flush();

$generator = $this->createGenerator();
$actual = $generator->generate(1, false);
self::assertValidateResults($actual, true, 1);
}

/**
* @throws ORMException
*/
public function testNotSimulateError(): void
{
$this->deleteCalculationState();
$generator = $this->createGenerator();
$actual = $generator->generate(1, false);
self::assertValidateResults($actual, false, 0);
}

public function testOne(): void
{
$generator = $this->createGenerator();
$actual = $generator->generate(1, true);
self::assertValidateResults($actual, true, 1);
}

protected function createGenerator(): CalculationGenerator
{
$generator = new CalculationGenerator($this->manager, $this->fakerService, $this->service);

return $this->updateGenerator($generator);
}

private function deleteCalculationState(): void
{
$repository = $this->manager->getRepository(CalculationState::class);
$entities = $repository->findAll();
foreach ($entities as $entity) {
$repository->remove($entity);
}
$this->manager->flush();
}
}
53 changes: 53 additions & 0 deletions tests/Generator/CustomerGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Calculation package.
*
* (c) bibi.nu <bibi@bibi.nu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Tests\Generator;

use App\Generator\AbstractEntityGenerator;
use App\Generator\CustomerGenerator;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* @extends GeneratorTestCase<CustomerGenerator>
*/
#[CoversClass(AbstractEntityGenerator::class)]
#[CoversClass(CustomerGenerator::class)]
class CustomerGeneratorTest extends GeneratorTestCase
{
public function testNegativeCount(): void
{
$generator = $this->createGenerator();
$actual = $generator->generate(-1, true);
self::assertValidateResults($actual, false, 0);
}

public function testNotSimulate(): void
{
$generator = $this->createGenerator();
$actual = $generator->generate(1, false);
self::assertValidateResults($actual, true, 1);
}

public function testOne(): void
{
$generator = $this->createGenerator();
$actual = $generator->generate(1, true);
self::assertValidateResults($actual, true, 1);
}

protected function createGenerator(): CustomerGenerator
{
$generator = new CustomerGenerator($this->manager, $this->fakerService);

return $this->updateGenerator($generator);
}
}
84 changes: 84 additions & 0 deletions tests/Generator/GeneratorTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/*
* This file is part of the Calculation package.
*
* (c) bibi.nu <bibi@bibi.nu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Tests\Generator;

use App\Generator\AbstractEntityGenerator;
use App\Service\FakerService;
use App\Tests\DatabaseTrait;
use App\Tests\KernelServiceTestCase;
use App\Utils\StringUtils;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @template TGenerator of AbstractEntityGenerator
*/
abstract class GeneratorTestCase extends KernelServiceTestCase
{
use DatabaseTrait;

protected FakerService $fakerService;
protected LoggerInterface $logger;
protected EntityManagerInterface $manager;
protected TranslatorInterface $translator;

protected function setUp(): void
{
parent::setUp();
$this->logger = $this->createMock(LoggerInterface::class);
$this->translator = $this->createMock(TranslatorInterface::class);
$this->manager = $this->getService(EntityManagerInterface::class);
$this->fakerService = $this->getService(FakerService::class);
}

protected static function assertValidateResults(JsonResponse $actual, bool $expected, int $count): array
{
$content = $actual->getContent();
self::assertIsString($content);

$actual = StringUtils::decodeJson($content);
self::assertArrayHasKey('result', $actual);
self::assertSame($expected, $actual['result']);
if ($count <= 0) {
return $actual;
}

self::assertArrayHasKey('count', $actual);
self::assertSame($count, $actual['count']);
self::assertArrayHasKey('items', $actual);
self::assertIsArray($actual['items']);
self::assertCount($count, $actual['items']);

return $actual;
}

/**
* @psalm-return TGenerator
*/
abstract protected function createGenerator(): AbstractEntityGenerator;

/**
* @psalm-param TGenerator $generator
*
* @psalm-return TGenerator
*/
protected function updateGenerator(AbstractEntityGenerator $generator): AbstractEntityGenerator
{
$generator->setTranslator($this->translator);
$generator->setLogger($this->logger);

return $generator;
}
}
61 changes: 61 additions & 0 deletions tests/Generator/ProductGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/*
* This file is part of the Calculation package.
*
* (c) bibi.nu <bibi@bibi.nu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Tests\Generator;

use App\Generator\AbstractEntityGenerator;
use App\Generator\ProductGenerator;
use App\Tests\EntityTrait\CategoryTrait;
use Doctrine\ORM\Exception\ORMException;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* @extends GeneratorTestCase<ProductGenerator>
*/
#[CoversClass(AbstractEntityGenerator::class)]
#[CoversClass(ProductGenerator::class)]
class ProductGeneratorTest extends GeneratorTestCase
{
use CategoryTrait;

public function testNegativeCount(): void
{
$generator = $this->createGenerator();
$actual = $generator->generate(-1, true);
self::assertValidateResults($actual, false, 0);
}

/**
* @throws ORMException
*/
public function testNotSimulate(): void
{
$this->getCategory();
$generator = $this->createGenerator();
$actual = $generator->generate(1, false);
self::assertValidateResults($actual, true, 1);
}

public function testOne(): void
{
$generator = $this->createGenerator();
$actual = $generator->generate(1, true);
self::assertValidateResults($actual, true, 1);
}

protected function createGenerator(): ProductGenerator
{
$generator = new ProductGenerator($this->manager, $this->fakerService);

return $this->updateGenerator($generator);
}
}

0 comments on commit 3fbd362

Please sign in to comment.