Skip to content

Commit

Permalink
[#18] Resolve PHP warning about undefined array key.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuapease committed Feb 4, 2025
1 parent b297fda commit 44d57c4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
5 changes: 4 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/|version|/phpunit.xsd"
bootstrap="vendor/autoload.php">
bootstrap="vendor/autoload.php"
displayDetailsOnTestsThatTriggerWarnings="true"
failOnWarning="true"
>
<testsuites>
<testsuite name="unit">
<directory>tests</directory>
Expand Down
8 changes: 6 additions & 2 deletions src/helpers/ParsingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ public static function getYouTubeIdFromUrl(string $url): ?string

if ($query) {
parse_str($query, $qs);
if ($qs['v'] || $qs['vi']) {
return $qs['v'] ?? $qs['vi'];

$v = $qs['v'] ?? null;
$vi = $qs['vi'] ?? null;

if ($v || $vi) {
return $v ?? $vi;
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/ParsingHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,27 @@ public function testGetVideoTypeFromUrl(): void
ParsingHelper::getVideoTypeFromUrl('https://www.youtube.com')
);
}

public function testGetYouTubeIdFromUrl(): void
{
$this->assertEquals(
'6xWpo5Dn254',
ParsingHelper::getYouTubeIdFromUrl('https://www.youtube.com/watch?v=6xWpo5Dn254')
);

$this->assertEquals(
'6xWpo5Dn254',
ParsingHelper::getYouTubeIdFromUrl('https://www.youtube.com/watch?vi=6xWpo5Dn254')
);

$this->assertEquals(
'--HXLM8GuxA',
ParsingHelper::getYouTubeIdFromUrl('https://youtu.be/--HXLM8GuxA?si=pNahKJLszKr8J00u')
);

$this->assertEquals(
'--HXLM8GuxA',
ParsingHelper::getYouTubeIdFromUrl('https://youtube.com/watch?v=--HXLM8GuxA')
);
}
}

0 comments on commit 44d57c4

Please sign in to comment.