|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Unit\Helpers; |
| 6 | + |
| 7 | +use Flowpack\ContentSecurityPolicy\Helpers\TagHelper; |
| 8 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +#[CoversClass(TagHelper::class)] |
| 12 | +class TagHelperTest extends TestCase |
| 13 | +{ |
| 14 | + public function testTagHasAttributeWithoutValueShouldReturnTrue(): void |
| 15 | + { |
| 16 | + $tag = '<script src="https://google.com"></script>'; |
| 17 | + self::assertTrue(TagHelper::tagHasAttribute($tag, 'src')); |
| 18 | + } |
| 19 | + |
| 20 | + public function testTagHasAttributeWithoutValueShouldReturnFalse(): void |
| 21 | + { |
| 22 | + $tag = '<script src="https://google.com"></script>'; |
| 23 | + self::assertFalse(TagHelper::tagHasAttribute($tag, 'bar')); |
| 24 | + } |
| 25 | + |
| 26 | + public function testTagHasAttributeWithValueShouldReturnTrue(): void |
| 27 | + { |
| 28 | + $tag = '<script src="https://google.com"></script>'; |
| 29 | + self::assertTrue(TagHelper::tagHasAttribute($tag, 'src', 'https://google.com')); |
| 30 | + } |
| 31 | + |
| 32 | + public function testTagHasAttributeWithValueShouldReturnFalse(): void |
| 33 | + { |
| 34 | + $tag = '<script src="https://google.com"></script>'; |
| 35 | + self::assertFalse(TagHelper::tagHasAttribute($tag, 'src', 'another value')); |
| 36 | + } |
| 37 | + |
| 38 | + public function testTagChangeAttributeValueShouldChangeValue(): void |
| 39 | + { |
| 40 | + $tag = '<script src="https://google.com"></script>'; |
| 41 | + |
| 42 | + self::assertSame( |
| 43 | + '<script src="https://test.com"></script>', |
| 44 | + TagHelper::tagChangeAttributeValue($tag, 'src', 'https://test.com') |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + public function testTagChangeAttributeValueShouldDoNothingIfAttributeDoesntExist(): void |
| 49 | + { |
| 50 | + $tag = '<script src="https://google.com"></script>'; |
| 51 | + |
| 52 | + self::assertSame( |
| 53 | + '<script src="https://google.com"></script>', |
| 54 | + TagHelper::tagChangeAttributeValue($tag, 'nonce', 'da65sf1g') |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + public function testTagAddAttributeShouldAddAttributeWithValue(): void |
| 59 | + { |
| 60 | + $tag = '<script src="https://google.com"></script>'; |
| 61 | + |
| 62 | + self::assertSame( |
| 63 | + '<script src="https://google.com" nonce="da65sf1g"></script>', |
| 64 | + TagHelper::tagAddAttribute($tag, 'nonce', 'da65sf1g') |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + public function testTagAddAttributeShouldAddAttributeWithoutValue(): void |
| 69 | + { |
| 70 | + $tag = '<script src="https://google.com"></script>'; |
| 71 | + |
| 72 | + self::assertSame( |
| 73 | + '<script src="https://google.com" defer></script>', |
| 74 | + TagHelper::tagAddAttribute($tag, 'defer') |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
0 commit comments