|
18 | 18 | use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
19 | 19 | use Symfony\Component\ExpressionLanguage\ParsedExpression;
|
20 | 20 | use Symfony\Component\ExpressionLanguage\SyntaxError;
|
| 21 | +use Symfony\Component\ExpressionLanguage\Tests\Fixtures\FooBackedEnum; |
| 22 | +use Symfony\Component\ExpressionLanguage\Tests\Fixtures\FooEnum; |
21 | 23 | use Symfony\Component\ExpressionLanguage\Tests\Fixtures\TestProvider;
|
22 | 24 |
|
23 | 25 | class ExpressionLanguageTest extends TestCase
|
@@ -78,6 +80,53 @@ public function testConstantFunction()
|
78 | 80 | $this->assertEquals('\constant("PHP_VERSION")', $expressionLanguage->compile('constant("PHP_VERSION")'));
|
79 | 81 | }
|
80 | 82 |
|
| 83 | + public function testEnumFunctionWithConstantThrows() |
| 84 | + { |
| 85 | + $this->expectException(\TypeError::class); |
| 86 | + $this->expectExceptionMessage('The string "PHP_VERSION" is not the name of a valid enum case.'); |
| 87 | + $expressionLanguage = new ExpressionLanguage(); |
| 88 | + $expressionLanguage->evaluate('enum("PHP_VERSION")'); |
| 89 | + } |
| 90 | + |
| 91 | + public function testCompiledEnumFunctionWithConstantThrows() |
| 92 | + { |
| 93 | + $this->expectException(\TypeError::class); |
| 94 | + $this->expectExceptionMessage('The string "PHP_VERSION" is not the name of a valid enum case.'); |
| 95 | + $expressionLanguage = new ExpressionLanguage(); |
| 96 | + eval($expressionLanguage->compile('enum("PHP_VERSION")').';'); |
| 97 | + } |
| 98 | + |
| 99 | + public function testEnumFunction() |
| 100 | + { |
| 101 | + $expressionLanguage = new ExpressionLanguage(); |
| 102 | + $this->assertSame(FooEnum::Foo, $expressionLanguage->evaluate('enum("Symfony\\\\Component\\\\ExpressionLanguage\\\\Tests\\\\Fixtures\\\\FooEnum::Foo")')); |
| 103 | + } |
| 104 | + |
| 105 | + public function testCompiledEnumFunction() |
| 106 | + { |
| 107 | + $result = null; |
| 108 | + $expressionLanguage = new ExpressionLanguage(); |
| 109 | + eval(sprintf('$result = %s;', $expressionLanguage->compile('enum("Symfony\\\\Component\\\\ExpressionLanguage\\\\Tests\\\\Fixtures\\\\FooEnum::Foo")'))); |
| 110 | + |
| 111 | + $this->assertSame(FooEnum::Foo, $result); |
| 112 | + } |
| 113 | + |
| 114 | + public function testBackedEnumFunction() |
| 115 | + { |
| 116 | + $expressionLanguage = new ExpressionLanguage(); |
| 117 | + $this->assertSame(FooBackedEnum::Bar, $expressionLanguage->evaluate('enum("Symfony\\\\Component\\\\ExpressionLanguage\\\\Tests\\\\Fixtures\\\\FooBackedEnum::Bar")')); |
| 118 | + $this->assertSame('Foo', $expressionLanguage->evaluate('enum("Symfony\\\\Component\\\\ExpressionLanguage\\\\Tests\\\\Fixtures\\\\FooBackedEnum::Bar").value')); |
| 119 | + } |
| 120 | + |
| 121 | + public function testCompiledEnumFunctionWithBackedEnum() |
| 122 | + { |
| 123 | + $result = null; |
| 124 | + $expressionLanguage = new ExpressionLanguage(); |
| 125 | + eval(sprintf('$result = %s;', $expressionLanguage->compile('enum("Symfony\\\\Component\\\\ExpressionLanguage\\\\Tests\\\\Fixtures\\\\FooBackedEnum::Bar")'))); |
| 126 | + |
| 127 | + $this->assertSame(FooBackedEnum::Bar, $result); |
| 128 | + } |
| 129 | + |
81 | 130 | public function testProviders()
|
82 | 131 | {
|
83 | 132 | $expressionLanguage = new ExpressionLanguage(null, [new TestProvider()]);
|
|
0 commit comments