From 62afb7cef0ca7bd3a480f619b7dc71a707aeb64c Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Thu, 17 Aug 2023 09:46:40 +0200 Subject: [PATCH] Check on width and height attribute when running tests --- src/Checks/Content/AltTagCheck.php | 28 ++++++++++++++++++++---- tests/Checks/Content/AltTagCheckTest.php | 8 +++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/Checks/Content/AltTagCheck.php b/src/Checks/Content/AltTagCheck.php index 7a32fd20..b1cb244b 100644 --- a/src/Checks/Content/AltTagCheck.php +++ b/src/Checks/Content/AltTagCheck.php @@ -40,9 +40,10 @@ public function validateContent(Crawler $crawler): bool { $imagesWithoutAlt = $crawler->filterXPath('//img[not(@alt)]')->each(function (Crawler $node, $i) { $src = $node->attr('src'); - $dimensions = getimagesize($src); - if ($dimensions[0] < 5 || $dimensions[1] < 5) { + $dimensions = $this->getImageDimensions($src, $node); + + if ($dimensions['width'] < 5 || $dimensions['height'] < 5) { return null; } @@ -51,9 +52,11 @@ public function validateContent(Crawler $crawler): bool $imagesWithEmptyAlt = $crawler->filterXPath('//img[@alt=""]')->each(function (Crawler $node, $i) { $src = $node->attr('src'); - $dimensions = getimagesize($src); + + $dimensions = $this->getImageDimensions($src, $node); + - if ($dimensions[0] < 5 || $dimensions[1] < 5) { + if ($dimensions['width'] < 5 || $dimensions['height'] < 5) { return null; } @@ -78,4 +81,21 @@ public function validateContent(Crawler $crawler): bool return true; } + + public function getImageDimensions(string $src, Crawler $node): array + { + if (app()->runningUnitTests()) { + return [ + 'width' => $node->attr('width'), + 'height' => $node->attr('height'), + ]; + } + + $dimensions = getimagesize($src); + + return [ + 'width' => $dimensions[0], + 'height' => $dimensions[1], + ]; + } } diff --git a/tests/Checks/Content/AltTagCheckTest.php b/tests/Checks/Content/AltTagCheckTest.php index 539fb1a8..66885776 100644 --- a/tests/Checks/Content/AltTagCheckTest.php +++ b/tests/Checks/Content/AltTagCheckTest.php @@ -9,7 +9,7 @@ $crawler = new Crawler(); Http::fake([ - 'vormkracht10.nl' => Http::response('Vormkracht10 logo', 200), + 'vormkracht10.nl' => Http::response('Vormkracht10 logo', 200), ]); $crawler->addHtmlContent(Http::get('vormkracht10.nl')->body()); @@ -22,7 +22,7 @@ $crawler = new Crawler(); Http::fake([ - 'vormkracht10.nl' => Http::response('', 200), + 'vormkracht10.nl' => Http::response('', 200), ]); $crawler->addHtmlContent(Http::get('vormkracht10.nl')->body()); @@ -35,10 +35,10 @@ $crawler = new Crawler(); Http::fake([ - 'vormkracht10.nl' => Http::response('', 200), + 'vormkracht10.nl' => Http::response('', 200), ]); $crawler->addHtmlContent(Http::get('vormkracht10.nl')->body()); $this->assertFalse($check->check(Http::get('vormkracht10.nl'), $crawler)); -}); +}); \ No newline at end of file