Skip to content

Commit

Permalink
Unit tests for services.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jun 20, 2024
1 parent 342f742 commit ef013e9
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 9 deletions.
18 changes: 9 additions & 9 deletions composer.lock

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

Binary file added tests/Data/swiss_test.sqlite
Binary file not shown.
22 changes: 22 additions & 0 deletions tests/Service/AkismetServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ public function testActivitySuccess(): void
self::assertIsArray($actual);
}

/**
* @throws Exception
*/
public function testEmptyKey(): void
{
$key = '';
$cache = new ArrayAdapter();
$logger = $this->createMock(LoggerInterface::class);
$security = $this->createMock(Security::class);
$translator = $this->createMockTranslator();

self::expectException(\InvalidArgumentException::class);
new AkismetService(
$key,
$cache,
$logger,
$security,
$this->requestStack,
$translator
);
}

/**
* @throws ExceptionInterface|Exception
*/
Expand Down
91 changes: 91 additions & 0 deletions tests/Service/RoleHierarchyServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?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\Service;

use App\Entity\User;
use App\Interfaces\RoleInterface;
use App\Model\Role;
use App\Service\RoleHierarchyService;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;

#[CoversClass(RoleHierarchyService::class)]
class RoleHierarchyServiceTest extends TestCase
{
/**
* @throws Exception
*/
public function testGetRoleNamesWithHierarchy(): void
{
$user = new User();
$hierarchy = $this->createMock(RoleHierarchyInterface::class);
$hierarchy->method('getReachableRoleNames')
->willReturn([RoleInterface::ROLE_ADMIN]);

$service = new RoleHierarchyService($hierarchy);
$actual = $service->getReachableRoleNames($user);
self::assertCount(1, $actual);
self::assertSame([RoleInterface::ROLE_ADMIN], $actual);
}

/**
* @throws Exception
*/
public function testGetRoleNamesWithNull(): void
{
$hierarchy = $this->createMock(RoleHierarchyInterface::class);
$service = new RoleHierarchyService($hierarchy);
$actual = $service->getRoleNames(null);
self::assertCount(0, $actual);
}

/**
* @throws Exception
*/
public function testGetRoleNamesWithRole(): void
{
$role = new Role('Fake');
$role->setRole(RoleInterface::ROLE_ADMIN);
$hierarchy = $this->createMock(RoleHierarchyInterface::class);
$service = new RoleHierarchyService($hierarchy);
$actual = $service->getRoleNames($role);
self::assertCount(1, $actual);
self::assertSame([RoleInterface::ROLE_ADMIN], $actual);
}

/**
* @throws Exception
*/
public function testGetRoleNamesWithUser(): void
{
$user = new User();
$hierarchy = $this->createMock(RoleHierarchyInterface::class);
$service = new RoleHierarchyService($hierarchy);
$actual = $service->getRoleNames($user);
self::assertCount(1, $actual);
self::assertSame([RoleInterface::ROLE_USER], $actual);
}

/**
* @throws Exception
*/
public function testHasRoleWithNull(): void
{
$hierarchy = $this->createMock(RoleHierarchyInterface::class);
$service = new RoleHierarchyService($hierarchy);
$actual = $service->hasRole(null, RoleInterface::ROLE_USER);
self::assertFalse($actual);
}
}
136 changes: 136 additions & 0 deletions tests/Service/SwissPostServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?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\Service;

use App\Service\SwissPostService;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(SwissPostService::class)]
class SwissPostServiceTest extends TestCase
{
private string $databaseName;
private SwissPostService $service;

protected function setUp(): void
{
$this->databaseName = __DIR__ . '/../Data/swiss_test.sqlite';
$this->service = new SwissPostService($this->databaseName);
}

public function testCount(): void
{
$actual = $this->service
->getTablesCount();
self::assertCount(3, $actual);
self::assertArrayHasKey('state', $actual);
self::assertArrayHasKey('city', $actual);
self::assertArrayHasKey('street', $actual);
self::assertSame(1, $actual['state']);
self::assertSame(1, $actual['city']);
self::assertSame(55, $actual['street']);
}

public function testFindAllFound(): void
{
$actual = $this->service
->findAll('1753');
self::assertCount(25, $actual);
self::assertIsArray($actual[0]);

$actual = $actual[0];
self::assertArrayHasKey('street', $actual);
self::assertArrayHasKey('zip', $actual);
self::assertArrayHasKey('city', $actual);
self::assertArrayHasKey('state', $actual);
self::assertArrayHasKey('display', $actual);
}

public function testFindAllNotFound(): void
{
$actual = $this->service
->findAll('fake');
self::assertCount(0, $actual);
}

public function testFindCityFound(): void
{
$actual = $this->service
->findCity('matran');
self::assertCount(1, $actual);
self::assertIsArray($actual[0]);

$actual = $actual[0];
self::assertArrayHasKey('name', $actual);
self::assertArrayHasKey('zip', $actual);
self::assertArrayHasKey('state', $actual);
self::assertArrayHasKey('display', $actual);
}

public function testFindCityNotFound(): void
{
$actual = $this->service
->findCity('fake');
self::assertCount(0, $actual);
}

public function testFindStreetFound(): void
{
$actual = $this->service
->findStreet('route du bois');
self::assertCount(1, $actual);
self::assertIsArray($actual[0]);

$actual = $actual[0];
self::assertArrayHasKey('street', $actual);
self::assertArrayHasKey('zip', $actual);
self::assertArrayHasKey('city', $actual);
self::assertArrayHasKey('state', $actual);
self::assertArrayHasKey('display', $actual);
}

public function testFindStreetNotFound(): void
{
$actual = $this->service
->findStreet('fake');
self::assertCount(0, $actual);
}

public function testFindZipFound(): void
{
$actual = $this->service
->findZip('1753');
self::assertCount(1, $actual);
self::assertIsArray($actual[0]);

$actual = $actual[0];
self::assertArrayHasKey('zip', $actual);
self::assertArrayHasKey('city', $actual);
self::assertArrayHasKey('state', $actual);
self::assertArrayHasKey('display', $actual);
}

public function testFindZipNotFound(): void
{
$actual = $this->service
->findZip('fake');
self::assertCount(0, $actual);
}

public function testGetDatabaseName(): void
{
$actual = $this->service
->getDatabaseName();
self::assertSame($this->databaseName, $actual);
}
}

0 comments on commit ef013e9

Please sign in to comment.