Skip to content

Commit

Permalink
Fix #65: Add enum tests (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jul 17, 2022
1 parent f77a663 commit 9260e1a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tests/Php8/InjectorTest.php
Expand Up @@ -9,10 +9,14 @@
use Countable;
use DateTimeImmutable;
use DateTimeInterface;
use Psr\Container\NotFoundExceptionInterface;
use stdClass;
use Yiisoft\Injector\Injector;
use Yiisoft\Injector\MissingRequiredArgumentException;
use Yiisoft\Injector\Tests\Common\BaseInjectorTest;
use Yiisoft\Injector\Tests\Php8\Support\IntEnum;
use Yiisoft\Injector\Tests\Php8\Support\NonBackedEnum;
use Yiisoft\Injector\Tests\Php8\Support\StrEnum;
use Yiisoft\Injector\Tests\Php8\Support\TimerUnionTypes;
use Yiisoft\Injector\Tests\Php8\Support\TypesIntersection;
use Yiisoft\Injector\Tests\Php8\Support\TypesIntersectionReferencedConstructor;
Expand Down Expand Up @@ -160,4 +164,62 @@ public function testTypeIntersectionNotResolved(): void

(new Injector($container))->make(TypesIntersection::class);
}

/**
* @requires PHP >= 8.1
*/
public function testResolveEnumFromContainer(): void
{
$str = $this->createEnumValue(StrEnum::class, 'Bar');
$int = $this->createEnumValue(IntEnum::class, 'Bar');
$nb = $this->createEnumValue(NonBackedEnum::class, 'Bar');
$container = $this->getContainer([
StrEnum::class => $str,
IntEnum::class => $int,
NonBackedEnum::class => $nb,
]);

$result = (new Injector($container))
->invoke(static fn (StrEnum $arg1, IntEnum $arg2, NonBackedEnum $arg3) => [$arg1, $arg2, $arg3]);

$this->assertSame([$str, $int, $nb], $result);
}

/**
* @requires PHP >= 8.1
*/
public function testResolveEnumFromArguments(): void
{
$str = $this->createEnumValue(StrEnum::class, 'Bar');
$int = $this->createEnumValue(IntEnum::class, 'Bar');
$nb = $this->createEnumValue(NonBackedEnum::class, 'Bar');
$container = $this->getContainer();

$result = (new Injector($container))
->invoke(
static fn (StrEnum $arg1, IntEnum $arg2, NonBackedEnum $arg3) => [$arg1, $arg2, $arg3],
[$nb, $int, $str]
);

$this->assertSame([$str, $int, $nb], $result);
}

/**
* @requires PHP >= 8.1
*/
public function testEnumCanNotBeAutowired(): void
{
$container = $this->getContainer();

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

(new Injector($container))
->invoke(static fn (StrEnum $arg1, IntEnum $arg2) => [$arg1, $arg2]);
}

private function createEnumValue(string $enumClass, string $case)
{
$reflection = new \ReflectionEnum($enumClass);
return $reflection->getCase($case)->getValue();
}
}
11 changes: 11 additions & 0 deletions tests/Php8/Support/IntEnum.php
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Injector\Tests\Php8\Support;

enum IntEnum: int
{
case Foo = 1;
case Bar = 2;
}
11 changes: 11 additions & 0 deletions tests/Php8/Support/NonBackedEnum.php
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Injector\Tests\Php8\Support;

enum NonBackedEnum
{
case Foo;
case Bar;
}
11 changes: 11 additions & 0 deletions tests/Php8/Support/StrEnum.php
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Injector\Tests\Php8\Support;

enum StrEnum: string
{
case Foo = 'foo';
case Bar = 'bar';
}

0 comments on commit 9260e1a

Please sign in to comment.