[FEATURE] Move expression language handling into EXT:core
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / ExpressionLanguage / ResolverTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Symfony\Component\ExpressionLanguage\ExpressionFunction;
19 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
20 use TYPO3\CMS\Core\ExpressionLanguage\DefaultProvider;
21 use TYPO3\CMS\Core\ExpressionLanguage\Resolver;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 /**
25 * Class ResolverTest
26 */
27 class ResolverTest extends UnitTestCase
28 {
29 /**
30 * @return array
31 */
32 public function basicExpressionsDataHandler(): array
33 {
34 return [
35 '1+1' => ['1+1', true],
36 '1 < 2' => ['1 < 2', true],
37 '2 < 1' => ['2 < 1', false],
38 'true' => ['true', true],
39 'false' => ['false', false],
40 'true != false' => ['true != false', true],
41 'true < false' => ['true < false', false],
42 ];
43 }
44
45 /**
46 * @test
47 * @dataProvider basicExpressionsDataHandler
48 * @param string $expression
49 * @param mixed $expectedResult
50 */
51 public function basicExpressionHandlingResultsWorksAsExpected(string $expression, $expectedResult)
52 {
53 $expressionLanguageResolver = new Resolver(new DefaultProvider());
54 $this->assertSame($expectedResult, $expressionLanguageResolver->evaluate($expression));
55 }
56
57 /**
58 * @return array
59 */
60 public function basicExpressionsWithVariablesDataHandler(): array
61 {
62 return [
63 'var1 + var2' => ['var1 + var2', true],
64 'var1 < var2' => ['var1 < var2', true],
65 'var2 < var1' => ['var2 < var1', false],
66 'varTrue' => ['varTrue', true],
67 'varFalse' => ['varFalse', false],
68 'varTrue != varFalse' => ['varTrue != varFalse', true],
69 'varTrue < varFalse' => ['varTrue < varFalse', false],
70 ];
71 }
72
73 /**
74 * @test
75 * @dataProvider basicExpressionsWithVariablesDataHandler
76 * @param string $expression
77 * @param mixed $expectedResult
78 */
79 public function basicExpressionHandlingWithCustomVariablesWorksAsExpected(string $expression, $expectedResult)
80 {
81 $contextProphecy = $this->prophesize(DefaultProvider::class);
82 $contextProphecy->getExpressionLanguageProviders()->willReturn([]);
83 $contextProphecy->getExpressionLanguageVariables()->willReturn([
84 'var1' => '1',
85 'var2' => '2',
86 'varTrue' => true,
87 'varFalse' => false,
88 ]);
89 $expressionLanguageResolver = new Resolver($contextProphecy->reveal());
90 $this->assertSame($expectedResult, $expressionLanguageResolver->evaluate($expression));
91 }
92
93 /**
94 * @return array
95 */
96 public function basicExpressionsWithVariablesAndExpressionLanguageProviderDataHandler(): array
97 {
98 return [
99 'testMeLowercase(var1) == var2' => ['testMeLowercase(var1) == var2', true],
100 'testMeLowercase(var2) == var1' => ['testMeLowercase(var2) == var1', false],
101 'testMeLowercase(var1) == var1' => ['testMeLowercase(var1) == var1', false],
102 'testMeLowercase(var2) == var2' => ['testMeLowercase(var2) == var2', true],
103 ];
104 }
105
106 /**
107 * @test
108 * @dataProvider basicExpressionsWithVariablesAndExpressionLanguageProviderDataHandler
109 * @param string $expression
110 * @param mixed $expectedResult
111 */
112 public function basicExpressionHandlingWithCustomVariablesAndExpressionLanguageProviderWorksAsExpected(string $expression, $expectedResult)
113 {
114 $expressionProvider = $this->prophesize(ExpressionFunctionProviderInterface::class);
115 $expressionProvider->getFunctions()->willReturn([
116 new ExpressionFunction('testMeLowercase', function ($str) {
117 return sprintf('(is_string(%1$s) ? strtolower(%1$s) : %1$s)', $str);
118 }, function ($arguments, $str) {
119 return is_string($str) ? strtolower($str) : $str;
120 })
121 ]);
122 $contextProphecy = $this->prophesize(DefaultProvider::class);
123 $contextProphecy->getExpressionLanguageProviders()->willReturn([$expressionProvider->reveal()]);
124 $contextProphecy->getExpressionLanguageVariables()->willReturn([
125 'var1' => 'FOO',
126 'var2' => 'foo'
127 ]);
128 $expressionLanguageResolver = new Resolver($contextProphecy->reveal());
129 $this->assertSame($expectedResult, $expressionLanguageResolver->evaluate($expression));
130 }
131 }