Skip to content

Commit 0335287

Browse files
authored
Add unit tests (#1090)
| Q | A | --------------- | ----- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | #939 | License | MIT Added unit tests. This directory didn't own any spec tests.
2 parents c652e38 + ae03f7c commit 0335287

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
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+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ResourceBundle\Tests\ExpressionLanguage;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Psr\Cache\CacheItemPoolInterface;
18+
use Sylius\Bundle\ResourceBundle\ExpressionLanguage\ExpressionLanguage;
19+
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
20+
21+
final class ExpressionLanguageTest extends TestCase
22+
{
23+
public function testItCanBeInstantiatedWithoutCache(): void
24+
{
25+
$expressionLanguage = new ExpressionLanguage();
26+
27+
$this->assertInstanceOf(ExpressionLanguage::class, $expressionLanguage);
28+
}
29+
30+
public function testItCanBeInstantiatedWithCacheItemPool(): void
31+
{
32+
$cache = $this->createMock(CacheItemPoolInterface::class);
33+
34+
$expressionLanguage = new ExpressionLanguage($cache);
35+
36+
$this->assertInstanceOf(ExpressionLanguage::class, $expressionLanguage);
37+
}
38+
39+
public function testItCanBeInstantiatedWithProviders(): void
40+
{
41+
$provider = $this->createMock(ExpressionFunctionProviderInterface::class);
42+
$provider->method('getFunctions')->willReturn([]);
43+
44+
$expressionLanguage = new ExpressionLanguage(null, [$provider]);
45+
46+
$this->assertInstanceOf(ExpressionLanguage::class, $expressionLanguage);
47+
}
48+
49+
public function testItThrowsExceptionForInvalidCacheType(): void
50+
{
51+
$this->expectException(\InvalidArgumentException::class);
52+
$this->expectExceptionMessage('Cache argument has to implement Psr\Cache\CacheItemPoolInterface');
53+
54+
new ExpressionLanguage('invalid');
55+
}
56+
57+
public function testItRegistersNotNullExpressionFunctionProvider(): void
58+
{
59+
$expressionLanguage = new ExpressionLanguage();
60+
61+
$result = $expressionLanguage->evaluate('notFoundOnNull(value)', ['value' => 'test']);
62+
63+
$this->assertSame('test', $result);
64+
}
65+
66+
public function testItCompilesExpressionsWithNotFoundOnNullFunction(): void
67+
{
68+
$expressionLanguage = new ExpressionLanguage();
69+
70+
$compiled = $expressionLanguage->compile('notFoundOnNull(value)', ['value']);
71+
72+
$this->assertStringContainsString('null !== ', $compiled);
73+
}
74+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
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+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ResourceBundle\Tests\ExpressionLanguage;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Sylius\Bundle\ResourceBundle\ExpressionLanguage\NotNullExpressionFunctionProvider;
18+
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
19+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20+
21+
final class NotNullExpressionFunctionProviderTest extends TestCase
22+
{
23+
private NotNullExpressionFunctionProvider $provider;
24+
25+
protected function setUp(): void
26+
{
27+
$this->provider = new NotNullExpressionFunctionProvider();
28+
}
29+
30+
public function testItProvidesExpressionFunctions(): void
31+
{
32+
$functions = $this->provider->getFunctions();
33+
34+
$this->assertIsArray($functions);
35+
$this->assertCount(1, $functions);
36+
$this->assertInstanceOf(ExpressionFunction::class, $functions[0]);
37+
}
38+
39+
public function testItProvidesNotFoundOnNullFunction(): void
40+
{
41+
$functions = $this->provider->getFunctions();
42+
43+
$this->assertSame('notFoundOnNull', $functions[0]->getName());
44+
}
45+
46+
public function testItReturnsValueWhenNotNull(): void
47+
{
48+
$functions = $this->provider->getFunctions();
49+
$evaluator = $functions[0]->getEvaluator();
50+
51+
$result = $evaluator([], 'some value');
52+
53+
$this->assertSame('some value', $result);
54+
}
55+
56+
public function testItThrowsNotFoundExceptionWhenNull(): void
57+
{
58+
$functions = $this->provider->getFunctions();
59+
$evaluator = $functions[0]->getEvaluator();
60+
61+
$this->expectException(NotFoundHttpException::class);
62+
$this->expectExceptionMessage('Requested page is invalid.');
63+
64+
$evaluator([], null);
65+
}
66+
67+
public function testItCompilesExpressionCorrectly(): void
68+
{
69+
$functions = $this->provider->getFunctions();
70+
$compiler = $functions[0]->getCompiler();
71+
72+
$compiled = $compiler('$result');
73+
74+
$this->assertStringContainsString('null !== $result', $compiled);
75+
$this->assertStringContainsString('$result', $compiled);
76+
$this->assertStringContainsString('throw new NotFoundHttpException', $compiled);
77+
}
78+
}

0 commit comments

Comments
 (0)