From cc238cbd8a28254162dbc448168068b245b63d52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Thu, 31 Oct 2024 17:52:14 +0100 Subject: [PATCH] [Icons] Fix commands receive polluted SVG --- src/Icons/src/Command/ImportIconCommand.php | 4 +-- src/Icons/src/Command/LockIconsCommand.php | 4 +-- src/Icons/src/Iconify.php | 19 ------------ src/Icons/tests/Unit/IconifyTest.php | 32 --------------------- 4 files changed, 4 insertions(+), 55 deletions(-) diff --git a/src/Icons/src/Command/ImportIconCommand.php b/src/Icons/src/Command/ImportIconCommand.php index b287231d203..f1c8a08b9fb 100644 --- a/src/Icons/src/Command/ImportIconCommand.php +++ b/src/Icons/src/Command/ImportIconCommand.php @@ -68,7 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io->comment(\sprintf('Importing %s...', $fullName)); try { - $svg = $this->iconify->fetchSvg($prefix, $name); + $iconSvg = $this->iconify->fetchIcon($prefix, $name)->toHtml(); } catch (IconNotFoundException $e) { $io->error($e->getMessage()); $result = Command::FAILURE; @@ -79,7 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $cursor = new Cursor($output); $cursor->moveUp(2); - $this->registry->add(\sprintf('%s/%s', $prefix, $name), $svg); + $this->registry->add(\sprintf('%s/%s', $prefix, $name), $iconSvg); $license = $this->iconify->metadataFor($prefix)['license']; diff --git a/src/Icons/src/Command/LockIconsCommand.php b/src/Icons/src/Command/LockIconsCommand.php index e452a7d723d..c3b3d2fa65a 100644 --- a/src/Icons/src/Command/LockIconsCommand.php +++ b/src/Icons/src/Command/LockIconsCommand.php @@ -81,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } try { - $svg = $this->iconify->fetchSvg($prefix, $name); + $iconSvg = $this->iconify->fetchIcon($prefix, $name)->toHtml(); } catch (IconNotFoundException) { // icon not found on iconify if ($io->isVerbose()) { @@ -90,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int continue; } - $this->registry->add(\sprintf('%s/%s', $prefix, $name), $svg); + $this->registry->add(\sprintf('%s/%s', $prefix, $name), $iconSvg); $license = $this->iconify->metadataFor($prefix)['license']; ++$count; diff --git a/src/Icons/src/Iconify.php b/src/Icons/src/Iconify.php index c948e24dd46..757dacb245c 100644 --- a/src/Icons/src/Iconify.php +++ b/src/Icons/src/Iconify.php @@ -85,25 +85,6 @@ public function fetchIcon(string $prefix, string $name): Icon ]); } - public function fetchSvg(string $prefix, string $name): string - { - if (!isset($this->sets()[$prefix])) { - throw new IconNotFoundException(\sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name)); - } - - $response = $this->http->request('GET', \sprintf('/%s/%s.svg', $prefix, $name)); - - if (200 !== $response->getStatusCode()) { - throw new IconNotFoundException(\sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name)); - } - - if (!str_starts_with($svg = $response->getContent(), 'sets()[$prefix]); diff --git a/src/Icons/tests/Unit/IconifyTest.php b/src/Icons/tests/Unit/IconifyTest.php index a3aa50bfba2..b940683660e 100644 --- a/src/Icons/tests/Unit/IconifyTest.php +++ b/src/Icons/tests/Unit/IconifyTest.php @@ -15,7 +15,6 @@ use Symfony\Component\Cache\Adapter\NullAdapter; use Symfony\Component\HttpClient\MockHttpClient; use Symfony\Component\HttpClient\Response\JsonMockResponse; -use Symfony\Component\HttpClient\Response\MockResponse; use Symfony\UX\Icons\Exception\IconNotFoundException; use Symfony\UX\Icons\Iconify; @@ -173,37 +172,6 @@ public function testGetMetadata(): void $this->assertSame('Font Awesome Solid', $metadata['name']); } - public function testFetchSvg(): void - { - $client = new MockHttpClient([ - new MockResponse(file_get_contents(__DIR__.'/../Fixtures/Iconify/collections.json'), [ - 'response_headers' => ['content-type' => 'application/json'], - ]), - new MockResponse(file_get_contents(__DIR__.'/../Fixtures/Iconify/icon.svg')), - ]); - $iconify = new Iconify(new NullAdapter(), 'https://localhost', $client); - - $svg = $iconify->fetchSvg('fa6-regular', 'bar'); - - $this->assertIsString($svg); - $this->stringContains('-.224l.235-.468ZM6.013 2.06c-.649-.1', $svg); - } - - public function testFetchSvgThrowIconNotFoundExceptionWhenStatusCodeNot200(): void - { - $client = new MockHttpClient([ - new MockResponse(file_get_contents(__DIR__.'/../Fixtures/Iconify/collections.json'), [ - 'response_headers' => ['content-type' => 'application/json'], - ]), - new MockResponse('', ['http_code' => 404]), - ]); - $iconify = new Iconify(new NullAdapter(), 'https://localhost', $client); - - $this->expectException(IconNotFoundException::class); - - $iconify->fetchSvg('fa6-regular', 'bar'); - } - private function createHttpClient(mixed $data, int $code = 200): MockHttpClient { $mockResponse = new JsonMockResponse($data, ['http_code' => $code]);