Working state
This commit is contained in:
@@ -8,7 +8,6 @@ use PHPUnit\Framework\TestCase;
|
||||
use Slim\Psr7\Factory\ServerRequestFactory;
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
|
||||
final class ClientIpResolverTest extends TestCase
|
||||
{
|
||||
public function testResolveReturnsDefaultWhenRemoteAddrMissing(): void
|
||||
@@ -54,4 +53,27 @@ final class ClientIpResolverTest extends TestCase
|
||||
|
||||
self::assertSame('127.0.0.1', $resolver->resolve($request));
|
||||
}
|
||||
|
||||
public function testResolveReturnsRemoteAddrWhenTrustedProxyHasNoForwardedHeader(): void
|
||||
{
|
||||
$request = (new ServerRequestFactory())->createServerRequest('GET', '/', [
|
||||
'REMOTE_ADDR' => '127.0.0.1',
|
||||
]);
|
||||
|
||||
$resolver = new ClientIpResolver(['127.0.0.1']);
|
||||
|
||||
self::assertSame('127.0.0.1', $resolver->resolve($request));
|
||||
}
|
||||
|
||||
public function testResolveTrimsWhitespaceAroundRemoteAndForwardedAddresses(): void
|
||||
{
|
||||
$request = (new ServerRequestFactory())->createServerRequest('GET', '/', [
|
||||
'REMOTE_ADDR' => ' 127.0.0.1 ',
|
||||
'HTTP_X_FORWARDED_FOR' => ' 203.0.113.10 , 198.51.100.12',
|
||||
]);
|
||||
|
||||
$resolver = new ClientIpResolver(['*']);
|
||||
|
||||
self::assertSame('203.0.113.10', $resolver->resolve($request));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ use App\Shared\Config;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
|
||||
final class ConfigTest extends TestCase
|
||||
{
|
||||
public function testGetTwigCacheReturnsFalseInDev(): void
|
||||
@@ -23,6 +22,25 @@ final class ConfigTest extends TestCase
|
||||
self::assertStringEndsWith('/var/cache/twig', $cachePath);
|
||||
}
|
||||
|
||||
public function testGetDatabasePathReturnsExistingFilePathUnchanged(): void
|
||||
{
|
||||
$dbFile = dirname(__DIR__, 2).'/database/app.sqlite';
|
||||
$dbDir = dirname($dbFile);
|
||||
|
||||
if (!is_dir($dbDir)) {
|
||||
mkdir($dbDir, 0755, true);
|
||||
}
|
||||
|
||||
if (!file_exists($dbFile)) {
|
||||
touch($dbFile);
|
||||
}
|
||||
|
||||
$path = Config::getDatabasePath();
|
||||
|
||||
self::assertSame($dbFile, $path);
|
||||
self::assertFileExists($dbFile);
|
||||
}
|
||||
|
||||
public function testGetDatabasePathCreatesDatabaseFileWhenMissing(): void
|
||||
{
|
||||
$dbFile = dirname(__DIR__, 2).'/database/app.sqlite';
|
||||
|
||||
@@ -7,7 +7,6 @@ use App\Shared\Http\FlashService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
|
||||
final class FlashServiceTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
@@ -35,6 +34,26 @@ final class FlashServiceTest extends TestCase
|
||||
self::assertArrayNotHasKey('count', $_SESSION['flash']);
|
||||
}
|
||||
|
||||
public function testGetCastsBooleanFalseToEmptyStringAndRemovesIt(): void
|
||||
{
|
||||
$_SESSION['flash']['flag'] = false;
|
||||
|
||||
$flash = new FlashService();
|
||||
|
||||
self::assertSame('', $flash->get('flag'));
|
||||
self::assertArrayNotHasKey('flag', $_SESSION['flash']);
|
||||
}
|
||||
|
||||
public function testSetOverridesPreviousMessageForSameKey(): void
|
||||
{
|
||||
$flash = new FlashService();
|
||||
|
||||
$flash->set('notice', 'Premier');
|
||||
$flash->set('notice', 'Second');
|
||||
|
||||
self::assertSame('Second', $flash->get('notice'));
|
||||
}
|
||||
|
||||
public function testGetReturnsNullWhenMissing(): void
|
||||
{
|
||||
$flash = new FlashService();
|
||||
|
||||
17
tests/Shared/NotFoundExceptionTest.php
Normal file
17
tests/Shared/NotFoundExceptionTest.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Shared;
|
||||
|
||||
use App\Shared\Exception\NotFoundException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotFoundExceptionTest extends TestCase
|
||||
{
|
||||
public function testConstructorFormatsEntityAndIdentifierInMessage(): void
|
||||
{
|
||||
$exception = new NotFoundException('Article', 15);
|
||||
|
||||
self::assertSame('Article introuvable : 15', $exception->getMessage());
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ use App\Shared\Http\SessionManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
|
||||
final class SessionManagerEdgeCasesTest extends TestCase
|
||||
{
|
||||
private SessionManager $manager;
|
||||
@@ -31,6 +30,14 @@ final class SessionManagerEdgeCasesTest extends TestCase
|
||||
self::assertFalse($this->manager->isAuthenticated());
|
||||
}
|
||||
|
||||
public function testGetUserIdCastsNumericStringToInteger(): void
|
||||
{
|
||||
$_SESSION['user_id'] = '42';
|
||||
|
||||
self::assertSame(42, $this->manager->getUserId());
|
||||
self::assertTrue($this->manager->isAuthenticated());
|
||||
}
|
||||
|
||||
public function testSetUserUsesDefaultRoleUser(): void
|
||||
{
|
||||
$this->manager->setUser(12, 'julien');
|
||||
|
||||
Reference in New Issue
Block a user