-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Library tests are now separated from "feature" tests, which explain better what each feature of the component is and how it works. Works better than a README if you ask me, or Respect\Doc for that matter - which suffered an overhaul that I still haven't take the time to understand. Previous coverage is unchanged, although more tests were added for clarity and better coverage of each feature usage. Changes ------- You can now indent INI declarations, every blank space before the start of any declaration will be removed. A bug happened when you manipulated the container by defining an item entry with an instance. The "ifs" surrounding `$this->configure()` were unnecessary. Describes development dependencies better on `composer.json` to run the test suite.
- Loading branch information
Showing
15 changed files
with
1,121 additions
and
636 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace Test\Feature; | ||
|
||
use Respect\Config\Container; | ||
|
||
class ItemConsuptionViaCallback extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testLateDefinitionOfVariableExpansionThroughContainerCallbackReturnsContainer() | ||
{ | ||
$c = new Container(<<<INI | ||
foo = [undef] | ||
bar = [foo] | ||
INI | ||
); | ||
$definition = array('undef' => 'Hello'); | ||
$result = $c($definition); | ||
|
||
$this->assertEquals( | ||
'Hello', | ||
$result->bar, | ||
'Calling the container as a function will append the array passed as content to it.' . PHP_EOL . | ||
'It will return the itself, as well.' | ||
); | ||
$this->assertSame( | ||
$result->bar, | ||
$c->bar, | ||
"But it doesn't matter on which instance of the container you call." | ||
); | ||
} | ||
|
||
public function testLateDefinitionOfVariableExpansionThroughItemCallbackReturnsValue() | ||
{ | ||
$c = new Container(<<<INI | ||
foo = [undef] | ||
bar = [foo] | ||
INI | ||
); | ||
|
||
$result = $c->bar(array('undef'=>'Hello')); | ||
$this->assertEquals('Hello', $result); | ||
} | ||
|
||
public function testRetrievalOfItemThroughInstanceTypeOnContainerCallbackReturnsValue() | ||
{ | ||
$called = false; | ||
$c = new Container(<<<INI | ||
[instanceof DateTime] | ||
time = now | ||
INI | ||
); | ||
$result = $c(function(\DateTime $date) use (&$called) { | ||
$called = true; | ||
return $date; | ||
}); | ||
|
||
$result2 = $c['DateTime']; | ||
$this->assertInstanceOf('DateTime', $result); | ||
$this->assertInstanceOf('DateTime', $result2); | ||
$this->assertTrue($called); | ||
} | ||
|
||
public function testRetrievalOfInstanceTypeThroughContainerCallbackReturnsValueEvenWithoutDeclaringItsType() | ||
{ | ||
$c = new Container(); | ||
$c(new \DateTime); | ||
$called = false; | ||
|
||
$result = $c(function(\DateTime $date) use (&$called) { | ||
$called = true; | ||
return $date; | ||
}); | ||
|
||
$result2 = $c['DateTime']; | ||
$this->assertInstanceOf('DateTime', $result); | ||
$this->assertInstanceOf('DateTime', $result2); | ||
$this->assertTrue($called); | ||
} | ||
|
||
public function testContainerCallbackReceivingACallableCallsItAndReturnsValue() | ||
{ | ||
$c = new Container(); | ||
$c(new \DateTime); | ||
$result = $c(array('Test\Stub\TimePrinter', 'returnTimePassedAsArgument')); | ||
$this->assertInstanceOf('DateTime', $result); | ||
} | ||
} | ||
|
||
namespace Test\Stub; | ||
|
||
class TimePrinter | ||
{ | ||
public function returnTimePassedAsArgument(\DateTime $time) | ||
{ | ||
return $time; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Test\Feature; | ||
|
||
use Respect\Config\Container; | ||
|
||
class ContainerAsArray extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @TODO Fix issue where passing INI to container construction does not | ||
* allow ArrayAccess usage. T.T | ||
*/ | ||
public function testUsingItemsAsContainerWasAnArray() | ||
{ | ||
$c = new Container(); | ||
$c->loadString(<<<INI | ||
fibonacci = [1, 1, 2, 3, 5] | ||
INI | ||
); | ||
|
||
$this->assertInstanceOf( | ||
'ArrayAccess', | ||
$c, | ||
'The container implements the \ArrayAccess interface, so it behaves like an array.' | ||
); | ||
$this->assertTrue( | ||
isset($c['fibonacci']) | ||
); | ||
$this->assertEquals( | ||
array(1, 1, 2, 3, 5), | ||
$c['fibonacci'], | ||
'The container implements the \ArrayAccess interface, so it behaves like an array.' | ||
); | ||
} | ||
|
||
public function testDefiningItemWithArrayLikeNotation() | ||
{ | ||
$c = new Container; | ||
|
||
$c['not'] = false; | ||
$this->assertTrue(isset($c['not'])); | ||
$this->assertFalse($c['not']); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Test\Feature; | ||
|
||
use Respect\Config\Container; | ||
|
||
class ContainerManipulation extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testDefiningNewItemOnContainer() | ||
{ | ||
$c = new Container; | ||
|
||
$c->name = 'John Snow'; | ||
|
||
$this->assertEquals( | ||
'John Snow', | ||
$c->name, | ||
'You can define new items just like you would define a property on an instance.' | ||
); | ||
} | ||
|
||
public function testDefinitionOfItemWithAnonymousFunctionExecutesItAndReturnsItsValueUponUsage() | ||
{ | ||
$c = new Container; | ||
|
||
$c->name = function() { return 'John Doe'; }; | ||
|
||
$this->assertEquals( | ||
'John Doe', | ||
$c->name, | ||
'The function gets executed and the return value is stored in the container.' | ||
); | ||
} | ||
|
||
public function testDefinitionOfItemOnContainerWithItems() | ||
{ | ||
$c = new Container(<<<INI | ||
respect_blah = "" | ||
INI | ||
); | ||
|
||
$c->panda = function() { return 'ok'; }; | ||
|
||
$this->assertEquals( | ||
'ok', | ||
$c->panda, | ||
'It works if the container has stuff or not.' | ||
); | ||
} | ||
|
||
/** | ||
* @group issue | ||
* @ticket 14 | ||
*/ | ||
public function testItemOverwrrite() | ||
{ | ||
$c = new Container(<<<INI | ||
[pdo StdClass] | ||
[db Test\Stub\DatabaseWow] | ||
con = [pdo]; | ||
INI | ||
); | ||
|
||
$c->pdo = new \PDO('sqlite::memory:'); | ||
|
||
$this->assertNotInstanceOf( | ||
'StdClass', | ||
$c->pdo, | ||
'Although PDO was defined with StdClass we overwritten it to a proper instance of PDO manually.' | ||
); | ||
|
||
$this->assertSame( | ||
$c->pdo, | ||
$c->db->c, | ||
'This should overwrite every usage of that element inside the container.' | ||
); | ||
} | ||
} | ||
|
||
namespace Test\Stub; | ||
|
||
class DatabaseWow | ||
{ | ||
public $c; | ||
|
||
public function __construct($con) | ||
{ | ||
$this->c = $con; | ||
} | ||
} | ||
|
Oops, something went wrong.