|
| 1 | +<?php |
| 2 | + |
| 3 | +// SPDX-FileCopyrightText: 2026 LibreSign |
| 4 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 5 | + |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace LibreSign\XObjectTemplate\Tests\Unit\Pdf\Svg; |
| 9 | + |
| 10 | +use InvalidArgumentException; |
| 11 | +use LibreSign\XObjectTemplate\Pdf\Svg\SvgPathCommandParser; |
| 12 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +final class SvgPathCommandParserTest extends TestCase |
| 16 | +{ |
| 17 | + public function testConvertPathDataSupportsLineCommandsAndClosePath(): void |
| 18 | + { |
| 19 | + $parser = new SvgPathCommandParser(); |
| 20 | + |
| 21 | + $result = $parser->convertPathData( |
| 22 | + 'M 0 0 L 10 0 H 15 V 5 Z', |
| 23 | + 0.0, |
| 24 | + 10.0, |
| 25 | + '/tmp/shape.svg', |
| 26 | + [1.0, 0.0, 0.0, 1.0, 0.0, 0.0], |
| 27 | + ); |
| 28 | + |
| 29 | + self::assertStringContainsString('0.000000 10.000000 m', $result); |
| 30 | + self::assertStringContainsString('10.000000 10.000000 l', $result); |
| 31 | + self::assertStringContainsString('15.000000 10.000000 l', $result); |
| 32 | + self::assertStringContainsString('15.000000 5.000000 l', $result); |
| 33 | + self::assertStringEndsWith('h', $result); |
| 34 | + } |
| 35 | + |
| 36 | + public function testConvertPathDataSupportsRelativeMoveImplicitLinesAndTransformMatrix(): void |
| 37 | + { |
| 38 | + $parser = new SvgPathCommandParser(); |
| 39 | + |
| 40 | + $result = $parser->convertPathData( |
| 41 | + 'm 1 1 2 0 0 2', |
| 42 | + 0.0, |
| 43 | + 20.0, |
| 44 | + '/tmp/relative.svg', |
| 45 | + [2.0, 0.0, 0.0, 2.0, 1.0, 3.0], |
| 46 | + ); |
| 47 | + |
| 48 | + self::assertStringContainsString('3.000000 15.000000 m', $result); |
| 49 | + self::assertStringContainsString('7.000000 15.000000 l', $result); |
| 50 | + self::assertStringContainsString('7.000000 11.000000 l', $result); |
| 51 | + } |
| 52 | + |
| 53 | + public function testConvertPathDataSupportsCubicQuadraticAndArcCommands(): void |
| 54 | + { |
| 55 | + $parser = new SvgPathCommandParser(); |
| 56 | + |
| 57 | + $result = $parser->convertPathData( |
| 58 | + 'M 0 10 C 2 8 4 8 6 10 Q 8 12 10 10 T 14 10 A 4 2 0 0 1 18 10', |
| 59 | + 0.0, |
| 60 | + 20.0, |
| 61 | + '/tmp/curves.svg', |
| 62 | + [1.0, 0.0, 0.0, 1.0, 0.0, 0.0], |
| 63 | + ); |
| 64 | + |
| 65 | + self::assertGreaterThanOrEqual(4, substr_count($result, ' c')); |
| 66 | + self::assertStringContainsString('0.000000 10.000000 m', $result); |
| 67 | + self::assertStringContainsString('6.000000 10.000000 c', $result); |
| 68 | + self::assertStringContainsString('14.000000 10.000000 c', $result); |
| 69 | + } |
| 70 | + |
| 71 | + #[DataProvider('provideInvalidPathScenarios')] |
| 72 | + public function testConvertPathDataRejectsInvalidSequences(string $pathData, string $expectedMessage): void |
| 73 | + { |
| 74 | + $parser = new SvgPathCommandParser(); |
| 75 | + |
| 76 | + $this->expectException(InvalidArgumentException::class); |
| 77 | + $this->expectExceptionMessage($expectedMessage); |
| 78 | + |
| 79 | + $parser->convertPathData( |
| 80 | + $pathData, |
| 81 | + 0.0, |
| 82 | + 10.0, |
| 83 | + '/tmp/invalid.svg', |
| 84 | + [1.0, 0.0, 0.0, 1.0, 0.0, 0.0], |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @return iterable<string, array{pathData: string, expectedMessage: string}> |
| 90 | + */ |
| 91 | + public static function provideInvalidPathScenarios(): iterable |
| 92 | + { |
| 93 | + yield 'empty path data' => [ |
| 94 | + 'pathData' => '', |
| 95 | + 'expectedMessage' => 'Unsupported or empty SVG path data in "/tmp/invalid.svg".', |
| 96 | + ]; |
| 97 | + |
| 98 | + yield 'path without initial command' => [ |
| 99 | + 'pathData' => '1 2 3', |
| 100 | + 'expectedMessage' => 'Invalid SVG path command sequence in "/tmp/invalid.svg".', |
| 101 | + ]; |
| 102 | + |
| 103 | + yield 'malformed move command' => [ |
| 104 | + 'pathData' => 'M 1', |
| 105 | + 'expectedMessage' => 'Malformed SVG path data in "/tmp/invalid.svg".', |
| 106 | + ]; |
| 107 | + |
| 108 | + yield 'unsupported command' => [ |
| 109 | + 'pathData' => 'M 1 1 R 2 2', |
| 110 | + 'expectedMessage' => 'SVG path command "R" is not supported for source "/tmp/invalid.svg".', |
| 111 | + ]; |
| 112 | + } |
| 113 | +} |
0 commit comments