-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ContainerBuilder:resolveImplement supports polymorphism (#175)
- Loading branch information
Showing
3 changed files
with
116 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\DI\Compiler: generated services factories from interfaces with class type hints in parameters. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\DI; | ||
use Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
interface IShape | ||
{ | ||
public function getName(): string; | ||
} | ||
|
||
class Circle implements IShape | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'circle'; | ||
} | ||
} | ||
|
||
class Triangle implements IShape | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'triangle'; | ||
} | ||
} | ||
|
||
class Ellipse extends Circle | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'ellipse'; | ||
} | ||
} | ||
|
||
interface ICircleFactory | ||
{ | ||
public function create(Circle $shape): Picture; | ||
} | ||
|
||
interface ITriangleFactory | ||
{ | ||
public function create(Triangle $shape): Picture; | ||
} | ||
|
||
interface IEllipseFactory | ||
{ | ||
public function create(Ellipse $shape): Picture; | ||
} | ||
|
||
class Picture | ||
{ | ||
public $shape; | ||
|
||
|
||
public function __construct(IShape $shape) | ||
{ | ||
$this->shape = $shape; | ||
} | ||
|
||
|
||
public function getName(): string | ||
{ | ||
return $this->shape->getName(); | ||
} | ||
} | ||
|
||
$compiler = new DI\Compiler; | ||
$container = createContainer($compiler, 'files/compiler.generatedFactory.polymorphism.neon'); | ||
|
||
Assert::type(ICircleFactory::class, $container->getService('circle')); | ||
$picture = $container->getService('circle')->create(new Circle); | ||
Assert::type(Picture::class, $picture); | ||
Assert::same('circle', $picture->getName()); | ||
|
||
Assert::type(ITriangleFactory::class, $container->getService('triangle')); | ||
$picture = $container->getService('triangle')->create(new Triangle); | ||
Assert::type(Picture::class, $picture); | ||
Assert::same('triangle', $picture->getName()); | ||
|
||
Assert::type(IEllipseFactory::class, $container->getService('ellipse')); | ||
$picture = $container->getService('ellipse')->create(new Ellipse); | ||
Assert::type(Picture::class, $picture); | ||
Assert::same('ellipse', $picture->getName()); | ||
|
||
Assert::type(ICircleFactory::class, $container->getService('circle')); | ||
$picture = $container->getService('circle')->create(new Ellipse); | ||
Assert::type(Picture::class, $picture); | ||
Assert::same('ellipse', $picture->getName()); | ||
|
||
Assert::throws(function () use ($container) { | ||
$container->getService('ellipse')->create(new Circle); | ||
}, TypeError::class); |
10 changes: 10 additions & 0 deletions
10
tests/DI/files/compiler.generatedFactory.polymorphism.neon
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,10 @@ | ||
services: | ||
|
||
circle: | ||
implement: ICircleFactory | ||
|
||
triangle: | ||
implement: ITriangleFactory | ||
|
||
ellipse: | ||
implement: IEllipseFactory |