Skip to content

Commit

Permalink
Added Faker unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jun 9, 2024
1 parent 9563d42 commit 728c516
Show file tree
Hide file tree
Showing 15 changed files with 846 additions and 7 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Faker/ProductProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public function productName(): string
*/
public function products(int $count = 1, bool $allowDuplicates = false): array
{
// products?
if (0 === $this->productsCount()) {
return [];
}

/** @var Product[] $products */
$products = static::randomElements($this->getEntities(), $count, $allowDuplicates);
if (\count($products) < 2) {
Expand Down
2 changes: 1 addition & 1 deletion src/Service/PhpInfoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Utility class to get PHP information.
*/
final class PhpInfoService
class PhpInfoService
{
/**
* The replaced string for sensitive parameters.
Expand Down
116 changes: 116 additions & 0 deletions tests/Controller/HtmlReportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?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\Controller;

use App\Controller\AbstractController;
use App\Report\HtmlReport;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;

#[CoversClass(HtmlReport::class)]
class HtmlReportTest extends TestCase
{
/**
* @throws Exception
*/
public function testRender(): void
{
$html = <<<HTML
<html>
<body>
<i>Test</i>
<br>
<div>Text</div><
</body>
</html>
HTML;
$controller = $this->createMock(AbstractController::class);
$report = new HtmlReport($controller, $html);
$report->addPage();
$report->updateLeftMargin($report->getLeftMargin());
$report->updateRightMargin($report->getRightMargin());
$report->setLeftMargin(0.0);
$report->setRightMargin(0.0);
$report->addPage();
$report->setLeftMargin(20.0);
$report->setRightMargin(20.0);
$report->addPage();
$actual = $report->render();

self::assertTrue($actual);
}

/**
* @throws Exception
*/
public function testRenderEmpty(): void
{
$controller = $this->createMock(AbstractController::class);
$report = new HtmlReport($controller, '');
$actual = $report->render();
self::assertFalse($actual);
}

/**
* @throws Exception
*/
public function testRenderList(): void
{
$html = <<<HTML
<html>
<body>
<ul>
<li>Milk</li>
<li>
Cheese
<ul>
<li>Blue cheese</li>
<li>Feta</li>
</ul>
</li>
</ul>
<ol>
<li>Mix flour, baking powder, sugar, and salt.</li>
<li>In another bowl, mix eggs, milk, and oil.</li>
<li>Stir both mixtures together.</li>
<li>Fill muffin tray 3/4 full.</li>
<li>Bake for 20 minutes.
<ol>
<li>Stir both mixtures together.</li>
<li>Fill muffin tray 3/4 full.</li>
</ol>
</li>
</ol>
</body>
</html>
HTML;

$controller = $this->createMock(AbstractController::class);
$report = new HtmlReport($controller, $html);
$report->addPage();
$actual = $report->render();
self::assertTrue($actual);
}

/**
* @throws Exception
*/
public function testRenderSpace(): void
{
$controller = $this->createMock(AbstractController::class);
$report = new HtmlReport($controller, ' ');
$actual = $report->render();
self::assertFalse($actual);
}
}
2 changes: 2 additions & 0 deletions tests/Controller/SchemaControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
namespace App\Tests\Controller;

use App\Controller\SchemaController;
use App\Report\SchemaReport;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\HttpFoundation\Response;

#[CoversClass(SchemaController::class)]
#[CoversClass(SchemaReport::class)]
class SchemaControllerTest extends AbstractControllerTestCase
{
public static function getRoutes(): \Iterator
Expand Down
84 changes: 84 additions & 0 deletions tests/Faker/CalculationStateProviderTest.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\Faker;

use App\Entity\CalculationState;
use App\Faker\CalculationStateProvider;
use App\Faker\EntityProvider;
use App\Faker\Factory;
use App\Repository\UserRepository;
use App\Utils\FormatUtils;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;

#[CoversClass(EntityProvider::class)]
#[CoversClass(CalculationStateProvider::class)]
class CalculationStateProviderTest extends TestCase
{
/**
* @throws Exception
*/
public function testWithEntity(): void
{
$entity = new CalculationState();
$entity->setCode('code');
$provider = $this->createProvider($entity);

$actual = $provider->statesCount();
self::assertSame(1, $actual);

$actual = $provider->state();
self::assertSame($entity, $actual);
}

/**
* @throws Exception
*/
public function testWithoutEntity(): void
{
$provider = $this->createProvider();

$actual = $provider->statesCount();
self::assertSame(0, $actual);

$actual = $provider->state();
self::assertNull($actual);
}

/**
* @throws Exception
*/
private function createProvider(?CalculationState $entity = null): CalculationStateProvider
{
$entities = $entity instanceof CalculationState ? [$entity] : [];
$repository = $this->createMock(UserRepository::class);
$repository->expects(self::any())
->method('findBy')
->willReturn($entities);

$repository->expects(self::any())
->method('findAll')
->willReturn($entities);

$manager = $this->createMock(EntityManagerInterface::class);
$manager->expects(self::any())
->method('getRepository')
->willReturn($repository);

$generator = Factory::create(FormatUtils::DEFAULT_LOCALE);

return new CalculationStateProvider($generator, $manager);
}
}
84 changes: 84 additions & 0 deletions tests/Faker/CategoryProviderTest.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\Faker;

use App\Entity\Category;
use App\Faker\CategoryProvider;
use App\Faker\EntityProvider;
use App\Faker\Factory;
use App\Repository\CategoryRepository;
use App\Utils\FormatUtils;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;

#[CoversClass(EntityProvider::class)]
#[CoversClass(CategoryProvider::class)]
class CategoryProviderTest extends TestCase
{
/**
* @throws Exception
*/
public function testWithEntity(): void
{
$entity = new Category();
$entity->setCode('code');
$provider = $this->createProvider($entity);

$actual = $provider->categoriesCount();
self::assertSame(1, $actual);

$actual = $provider->category();
self::assertSame($entity, $actual);
}

/**
* @throws Exception
*/
public function testWithoutEntity(): void
{
$provider = $this->createProvider();

$actual = $provider->categoriesCount();
self::assertSame(0, $actual);

$actual = $provider->category();
self::assertNull($actual);
}

/**
* @throws Exception
*/
private function createProvider(?Category $entity = null): CategoryProvider
{
$entities = $entity instanceof Category ? [$entity] : [];
$repository = $this->createMock(CategoryRepository::class);
$repository->expects(self::any())
->method('findBy')
->willReturn($entities);

$repository->expects(self::any())
->method('findAll')
->willReturn($entities);

$manager = $this->createMock(EntityManagerInterface::class);
$manager->expects(self::any())
->method('getRepository')
->willReturn($repository);

$generator = Factory::create(FormatUtils::DEFAULT_LOCALE);

return new CategoryProvider($generator, $manager);
}
}
31 changes: 31 additions & 0 deletions tests/Faker/CustomAddressTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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\Faker;

use App\Faker\CustomAddress;
use App\Faker\Factory;
use App\Utils\FormatUtils;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(CustomAddress::class)]
class CustomAddressTest extends TestCase
{
public function testCustomAddress(): void
{
$generator = Factory::create(FormatUtils::DEFAULT_LOCALE);
$customAddress = new CustomAddress($generator);
$actual = $customAddress->zipCity();
self::assertNotEmpty($actual);
}
}
Loading

0 comments on commit 728c516

Please sign in to comment.