Skip to content

Commit

Permalink
Updated PhpReader to pass an empty array to ArrayIterator if no valid…
Browse files Browse the repository at this point in the history
… value was given

Fixes #3
  • Loading branch information
tysonphillips committed Oct 31, 2017
1 parent d0716af commit 2ca3814
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/Reader/PhpReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ public function __construct(SplFileObject $file)
public function getIterator()
{
if (!$this->file->isFile()) {
throw new ReaderParseException("Invalid file.");
throw new ReaderParseException('Invalid file.');
}
return new ArrayIterator(include $this->file->getPathname());

// The file must return an array or object
$result = include $this->file->getPathname();
return new ArrayIterator((is_array($result) || is_object($result) ? $result : array()));
}
}
4 changes: 4 additions & 0 deletions tests/Reader/Fixtures/ConfigObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
return (object)array(
'key' => 'value'
);
1 change: 1 addition & 0 deletions tests/Reader/Fixtures/Empty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
2 changes: 2 additions & 0 deletions tests/Reader/Fixtures/EmptyArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
return array();
2 changes: 2 additions & 0 deletions tests/Reader/Fixtures/EmptyNull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
return null;
2 changes: 2 additions & 0 deletions tests/Reader/Fixtures/EmptyObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
return new stdClass();
32 changes: 29 additions & 3 deletions tests/Reader/PhpReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,46 @@ protected function getFixturePath()
/**
* @covers ::getIterator
* @uses \Minphp\Configure\Reader\PhpReader
* @dataProvider getIteratorDataProvider
*
* @param string $path The path to the file to mock
* @param bool $empty True to verify the result is an empty array, or false that it contains a 'key' => 'value' pair
*/
public function testGetIterator()
public function testGetIterator($path, $empty)
{
$file = $this->getFileMock($this->getFixturePath() . "Config.php");
$file = $this->getFileMock($path);
$reader = new PhpReader($file);

// Ensure we can load the same file multiple times and get the same result
for ($i = 0; $i < 2; $i++) {
$result = $reader->getIterator($file);
$this->assertInstanceOf('\ArrayIterator', $result);
$this->assertEquals("value", $result['key']);

if ($empty === true) {
$this->assertEmpty($result);
} else {
$this->assertEquals("value", $result['key']);
}
}
}

/**
* Data Provider for ::testGetIterator
*/
public function getIteratorDataProvider()
{
$path = $this->getFixturePath();

return array(
array($path . 'Config.php', false),
array($path . 'ConfigObject.php', false),
array($path . 'Empty.php', true),
array($path . 'EmptyArray.php', true),
array($path . 'EmptyNull.php', true),
array($path . 'EmptyObject.php', true),
);
}

/**
* @covers ::getIterator
* @uses \Minphp\Configure\Reader\PhpReader
Expand Down

0 comments on commit 2ca3814

Please sign in to comment.