-
-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lazy services #232
Open
xepozz
wants to merge
27
commits into
master
Choose a base branch
from
lazy-services
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lazy services #232
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
a0fdad8
Decorate definition when it contains lazy
xepozz 61f2bff
Add tests
xepozz 1b4cfae
Normalize definition out of the building process
xepozz abb2e83
Rework test cases
xepozz 15f8a66
Add class resolver
xepozz dbed5b3
Apply fixes from StyleCI
samdark 4bdd4bc
Merge branch 'master' into lazy-services
xepozz 592c239
Fix tests
xepozz e4bd46f
Apply fixes from StyleCI
samdark 2f8c8b5
Merge branch 'master' into lazy-services
xepozz 8212a39
Merge branch 'master' into lazy-services
xepozz 5136d8d
[ci-review] Apply changes from Rector action.
47dc088
Fix resolving lazy service
xepozz 40ad592
Merge remote-tracking branch 'origin/lazy-services' into lazy-services
xepozz 125231d
Merge branch 'master' into lazy-services
xepozz 51789d0
Merge branch 'master' into lazy-services
xepozz 1d59fbd
Use `friendsofphp/proxy-manager-lts` package
xepozz 95cdd9e
Fix psalm
xepozz 0b0b784
Rework lazy definition
xepozz 20db77b
Merge branch 'master' into lazy-services
xepozz d1b0c55
Apply Rector changes (CI)
xepozz feff3b7
Apply fixes from StyleCI
StyleCIBot 5bf1e81
Fix annotation
xepozz 1d04fbc
Add new test case
xepozz bc7f70e
Fix psalm
xepozz fedff5e
Merge branch 'master' into lazy-services
xepozz 51fefa2
Suggestion for lazy service (#355)
vjik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Di\Tests\Unit; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ProxyManager\Factory\LazyLoadingValueHolderFactory; | ||
use ProxyManager\Proxy\LazyLoadingInterface; | ||
use Yiisoft\Di\Container; | ||
use Yiisoft\Di\ContainerConfig; | ||
use Yiisoft\Di\Tests\Support\EngineMarkOne; | ||
|
||
class LazyServiceContainerTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
if (!class_exists(LazyLoadingValueHolderFactory::class)) { | ||
$this->markTestSkipped( | ||
'You should install `friendsofphp/proxy-manager-lts` if you want to use lazy services.' | ||
); | ||
} | ||
} | ||
|
||
public function testIsTheSameObject(): void | ||
{ | ||
$class = EngineMarkOne::class; | ||
$number = 55; | ||
|
||
$config = ContainerConfig::create() | ||
->withDefinitions([ | ||
EngineMarkOne::class => [ | ||
'class' => $class, | ||
'setNumber()' => [$number], | ||
'lazy' => true, | ||
], | ||
]); | ||
$container = new Container($config); | ||
|
||
/* @var EngineMarkOne $object */ | ||
$object = $container->get($class); | ||
|
||
self::assertInstanceOf(LazyLoadingInterface::class, $object); | ||
self::assertInstanceOf(EngineMarkOne::class, $object); | ||
self::assertFalse($object->isProxyInitialized()); | ||
self::assertEquals($number, $object->getNumber()); | ||
self::assertTrue($object->isProxyInitialized()); | ||
|
||
/* @var EngineMarkOne $object */ | ||
$object = $container->get($class); | ||
|
||
self::assertInstanceOf(LazyLoadingInterface::class, $object); | ||
self::assertInstanceOf(EngineMarkOne::class, $object); | ||
self::assertTrue($object->isProxyInitialized()); | ||
} | ||
|
||
/** | ||
* @dataProvider lazyDefinitionDataProvider | ||
*/ | ||
public function testLazy(array $definitions, string $id): void | ||
{ | ||
$config = ContainerConfig::create() | ||
->withDefinitions($definitions); | ||
$container = new Container($config); | ||
|
||
$object = $container->get($id); | ||
|
||
self::assertInstanceOf(LazyLoadingInterface::class, $object); | ||
} | ||
|
||
public function lazyDefinitionDataProvider(): array | ||
{ | ||
return [ | ||
'class as definition name' => [ | ||
[ | ||
EngineMarkOne::class => [ | ||
'lazy' => true, | ||
], | ||
], | ||
EngineMarkOne::class, | ||
], | ||
'class as key' => [ | ||
[ | ||
EngineMarkOne::class => [ | ||
'class' => EngineMarkOne::class, | ||
'lazy' => true, | ||
], | ||
], | ||
EngineMarkOne::class, | ||
], | ||
'alias as key' => [ | ||
[ | ||
'mark_one' => [ | ||
'class' => EngineMarkOne::class, | ||
'lazy' => true, | ||
], | ||
], | ||
'mark_one', | ||
], | ||
'dedicated array definition' => [ | ||
[ | ||
EngineMarkOne::class => [ | ||
'definition' => ['class' => EngineMarkOne::class], | ||
'lazy' => true, | ||
], | ||
], | ||
EngineMarkOne::class, | ||
], | ||
'dedicated callback definition' => [ | ||
[ | ||
EngineMarkOne::class => [ | ||
'definition' => fn () => new EngineMarkOne(), | ||
'lazy' => true, | ||
], | ||
], | ||
EngineMarkOne::class, | ||
], | ||
]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's always
false
in tests