|
| 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