Skip to content

Commit

Permalink
Merge pull request #7 from jshannon63/analysis-qJax4V
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
jshannon63 authored Nov 17, 2017
2 parents 19f3cef + af6e6cf commit 6ce8028
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 42 deletions.
5 changes: 2 additions & 3 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,9 @@ public function make(string $id)
// dependency so if the dependency is rebound later, it can force
// the upstream bindings to refresh its' dependency cache.
else {
if(!$this->has($dependency->name)){
if (! $this->has($dependency->name)) {
$dependencies[] = $this->make($dependency->name); // recursive call
}
else{
} else {
$dependencies[] = $this[$dependency->name];
}
$this->bindings[$dependency->name]['depender'][] = $id;
Expand Down
72 changes: 33 additions & 39 deletions tests/unit/containerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

namespace Tests;

use Jshannon63\Cobalt\NotFoundException;
use Jshannon63\Cobalt\ContainerException;
use PHPUnit\Framework\TestCase;
use Jshannon63\Cobalt\Container;
use Jshannon63\Cobalt\NotFoundException;
use Jshannon63\Cobalt\ContainerException;

// test interface for Foo
interface FooInterface
{
}

class FooNotInstantiable implements FooInterface{
private function __construct() { }
class FooNotInstantiable implements FooInterface
{
private function __construct()
{
}
}

// test class Foo
Expand Down Expand Up @@ -91,7 +94,9 @@ public function __construct(Container $app)
}
}

class Yaz{}
class Yaz
{
}

class Zaz
{
Expand All @@ -117,7 +122,6 @@ public function testContainerInitialization()
// check Container instance is bound as singleton
$app2 = $app->make(Container::class);
$this->assertTrue($app === $app2);

}

// make sure a singleton binding returns the same instance each time.
Expand Down Expand Up @@ -186,15 +190,14 @@ public function testSingletonClosureInjection()

$app->bind('Foo', function () {
return new Foo(new Bar(new Baz('Dependency Injection Rocks!')));
},true);
}, true);

$foo = $app->resolve('Foo');
$foo2 = $app->resolve('Foo');


$this->assertContains('Dependency Injection Rocks!', $foo->bar()->baz()->sayWords());

$this->assertSame($foo,$foo2);
$this->assertSame($foo, $foo2);
}

// store a new binding through ArrayAccess method and retrieve it with ge
Expand Down Expand Up @@ -229,9 +232,9 @@ public function testCachedBindings()

$app = new Container();

$app->bind('Foo',Foo::class,false);
$app->bind('Foo', Foo::class, false);
$instance1 = $app->make('Foo');
for($i=0;$i<50000;$i++){
for ($i = 0; $i < 50000; $i++) {
$instance2 = $app->make('Foo');
}
$this->assertFalse($instance1 === $instance2);
Expand All @@ -240,19 +243,18 @@ public function testCachedBindings()

$app2 = new Container('cached');

$app2->bind('Foo2',Foo::class,false);
$app2->bind('Foo2', Foo::class, false);
$instance3 = $app2->make('Foo2');
for($i=0;$i<50000;$i++){
for ($i = 0; $i < 50000; $i++) {
$instance4 = $app2->make('Foo2');
}
$this->assertFalse($instance3 === $instance4);

$after = microtime(true);

// cached mode should be at least 5x as fast.
echo ($between-$before)/(($after-$between));
$this->assertGreaterThan(($after-$between)*5,$between-$before);

echo($between - $before) / (($after - $between));
$this->assertGreaterThan(($after - $between) * 5, $between - $before);
}

public function testBindingsListing()
Expand All @@ -261,8 +263,7 @@ public function testBindingsListing()

$this->assertTrue($app->has(Container::class));

$this->assertArrayHasKey(Container::class,$app->getBindings());

$this->assertArrayHasKey(Container::class, $app->getBindings());
}

public function testInstanceBinding()
Expand All @@ -271,8 +272,7 @@ public function testInstanceBinding()

$fiz = $app->instance('Fiz', new Fiz($app));

$this->assertInstanceOf(Fiz::class,$fiz);

$this->assertInstanceOf(Fiz::class, $fiz);
}

public function testGetMethods()
Expand All @@ -282,9 +282,8 @@ public function testGetMethods()
$myapp = $app->get(Container::class);
$myapp2 = $app->offsetGet(Container::class);

$this->assertSame($app,$myapp);
$this->assertSame($app,$myapp2);

$this->assertSame($app, $myapp);
$this->assertSame($app, $myapp2);
}

public function testSetterUnsetter()
Expand All @@ -300,14 +299,13 @@ public function testSetterUnsetter()
$this->assertFalse($app->has('Fiz'));

$this->assertFalse($app->offsetExists('Fiz'));

}

public function testGetContainer()
{
$app = new Container();

$this->assertSame($app,$app::getContainer());
$this->assertSame($app, $app::getContainer());
}

public function testOffsetGetException()
Expand All @@ -334,19 +332,19 @@ public function testBindingException()

$this->expectException(ContainerException::class);

$app->bind('abc','123');
$app->bind('abc', '123');
}

public function testSharedModeForcesSingleton()
{
$app = new Container('shared');

$app->bind('Foo',Foo::class,false);
$app->bind('Foo', Foo::class, false);

$instance1 = $app['Foo'];
$instance2 = $app['Foo'];

$this->assertSame($instance1,$instance2);
$this->assertSame($instance1, $instance2);
}

public function testRebindingBustsDependerCache()
Expand All @@ -357,26 +355,23 @@ public function testRebindingBustsDependerCache()

$foo = $app->resolve(Foo::class);

$this->assertContains(Foo::class,$app->getBinding(Bar::class)['depender']);
$this->assertContains(Foo::class, $app->getBinding(Bar::class)['depender']);
$this->assertTrue($app->getBinding(Foo::class)['cached']);

$app->bind(Bar::class);

$bar = $app->resolve(Bar::class);

$this->assertFalse($app->getBinding(Foo::class)['cached']);

}

public function testClassNotInstantiable()
{

$app = new Container();

$this->expectException(ContainerException::class);

$app->make(FooNotInstantiable::class);

}

public function testNonClassWithoutDefaultValue()
Expand All @@ -386,21 +381,20 @@ public function testNonClassWithoutDefaultValue()
$this->expectException(ContainerException::class);

$app->make(Zaz::class);

}

public function testNonConstructorClasses()
{
$app = new Container();

$app->bind('YazFactory',Yaz::class,false);
$app->bind('YazSingleton',Yaz::class,true);
$app->bind('YazFactory', Yaz::class, false);
$app->bind('YazSingleton', Yaz::class, true);

$yaz = $app->make('YazFactory');
$yaz1= $app->make('YazSingleton');
$yaz2= $app->make('YazSingleton');
$yaz1 = $app->make('YazSingleton');
$yaz2 = $app->make('YazSingleton');

$this->assertNotSame($yaz,$yaz1);
$this->assertSame($yaz1,$yaz2);
$this->assertNotSame($yaz, $yaz1);
$this->assertSame($yaz1, $yaz2);
}
}

0 comments on commit 6ce8028

Please sign in to comment.