Skip to content
This repository has been archived by the owner on Nov 10, 2020. It is now read-only.

Commit

Permalink
Handling empty arguments array gracefully. Updated unit tests, adding…
Browse files Browse the repository at this point in the history
… new test for empty arguments.
  • Loading branch information
pointybeard committed Jun 5, 2015
1 parent 54cc4f6 commit 8a8d854
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
10 changes: 7 additions & 3 deletions src/Lib/ArgumentIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace pointybeard\ShellArgs\Lib;

class ArgumentIterator implements \Iterator
class ArgumentIterator implements \Iterator, \Countable
{
private $args;
private $keys;
private $args = [];
private $keys = [];
private $position = 0;

public function __construct($ignoreFirst = true, array $args = null)
Expand Down Expand Up @@ -92,4 +92,8 @@ public function valid()
{
return isset($this->args[$this->position]);
}

public function count(){
return count($this->keys);
}
}
36 changes: 28 additions & 8 deletions tests/ArgumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

class CommandTest extends \PHPUnit_Framework_TestCase
{
// Create iterator using array of argments and check
// they have been set correctly.
/**
* Create iterator using array of argments and check they have been set correctly.
*/
public function testValidPassArgsToConstructor()
{
$it = new ArgumentIterator(false, [
Expand All @@ -28,9 +29,20 @@ public function testValidPassArgsToConstructor()
$this->assertEquals('hithere', $it->current()->name());
$this->assertTrue($it->current()->value());
}

/**
* This test checks to see that an empty array state is handled properly
*/
public function testEmptyArgumentArray()
{
$it = new ArgumentIterator(false, []);
$this->assertFalse($it->find('hithere'));
$this->assertEquals(0, $it->count());
}

// Set the "ignoreFirst" property to true and check that
// the first item (file name) has been ignored
/**
* This test set the "ignoreFirst" property to true and check that the first item (file name) is ignored
*/
public function testValidIgnoreFirstArg()
{
$it = new ArgumentIterator(true, [
Expand All @@ -41,7 +53,9 @@ public function testValidIgnoreFirstArg()
$this->assertFalse($it->find('../blah/blah'));
}

// Seed the global $argv array with some data
/**
* This test seeds the global $argv array with some data to emulate receiving data from the command line
*/
public function testValidARGV()
{
// Seed the $argv array
Expand All @@ -61,7 +75,9 @@ public function testValidARGV()
$this->assertEquals('\Users\pointybeard\Sites\shellargs\\', $it->find('p')->value());
}

// Give a argument string to the constructor
/**
* Give an argument string to the constructor
*/
public function testValidArgString()
{
$it = new ArgumentIterator(false, ['--hithere -i -c cheese']);
Expand All @@ -75,7 +91,7 @@ public function testValidArgString()
*
* Note that the use of __get() magic method in the Argument class is depricated.
* It has been been superceeded by name() and value() getter methods. This test remains
* until depricated code has been removed.
* until the depricated code has been removed.
*
* @depends testValidArgString
*/
Expand All @@ -91,7 +107,11 @@ public function testArgumentMagicMethodGetter(ArgumentIterator $it)
$this->setExpectedException('PHPUnit_Framework_Error');
$this->assertFalse($it->find('hithere')->banana);
}


/**
* This test checks that the find method behaves correctly when passing an array of
* argument names.
*/
public function testValidFindArgumentArray()
{
$it = new ArgumentIterator(false, ['--config=/path/to/file --help']);
Expand Down

0 comments on commit 8a8d854

Please sign in to comment.