|
| 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 spec\Sylius\Component\Grid\FieldTypes; |
| 15 | + |
| 16 | +use PhpSpec\ObjectBehavior; |
| 17 | +use Sylius\Component\Grid\DataExtractor\DataExtractorInterface; |
| 18 | +use Sylius\Component\Grid\Definition\Field; |
| 19 | +use Sylius\Component\Grid\FieldTypes\FieldTypeInterface; |
| 20 | + |
| 21 | +final class CallbackFieldTypeSpec extends ObjectBehavior |
| 22 | +{ |
| 23 | + function let(DataExtractorInterface $dataExtractor): void |
| 24 | + { |
| 25 | + $this->beConstructedWith($dataExtractor); |
| 26 | + } |
| 27 | + |
| 28 | + function it_is_a_grid_field_type(): void |
| 29 | + { |
| 30 | + $this->shouldImplement(FieldTypeInterface::class); |
| 31 | + } |
| 32 | + |
| 33 | + function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_callback_with_htmlspecialchars( |
| 34 | + DataExtractorInterface $dataExtractor, |
| 35 | + Field $field, |
| 36 | + ): void { |
| 37 | + $dataExtractor->get($field, ['foo' => 'bar'])->willReturn('bar'); |
| 38 | + |
| 39 | + $this->render($field, ['foo' => 'bar'], [ |
| 40 | + 'callback' => fn (string $value): string => "<strong>$value</strong>", |
| 41 | + 'htmlspecialchars' => true, |
| 42 | + ])->shouldReturn('<strong>bar</strong>'); |
| 43 | + } |
| 44 | + |
| 45 | + function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_callback_without_htmlspecialchars( |
| 46 | + DataExtractorInterface $dataExtractor, |
| 47 | + Field $field, |
| 48 | + ): void { |
| 49 | + $dataExtractor->get($field, ['foo' => 'bar'])->willReturn('bar'); |
| 50 | + |
| 51 | + $this->render($field, ['foo' => 'bar'], [ |
| 52 | + 'callback' => fn (string $value): string => "<strong>$value</strong>", |
| 53 | + 'htmlspecialchars' => false, |
| 54 | + ])->shouldReturn('<strong>bar</strong>'); |
| 55 | + } |
| 56 | + |
| 57 | + function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_function_callback( |
| 58 | + DataExtractorInterface $dataExtractor, |
| 59 | + Field $field, |
| 60 | + ): void { |
| 61 | + $dataExtractor->get($field, ['foo' => 'bar'])->willReturn('bar'); |
| 62 | + |
| 63 | + $this->render($field, ['foo' => 'bar'], [ |
| 64 | + 'callback' => 'strtoupper', |
| 65 | + 'htmlspecialchars' => true, |
| 66 | + ])->shouldReturn('BAR'); |
| 67 | + } |
| 68 | + |
| 69 | + function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_static_callback( |
| 70 | + DataExtractorInterface $dataExtractor, |
| 71 | + Field $field, |
| 72 | + ): void { |
| 73 | + $dataExtractor->get($field, ['foo' => 'bar'])->willReturn('BAR'); |
| 74 | + |
| 75 | + $this->render($field, ['foo' => 'bar'], [ |
| 76 | + 'callback' => [self::class, 'callable'], |
| 77 | + 'htmlspecialchars' => true, |
| 78 | + ])->shouldReturn('bar'); |
| 79 | + } |
| 80 | + |
| 81 | + static function callable($value) |
| 82 | + { |
| 83 | + return strtolower($value); |
| 84 | + } |
| 85 | +} |
0 commit comments