|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BNETDocs\Tests\Libraries\Core; |
| 4 | + |
| 5 | +use \BNETDocs\Libraries\Core\StringProcessor; |
| 6 | +use \PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +class StringProcessorTest extends TestCase |
| 9 | +{ |
| 10 | + // fuzzyMatch |
| 11 | + |
| 12 | + public function testFuzzyMatchWildcardSuffix(): void |
| 13 | + { |
| 14 | + $this->assertTrue(StringProcessor::fuzzyMatch('api.*', 'api.enabled')); |
| 15 | + } |
| 16 | + |
| 17 | + public function testFuzzyMatchWildcardSuffixNoMatch(): void |
| 18 | + { |
| 19 | + $this->assertFalse(StringProcessor::fuzzyMatch('api.*', 'other.enabled')); |
| 20 | + } |
| 21 | + |
| 22 | + public function testFuzzyMatchExact(): void |
| 23 | + { |
| 24 | + $this->assertTrue(StringProcessor::fuzzyMatch('exact', 'exact')); |
| 25 | + } |
| 26 | + |
| 27 | + public function testFuzzyMatchCaseInsensitive(): void |
| 28 | + { |
| 29 | + $this->assertTrue(StringProcessor::fuzzyMatch('exact', 'EXACT')); |
| 30 | + } |
| 31 | + |
| 32 | + public function testFuzzyMatchWildcardMiddle(): void |
| 33 | + { |
| 34 | + $this->assertTrue(StringProcessor::fuzzyMatch('foo.*.bar', 'foo.baz.bar')); |
| 35 | + } |
| 36 | + |
| 37 | + // isBrowser |
| 38 | + |
| 39 | + public function testIsBrowserFirefox(): void |
| 40 | + { |
| 41 | + $this->assertTrue(StringProcessor::isBrowser('Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0')); |
| 42 | + } |
| 43 | + |
| 44 | + public function testIsBrowserCurl(): void |
| 45 | + { |
| 46 | + $this->assertFalse(StringProcessor::isBrowser('curl/7.88.1')); |
| 47 | + } |
| 48 | + |
| 49 | + public function testIsBrowserEmpty(): void |
| 50 | + { |
| 51 | + $this->assertFalse(StringProcessor::isBrowser('')); |
| 52 | + } |
| 53 | + |
| 54 | + // sanitizeForUrl |
| 55 | + |
| 56 | + public function testSanitizeForUrlBasic(): void |
| 57 | + { |
| 58 | + $this->assertSame('hello-world', StringProcessor::sanitizeForUrl('Hello World!')); |
| 59 | + } |
| 60 | + |
| 61 | + public function testSanitizeForUrlTrimsLeadingTrailingDashes(): void |
| 62 | + { |
| 63 | + $this->assertSame('hello', StringProcessor::sanitizeForUrl(' hello ')); |
| 64 | + } |
| 65 | + |
| 66 | + public function testSanitizeForUrlCollapsesMultipleSeparators(): void |
| 67 | + { |
| 68 | + $this->assertSame('foo-bar', StringProcessor::sanitizeForUrl('foo---bar')); |
| 69 | + } |
| 70 | + |
| 71 | + public function testSanitizeForUrlLowercaseFalse(): void |
| 72 | + { |
| 73 | + $this->assertSame('Hello-World', StringProcessor::sanitizeForUrl('Hello World', false)); |
| 74 | + } |
| 75 | + |
| 76 | + public function testSanitizeForUrlArray(): void |
| 77 | + { |
| 78 | + $this->assertSame(['hello-world', 'foo-bar'], StringProcessor::sanitizeForUrl(['Hello World', 'Foo Bar'])); |
| 79 | + } |
| 80 | + |
| 81 | + // stripExcessLines |
| 82 | + |
| 83 | + public function testStripExcessLinesCollapsesMultiple(): void |
| 84 | + { |
| 85 | + $this->assertSame("a\n\nb", StringProcessor::stripExcessLines("a\n\n\n\nb")); |
| 86 | + } |
| 87 | + |
| 88 | + public function testStripExcessLinesLeavesDoubleNewlineAlone(): void |
| 89 | + { |
| 90 | + $this->assertSame("a\n\nb", StringProcessor::stripExcessLines("a\n\nb")); |
| 91 | + } |
| 92 | + |
| 93 | + public function testStripExcessLinesSingleNewlineUnchanged(): void |
| 94 | + { |
| 95 | + $this->assertSame("a\nb", StringProcessor::stripExcessLines("a\nb")); |
| 96 | + } |
| 97 | + |
| 98 | + // stripLeftPattern |
| 99 | + |
| 100 | + public function testStripLeftPatternRemovesPrefix(): void |
| 101 | + { |
| 102 | + $this->assertSame('bar', StringProcessor::stripLeftPattern('foobar', 'foo')); |
| 103 | + } |
| 104 | + |
| 105 | + public function testStripLeftPatternNoMatchReturnsOriginal(): void |
| 106 | + { |
| 107 | + $this->assertSame('foobar', StringProcessor::stripLeftPattern('foobar', 'baz')); |
| 108 | + } |
| 109 | + |
| 110 | + public function testStripLeftPatternEmptyNeedle(): void |
| 111 | + { |
| 112 | + $this->assertSame('foobar', StringProcessor::stripLeftPattern('foobar', '')); |
| 113 | + } |
| 114 | + |
| 115 | + // stripToSnippet |
| 116 | + |
| 117 | + public function testStripToSnippetShortStringUnchanged(): void |
| 118 | + { |
| 119 | + $this->assertSame('short', StringProcessor::stripToSnippet('short', 100)); |
| 120 | + } |
| 121 | + |
| 122 | + public function testStripToSnippetTruncatesAtWordBoundary(): void |
| 123 | + { |
| 124 | + $result = StringProcessor::stripToSnippet('Hello world foo', 8); |
| 125 | + $this->assertStringEndsWith('...', $result); |
| 126 | + $this->assertLessThanOrEqual(8, strlen($result)); |
| 127 | + } |
| 128 | +} |
0 commit comments