-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into reset-hook
- Loading branch information
Showing
7 changed files
with
126 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Di\Reference; | ||
|
||
use Yiisoft\Definitions\Reference; | ||
|
||
/** | ||
* TagReference is a helper class that is used to specify a reference to a tag. | ||
* For example, `TagReference::to('my-tag')` specifies a reference to all services that are tagged with `tag@my-tag`. | ||
*/ | ||
final class TagReference | ||
{ | ||
private const PREFIX = 'tag@'; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
|
||
public static function to(string $tag): Reference | ||
{ | ||
return Reference::to(self::PREFIX . $tag); | ||
} | ||
|
||
public static function extractTagFromAlias(string $alias): string | ||
{ | ||
if (!str_starts_with($alias, self::PREFIX)) { | ||
throw new \InvalidArgumentException(sprintf('Alias "%s" is not a tag alias.', $alias)); | ||
} | ||
return substr($alias, 4); | ||
} | ||
|
||
public static function isTagAlias(string $id): bool | ||
{ | ||
return str_starts_with($id, self::PREFIX); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Di\Tests\Unit\Reference; | ||
|
||
use Error; | ||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\ContainerInterface; | ||
use Yiisoft\Di\Reference\TagReference; | ||
|
||
final class TagReferenceTest extends TestCase | ||
{ | ||
public function testConstructorIsPrivate(): void | ||
{ | ||
$this->expectException(Error::class); | ||
new TagReference(); | ||
} | ||
|
||
public function testConstructor(): void | ||
{ | ||
$reflection = new \ReflectionClass(TagReference::class); | ||
$reflectionMethod = $reflection->getConstructor(); | ||
$this->assertTrue($reflectionMethod->isPrivate()); | ||
if (PHP_VERSION_ID < 81000) { | ||
$reflectionMethod->setAccessible(true); | ||
} | ||
|
||
$reflectionMethod->invoke($reflection->newInstanceWithoutConstructor()); | ||
} | ||
|
||
public function testAliases(): void | ||
{ | ||
$this->assertFalse(TagReference::isTagAlias('test')); | ||
$this->assertFalse(TagReference::isTagAlias('tag#test')); | ||
$this->assertTrue(TagReference::isTagAlias('tag@test')); | ||
} | ||
|
||
public function testExtractTag(): void | ||
{ | ||
$this->assertEquals('test', TagReference::extractTagFromAlias('tag@test')); | ||
} | ||
|
||
public function testExtractWrongTagDelimiter(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
TagReference::extractTagFromAlias('tag#test'); | ||
} | ||
|
||
public function testExtractWrongTagFormat(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
TagReference::extractTagFromAlias('test'); | ||
} | ||
|
||
public function testReference(): void | ||
{ | ||
$reference = TagReference::to('test'); | ||
$spyContainer = new class () implements ContainerInterface { | ||
public function get($id) | ||
{ | ||
return $id; | ||
} | ||
|
||
public function has($id): bool | ||
{ | ||
return true; | ||
} | ||
}; | ||
|
||
$result = $reference->resolve($spyContainer); | ||
|
||
$this->assertEquals('tag@test', $result); | ||
} | ||
} |