Skip to content

Commit

Permalink
Added report unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jun 8, 2024
1 parent b603713 commit 9563d42
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/Controller/AboutControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
use App\Controller\AboutPhpController;
use App\Controller\AboutPolicyController;
use App\Controller\AboutSymfonyController;
use App\Report\HtmlReport;
use App\Report\MySqlReport;
use App\Report\PhpIniReport;
use App\Report\SymfonyReport;
use App\Spreadsheet\MySqlDocument;
use App\Spreadsheet\PhpIniDocument;
use App\Spreadsheet\SymfonyDocument;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -27,6 +34,13 @@
#[CoversClass(AboutPhpController::class)]
#[CoversClass(AboutPolicyController::class)]
#[CoversClass(AboutSymfonyController::class)]
#[CoversClass(MySqlReport::class)]
#[CoversClass(MySqlDocument::class)]
#[CoversClass(PhpIniReport::class)]
#[CoversClass(PhpIniDocument::class)]
#[CoversClass(SymfonyReport::class)]
#[CoversClass(SymfonyDocument::class)]
#[CoversClass(HtmlReport::class)]
class AboutControllerTest extends AbstractControllerTestCase
{
public static function getRoutes(): \Iterator
Expand Down
2 changes: 2 additions & 0 deletions tests/Controller/HelpControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
namespace App\Tests\Controller;

use App\Controller\HelpController;
use App\Report\HelpReport;
use App\Service\HelpService;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Finder\Finder;

#[CoversClass(HelpController::class)]
#[CoversClass(HelpReport::class)]
class HelpControllerTest extends AbstractControllerTestCase
{
private const IMAGES_PATH = 'public/help/images';
Expand Down
2 changes: 2 additions & 0 deletions tests/Controller/TestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
namespace App\Tests\Controller;

use App\Controller\TestController;
use App\Report\HtmlReport;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\HttpFoundation\Response;

#[CoversClass(TestController::class)]
#[CoversClass(HtmlReport::class)]
class TestControllerTest extends AbstractControllerTestCase
{
private const ROUTES = [
Expand Down
43 changes: 43 additions & 0 deletions tests/Report/CalculationStatesReportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\Report;

use App\Controller\AbstractController;
use App\Entity\CalculationState;
use App\Report\CalculationStatesReport;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;

#[CoversClass(CalculationStatesReport::class)]
class CalculationStatesReportTest extends TestCase
{
/**
* @throws Exception
*/
public function testRender(): void
{
$controller = $this->createMock(AbstractController::class);

$state1 = new CalculationState();
$state1->setCode('Code1');

$state2 = new CalculationState();
$state2->setCode('Code2')
->setColor('');

$report = new CalculationStatesReport($controller, [$state1, $state2]);
$actual = $report->render();
self::assertTrue($actual);
}
}
95 changes: 95 additions & 0 deletions tests/Report/CommandsReportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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\Report;

use App\Controller\AbstractController;
use App\Report\CommandsReport;
use App\Service\CommandService;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;

/**
* @psalm-import-type CommandType from CommandService
*/
#[CoversClass(CommandsReport::class)]
class CommandsReportTest extends TestCase
{
/**
* @throws Exception
*
* @psalm-suppress InvalidArgument
*/
public function testRender(): void
{
$controller = $this->createMock(AbstractController::class);

$argument1 = [
'name' => 'Argument',
'is_required' => false,
'is_array' => false,
'description' => 'Description',
'display' => 'Display',
'arguments' => 'Arguments',
];
$argument2 = [
'name' => 'Argument',
'is_required' => false,
'is_array' => false,
'description' => '',
'display' => '',
'arguments' => '',
];
$option = [
'name' => 'Option',
'shortcut' => 'Shortcut',
'name_shortcut' => 'Name Shortcut',
'accept_value' => true,
'is_value_required' => true,
'is_multiple' => true,
'description' => 'Description',
'default' => 'Default',
'display' => 'Display',
'arguments' => 'Arguments',
];
$command1 = [
'name' => 'Command1',
'description' => 'Description',
'usage' => ['Usage 1', 'Usage 2'],
'help' => '<a href="https://symfony.com>storing-uuids-in-databases">database</a>. The <span class="info">list</span>.',
'hidden' => false,
'definition' => [
'arguments' => [
'Argument1' => $argument1,
'Argument2' => $argument2,
],
'options' => ['Option' => $option],
],
];
$command2 = [
'name' => 'Command2',
'description' => '',
'usage' => [],
'help' => '',
'hidden' => false,
'definition' => [
'arguments' => [],
'options' => [],
],
];
$commands = [$command1, $command2];
$report = new CommandsReport($controller, ['Group' => $commands]);
$actual = $report->render();
self::assertTrue($actual);
}
}
66 changes: 66 additions & 0 deletions tests/Report/CustomersReportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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\Report;

use App\Controller\AbstractController;
use App\Entity\Customer;
use App\Report\CustomersReport;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;

#[CoversClass(CustomersReport::class)]
class CustomersReportTest extends TestCase
{
/**
* @throws Exception
*/
public function testRenderGrouped(): void
{
$controller = $this->createMock(AbstractController::class);
$customer1 = new Customer();
$customer1->setFirstName('First Name')
->setLastName('A Last Name')
->setEmail('email@email.com');

$customer2 = new Customer();
$customer2->setFirstName('First Name')
->setLastName('B Last Name')
->setEmail('email@email.com');

$customer3 = new Customer();
$customer3->setFirstName('')
->setLastName('')
->setEmail('email@email.com');

$report = new CustomersReport($controller, [$customer1, $customer2, $customer3]);
$actual = $report->render();
self::assertTrue($actual);
}

/**
* @throws Exception
*/
public function testRenderNotGrouped(): void
{
$controller = $this->createMock(AbstractController::class);
$customer = new Customer();
$customer->setFirstName('First Name')
->setLastName('Last Name')
->setEmail('email@email.com');

$report = new CustomersReport($controller, [$customer], false);
$actual = $report->render();
self::assertTrue($actual);
}
}
59 changes: 59 additions & 0 deletions tests/Report/MemoryImageReportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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\Report;

use App\Controller\AbstractController;
use App\Report\MemoryImageReport;
use fpdf\PdfException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;

#[CoversClass(MemoryImageReport::class)]
class MemoryImageReportTest extends TestCase
{
/**
* @throws Exception
*/
public function testEmptyImage(): void
{
self::expectException(PdfException::class);
$controller = $this->createMock(AbstractController::class);
$image = __DIR__ . '/../Data/empty.txt';
$report = new MemoryImageReport($controller, $image);
$report->render();
}

/**
* @throws Exception
*/
public function testInvalidImage(): void
{
self::expectException(PdfException::class);
$controller = $this->createMock(AbstractController::class);
$report = new MemoryImageReport($controller, __FILE__);
$report->render();
}

/**
* @throws Exception
*/
public function testRender(): void
{
$controller = $this->createMock(AbstractController::class);
$image = __DIR__ . '/../Data/images/example.png';
$report = new MemoryImageReport($controller, $image);
$actual = $report->render();
self::assertTrue($actual);
}
}

0 comments on commit 9563d42

Please sign in to comment.