|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: Nette\DI\Compiler: generated services factories from interfaces with class type hints in parameters. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +use Nette\DI; |
| 10 | +use Tester\Assert; |
| 11 | + |
| 12 | + |
| 13 | +require __DIR__ . '/../bootstrap.php'; |
| 14 | + |
| 15 | +interface IShape |
| 16 | +{ |
| 17 | + public function getName(): string; |
| 18 | +} |
| 19 | + |
| 20 | +class Circle implements IShape |
| 21 | +{ |
| 22 | + public function getName(): string |
| 23 | + { |
| 24 | + return 'circle'; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +class Triangle implements IShape |
| 29 | +{ |
| 30 | + public function getName(): string |
| 31 | + { |
| 32 | + return 'triangle'; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +class Ellipse extends Circle |
| 37 | +{ |
| 38 | + public function getName(): string |
| 39 | + { |
| 40 | + return 'ellipse'; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +interface ICircleFactory |
| 45 | +{ |
| 46 | + public function create(Circle $shape): Picture; |
| 47 | +} |
| 48 | + |
| 49 | +interface ITriangleFactory |
| 50 | +{ |
| 51 | + public function create(Triangle $shape): Picture; |
| 52 | +} |
| 53 | + |
| 54 | +interface IEllipseFactory |
| 55 | +{ |
| 56 | + public function create(Ellipse $shape): Picture; |
| 57 | +} |
| 58 | + |
| 59 | +class Picture |
| 60 | +{ |
| 61 | + public $shape; |
| 62 | + |
| 63 | + |
| 64 | + public function __construct(IShape $shape) |
| 65 | + { |
| 66 | + $this->shape = $shape; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + public function getName(): string |
| 71 | + { |
| 72 | + return $this->shape->getName(); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +$compiler = new DI\Compiler; |
| 77 | +$container = createContainer($compiler, 'files/compiler.generatedFactory.polymorphism.neon'); |
| 78 | + |
| 79 | +Assert::type(ICircleFactory::class, $container->getService('circle')); |
| 80 | +$picture = $container->getService('circle')->create(new Circle); |
| 81 | +Assert::type(Picture::class, $picture); |
| 82 | +Assert::same('circle', $picture->getName()); |
| 83 | + |
| 84 | +Assert::type(ITriangleFactory::class, $container->getService('triangle')); |
| 85 | +$picture = $container->getService('triangle')->create(new Triangle); |
| 86 | +Assert::type(Picture::class, $picture); |
| 87 | +Assert::same('triangle', $picture->getName()); |
| 88 | + |
| 89 | +Assert::type(IEllipseFactory::class, $container->getService('ellipse')); |
| 90 | +$picture = $container->getService('ellipse')->create(new Ellipse); |
| 91 | +Assert::type(Picture::class, $picture); |
| 92 | +Assert::same('ellipse', $picture->getName()); |
| 93 | + |
| 94 | +Assert::type(ICircleFactory::class, $container->getService('circle')); |
| 95 | +$picture = $container->getService('circle')->create(new Ellipse); |
| 96 | +Assert::type(Picture::class, $picture); |
| 97 | +Assert::same('ellipse', $picture->getName()); |
| 98 | + |
| 99 | +Assert::throws(function () use ($container) { |
| 100 | + $container->getService('ellipse')->create(new Circle); |
| 101 | +}, TypeError::class); |
0 commit comments