Skip to content
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
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a0fdad8
Decorate definition when it contains lazy
xepozz Aug 1, 2021
61f2bff
Add tests
xepozz Aug 1, 2021
1b4cfae
Normalize definition out of the building process
xepozz Aug 1, 2021
abb2e83
Rework test cases
xepozz Aug 1, 2021
15f8a66
Add class resolver
xepozz Aug 1, 2021
dbed5b3
Apply fixes from StyleCI
samdark Aug 1, 2021
4bdd4bc
Merge branch 'master' into lazy-services
xepozz Aug 7, 2021
592c239
Fix tests
xepozz Aug 7, 2021
e4bd46f
Apply fixes from StyleCI
samdark Aug 7, 2021
2f8c8b5
Merge branch 'master' into lazy-services
xepozz Aug 10, 2021
8212a39
Merge branch 'master' into lazy-services
xepozz Nov 20, 2022
5136d8d
[ci-review] Apply changes from Rector action.
Nov 20, 2022
47dc088
Fix resolving lazy service
xepozz Nov 20, 2022
40ad592
Merge remote-tracking branch 'origin/lazy-services' into lazy-services
xepozz Nov 20, 2022
125231d
Merge branch 'master' into lazy-services
xepozz Dec 2, 2022
51789d0
Merge branch 'master' into lazy-services
xepozz Dec 3, 2022
1d59fbd
Use `friendsofphp/proxy-manager-lts` package
xepozz Dec 3, 2022
95cdd9e
Fix psalm
xepozz Dec 3, 2022
0b0b784
Rework lazy definition
xepozz Dec 3, 2022
20db77b
Merge branch 'master' into lazy-services
xepozz Jul 29, 2023
d1b0c55
Apply Rector changes (CI)
xepozz Jul 29, 2023
feff3b7
Apply fixes from StyleCI
StyleCIBot Jul 29, 2023
5bf1e81
Fix annotation
xepozz Aug 1, 2023
1d04fbc
Add new test case
xepozz Aug 1, 2023
bc7f70e
Fix psalm
xepozz Aug 1, 2023
fedff5e
Merge branch 'master' into lazy-services
xepozz Mar 3, 2024
51fefa2
Suggestion for lazy service (#355)
vjik Apr 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
xepozz committed Aug 1, 2021
commit 61f2bff2c87b628393f8f3190d2d726cc008934f
18 changes: 18 additions & 0 deletions tests/Unit/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ArrayIterator;
use PHPUnit\Framework\TestCase;
use ProxyManager\Proxy\LazyLoadingInterface;
use Psr\Container\ContainerInterface;
use TypeError;
use Yiisoft\Di\AbstractContainerConfigurator;
Expand Down Expand Up @@ -1355,6 +1356,23 @@ public function testNestedResetter(): void
$this->assertSame($color, $car->getColor());
}

public function testLazy(): void
{
$number = 42;
$container = new Container([
EngineMarkOne::class => [
'class' => EngineMarkOne::class,
'setNumber()' => [$number],
'lazy' => true,
],
]);

$engine = $container->get(EngineMarkOne::class);

self::assertInstanceOf(LazyLoadingInterface::class, $engine);
self::assertSame($number, $engine->getNumber());
}

public function testResetterInCompositeContainer(): void
{
$composite = new CompositeContainer();
Expand Down
18 changes: 12 additions & 6 deletions tests/Unit/DefinitionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@ public function testParseCallableDefinition(): void
$definition = [
'definition' => $fn,
'tags' => ['one', 'two'],
'lazy' => true,
];
[$definition, $meta] = (new DefinitionParser(['tags']))->parse($definition);
[$definition, $meta] = DefinitionParser::parse($definition);

$this->assertSame($fn, $definition);
$this->assertSame(['tags' => ['one', 'two']], $meta);
$this->assertSame(['tags' => ['one', 'two'], 'lazy' => true], $meta);
}

public function testParseArrayCallableDefinition(): void
{
$definition = [
'definition' => [StaticFactory::class, 'create'],
'tags' => ['one', 'two'],
'lazy' => true,
];
[$definition, $meta] = (new DefinitionParser(['tags']))->parse($definition);
[$definition, $meta] = DefinitionParser::parse($definition);

$this->assertSame([StaticFactory::class, 'create'], $definition);
$this->assertSame(['tags' => ['one', 'two']], $meta);
$this->assertSame(['tags' => ['one', 'two'], 'lazy' => true], $meta);
}

public function testParseArrayDefinition(): void
Expand All @@ -40,14 +44,16 @@ public function testParseArrayDefinition(): void
'class' => EngineMarkOne::class,
'__construct()' => [42],
'tags' => ['one', 'two'],
'lazy' => true,
];
[$definition, $meta] = (new DefinitionParser(['tags']))->parse($definition);
[$definition, $meta] = DefinitionParser::parse($definition);

$this->assertSame([
EngineMarkOne::class,
[42],
[],
DefinitionParser::IS_PREPARED_ARRAY_DEFINITION_DATA => true,
], $definition);
$this->assertSame(['tags' => ['one', 'two']], $meta);
$this->assertSame(['tags' => ['one', 'two'], 'lazy' => true], $meta);
}
}