-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from gsteel/docs/delegators-and-aliases
Better documentation for delegators and service aliases
- Loading branch information
Showing
5 changed files
with
156 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\ServiceManager; | ||
|
||
use Laminas\ServiceManager\Factory\InvokableFactory; | ||
use Laminas\ServiceManager\ServiceManager; | ||
use LaminasTest\ServiceManager\TestAsset\DelegatorAndAliasBehaviorTest\TargetObject; | ||
use LaminasTest\ServiceManager\TestAsset\DelegatorAndAliasBehaviorTest\TargetObjectDelegator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class DelegatorAndAliasBehaviorTest extends TestCase | ||
{ | ||
public function testThatADelegatorTargetingAServiceWillExecute(): void | ||
{ | ||
$serviceManager = new ServiceManager([ | ||
'factories' => [ | ||
TargetObject::class => InvokableFactory::class, | ||
], | ||
'delegators' => [ | ||
TargetObject::class => [ | ||
TargetObjectDelegator::class, | ||
], | ||
], | ||
]); | ||
|
||
$service = $serviceManager->get(TargetObject::class); | ||
self::assertInstanceOf(TargetObject::class, $service); | ||
self::assertEquals(TargetObjectDelegator::DELEGATED_VALUE, $service->value); | ||
} | ||
|
||
public function testThatADelegatorWillNotExecuteWhenItTargetsAnAlias(): void | ||
{ | ||
$serviceManager = new ServiceManager([ | ||
'factories' => [ | ||
TargetObject::class => InvokableFactory::class, | ||
], | ||
'aliases' => [ | ||
'Some Alias' => TargetObject::class, | ||
], | ||
'delegators' => [ | ||
'Some Alias' => [ | ||
TargetObjectDelegator::class, | ||
], | ||
], | ||
]); | ||
|
||
$service = $serviceManager->get('Some Alias'); | ||
self::assertInstanceOf(TargetObject::class, $service); | ||
self::assertEquals(TargetObject::INITIAL_VALUE, $service->value); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
test/TestAsset/DelegatorAndAliasBehaviorTest/TargetObject.php
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\ServiceManager\TestAsset\DelegatorAndAliasBehaviorTest; | ||
|
||
final class TargetObject | ||
{ | ||
public const INITIAL_VALUE = 'Default'; | ||
|
||
/** @var string */ | ||
public $value; | ||
|
||
public function __construct() | ||
{ | ||
$this->value = self::INITIAL_VALUE; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
test/TestAsset/DelegatorAndAliasBehaviorTest/TargetObjectDelegator.php
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\ServiceManager\TestAsset\DelegatorAndAliasBehaviorTest; | ||
|
||
use Psr\Container\ContainerInterface; | ||
|
||
use function assert; | ||
|
||
final class TargetObjectDelegator | ||
{ | ||
public const DELEGATED_VALUE = 'Delegated Value'; | ||
|
||
public function __invoke(ContainerInterface $container, string $serviceName, callable $callback): TargetObject | ||
{ | ||
$service = $callback(); | ||
assert($service instanceof TargetObject); | ||
|
||
$service->value = self::DELEGATED_VALUE; | ||
|
||
return $service; | ||
} | ||
} |