Skip to content

Commit

Permalink
LunrCliParser: Type-safe access to $_SERVER['argv']
Browse files Browse the repository at this point in the history
  • Loading branch information
pprkut committed Dec 10, 2024
1 parent 6393069 commit c116711
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Lunr/Shadow/LunrCliParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace Lunr\Shadow;

use UnexpectedValueException;

/**
* Getopt like command line argument parser. However, it
* does a few things different from getopt. While getopt
Expand Down Expand Up @@ -94,7 +96,25 @@ public function __destruct()
*/
public function parse(): array
{
$this->args = $_SERVER['argv'];
if (!is_array($_SERVER['argv']))
{
throw new UnexpectedValueException('Command line arguments are not stored in an array!');
}

if (!array_is_list($_SERVER['argv']))
{
throw new UnexpectedValueException('Command line arguments are not stored as a list!');
}

foreach ($_SERVER['argv'] as $index => $arg)
{
if (!is_string($arg))
{
throw new UnexpectedValueException('Command line argument is not a string!');
}

$this->args[$index] = $arg;
}

foreach ($this->args as $index => $arg)
{
Expand Down
53 changes: 53 additions & 0 deletions src/Lunr/Shadow/Tests/LunrCliParserParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Lunr\Shadow\Tests;

use UnexpectedValueException;

/**
* This class contains test methods for parse() in the LunrCliParser class.
*
Expand All @@ -18,6 +20,57 @@
class LunrCliParserParseTest extends LunrCliParserTest
{

/**
* Test that parsing with an invalid argv throws an exception.
*
* @covers Lunr\Shadow\LunrCliParser::parse
*/
public function testParseArgvWithArgvAsString(): void
{
$_SERVER['argv'] = 'script.php';

$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Command line arguments are not stored in an array!');

$value = $this->class->parse();

$this->assertArrayEmpty($value);
}

/**
* Test that parsing with an invalid argv throws an exception.
*
* @covers Lunr\Shadow\LunrCliParser::parse
*/
public function testParseArgvWithArgvAsAssociativeArray(): void
{
$_SERVER['argv'] = [ 1 => 'script.php' ];

$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Command line arguments are not stored as a list!');

$value = $this->class->parse();

$this->assertArrayEmpty($value);
}

/**
* Test that parsing with an invalid argv throws an exception.
*
* @covers Lunr\Shadow\LunrCliParser::parse
*/
public function testParseArgvWithArgvAsIntArray(): void
{
$_SERVER['argv'] = [ 1, 'script.php' ];

$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Command line argument is not a string!');

$value = $this->class->parse();

$this->assertArrayEmpty($value);
}

/**
* Test that parsing no arguments returns an empty array.
*
Expand Down

0 comments on commit c116711

Please sign in to comment.