Skip to content

Commit

Permalink
Continue add unit tests for service.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jun 19, 2024
1 parent 00d7238 commit 342f742
Show file tree
Hide file tree
Showing 8 changed files with 440 additions and 22 deletions.
3 changes: 3 additions & 0 deletions tests/Data/diagrams/no_title.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
classDiagram
class NoTitle {
}
10 changes: 10 additions & 0 deletions tests/Data/diagrams/user.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: User
---
classDiagram
class UserInterface {
<<Interface>>
+getRoles() array
+getUserIdentifier() string
+eraseCredentials()
}
87 changes: 86 additions & 1 deletion tests/Security/EntityVoterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

use App\Entity\Calculation;
use App\Entity\User;
use App\Enums\EntityName;
use App\Enums\EntityPermission;
use App\Interfaces\RoleInterface;
use App\Security\EntityVoter;
use App\Service\ApplicationService;
Expand All @@ -35,7 +37,14 @@ class EntityVoterTest extends TestCase
*/
protected function setUp(): void
{
$this->voter = new EntityVoter($this->createMock(ApplicationService::class));
$builder = new RoleBuilderService();
$service = $this->createMock(ApplicationService::class);
$service->method('getAdminRights')
->willReturn($builder->getRoleAdmin()->getRights());
$service->method('getUserRights')
->willReturn($builder->getRoleUser()->getRights());

$this->voter = new EntityVoter($service);
}

public static function getSupportsAttribute(): \Iterator
Expand Down Expand Up @@ -65,6 +74,19 @@ public function testAbstainSubject(): void
}

public function testAdmin(): void
{
$builder = new RoleBuilderService();
$role = $builder->getRoleAdmin();
$user = $this->getAdminUser()
->setRights($role->getRights());

$attribute = 'ADD';
$subject = User::class;
$expected = VoterInterface::ACCESS_GRANTED;
$this->assertVote($user, $subject, $attribute, $expected);
}

public function testAdminOverwrite(): void
{
$builder = new RoleBuilderService();
$role = $builder->getRoleAdmin();
Expand All @@ -87,6 +109,20 @@ public function testDisable(): void
$this->assertVote($user, $subject, $attribute, $expected);
}

public function testEntityName(): void
{
$user = $this->getAdminUser();
$expected = VoterInterface::ACCESS_ABSTAIN;
$this->assertVote($user, EntityName::CALCULATION, 'fake', $expected);
}

public function testEntityPermission(): void
{
$user = $this->getAdminUser();
$expected = VoterInterface::ACCESS_ABSTAIN;
$this->assertVote($user, 'fake', EntityPermission::ADD, $expected);
}

public function testSuperAdmin(): void
{
$user = $this->getSuperAdminUser();
Expand All @@ -103,6 +139,55 @@ public function testSupportsAttribute(string $value, bool $expected): void
self::assertSame($expected, $actual);
}

public function testUser(): void
{
$builder = new RoleBuilderService();
$role = $builder->getRoleUser();
$user = $this->getDefaultUser()
->setRights($role->getRights());

$attribute = 'ADD';
$subject = Calculation::class;
$expected = VoterInterface::ACCESS_GRANTED;
$this->assertVote($user, $subject, $attribute, $expected);
}

/**
* @throws \ReflectionException
*
* @psalm-suppress UnusedMethodCall
*/
public function testVoteOnAttribute(): void
{
$class = new \ReflectionClass($this->voter);
$method = $class->getMethod('voteOnAttribute');
$method->setAccessible(true);

$builder = new RoleBuilderService();
$role = $builder->getRoleUser();
$user = $this->getDefaultUser()
->setRights($role->getRights());
$token = $this->getUserToken($user);

$args = [
'fake',
EntityName::CALCULATION->name,
$token,
];
/** @psalm-var bool $actual */
$actual = $method->invoke($this->voter, ...$args);
self::assertFalse($actual);

$args = [
EntityPermission::ADD->name,
'fake',
$token,
];
/** @psalm-var bool $actual */
$actual = $method->invoke($this->voter, ...$args);
self::assertFalse($actual);
}

private function assertVote(User $user, mixed $subject, mixed $attribute, mixed $expected): void
{
$token = $this->getUserToken($user);
Expand Down
72 changes: 58 additions & 14 deletions tests/Service/CalculationUpdateServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@

namespace App\Tests\Service;

use App\Entity\Calculation;
use App\Entity\CalculationState;
use App\Service\CalculationUpdateService;
use App\Tests\DatabaseTrait;
use App\Tests\DateAssertTrait;
use App\Tests\EntityTrait\CalculationTrait;
use App\Tests\Web\AbstractAuthenticateWebTestCase;
use App\Utils\DateUtils;
use App\Utils\FormatUtils;
Expand All @@ -30,9 +29,19 @@
#[CoversClass(CalculationUpdateService::class)]
class CalculationUpdateServiceTest extends AbstractAuthenticateWebTestCase
{
use CalculationTrait;
use DatabaseTrait;
use DateAssertTrait;

/**
* @throws ORMException
*/
protected function tearDown(): void
{
$this->deleteCalculation();
parent::tearDown();
}

/**
* @throws \Exception
*/
Expand Down Expand Up @@ -104,20 +113,35 @@ public function testUpdateEmpty(): void
/**
* @throws ORMException
*/
public function testUpdateOne(): void
public function testUpdateNoCalculation(): void
{
$date = $this->getDateTo();
$state = $this->getCalculationState();
$calculation = $this->getCalculation($state);
$calculation->setDate($date)
->setOverallTotal(0.0);
$this->addEntity($calculation);

$this->loginUsername(self::ROLE_ADMIN);
$state = new CalculationState();
$state->setCode('code');
$this->addEntity($state);
$service = $this->getService(CalculationUpdateService::class);
$query = $service->createQuery();

$result = $service->update($query);
self::assertCount(0, $result);
self::assertCount(0, $result->getResults());
self::assertFalse($result->isValid());
}

/**
* @throws ORMException
*/
public function testUpdateOne(): void
{
$date = $this->getDateTo();
$calculation = new Calculation();
$calculation->setState($state)
->setCustomer('customer')
->setDescription('description')
->setOverallTotal(100.0)
->setDate($date);
$state = $this->getCalculationState();
$calculation = $this->getCalculation($state);
$calculation->setDate($date)
->setOverallTotal(100.0);
$this->addEntity($calculation);

$this->loginUsername(self::ROLE_ADMIN);
Expand All @@ -128,9 +152,29 @@ public function testUpdateOne(): void
self::assertCount(1, $result);
self::assertCount(1, $result->getResults());
self::assertTrue($result->isValid());
}

$this->deleteEntity($calculation);
$this->deleteEntity($state);
/**
* @throws ORMException
*/
public function testUpdateOneNoSimulate(): void
{
$date = $this->getDateTo();
$state = $this->getCalculationState();
$calculation = $this->getCalculation($state);
$calculation->setDate($date)
->setOverallTotal(100.0);
$this->addEntity($calculation);

$this->loginUsername(self::ROLE_ADMIN);
$service = $this->getService(CalculationUpdateService::class);
$query = $service->createQuery();
$query->setSimulate(false);

$result = $service->update($query);
self::assertCount(1, $result);
self::assertCount(1, $result->getResults());
self::assertTrue($result->isValid());
}

/**
Expand Down
78 changes: 78 additions & 0 deletions tests/Service/DiagramServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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\DiagramService;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

#[CoversClass(DiagramService::class)]
class DiagramServiceTest extends TestCase
{
private DiagramService $service;

protected function setUp(): void
{
$path = __DIR__ . '/../Data/diagrams';
$cache = new ArrayAdapter();
$this->service = new DiagramService($path, $cache);
}

/**
* @throws InvalidArgumentException
*/
public function testCount(): void
{
$files = $this->service->getFiles();
self::assertCount(2, $files);
}

/**
* @throws InvalidArgumentException
*/
public function testGetFileFound(): void
{
$expected = 'user';
$actual = $this->service->getFile($expected);
self::assertIsArray($actual);
self::assertArrayHasKey('name', $actual);
self::assertArrayHasKey('title', $actual);
self::assertArrayHasKey('content', $actual);
self::assertSame($expected, $actual['name']);
}

/**
* @throws InvalidArgumentException
*/
public function testGetFileNotFound(): void
{
$actual = $this->service->getFile('fake_name');
self::assertNull($actual);
}

/**
* @throws InvalidArgumentException
*/
public function testGetFileNoTitle(): void
{
$expected = 'no_title';
$actual = $this->service->getFile($expected);
self::assertIsArray($actual);
self::assertArrayHasKey('name', $actual);
self::assertArrayHasKey('title', $actual);
self::assertArrayHasKey('content', $actual);
self::assertSame($expected, $actual['name']);
}
}
49 changes: 49 additions & 0 deletions tests/Service/RecaptchaResponseServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\RecaptchaResponseService;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use ReCaptcha\Response;

#[CoversClass(RecaptchaResponseService::class)]
class RecaptchaResponseServiceTest extends TestCase
{
public function testFormatSuccess(): void
{
$response = new Response(true);
$service = new RecaptchaResponseService();
$actual = $service->format($response);
self::assertStringContainsString('success', $actual);
self::assertStringContainsString('true', $actual);
}

public function testFormatWithChallenge(): void
{
$response = new Response(true, challengeTs: '2024-10-02');
$service = new RecaptchaResponseService();
$actual = $service->format($response);
self::assertStringContainsString('success', $actual);
self::assertStringContainsString('challengeTs', $actual);
}

public function testFormatWithErrorCodes(): void
{
$response = new Response(true, ['Fake Error']);
$service = new RecaptchaResponseService();
$actual = $service->format($response);
self::assertStringContainsString('error-codes', $actual);
self::assertStringContainsString('Fake Error', $actual);
}
}
Loading

0 comments on commit 342f742

Please sign in to comment.