|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\DependencyInjection\Tests; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\DependencyInjection\Alias; |
| 16 | + |
| 17 | +class AliasTest extends TestCase |
| 18 | +{ |
| 19 | + public function testConstructor() |
| 20 | + { |
| 21 | + $alias = new Alias('foo'); |
| 22 | + |
| 23 | + $this->assertEquals('foo', (string) $alias); |
| 24 | + $this->assertTrue($alias->isPublic()); |
| 25 | + } |
| 26 | + |
| 27 | + public function testCanConstructANonPublicAlias() |
| 28 | + { |
| 29 | + $alias = new Alias('foo', false); |
| 30 | + |
| 31 | + $this->assertEquals('foo', (string) $alias); |
| 32 | + $this->assertFalse($alias->isPublic()); |
| 33 | + } |
| 34 | + |
| 35 | + public function testCanConstructAPrivateAlias() |
| 36 | + { |
| 37 | + $alias = new Alias('foo', false, false); |
| 38 | + |
| 39 | + $this->assertEquals('foo', (string) $alias); |
| 40 | + $this->assertFalse($alias->isPublic()); |
| 41 | + $this->assertFalse($alias->isPrivate()); |
| 42 | + } |
| 43 | + |
| 44 | + public function testCanSetPublic() |
| 45 | + { |
| 46 | + $alias = new Alias('foo', false); |
| 47 | + $alias->setPublic(true); |
| 48 | + |
| 49 | + $this->assertTrue($alias->isPublic()); |
| 50 | + } |
| 51 | + |
| 52 | + public function testCanDeprecateAnAlias() |
| 53 | + { |
| 54 | + $alias = new Alias('foo', false); |
| 55 | + $alias->setDeprecated(true, 'The %service_id% service is deprecated.'); |
| 56 | + |
| 57 | + $this->assertTrue($alias->isDeprecated()); |
| 58 | + } |
| 59 | + |
| 60 | + public function testItHasADefaultDeprecationMessage() |
| 61 | + { |
| 62 | + $alias = new Alias('foo', false); |
| 63 | + $alias->setDeprecated(); |
| 64 | + |
| 65 | + $expectedMessage = 'The "foo" service alias is deprecated. You should stop using it, as it will soon be removed.'; |
| 66 | + $this->assertEquals($expectedMessage, $alias->getDeprecationMessage('foo')); |
| 67 | + } |
| 68 | + |
| 69 | + public function testReturnsCorrectDeprecationMessage() |
| 70 | + { |
| 71 | + $alias = new Alias('foo', false); |
| 72 | + $alias->setDeprecated(true, 'The "%service_id%" is deprecated.'); |
| 73 | + |
| 74 | + $expectedMessage = 'The "foo" is deprecated.'; |
| 75 | + $this->assertEquals($expectedMessage, $alias->getDeprecationMessage('foo')); |
| 76 | + } |
| 77 | + |
| 78 | + public function testCanOverrideDeprecation() |
| 79 | + { |
| 80 | + $alias = new Alias('foo', false); |
| 81 | + $alias->setDeprecated(); |
| 82 | + |
| 83 | + $initial = $alias->isDeprecated(); |
| 84 | + $alias->setDeprecated(false); |
| 85 | + $final = $alias->isDeprecated(); |
| 86 | + |
| 87 | + $this->assertTrue($initial); |
| 88 | + $this->assertFalse($final); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @dataProvider invalidDeprecationMessageProvider |
| 93 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
| 94 | + */ |
| 95 | + public function testCannotDeprecateWithAnInvalidTemplate($message) |
| 96 | + { |
| 97 | + $def = new Alias('foo'); |
| 98 | + $def->setDeprecated(true, $message); |
| 99 | + } |
| 100 | + |
| 101 | + public function invalidDeprecationMessageProvider() |
| 102 | + { |
| 103 | + return [ |
| 104 | + "With \rs" => ["invalid \r message %service_id%"], |
| 105 | + "With \ns" => ["invalid \n message %service_id%"], |
| 106 | + 'With */s' => ['invalid */ message %service_id%'], |
| 107 | + 'message not containing required %service_id% variable' => ['this is deprecated'], |
| 108 | + ]; |
| 109 | + } |
| 110 | +} |
0 commit comments