-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a88f04
commit e267ee6
Showing
8 changed files
with
431 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Fulcrum\Tests\Unit\Container; | ||
|
||
use Fulcrum\Container\DIContainer; | ||
use Fulcrum\Tests\Unit\UnitTestCase; | ||
|
||
class ContainerGetTest extends UnitTestCase | ||
{ | ||
public function testGet() | ||
{ | ||
$initialParameters = [ | ||
'foo' => 'Hello World', | ||
'bar' => 10, | ||
'baz' => 'Fulcrum', | ||
]; | ||
$container = new DIContainer($initialParameters); | ||
|
||
foreach ($initialParameters as $uniqueId => $value) { | ||
$this->assertEquals($value, $container->get($uniqueId)); | ||
} | ||
} | ||
|
||
public function testShouldGetByDotNotation() | ||
{ | ||
$container = new DIContainer([ | ||
'foo' => [ | ||
'bar' => [ | ||
'baz' => 'Hello World', | ||
], | ||
'baz' => 'Fulcrum', | ||
], | ||
]); | ||
|
||
$this->assertEquals('Fulcrum', $container->get('foo', 'baz')); | ||
$this->assertEquals([ | ||
'baz' => 'Hello World', | ||
], $container->get('foo', 'bar')); | ||
$this->assertEquals('Hello World', $container->get('foo', 'bar.baz')); | ||
} | ||
} |
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,79 @@ | ||
<?php | ||
|
||
namespace Fulcrum\Tests\Unit\Container; | ||
|
||
use Fulcrum\Container\DIContainer; | ||
use Fulcrum\Tests\Unit\UnitTestCase; | ||
|
||
class ContainerHasTest extends UnitTestCase | ||
{ | ||
public function testHas() | ||
{ | ||
$initialParameters = [ | ||
'foo' => 'Hello World', | ||
'bar' => 10, | ||
'baz' => 'Fulcrum', | ||
]; | ||
|
||
$container = new DIContainer($initialParameters); | ||
|
||
$this->assertTrue($container->has('foo')); | ||
$this->assertTrue($container->has('bar')); | ||
$this->assertTrue($container->has('baz')); | ||
} | ||
|
||
public function testContainerShouldHaveAfterAddingValues() | ||
{ | ||
$container = new DIContainer(); | ||
|
||
$container['fulcrum'] = 'some value'; | ||
$this->assertTrue($container->has('fulcrum')); | ||
|
||
$container['some_number'] = 52; | ||
$this->assertTrue($container->has('some_number')); | ||
|
||
$container['some_array'] = [ | ||
'foo' => 'foobar', | ||
]; | ||
$this->assertTrue($container->has('some_array')); | ||
} | ||
|
||
public function testContainerShouldNotValues() | ||
{ | ||
$initialParameters = [ | ||
'foo' => 'Hello World', | ||
'bar' => 10, | ||
'baz' => 'Fulcrum', | ||
]; | ||
|
||
$container = new DIContainer($initialParameters); | ||
|
||
$this->assertFalse($container->has('foobar')); | ||
$this->assertFalse($container->has('barbaz')); | ||
$this->assertFalse($container->has('zab')); | ||
|
||
$container['fulcrum'] = 'some value'; | ||
$container['some_number'] = 52; | ||
$container['some_array'] = [ | ||
'foo' => 'foobar', | ||
]; | ||
|
||
$this->assertFalse($container->has('doesnotexist')); | ||
} | ||
|
||
public function testShouldCheckDeeply() | ||
{ | ||
$container = new DIContainer([ | ||
'foo' => [ | ||
'bar' => [ | ||
'baz' => 'Hello World', | ||
], | ||
'baz' => 'Fulcrum', | ||
], | ||
]); | ||
|
||
$this->assertTrue($container->has('foo', 'baz')); | ||
$this->assertTrue($container->has('foo', 'bar')); | ||
$this->assertTrue($container->has('foo', 'bar.baz')); | ||
} | ||
} |
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,127 @@ | ||
<?php | ||
|
||
namespace Fulcrum\Tests\Unit\Container; | ||
|
||
use Brain\Monkey\Functions; | ||
use Fulcrum\Container\Exception\InvalidConcreteException; | ||
use Fulcrum\Container\DIContainer; | ||
use Fulcrum\Tests\Unit\UnitTestCase; | ||
|
||
class ContainerRegisterConcreteTest extends UnitTestCase | ||
{ | ||
public function testThrowsErrorForEmptyConcrete() | ||
{ | ||
$container = new DIContainer(); | ||
|
||
$errorMessage = 'Invalid concrete configuration. The "concrete" cannot be empty.'; | ||
Functions\when('__') | ||
->justEcho($errorMessage); | ||
|
||
$this->expectException(InvalidConcreteException::class); | ||
$this->expectOutputString($errorMessage); | ||
$container->registerConcrete([], 'foo'); | ||
$this->assertFalse($container->has('foo')); | ||
|
||
$this->expectException(InvalidConcreteException::class); | ||
$this->expectOutputString($errorMessage); | ||
$container->registerConcrete([ | ||
'concrete' => '', | ||
], 'foo'); | ||
$this->assertFalse($container->has('foo')); | ||
|
||
$this->expectException(InvalidConcreteException::class); | ||
$this->expectOutputString($errorMessage); | ||
$container->registerConcrete([ | ||
'autoload' => false, | ||
'concrete' => null, | ||
], 'foo'); | ||
$this->assertFalse($container->has('foo')); | ||
|
||
$this->expectException(InvalidConcreteException::class); | ||
$this->expectOutputString($errorMessage); | ||
$container->registerConcrete([ | ||
'autoload' => false, | ||
'concrete' => false, | ||
], 'foo'); | ||
$this->assertFalse($container->has('foo')); | ||
} | ||
|
||
public function testThrowsErrorWhenConcreteIsNotCallable() | ||
{ | ||
$container = new DIContainer(); | ||
|
||
$errorMessage = 'The specified concrete is not callable'; | ||
Functions\when('__') | ||
->justEcho($errorMessage); | ||
|
||
$this->expectException(InvalidConcreteException::class); | ||
$this->expectOutputString($errorMessage); | ||
$container->registerConcrete([ | ||
'concrete' => 'thisisnotcallablefunction', | ||
], 'foo'); | ||
$this->assertFalse($container->has('foo')); | ||
|
||
$this->expectException(InvalidConcreteException::class); | ||
$this->expectOutputString($errorMessage); | ||
$container->registerConcrete([ | ||
'concrete' => 42, | ||
], 'foo'); | ||
$this->assertFalse($container->has('foo')); | ||
} | ||
|
||
public function testConcreteShouldNotAutoloaded() | ||
{ | ||
$container = new DIContainer(); | ||
|
||
$concreteConfig = [ | ||
'autoload' => false, | ||
'concrete' => function () { | ||
return (object) ['bar' => 'some value']; | ||
}, | ||
]; | ||
|
||
$this->assertNull($container->registerConcrete($concreteConfig, 'foo')); | ||
$this->assertTrue($container->has('foo')); | ||
} | ||
|
||
public function testShouldInstantiateOutOfContainer() | ||
{ | ||
$container = new DIContainer(); | ||
|
||
$concreteConfig = [ | ||
'autoload' => false, | ||
'concrete' => function () { | ||
return (object) ['bar' => 'some value']; | ||
}, | ||
]; | ||
|
||
$this->assertNull($container->registerConcrete($concreteConfig, 'foo')); | ||
$this->assertTrue($container->has('foo')); | ||
|
||
// Now instantiate it. | ||
$foo = $container['foo']; | ||
$this->assertInstanceOf('stdClass', $foo); | ||
$this->assertEquals('some value', $foo->bar); | ||
} | ||
|
||
public function testConcreteShouldAutoload() | ||
{ | ||
$container = new DIContainer(); | ||
|
||
$concreteConfig = [ | ||
'autoload' => true, | ||
'concrete' => function () { | ||
$instance = new \stdClass(); | ||
$instance->bar = 'some value'; | ||
|
||
return $instance; | ||
}, | ||
]; | ||
|
||
$foo = $container->registerConcrete($concreteConfig, 'foo'); | ||
|
||
$this->assertTrue($container->has('foo')); | ||
$this->assertInstanceOf('stdClass', $foo); | ||
$this->assertEquals('some value', $foo->bar); | ||
} | ||
} |
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,58 @@ | ||
<?php | ||
|
||
namespace Fulcrum\Tests\Unit\Container; | ||
|
||
use Fulcrum\Container\DIContainer; | ||
use Fulcrum\Tests\Unit\UnitTestCase; | ||
|
||
class ContainerStoreTest extends UnitTestCase | ||
{ | ||
public function testShouldStoreWhenNotDotNotation() | ||
{ | ||
$container = new DIContainer(); | ||
|
||
$container->store('foo', 'Hello World'); | ||
$this->assertEquals('Hello World', $container->get('foo')); | ||
|
||
$container->store('bar', 'Fulcrum Rocks!'); | ||
$this->assertEquals('Fulcrum Rocks!', $container->get('bar')); | ||
|
||
$container->store('foo.bar', 'This unique key has a dot.'); | ||
$this->assertEquals('This unique key has a dot.', $container->get('foo.bar')); | ||
} | ||
|
||
public function testShouldDeeplyStoreWhenDotNotation() | ||
{ | ||
$container = new DIContainer([ | ||
'foo' => [ | ||
'bar' => [ | ||
'baz' => 'Hi there', | ||
], | ||
'baz' => 10, | ||
], | ||
]); | ||
|
||
$this->assertEquals(10, $container->get('foo')['baz']); | ||
$container->store('foo', 47, 'baz'); | ||
$this->assertEquals(47, $container->get('foo')['baz']); | ||
|
||
$this->assertEquals('Hi there', $container->get('foo')['bar']['baz']); | ||
$container->store('foo', 'Hello World', 'bar.baz'); | ||
$this->assertEquals('Hello World', $container->get('foo')['bar']['baz']); | ||
} | ||
|
||
public function testShouldDeeplyStoreWhenDoesNotExist() | ||
{ | ||
$container = new DIContainer(); | ||
|
||
$container->store('foo', 47, 'baz'); | ||
$this->assertEquals(47, $container->get('foo')['baz']); | ||
$container->store('foo', 19, 'baz'); | ||
$this->assertEquals(19, $container->get('foo')['baz']); | ||
|
||
$container->store('foo', 'Hello World', 'bar.baz'); | ||
$this->assertEquals('Hello World', $container->get('foo')['bar']['baz']); | ||
$container->store('foo', 'Changing it', 'bar.baz'); | ||
$this->assertEquals('Changing it', $container->get('foo')['bar']['baz']); | ||
} | ||
} |
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,57 @@ | ||
<?php | ||
|
||
namespace Fulcrum\Tests\Unit\Container; | ||
|
||
use Fulcrum\Container\DIContainer; | ||
use Fulcrum\Tests\Unit\UnitTestCase; | ||
|
||
class ContainerTest extends UnitTestCase | ||
{ | ||
public function testInitialParametersShouldLoad() | ||
{ | ||
$initialParameters = [ | ||
'foo' => 'Hello World', | ||
'bar' => 10, | ||
'baz' => 'Fulcrum', | ||
]; | ||
|
||
$container = new DIContainer($initialParameters); | ||
|
||
$this->assertEquals('Hello World', $container['foo']); | ||
$this->assertEquals(10, $container['bar']); | ||
$this->assertEquals('Fulcrum', $container['baz']); | ||
|
||
foreach ($initialParameters as $uniqueId => $value) { | ||
$this->assertEquals($value, $container[$uniqueId]); | ||
} | ||
} | ||
|
||
public function testAddingValuesToContainer() | ||
{ | ||
$container = new DIContainer(); | ||
|
||
$container['fulcrum'] = 'some value'; | ||
$this->assertEquals('some value', $container['fulcrum']); | ||
|
||
$container['some_number'] = 52; | ||
$this->assertEquals(52, $container['some_number']); | ||
|
||
$container['some_array'] = [ | ||
'foo' => 'foobar', | ||
]; | ||
$this->assertEquals([ | ||
'foo' => 'foobar', | ||
], $container['some_array']); | ||
} | ||
|
||
public function testAddingClosureToContainer() | ||
{ | ||
$container = new DIContainer(); | ||
|
||
$container['some_closure'] = function () { | ||
return new \stdClass(); | ||
}; | ||
|
||
$this->assertInstanceOf('stdClass', $container['some_closure']); | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace Fulcrum\Tests\Unit; | ||
|
||
use Brain\Monkey; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
abstract class UnitTestCase extends TestCase | ||
{ | ||
/** | ||
* Prepares the test environment before each test. | ||
*/ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
Monkey\setUp(); | ||
} | ||
|
||
/** | ||
* Cleans up the test environment after each test. | ||
*/ | ||
protected function tearDown() | ||
{ | ||
Monkey\tearDown(); | ||
parent::tearDown(); | ||
} | ||
} |
Oops, something went wrong.