‪TYPO3CMS  10.4
ResolverTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use Prophecy\Argument;
21 use Symfony\Component\ExpressionLanguage\ExpressionFunction;
28 use TYPO3\CMS\Core\Package\PackageManager;
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
35 class ‪ResolverTest extends UnitTestCase
36 {
37  public function ‪setUp(): void
38  {
39  parent::setUp();
40  $this->resetSingletonInstances = true;
41 
42  $cacheFrontendProphecy = $this->prophesize(PhpFrontend::class);
43  $cacheFrontendProphecy->require(Argument::any())->willReturn(false);
44  $cacheFrontendProphecy->set(Argument::any(), Argument::any())->willReturn(null);
45  $cacheManagerProphecy = $this->prophesize(CacheManager::class);
46  $cacheManagerProphecy->getCache('core')->willReturn($cacheFrontendProphecy->reveal());
47  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
48 
49  $packageManagerProphecy = $this->prophesize(PackageManager::class);
50  $corePackageProphecy = $this->prophesize(PackageInterface::class);
51  $corePackageProphecy->getPackagePath()->willReturn(__DIR__ . '/../../../../../../../sysext/core/');
52  $packageManagerProphecy->getActivePackages()->willReturn([
53  $corePackageProphecy->reveal()
54  ]);
55  GeneralUtility::setSingletonInstance(PackageManager::class, $packageManagerProphecy->reveal());
56  }
57 
61  public function ‪basicExpressionsDataHandler(): array
62  {
63  return [
64  '1+1' => ['1+1', true],
65  '1 < 2' => ['1 < 2', true],
66  '2 < 1' => ['2 < 1', false],
67  'true' => ['true', true],
68  'false' => ['false', false],
69  'true != false' => ['true != false', true],
70  'true < false' => ['true < false', false],
71  ];
72  }
73 
80  public function ‪basicExpressionHandlingResultsWorksAsExpected(string $expression, $expectedResult)
81  {
82  $expressionLanguageResolver = new ‪Resolver('default', []);
83  self::assertSame($expectedResult, $expressionLanguageResolver->evaluate($expression));
84  }
85 
90  {
91  return [
92  'var1 + var2' => ['var1 + var2', true],
93  'var1 < var2' => ['var1 < var2', true],
94  'var2 < var1' => ['var2 < var1', false],
95  'varTrue' => ['varTrue', true],
96  'varFalse' => ['varFalse', false],
97  'varTrue != varFalse' => ['varTrue != varFalse', true],
98  'varTrue < varFalse' => ['varTrue < varFalse', false],
99  ];
100  }
101 
108  public function ‪basicExpressionHandlingWithCustomVariablesWorksAsExpected(string $expression, $expectedResult)
109  {
110  $contextProphecy = $this->prophesize(DefaultProvider::class);
111  $contextProphecy->getExpressionLanguageProviders()->willReturn([]);
112  $contextProphecy->getExpressionLanguageVariables()->willReturn([
113  'var1' => '1',
114  'var2' => '2',
115  'varTrue' => true,
116  'varFalse' => false,
117  ]);
118  GeneralUtility::addInstance(DefaultProvider::class, $contextProphecy->reveal());
119  $expressionLanguageResolver = new ‪Resolver('default', []);
120  self::assertSame($expectedResult, $expressionLanguageResolver->evaluate($expression));
121  }
122 
127  {
128  return [
129  'testMeLowercase(var1) == var2' => ['testMeLowercase(var1) == var2', true],
130  'testMeLowercase(var2) == var1' => ['testMeLowercase(var2) == var1', false],
131  'testMeLowercase(var1) == var1' => ['testMeLowercase(var1) == var1', false],
132  'testMeLowercase(var2) == var2' => ['testMeLowercase(var2) == var2', true],
133  ];
134  }
135 
143  {
144  $expressionProvider = $this->prophesize(DefaultFunctionsProvider::class);
145  $expressionProvider->getFunctions()->willReturn([
146  new ExpressionFunction('testMeLowercase', function ($str) {
147  return sprintf('(is_string(%1$s) ? strtolower(%1$s) : %1$s)', $str);
148  }, function ($arguments, $str) {
149  return is_string($str) ? strtolower($str) : $str;
150  })
151  ]);
152  $contextProphecy = $this->prophesize(DefaultProvider::class);
153  $contextProphecy->getExpressionLanguageProviders()->willReturn([DefaultFunctionsProvider::class]);
154  $contextProphecy->getExpressionLanguageVariables()->willReturn([
155  'var1' => 'FOO',
156  'var2' => 'foo'
157  ]);
158  GeneralUtility::addInstance(DefaultProvider::class, $contextProphecy->reveal());
159  GeneralUtility::addInstance(DefaultFunctionsProvider::class, $expressionProvider->reveal());
160  $expressionLanguageResolver = new ‪Resolver('default', []);
161  self::assertSame($expectedResult, $expressionLanguageResolver->evaluate($expression));
162  }
163 }
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage\ResolverTest
Definition: ResolverTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage\ResolverTest\basicExpressionsWithVariablesDataHandler
‪array basicExpressionsWithVariablesDataHandler()
Definition: ResolverTest.php:89
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage\ResolverTest\setUp
‪setUp()
Definition: ResolverTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage
Definition: ResolverTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage\ResolverTest\basicExpressionsWithVariablesAndExpressionLanguageProviderDataHandler
‪array basicExpressionsWithVariablesAndExpressionLanguageProviderDataHandler()
Definition: ResolverTest.php:126
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:25
‪TYPO3\CMS\Core\ExpressionLanguage\DefaultProvider
Definition: DefaultProvider.php:27
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:22
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage\ResolverTest\basicExpressionHandlingWithCustomVariablesWorksAsExpected
‪basicExpressionHandlingWithCustomVariablesWorksAsExpected(string $expression, $expectedResult)
Definition: ResolverTest.php:108
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage\ResolverTest\basicExpressionsDataHandler
‪array basicExpressionsDataHandler()
Definition: ResolverTest.php:61
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider
Definition: DefaultFunctionsProvider.php:35
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver
Definition: Resolver.php:27
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage\ResolverTest\basicExpressionHandlingWithCustomVariablesAndExpressionLanguageProviderWorksAsExpected
‪basicExpressionHandlingWithCustomVariablesAndExpressionLanguageProviderWorksAsExpected(string $expression, $expectedResult)
Definition: ResolverTest.php:142
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage\ResolverTest\basicExpressionHandlingResultsWorksAsExpected
‪basicExpressionHandlingResultsWorksAsExpected(string $expression, $expectedResult)
Definition: ResolverTest.php:80
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46