Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions src/Screenshot/ScreenshotHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,23 @@ public static function generateFilename(string $url, string $directory, string $
*/
public static function slugifyUrl(string $url, int $maxLength = 40): string
{
$slug = preg_replace('/^https?:\/\//', '', $url);

if (is_string($slug)) {
$slug = preg_replace('/^www\./', '', $slug);
}
$slug = preg_replace('#^(https?://)?(www\.)?#i', '', $url);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about :

Suggested change
$slug = preg_replace('#^(https?://)?(www\.)?#i', '', $url);
$slug = preg_replace(
['#^(https?://)?(www\.)?#i', '/[^a-z0-9]+/i'],
['', '-'],
trim($url)
);
$slug = strtolower(trim($slug ?? '', '-'));
$slug = substr($slug, 0, $maxLength);
$slug = rtrim($slug, '-');
return $slug !== '' ? $slug : 'screenshot';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it's simpler, but it removes the behaviour with "invalid-url" fallback

if (null === $slug) {
$slug = 'invalid-url';
}

$slug = preg_replace('/[^a-zA-Z0-9]+/', '-', $slug);
if (null === $slug) {
$slug = 'invalid-url';
} else {
$slug = trim($slug, '-');
$slug = strtolower($slug);
}

$slug = trim($slug, '-');

$slug = strtolower($slug);

if (strlen($slug) > $maxLength) {
$slug = substr($slug, 0, $maxLength);

$slug = rtrim($slug, '-');
}

if (empty($slug)) {
$slug = 'screenshot';
}
$slug = substr($slug, 0, $maxLength);
$slug = rtrim($slug, '-');

return $slug;
return $slug ?: 'screenshot';
}

/**
Expand Down
37 changes: 17 additions & 20 deletions tests/Unit/Screenshot/ScreenshotHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Playwright\Tests\Unit\Screenshot;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Playwright\Screenshot\ScreenshotHelper;

Expand All @@ -37,30 +38,26 @@ protected function tearDown(): void
}
}

public function testSlugifyUrl(): void
#[DataProvider('provideUrls')]
public function testSlugifyUrl(string $input, string $expected, int $maxLength = 40): void
{
$testCases = [
$result = ScreenshotHelper::slugifyUrl($input, $maxLength);
$this->assertSame($expected, $result, "Failed for input: $input");
}

public static function provideUrls(): array
{
return [
['https://example.com', 'example-com'],
['https://www.github.com/user/repo', 'github-com-user-repo'],
['http://api.service.com/v1/users?id=123', 'api-service-com-v1-users-id-123'],
['https://sub.domain.co.uk/path/file.html', 'sub-domain-co-uk-path-file-html'],
['invalid-chars!@#$%^&*()', 'invalid-chars'],
['!@#$%^&*()', 'screenshot'],
['', 'screenshot'],
['https://sub.domain.co.uk/path/file.html', 'sub-domain', 11],
['https://example.com/very-long-path-that-exceeds-the-maximum-length-limit', 'example-com-very-lon', 20],
];

foreach ($testCases as [$input, $expected]) {
$result = ScreenshotHelper::slugifyUrl($input);
$this->assertEquals($expected, $result, "Failed for input: $input");
}
}

public function testSlugifyUrlWithMaxLength(): void
{
$longUrl = 'https://example.com/very-long-path-that-exceeds-the-maximum-length-limit';
$result = ScreenshotHelper::slugifyUrl($longUrl, 20);

$this->assertLessThanOrEqual(20, strlen($result));
$this->assertEquals('example-com-very-lon', $result);
}

public function testGenerateFilename(): void
Expand All @@ -75,7 +72,7 @@ public function testGenerateFilename(): void
);

$this->assertDirectoryExists($this->testDir);
$this->assertEquals($this->testDir.'/'.$basename, $filename);
$this->assertSame($this->testDir.'/'.$basename, $filename);
}

public function testGenerateFilenameCreatesDirectory(): void
Expand Down Expand Up @@ -118,7 +115,7 @@ public function testGetDirectoryInfoEmpty(): void
'newestFile' => null,
];

$this->assertEquals($expected, $info);
$this->assertSame($expected, $info);
}

public function testGetDirectoryInfoWithFiles(): void
Expand Down Expand Up @@ -152,7 +149,7 @@ public function testCleanupOldScreenshots(): void

$cleaned = ScreenshotHelper::cleanupOldScreenshots($this->testDir, 3600);

$this->assertEquals(1, $cleaned);
$this->assertSame(1, $cleaned);
$this->assertFileDoesNotExist($oldFile);
$this->assertFileExists($newFile);
}
Expand All @@ -168,7 +165,7 @@ public function testCleanupOldScreenshotsByCount(): void

$cleaned = ScreenshotHelper::cleanupOldScreenshots($this->testDir, 86400, 3);

$this->assertEquals(2, $cleaned);
$this->assertSame(2, $cleaned);

$remaining = glob($this->testDir.'/*.png');
$this->assertCount(3, $remaining);
Expand Down