|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Tests\Integration\DesignSystemTwig\Twig\Components; |
| 10 | + |
| 11 | +use Ibexa\DesignSystemTwig\Twig\Components\Badge; |
| 12 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 13 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 14 | +use Symfony\Component\DomCrawler\Crawler; |
| 15 | +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
| 16 | +use Symfony\UX\TwigComponent\Test\InteractsWithTwigComponents; |
| 17 | + |
| 18 | +final class BadgeTest extends KernelTestCase |
| 19 | +{ |
| 20 | + use InteractsWithTwigComponents; |
| 21 | + |
| 22 | + public function testMount(): void |
| 23 | + { |
| 24 | + $component = $this->mountTwigComponent(Badge::class, [ |
| 25 | + 'size' => 'small', |
| 26 | + 'value' => 7, |
| 27 | + 'maxBadgeValue' => 15, |
| 28 | + ]); |
| 29 | + |
| 30 | + self::assertInstanceOf(Badge::class, $component, 'Component should mount as Badge.'); |
| 31 | + self::assertSame('small', $component->size, 'Prop "size" should be "small".'); |
| 32 | + self::assertSame(7, $component->value, 'Prop "value" should be 7.'); |
| 33 | + self::assertSame(15, $component->maxBadgeValue, 'Prop "maxBadgeValue" should be 15.'); |
| 34 | + } |
| 35 | + |
| 36 | + public function testDefaultRender(): void |
| 37 | + { |
| 38 | + $crawler = $this->renderTwigComponent(Badge::class, ['value' => 1])->crawler(); |
| 39 | + $badge = $this->getBadge($crawler); |
| 40 | + $class = $this->getClassAttr($badge); |
| 41 | + |
| 42 | + self::assertStringContainsString('ids-badge', $class, 'Base class "ids-badge" should be present.'); |
| 43 | + self::assertStringNotContainsString('ids-badge--small', $class, 'Default size should be "medium", not "small".'); |
| 44 | + self::assertStringNotContainsString('ids-badge--stretched', $class, 'With value below threshold, stretched modifier should not be present.'); |
| 45 | + self::assertSame('1', $this->getText($badge), 'Badge text should equal value when under the cap.'); |
| 46 | + self::assertSame('99', $badge->attr('data-ids-max-badge-value'), 'Default max should be 99.'); |
| 47 | + } |
| 48 | + |
| 49 | + public function testSizeVariantSmallAddsClass(): void |
| 50 | + { |
| 51 | + $crawler = $this->renderTwigComponent(Badge::class, ['size' => 'small', 'value' => 1])->crawler(); |
| 52 | + $badge = $this->getBadge($crawler); |
| 53 | + $class = $this->getClassAttr($badge); |
| 54 | + |
| 55 | + self::assertStringContainsString('ids-badge--small', $class, 'Small size should add "ids-badge--small" class.'); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param non-empty-string $size |
| 60 | + */ |
| 61 | + #[DataProvider('stretchedProvider')] |
| 62 | + public function testStretchedModifier(string $size, int $value, bool $expected): void |
| 63 | + { |
| 64 | + $crawler = $this->renderTwigComponent(Badge::class, ['size' => $size, 'value' => $value])->crawler(); |
| 65 | + $badge = $this->getBadge($crawler); |
| 66 | + $class = $this->getClassAttr($badge); |
| 67 | + |
| 68 | + if ($expected) { |
| 69 | + self::assertStringContainsString('ids-badge--stretched', $class, 'Expected stretched modifier to be present.'); |
| 70 | + } else { |
| 71 | + self::assertStringNotContainsString('ids-badge--stretched', $class, 'Expected stretched modifier to be absent.'); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return iterable<string, array{0: string, 1: int, 2: bool}> |
| 77 | + */ |
| 78 | + public static function stretchedProvider(): iterable |
| 79 | + { |
| 80 | + yield 'medium below' => ['medium', 99, false]; |
| 81 | + yield 'medium at' => ['medium', 100, true]; |
| 82 | + yield 'small below' => ['small', 9, false]; |
| 83 | + yield 'small at' => ['small', 10, true]; |
| 84 | + } |
| 85 | + |
| 86 | + public function testFormattedValueIsCappedByMax(): void |
| 87 | + { |
| 88 | + $crawler = $this->renderTwigComponent(Badge::class, [ |
| 89 | + 'value' => 150, |
| 90 | + 'maxBadgeValue' => 99, |
| 91 | + ])->crawler(); |
| 92 | + |
| 93 | + $badge = $this->getBadge($crawler); |
| 94 | + self::assertSame('99+', $this->getText($badge), 'When value exceeds maxBadgeValue, text should display "<max>+".'); |
| 95 | + self::assertSame('99', $badge->attr('data-ids-max-badge-value'), 'data-ids-max-badge-value should match the exposed "max_value".'); |
| 96 | + } |
| 97 | + |
| 98 | + public function testFormattedValueDisplaysRawValueWhenUnderMax(): void |
| 99 | + { |
| 100 | + $crawler = $this->renderTwigComponent(Badge::class, [ |
| 101 | + 'value' => 42, |
| 102 | + 'maxBadgeValue' => 99, |
| 103 | + ])->crawler(); |
| 104 | + |
| 105 | + $badge = $this->getBadge($crawler); |
| 106 | + self::assertSame('42', $this->getText($badge), 'When value <= maxBadgeValue, text should display the raw value.'); |
| 107 | + } |
| 108 | + |
| 109 | + public function testInvalidPropsCauseResolverErrorOnMount(): void |
| 110 | + { |
| 111 | + $this->expectException(InvalidOptionsException::class); |
| 112 | + $this->mountTwigComponent(Badge::class, ['size' => 'giant', 'value' => 1]); |
| 113 | + } |
| 114 | + |
| 115 | + public function testInvalidValueTypeCauseResolverErrorOnMount(): void |
| 116 | + { |
| 117 | + $this->expectException(InvalidOptionsException::class); |
| 118 | + |
| 119 | + $this->mountTwigComponent(Badge::class, ['value' => 'not-int']); |
| 120 | + } |
| 121 | + |
| 122 | + private function getBadge(Crawler $crawler): Crawler |
| 123 | + { |
| 124 | + $node = $crawler->filter('div.ids-badge')->first(); |
| 125 | + self::assertGreaterThan(0, $node->count(), 'Badge wrapper ".ids-badge" should be present.'); |
| 126 | + |
| 127 | + return $node; |
| 128 | + } |
| 129 | + |
| 130 | + private function getClassAttr(Crawler $node): string |
| 131 | + { |
| 132 | + return (string) $node->attr('class'); |
| 133 | + } |
| 134 | + |
| 135 | + private function getText(Crawler $node): string |
| 136 | + { |
| 137 | + return trim($node->text('')); |
| 138 | + } |
| 139 | +} |
0 commit comments