Skip to content

Commit

Permalink
- cleans up the unit tests (cannot skip tests now)
Browse files Browse the repository at this point in the history
- benchmark tests updated
  • Loading branch information
kodeart committed Sep 10, 2019
1 parent ece87f9 commit 6aee2bd
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 89 deletions.
4 changes: 2 additions & 2 deletions Tests/PhpBench/SimpleAppBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class SimpleAppBench extends AbstractBench
* @Iterations(5)
* @Assert(1000)
*/
public function benchAppCall()
public function benchAppInvoke()
{
$dispatcher = $this->di->inject(PostCommandDispatcher::class, ['hello']);
$this->di->call([$dispatcher, 'get']);
($this->di)([$dispatcher, 'get']);
}

protected function modules(...$modules)
Expand Down
3 changes: 0 additions & 3 deletions Tests/Unit/BindMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@

class BindMethodTest extends DITestCase
{

public function testUntargetedBinding()
{
$this->assertSkippedTest(__FUNCTION__);

$this->di->named('$arg', 'foobar');
$this->di->bind(TestClassWithPrimitiveConstructorArgument::class, '$arg');

Expand Down
8 changes: 0 additions & 8 deletions Tests/Unit/DITestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ abstract class DITestCase extends TestCase
{
/** @var DIContainer */
protected $di;
protected $skippedTests = [];

abstract protected function createContainer(): DIContainer;

Expand All @@ -22,11 +21,4 @@ protected function tearDown(): void
{
$this->di = null;
}

protected function assertSkippedTest($function)
{
if (isset($this->skippedTests[$function])) {
$this->markTestSkipped($this->skippedTests[$function]);
}
}
}
22 changes: 6 additions & 16 deletions Tests/Unit/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Koded\Tests\Unit;

use Koded\DIContainer;
use Koded\DIException;
use Koded\{DIContainer, DIException};
use Psr\Container\NotFoundExceptionInterface;

class ExceptionsTest extends DITestCase
Expand All @@ -24,10 +23,11 @@ public function testForCircularDependency()
$this->di->inject(TestCircularDependencyA::class);
}

// public function testInvokeMethodForInvalidMethod()
// {
// $this->di->call([\stdClass::class, 'fubar']);
// }
public function testInvokeMethodForInvalidMethod()
{
$this->expectException(\TypeError::class);
($this->di)([\stdClass::class, 'fubar']);
}

public function testForClassWithNonPublicConstructor()
{
Expand Down Expand Up @@ -75,16 +75,6 @@ public function testForPsr11GetMethod()
$this->di->get('Fubar');
}

// public function testSingletonCreateWithBinding()
// {
// $this->expectException(DIException::class);
// $this->expectExceptionCode(DIException::E_CANNOT_BIND_INSTANCE);
// $this->expectExceptionMessage('Cannot create a singleton instance');
//
// $this->di->bind(TestOtherInterface::class, TestClassWithInterfaceAndNoConstructor::class);
// $this->di->singleton(TestClassWithInterfaceAndNoConstructor::class);
// }

protected function createContainer(): DIContainer
{
return new DIContainer;
Expand Down
13 changes: 0 additions & 13 deletions Tests/Unit/InjectObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@ public function testInjectOnDemand()

public function testClassWithoutConstructorArguments()
{
$this->assertSkippedTest(__FUNCTION__);

$instance = $this->di->inject(TestClassWithoutConstructorArguments::class);

$this->assertInstanceOf(TestClassWithoutConstructorArguments::class, $instance);
}

public function testClassWithConstructorArguments()
{
$this->assertSkippedTest(__FUNCTION__);

$this->di->named('$pdo', new PDO('sqlite:'));
$instance = $this->di->inject(TestClassWithConstructorArguments::class);

Expand All @@ -38,8 +33,6 @@ public function testClassWithConstructorArguments()

public function testChildClassWithInterfaceWithoutMapping()
{
$this->assertSkippedTest(__FUNCTION__);

$this->expectException(DIException::class);
$this->expectExceptionCode(DIException::E_CANNOT_INSTANTIATE);

Expand All @@ -48,8 +41,6 @@ public function testChildClassWithInterfaceWithoutMapping()

public function testChildClassWithInterfaceWithMapping()
{
$this->assertSkippedTest(__FUNCTION__);

$this->di->bind(TestInterface::class, TestClassWithInterfaceAndNoConstructor::class);
$instance = $this->di->inject(TestClassWithInterfaceDependency::class);

Expand All @@ -58,8 +49,6 @@ public function testChildClassWithInterfaceWithMapping()

public function testClassWithMultipleDependencies()
{
$this->assertSkippedTest(__FUNCTION__);

$instance = $this->di->inject(TestClassWithMultipleDependencies::class, ['val1', 42, false, ['val2']]);

$this->assertSame('val1', $instance->a);
Expand All @@ -81,8 +70,6 @@ public function testChildClassWithNonPublicConstructor()

public function testArrayObject()
{
$this->assertSkippedTest(__FUNCTION__);

/** @var ArrayObject $instance */
$instance = $this->di->inject(ArrayObject::class, [['foo' => 'bar'], ArrayObject::ARRAY_AS_PROPS]);

Expand Down
18 changes: 8 additions & 10 deletions Tests/Unit/InvokeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@ class InvokeTest extends DITestCase
{
public function testInvokeMethod()
{
$this->assertSkippedTest(__FUNCTION__);

$instance = $this->di->inject(TestClassForInvokeMethod::class, ['initial value']);

$this->assertSame('initial value', $this->di->call([$instance, 'get']));
$this->assertSame('from arguments', $this->di->call([$instance, 'get'], ['from arguments']));
$this->assertSame('initial value', ($this->di)([$instance, 'get']));
$this->assertSame('from arguments', ($this->di)([$instance, 'get'], ['from arguments']));

$this->assertSame('from __invokable', $this->di->call($instance, ['from __invokable']));
$this->assertSame('initial value', $this->di->call($instance));
$this->assertSame('from __invokable', ($this->di)($instance, ['from __invokable']));
$this->assertSame('initial value', ($this->di)($instance));

$cb = function($value) {
return $value;
};
$this->assertSame(null, $this->di->call($cb), 'Injects NULL for non-typed function arguments');
$this->assertSame('from closure', $this->di->call($cb, ['from closure']));
$this->assertSame(null, ($this->di)($cb), 'Injects NULL for non-typed function arguments');
$this->assertSame('from closure', ($this->di)($cb, ['from closure']));

$this->assertSame('from function', $this->di->call('sprintf', ['%s', 'from function']));
$this->assertSame('from function', ($this->di)('sprintf', ['%s', 'from function']));

$this->assertSame('from static method', $this->di->call(
$this->assertSame('from static method', ($this->di)(
[TestClassForInvokeMethod::class, 'value'],
['from static method']
));
Expand Down
9 changes: 0 additions & 9 deletions Tests/Unit/NamedMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@

class NamedMethodTest extends DITestCase
{
protected $skippedTests = [
'testShouldSetTheNamedParameter',
'testShouldThrowExceptionForInvalidParameterName'
];

public function testShouldSetTheNamedParameter()
{
$this->assertSkippedTest(__FUNCTION__);

$name = '$name';
$this->di->named($name, 42);
$this->assertSame(42, $this->di->getStorage()[DIContainer::NAMED][$name]);
Expand All @@ -29,8 +22,6 @@ public function testShouldSetTheNamedParameter()
*/
public function testShouldThrowExceptionForInvalidParameterName($name)
{
$this->assertSkippedTest(__FUNCTION__);

$this->expectException(DIException::class);
$this->expectExceptionCode(DIException::E_INVALID_PARAMETER_NAME);

Expand Down
6 changes: 0 additions & 6 deletions Tests/Unit/Psr11Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,20 @@ class Psr11Test extends DITestCase
{
public function testGetMethodForInjectedDependency()
{
$this->assertSkippedTest(__FUNCTION__);

$instance = $this->di->get(TestClassWithInterfaceDependency::class);
$this->assertInstanceOf(TestClassWithInterfaceDependency::class, $instance);
$this->assertInstanceOf(TestClassWithInterfaceAndNoConstructor::class, $instance->getDependency());
}

public function testHasMethod()
{
$this->assertSkippedTest(__FUNCTION__);

$this->assertFalse($this->di->has('Fubar'));
$this->assertTrue($this->di->has(TestInterface::class));
$this->assertTrue($this->di->has(TestClassWithInterfaceDependency::class));
}

public function testNamedDependency()
{
$this->assertSkippedTest(__FUNCTION__);

$this->di->named('$named', 42);
$this->assertTrue($this->di->has('$named'));
$this->assertSame(42, $this->di->get('$named'));
Expand Down
11 changes: 5 additions & 6 deletions Tests/Unit/ShareMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace Koded\Tests\Unit;

use Koded\{DIContainer, DIModule, Stdlib\Interfaces\ArrayDataFilter};
use Koded\{DIContainer, DIModule};
use Koded\Stdlib\Interfaces\ArrayDataFilter;

class ShareMethodTest extends DITestCase
{
public function test()
public function testImmutability()
{
$this->assertSkippedTest(__FUNCTION__);

$actual = $this->di->get(TestClassWithInterfaceDependency::class);
$this->assertInstanceOf(TestClassWithInterfaceDependency::class, $actual);

Expand All @@ -33,8 +32,8 @@ public function testImplementedInterfaces()

protected function createContainer(): DIContainer
{
return new DIContainer(new class implements DIModule {

return new DIContainer(new class implements DIModule
{
public function configure(DIContainer $injector): void
{
$injector->bind(TestInterface::class, TestClassWithInterfaceAndNoConstructor::class);
Expand Down
10 changes: 2 additions & 8 deletions Tests/Unit/SingletonMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@

namespace Koded\Tests\Unit;

use Koded\{DIContainer, DIException};
use Koded\DIContainer;

class SingletonMethodTest extends DITestCase
{
public function testSingletonCreateWithoutBinding()
{
$this->assertSkippedTest(__FUNCTION__);

$singleton = $this->di->singleton(TestClassWithInterfaceAndNoConstructor::class);
$other = $this->di->singleton(TestClassWithInterfaceAndNoConstructor::class);

$this->assertSame($singleton, $other);
}

public function testSingletonCreateWithInjectMethod()
public function testSingletonCreateWithInjectMethod()
{
$this->assertSkippedTest(__FUNCTION__);

$singleton = $this->di->singleton(TestClassWithInterfaceAndNoConstructor::class);
$other = $this->di->inject(TestClassWithInterfaceAndNoConstructor::class);

Expand All @@ -28,8 +24,6 @@ public function testSingletonCreateWithInjectMethod()

public function testSingletonInstance()
{
$this->assertSkippedTest(__FUNCTION__);

$instance = $this->di->singleton(TestSingletonInstance::class);
$this->assertSame('foobar', $instance->var);

Expand Down
8 changes: 1 addition & 7 deletions Tests/Unit/WiringAndInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,20 @@ class WiringAndInjectionTest extends DITestCase
{
public function testWithSingleton()
{
$this->assertSkippedTest(__FUNCTION__);

$this->di->singleton(PDO::class, ['sqlite:']);
$dispatcher = $this->di->inject(PostCommandDispatcher::class, ['hello']);
$this->assert($dispatcher);
}

public function testWithNamedValueParameter()
{
$this->assertSkippedTest(__FUNCTION__);

$this->di->named('$dsn', 'sqlite:');
$dispatcher = $this->di->inject(PostCommandDispatcher::class, ['hello']);
$this->assert($dispatcher);
}

public function testWithNamedInstanceParameter()
{
$this->assertSkippedTest(__FUNCTION__);

$this->di->named('$pdo', new PDO('sqlite:'));
$dispatcher = $this->di->inject(PostCommandDispatcher::class, ['hello']);
$this->assert($dispatcher);
Expand All @@ -50,7 +44,7 @@ public function configure(DIContainer $injector): void

private function assert($dispatcher): void
{
[$user, $post] = $this->di->call([$dispatcher, 'get']);
[$user, $post] = ($this->di)([$dispatcher, 'get']);

$this->assertSame('anonymous', $user);
$this->assertSame([42, 'Hello from DIC', 'hello'], $post);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use JsonSerializable;
use Koded\Stdlib\Config;
use PDO;
use PHPUnit\Exception;
use Exception;

interface PostRepository
{
Expand Down

0 comments on commit 6aee2bd

Please sign in to comment.