Skip to content

Commit 44d57c4

Browse files
committed
[#18] Resolve PHP warning about undefined array key.
1 parent b297fda commit 44d57c4

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

phpunit.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/|version|/phpunit.xsd"
4-
bootstrap="vendor/autoload.php">
4+
bootstrap="vendor/autoload.php"
5+
displayDetailsOnTestsThatTriggerWarnings="true"
6+
failOnWarning="true"
7+
>
58
<testsuites>
69
<testsuite name="unit">
710
<directory>tests</directory>

src/helpers/ParsingHelper.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ public static function getYouTubeIdFromUrl(string $url): ?string
6060

6161
if ($query) {
6262
parse_str($query, $qs);
63-
if ($qs['v'] || $qs['vi']) {
64-
return $qs['v'] ?? $qs['vi'];
63+
64+
$v = $qs['v'] ?? null;
65+
$vi = $qs['vi'] ?? null;
66+
67+
if ($v || $vi) {
68+
return $v ?? $vi;
6569
}
6670
}
6771

tests/ParsingHelperTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,27 @@ public function testGetVideoTypeFromUrl(): void
1313
ParsingHelper::getVideoTypeFromUrl('https://www.youtube.com')
1414
);
1515
}
16+
17+
public function testGetYouTubeIdFromUrl(): void
18+
{
19+
$this->assertEquals(
20+
'6xWpo5Dn254',
21+
ParsingHelper::getYouTubeIdFromUrl('https://www.youtube.com/watch?v=6xWpo5Dn254')
22+
);
23+
24+
$this->assertEquals(
25+
'6xWpo5Dn254',
26+
ParsingHelper::getYouTubeIdFromUrl('https://www.youtube.com/watch?vi=6xWpo5Dn254')
27+
);
28+
29+
$this->assertEquals(
30+
'--HXLM8GuxA',
31+
ParsingHelper::getYouTubeIdFromUrl('https://youtu.be/--HXLM8GuxA?si=pNahKJLszKr8J00u')
32+
);
33+
34+
$this->assertEquals(
35+
'--HXLM8GuxA',
36+
ParsingHelper::getYouTubeIdFromUrl('https://youtube.com/watch?v=--HXLM8GuxA')
37+
);
38+
}
1639
}

0 commit comments

Comments
 (0)