Working state

This commit is contained in:
julien
2026-03-16 08:50:42 +01:00
parent 81200f81cd
commit b5a728e669
7 changed files with 128 additions and 4 deletions

View File

@@ -63,6 +63,32 @@ final class MediaControllerUploadCompatibilityTest extends ControllerTestCase
]);
}
public function testUploadPrefersFileFieldWhenBothFileAndImageArePresent(): void
{
$fileField = $this->makeValidUploadedFile();
$imageField = $this->makeValidUploadedFile();
$this->sessionManager->method('getUserId')->willReturn(11);
$this->mediaService->expects($this->once())
->method('store')
->with($fileField, 11)
->willReturn('/media/preferred-file-field.webp');
$req = $this->makePost('/admin/media/upload')->withUploadedFiles([
'file' => $fileField,
'image' => $imageField,
]);
$res = $this->controller->upload($req, $this->makeResponse());
$this->assertStatus($res, 200);
$this->assertJsonContains($res, [
'success' => true,
'url' => '/media/preferred-file-field.webp',
'file' => '/media/preferred-file-field.webp',
]);
}
public function testUploadSuccessResponseContainsBothUrlAndFileKeys(): void
{
$file = $this->makeValidUploadedFile();

View File

@@ -59,6 +59,21 @@ final class PostExtensionTest extends TestCase
self::assertNull($this->call('post_thumbnail', $post));
}
public function testPostExcerptReturnsHtmlUnchangedWhenContentIsShortEnough(): void
{
$post = new Post(4, 'Titre', '<p><strong>Bonjour</strong> monde</p>', 'titre-4');
self::assertSame('<strong>Bonjour</strong> monde', $this->call('post_excerpt', $post, 50));
}
public function testPostInitialsFallsBackToFirstRawCharacterWhenOnlyStopWordsRemain(): void
{
$post = new Post(5, 'de la', '<p>Contenu</p>', 'slug-5');
self::assertSame('D', $this->call('post_initials', $post));
}
public function testPostInitialsUseMeaningfulWordsAndFallback(): void
{
$post = new Post(1, 'Article de Blog', '<p>Contenu</p>', 'slug');

View File

@@ -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));
}
}

View File

@@ -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';

View File

@@ -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();

View 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());
}
}

View File

@@ -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');