-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
728c516
commit 3fbd362
Showing
6 changed files
with
301 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |