|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Component\DependencyInjection\Tests\Compiler; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Symfony\Component\DependencyInjection\Compiler\AliasDeprecatedPublicServicesPass; |
| 7 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 8 | +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 9 | + |
| 10 | +final class AliasDeprecatedPublicServicesPassTest extends TestCase |
| 11 | +{ |
| 12 | + public function testProcess() |
| 13 | + { |
| 14 | + $container = new ContainerBuilder(); |
| 15 | + $container |
| 16 | + ->register('foo') |
| 17 | + ->setPublic(true) |
| 18 | + ->addTag('container.private', ['package' => 'foo/bar', 'version' => '1.2']); |
| 19 | + |
| 20 | + (new AliasDeprecatedPublicServicesPass())->process($container); |
| 21 | + |
| 22 | + $this->assertTrue($container->hasAlias('foo')); |
| 23 | + |
| 24 | + $alias = $container->getAlias('foo'); |
| 25 | + |
| 26 | + $this->assertSame('.container.private.foo', (string) $alias); |
| 27 | + $this->assertTrue($alias->isPublic()); |
| 28 | + $this->assertFalse($alias->isPrivate()); |
| 29 | + $this->assertSame([ |
| 30 | + 'package' => 'foo/bar', |
| 31 | + 'version' => '1.2', |
| 32 | + 'message' => 'Accessing the "foo" service directly from the container is deprecated, use dependency injection instead.', |
| 33 | + ], $alias->getDeprecation('foo')); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @dataProvider processWithMissingAttributeProvider |
| 38 | + */ |
| 39 | + public function testProcessWithMissingAttribute(string $attribute, array $attributes) |
| 40 | + { |
| 41 | + $this->expectException(InvalidArgumentException::class); |
| 42 | + $this->expectExceptionMessage(sprintf('The "%s" attribute is mandatory for the "container.private" tag on the "foo" service.', $attribute)); |
| 43 | + |
| 44 | + $container = new ContainerBuilder(); |
| 45 | + $container |
| 46 | + ->register('foo') |
| 47 | + ->addTag('container.private', $attributes); |
| 48 | + |
| 49 | + (new AliasDeprecatedPublicServicesPass())->process($container); |
| 50 | + } |
| 51 | + |
| 52 | + public function processWithMissingAttributeProvider() |
| 53 | + { |
| 54 | + return [ |
| 55 | + ['package', ['version' => '1.2']], |
| 56 | + ['version', ['package' => 'foo/bar']], |
| 57 | + ]; |
| 58 | + } |
| 59 | + |
| 60 | + public function testProcessWithNonPublicService() |
| 61 | + { |
| 62 | + $this->expectException(InvalidArgumentException::class); |
| 63 | + $this->expectExceptionMessage('The "foo" service is private: it cannot have the "container.private" tag.'); |
| 64 | + |
| 65 | + $container = new ContainerBuilder(); |
| 66 | + $container |
| 67 | + ->register('foo') |
| 68 | + ->setPublic(false) |
| 69 | + ->addTag('container.private', ['package' => 'foo/bar', 'version' => '1.2']); |
| 70 | + |
| 71 | + (new AliasDeprecatedPublicServicesPass())->process($container); |
| 72 | + } |
| 73 | +} |
0 commit comments