Skip to content

Commit

Permalink
Merge pull request #175 from koriym/internal-types
Browse files Browse the repository at this point in the history
Add internal type binding test
  • Loading branch information
koriym committed Apr 21, 2018
2 parents 6020a71 + 2f073be commit 804da3e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Argument.php
Expand Up @@ -109,7 +109,7 @@ private function setDefaultValue(\ReflectionParameter $parameter)
private function getType(\ReflectionParameter $parameter) : string
{
$type = $parameter->getType();
if ($type instanceof \ReflectionType && \in_array((string) $type, ['bool', 'int', 'string', 'array'], true)) {
if ($type instanceof \ReflectionType && \in_array((string) $type, ['bool', 'int', 'string', 'array', 'resource', 'callable'], true)) {
return '';
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ArgumentTest.php
Expand Up @@ -29,7 +29,7 @@ public function testToString()

public function testToStringScalar()
{
$argument = new Argument(new \ReflectionParameter([FakeScalarType::class, 'stringId'], 'id'), Name::ANY);
$argument = new Argument(new \ReflectionParameter([FakeInternalTypes::class, 'stringId'], 'id'), Name::ANY);
$this->assertSame('-' . Name::ANY, (string) $argument);
}
}
16 changes: 16 additions & 0 deletions tests/Fake/FakeInternalTypeModule.php
@@ -0,0 +1,16 @@
<?php
namespace Ray\Di;

class FakeInternalTypeModule extends AbstractModule
{
protected function configure()
{
$this->bind('')->annotatedWith('type-bool')->toInstance(false);
$this->bind('')->annotatedWith('type-int')->toInstance(1);
$this->bind('')->annotatedWith('type-string')->toInstance('1');
$this->bind('')->annotatedWith('type-array')->toInstance([1]);
$this->bind('')->annotatedWith('type-callable')->toInstance(function(){});
$this->bind('')->annotatedWith('type-object')->toInstance(new \stdClass);
$this->bind('')->annotatedWith('type-resource')->toInstance(fopen("foo", "w"));
}
}
35 changes: 35 additions & 0 deletions tests/Fake/FakeInternalTypes.php
@@ -0,0 +1,35 @@
<?php
namespace Ray\Di;

use Ray\Di\Di\Named;

class FakeInternalTypes
{
public $bool;
public $int;
public $string;
public $array;
public $callable;

/**
* @Named("bool=type-bool,int=type-int,string=type-string,array=type-array,callable=type-callable")
*/
public function __construct(
bool $bool,
int $int,
string $string,
array $array,
callable $callable
) {
$this->bool = $bool;
$this->int = $int;
$this->string = $string;
$this->array = $array;
$this->callable = $callable;
}

public function stringId(string $id)
{
unset($id);
}
}
10 changes: 0 additions & 10 deletions tests/Fake/FakeScalarType.php

This file was deleted.

12 changes: 12 additions & 0 deletions tests/InjectorTest.php
Expand Up @@ -294,4 +294,16 @@ public function testIsOptionalValue()
$this->assertInstanceOf(\PDO::class, $pdo);
}
}

public function testInternalTypes()
{
$injector = new Injector(new FakeInternalTypeModule);
/* @var FakeInternalTypes $types */
$types = $injector->getInstance(FakeInternalTypes::class);
$this->assertInternalType('bool', $types->bool);
$this->assertInternalType('callable', $types->callable);
$this->assertInternalType('array', $types->array);
$this->assertInternalType('string', $types->string);
$this->assertInternalType('int', $types->int);
}
}

0 comments on commit 804da3e

Please sign in to comment.