From 8cd2a0e4ea00eda01675787c9127ba0f16ece1ee Mon Sep 17 00:00:00 2001 From: mondrake Date: Mon, 30 Dec 2024 12:33:28 +0100 Subject: [PATCH 1/4] add testSetDefaultMapClass --- tests/src/MapHandlerTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/src/MapHandlerTest.php b/tests/src/MapHandlerTest.php index 7309913b..8a35302d 100644 --- a/tests/src/MapHandlerTest.php +++ b/tests/src/MapHandlerTest.php @@ -4,6 +4,8 @@ use FileEye\MimeMap\Extension; use FileEye\MimeMap\MalformedTypeException; +use FileEye\MimeMap\Map\DefaultMap; +use FileEye\MimeMap\Map\EmptyMap; use FileEye\MimeMap\Map\MimeMapInterface; use FileEye\MimeMap\MapHandler; use FileEye\MimeMap\MappingException; @@ -21,6 +23,14 @@ public function setUp(): void $this->map = MapHandler::map(); } + public function testSetDefaultMapClass(): void + { + MapHandler::setDefaultMapClass(EmptyMap::class); + $this->assertInstanceOf(EmptyMap::class, MapHandler::map()); + MapHandler::setDefaultMapClass(DefaultMap::class); + $this->assertInstanceOf(DefaultMap::class, MapHandler::map()); + } + public function testMap(): void { $this->assertStringContainsString('DefaultMap.php', $this->map->getFileName()); From 7d0d4b9a03de648a2837e70bbb4afe081c4b6ac9 Mon Sep 17 00:00:00 2001 From: mondrake Date: Mon, 30 Dec 2024 12:45:15 +0100 Subject: [PATCH 2/4] add testWriteMapToPhpClassFileFailure --- tests/src/MapUpdaterTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/src/MapUpdaterTest.php b/tests/src/MapUpdaterTest.php index 921ac7c3..ef80a75c 100644 --- a/tests/src/MapUpdaterTest.php +++ b/tests/src/MapUpdaterTest.php @@ -171,6 +171,20 @@ public function testWriteMapToPhpClassFile(): void $this->fileSystem->remove(__DIR__ . '/../fixtures/MiniMap.php'); } + public function testWriteMapToPhpClassFileFailure(): void + { + $this->fileSystem->copy(__DIR__ . '/../fixtures/MiniMap.php.test', __DIR__ . '/../fixtures/MiniMap.php'); + include_once(__DIR__ . '/../fixtures/MiniMap.php'); + // @phpstan-ignore class.notFound, argument.type + MapHandler::setDefaultMapClass(MiniMap::class); + $map_a = MapHandler::map(); + $this->assertStringContainsString('fixtures/MiniMap.php', $map_a->getFileName()); + $content = file_get_contents($map_a->getFileName()); + assert(is_string($content)); + $this->assertStringNotContainsString('text/plain', $content); + $this->updater->writeMapToPhpClassFile("foo://bar.stub"); + } + public function testGetDefaultMapBuildFile(): void { $this->assertStringContainsString('default_map_build.yml', MapUpdater::getDefaultMapBuildFile()); From fef31a325cfcdcaef37d7b20145d2789bc603fd2 Mon Sep 17 00:00:00 2001 From: mondrake Date: Mon, 30 Dec 2024 14:58:35 +0100 Subject: [PATCH 3/4] fix --- tests/src/MapHandlerTest.php | 10 ++++++++++ tests/src/MapUpdaterTest.php | 2 ++ 2 files changed, 12 insertions(+) diff --git a/tests/src/MapHandlerTest.php b/tests/src/MapHandlerTest.php index 8a35302d..5908879e 100644 --- a/tests/src/MapHandlerTest.php +++ b/tests/src/MapHandlerTest.php @@ -47,8 +47,10 @@ public function testAdd(): void { // Adding a new type with a new extension. $this->map->addTypeExtensionMapping('bingo/bongo', 'bngbng'); + $this->map->addTypeDescription('bingo/bongo', 'Bingo, Bongo!'); $this->assertSame(['bngbng'], (new Type('bingo/bongo'))->getExtensions()); $this->assertSame('bngbng', (new Type('bingo/bongo'))->getDefaultExtension()); + $this->assertSame('Bingo, Bongo!', (new Type('bingo/bongo'))->getDescription()); $this->assertSame(['bingo/bongo'], (new Extension('bngbng'))->getTypes()); $this->assertSame('bingo/bongo', (new Extension('bngbng'))->getDefaultType()); @@ -200,6 +202,14 @@ public function testSetExtensionDefaultType(): void $this->assertSame(['image/vnd.dvb.subtitle', 'text/vnd.dvb.subtitle', 'text/x-microdvd', 'text/x-mpsub', 'text/x-subviewer'], (new Extension('SUB'))->getTypes()); } + public function testAddAliasToType(): void + { + $this->assertSame(['image/psd', 'image/x-psd', 'image/photoshop', 'image/x-photoshop', 'application/photoshop', 'application/x-photoshop',], (new Type('image/vnd.adobe.photoshop'))->getAliases()); + $this->map->addTypeAlias('image/vnd.adobe.photoshop', 'application/x-foo-bar'); + $this->assertSame(['image/psd', 'image/x-psd', 'image/photoshop', 'image/x-photoshop', 'application/photoshop', 'application/x-photoshop', 'application/x-foo-bar',], (new Type('image/vnd.adobe.photoshop'))->getAliases()); + $this->assertContains('application/x-foo-bar', $this->map->listAliases()); + } + public function testReAddAliasToType(): void { $this->assertSame(['image/psd', 'image/x-psd', 'image/photoshop', 'image/x-photoshop', 'application/photoshop', 'application/x-photoshop',], (new Type('image/vnd.adobe.photoshop'))->getAliases()); diff --git a/tests/src/MapUpdaterTest.php b/tests/src/MapUpdaterTest.php index ef80a75c..f7b36daf 100644 --- a/tests/src/MapUpdaterTest.php +++ b/tests/src/MapUpdaterTest.php @@ -182,6 +182,8 @@ public function testWriteMapToPhpClassFileFailure(): void $content = file_get_contents($map_a->getFileName()); assert(is_string($content)); $this->assertStringNotContainsString('text/plain', $content); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage("Failed loading file foo://bar.stub"); $this->updater->writeMapToPhpClassFile("foo://bar.stub"); } From 49b8755d94ec6db9f4fe759f2551335f6c302ec3 Mon Sep 17 00:00:00 2001 From: mondrake Date: Mon, 30 Dec 2024 15:02:15 +0100 Subject: [PATCH 4/4] fix --- tests/src/MapHandlerTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/src/MapHandlerTest.php b/tests/src/MapHandlerTest.php index 5908879e..0fe3546c 100644 --- a/tests/src/MapHandlerTest.php +++ b/tests/src/MapHandlerTest.php @@ -28,6 +28,7 @@ public function testSetDefaultMapClass(): void MapHandler::setDefaultMapClass(EmptyMap::class); $this->assertInstanceOf(EmptyMap::class, MapHandler::map()); MapHandler::setDefaultMapClass(DefaultMap::class); + // @phpstan-ignore method.impossibleType $this->assertInstanceOf(DefaultMap::class, MapHandler::map()); }