Skip to content

Commit

Permalink
CHG: More testing to cover edges cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gravataLonga committed May 9, 2020
1 parent 03978da commit e3eaeeb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,23 @@ All notable changes to `container` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## 1.4.1 - 2020-05-06

### Added
- Added: GrumpPHP, thank to [drupol](https://github.com/drupol).

### Deprecated
- Nothing

### Fixed
- Some internal change of method visibility.

### Removed
- Nothing

### Security
- Nothing

## 1.4.0 - 2020-05-06

### Added
Expand Down
4 changes: 3 additions & 1 deletion src/Container.php
Expand Up @@ -213,7 +213,7 @@ function (ReflectionParameter $param) use ($arguments) {
return $this->get($param->getName());
}

throw ContainerException::findType($param->getClass());
throw ContainerException::findType($type);
}

if (true === $type->isBuiltin()) {
Expand All @@ -224,6 +224,8 @@ function (ReflectionParameter $param) use ($arguments) {
if ($type->allowsNull()) {
return null;
}

throw ContainerException::findType($type);
}

if (ContainerInterface::class === $type->getName()) {
Expand Down
8 changes: 4 additions & 4 deletions src/ContainerException.php
Expand Up @@ -6,18 +6,18 @@

use Exception;
use Psr\Container\ContainerExceptionInterface;
use ReflectionClass;
use ReflectionNamedType;

final class ContainerException extends Exception implements ContainerExceptionInterface
{
/**
* @param ReflectionClass|null $class
* @param ReflectionNamedType|null $class
*
* @return ContainerException
*/
public static function findType(?ReflectionClass $class): ContainerException
public static function findType(?ReflectionNamedType $class): ContainerException
{
return new self(sprintf('Unable to find type hint of %s', ($class ? $class->getName() : '')));
return new self(sprintf('Unable to find type hint (%s)', ($class ? $class->getName() : '')));
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/ResolveConstructionTest.php
Expand Up @@ -5,6 +5,7 @@
namespace Tests;

use Gravatalonga\Container\Container;
use Gravatalonga\Container\ContainerException;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Tests\Stub\Bar;
Expand Down Expand Up @@ -106,4 +107,26 @@ public function testIfParamIsNullableButWeHaveValueFromContainer()
self::assertInstanceOf(FooBarWithNullClass::class, $class);
self::assertEquals('my-var', $class->name);
}

/**
* @test
*/
public function testGotExceptionFromVariableNameAndBuiltTypeNotIntoContainer()
{
$this->expectException(ContainerException::class);
$this->expectExceptionMessage("Unable to find type hint (string)");
$container = new Container();
$class = $container->get(FooBarClass::class);
}

/**
* @test
*/
public function testGotExceptionFromVariableNameNotIntoContainer()
{
$this->expectException(ContainerException::class);
$this->expectExceptionMessage("Unable to find type hint ()");
$container = new Container();
$class = $container->get(FooBarWithoutBuiltInTypeClass::class);
}
}

0 comments on commit e3eaeeb

Please sign in to comment.