‪TYPO3CMS  9.5
ResolverTest.php
Go to the documentation of this file.
1 <?php
2 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 
18 use Prophecy\Argument;
19 use Symfony\Component\ExpressionLanguage\ExpressionFunction;
27 use TYPO3\CMS\Core\Package\PackageManager;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
34 class ‪ResolverTest extends UnitTestCase
35 {
36  public function ‪setUp()
37  {
38  parent::setUp();
39  $this->resetSingletonInstances = true;
40 
41  $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
42  $cacheFrontendProphecy->has(Argument::any())->willReturn(false);
43  $cacheFrontendProphecy->set(Argument::any(), Argument::any())->willReturn(null);
44  $cacheManagerProphecy = $this->prophesize(CacheManager::class);
45  $cacheManagerProphecy->getCache('cache_core')->willReturn($cacheFrontendProphecy->reveal());
46  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
47 
48  $packageManagerProphecy = $this->prophesize(PackageManager::class);
49  $corePackageProphecy = $this->prophesize(PackageInterface::class);
50  $corePackageProphecy->getPackagePath()->willReturn(__DIR__ . '/../../../../../../../sysext/core/');
51  $packageManagerProphecy->getActivePackages()->willReturn([
52  $corePackageProphecy->reveal()
53  ]);
54  GeneralUtility::setSingletonInstance(PackageManager::class, $packageManagerProphecy->reveal());
55  }
56 
60  public function ‪basicExpressionsDataHandler(): array
61  {
62  return [
63  '1+1' => ['1+1', true],
64  '1 < 2' => ['1 < 2', true],
65  '2 < 1' => ['2 < 1', false],
66  'true' => ['true', true],
67  'false' => ['false', false],
68  'true != false' => ['true != false', true],
69  'true < false' => ['true < false', false],
70  ];
71  }
72 
79  public function ‪basicExpressionHandlingResultsWorksAsExpected(string $expression, $expectedResult)
80  {
81  $request = new ‪ServerRequest();
82  $expressionLanguageResolver = new ‪Resolver('default', [], $request);
83  $this->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  $request = new ‪ServerRequest();
119  GeneralUtility::addInstance(DefaultProvider::class, $contextProphecy->reveal());
120  $expressionLanguageResolver = new ‪Resolver('default', [], $request);
121  $this->assertSame($expectedResult, $expressionLanguageResolver->evaluate($expression));
122  }
123 
128  {
129  return [
130  'testMeLowercase(var1) == var2' => ['testMeLowercase(var1) == var2', true],
131  'testMeLowercase(var2) == var1' => ['testMeLowercase(var2) == var1', false],
132  'testMeLowercase(var1) == var1' => ['testMeLowercase(var1) == var1', false],
133  'testMeLowercase(var2) == var2' => ['testMeLowercase(var2) == var2', true],
134  ];
135  }
136 
144  {
145  $expressionProvider = $this->prophesize(DefaultFunctionsProvider::class);
146  $expressionProvider->getFunctions()->willReturn([
147  new ExpressionFunction('testMeLowercase', function ($str) {
148  return sprintf('(is_string(%1$s) ? strtolower(%1$s) : %1$s)', $str);
149  }, function ($arguments, $str) {
150  return is_string($str) ? strtolower($str) : $str;
151  })
152  ]);
153  $contextProphecy = $this->prophesize(DefaultProvider::class);
154  $contextProphecy->getExpressionLanguageProviders()->willReturn([DefaultFunctionsProvider::class]);
155  $contextProphecy->getExpressionLanguageVariables()->willReturn([
156  'var1' => 'FOO',
157  'var2' => 'foo'
158  ]);
159  $request = new ‪ServerRequest();
160  GeneralUtility::addInstance(DefaultProvider::class, $contextProphecy->reveal());
161  GeneralUtility::addInstance(DefaultFunctionsProvider::class, $expressionProvider->reveal());
162  $expressionLanguageResolver = new ‪Resolver('default', [], $request);
163  $this->assertSame($expectedResult, $expressionLanguageResolver->evaluate($expression));
164  }
165 }
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage\ResolverTest
Definition: ResolverTest.php:35
‪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:36
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage
Definition: ResolverTest.php:3
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage\ResolverTest\basicExpressionsWithVariablesAndExpressionLanguageProviderDataHandler
‪array basicExpressionsWithVariablesAndExpressionLanguageProviderDataHandler()
Definition: ResolverTest.php:127
‪TYPO3\CMS\Core\ExpressionLanguage\DefaultProvider
Definition: DefaultProvider.php:25
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:21
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:34
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.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:60
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:21
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider
Definition: DefaultFunctionsProvider.php:33
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver
Definition: Resolver.php:25
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage\ResolverTest\basicExpressionHandlingWithCustomVariablesAndExpressionLanguageProviderWorksAsExpected
‪basicExpressionHandlingWithCustomVariablesAndExpressionLanguageProviderWorksAsExpected(string $expression, $expectedResult)
Definition: ResolverTest.php:143
‪TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage\ResolverTest\basicExpressionHandlingResultsWorksAsExpected
‪basicExpressionHandlingResultsWorksAsExpected(string $expression, $expectedResult)
Definition: ResolverTest.php:79
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45