|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PHPSu\ShellCommandBuilder\Tests\Definiton; |
| 6 | + |
| 7 | +use PHPSu\ShellCommandBuilder\Definition\Pattern; |
| 8 | +use PHPSu\ShellCommandBuilder\Exception\ShellBuilderException; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +final class PatternTest extends TestCase |
| 12 | +{ |
| 13 | + public function testSplitNothingSpecial(): void |
| 14 | + { |
| 15 | + $this->assertEquals(['a', 'b', 'c', 'd'], Pattern::split('a b c d')); |
| 16 | + } |
| 17 | + |
| 18 | + public function testSplitQuotedStrings(): void |
| 19 | + { |
| 20 | + $this->assertEquals(['a', 'b b', 'a'], Pattern::split('a "b b" a')); |
| 21 | + } |
| 22 | + |
| 23 | + public function testSplitSingleQuotedStrings(): void |
| 24 | + { |
| 25 | + $this->assertEquals(["a", "'b' c", "d"], Pattern::split('a "\'b\' c" d')); |
| 26 | + } |
| 27 | + |
| 28 | + public function testSplitFileName(): void |
| 29 | + { |
| 30 | + $this->assertEquals(['/home/user/dev/hallo welt.txt'], Pattern::split('/home/user/dev/hallo\ welt.txt')); |
| 31 | + } |
| 32 | + |
| 33 | + public function testSplitDoubleQuotedStrings(): void |
| 34 | + { |
| 35 | + $this->assertEquals(["a", "\"b\" c", "d"], Pattern::split('a "\"b\" c" d')); |
| 36 | + } |
| 37 | + |
| 38 | + public function testSplitEscapedSpaceStrings(): void |
| 39 | + { |
| 40 | + $this->assertEquals(["a", "b c", "d"], Pattern::split('a b\ c d')); |
| 41 | + } |
| 42 | + |
| 43 | + public function testBadDoubleQuotes(): void |
| 44 | + { |
| 45 | + $this->expectException(ShellBuilderException::class); |
| 46 | + $this->expectExceptionMessage('The given input has mismatching Quotes'); |
| 47 | + Pattern::split("a \"b c d e"); |
| 48 | + } |
| 49 | + |
| 50 | + public function testBadSingleQuotes(): void |
| 51 | + { |
| 52 | + $this->expectException(ShellBuilderException::class); |
| 53 | + $this->expectExceptionMessage('The given input has mismatching Quotes'); |
| 54 | + Pattern::split("a 'b c d e"); |
| 55 | + } |
| 56 | + |
| 57 | + public function testBadMultipleQuotes(): void |
| 58 | + { |
| 59 | + $this->expectException(ShellBuilderException::class); |
| 60 | + $this->expectExceptionMessage('The given input has mismatching Quotes'); |
| 61 | + Pattern::split("one '\"\"\""); |
| 62 | + } |
| 63 | + |
| 64 | + public function testSplitTrailingWhitespace(): void |
| 65 | + { |
| 66 | + $this->assertEquals(["a", "b", "c", "d"], Pattern::split('a b c d ')); |
| 67 | + } |
| 68 | + |
| 69 | + public function testMultibyte(): void |
| 70 | + { |
| 71 | + // currently multibyte is not escaped - make sure escaping happens before |
| 72 | + $this->assertEquals(["あい", "あい"], Pattern::split('あい あい')); |
| 73 | + } |
| 74 | + |
| 75 | + public function testSplitPercentSign(): void |
| 76 | + { |
| 77 | + $this->assertEquals(["abc", "%foo bar%"], Pattern::split('abc \'%foo bar%\'')); |
| 78 | + } |
| 79 | +} |
0 commit comments