|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Filter\String; |
| 6 | + |
| 7 | +use Membrane\Filter\String\ToKebabCase; |
| 8 | +use Membrane\Result\{Message, MessageSet, Result}; |
| 9 | +use PHPUnit\Framework\Attributes\{CoversClass, DataProvider, Test, TestDox, UsesClass}; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +#[CoversClass(ToKebabCase::class)] |
| 13 | +#[UsesClass(Message::class)] |
| 14 | +#[UsesClass(MessageSet::class)] |
| 15 | +#[UsesClass(Result::class)] |
| 16 | +class ToKebabCaseTest extends TestCase |
| 17 | +{ |
| 18 | + #[Test] |
| 19 | + #[TestDox('toString() returns a description of its behaviour')] |
| 20 | + public function itCaststoString(): void |
| 21 | + { |
| 22 | + self::assertSame( |
| 23 | + 'Convert text to kebab-case', |
| 24 | + (new ToKebabCase())->__toString() |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + #[Test] |
| 29 | + #[TestDox('__toPHP() returns a string of evaluable PHP')] |
| 30 | + public function itCaststoPHP(): void |
| 31 | + { |
| 32 | + $sut = new ToKebabCase(); |
| 33 | + |
| 34 | + self::assertEquals($sut, eval('return ' . $sut->__toPHP() . ';')); |
| 35 | + } |
| 36 | + |
| 37 | + #[Test] |
| 38 | + #[TestDox('It will return an invalid Result if it filters values that are not strings')] |
| 39 | + public function returnsInvalidResultForNonStringValues(): void |
| 40 | + { |
| 41 | + $value = 5; |
| 42 | + $expected = Result::invalid($value, new MessageSet(null, new Message( |
| 43 | + 'Expected string value, received %s', |
| 44 | + [gettype($value)], |
| 45 | + ))); |
| 46 | + |
| 47 | + self::assertEquals($expected, (new ToKebabCase())->filter($value)); |
| 48 | + } |
| 49 | + |
| 50 | + #[Test] |
| 51 | + #[TestDox('It filters strings to PascalCase')] |
| 52 | + #[DataProvider('provideStringsToFilter')] |
| 53 | + public function filtersStringsToKebabCase(string $value, Result $expected): void |
| 54 | + { |
| 55 | + self::assertEquals($expected, (new ToKebabCase())->filter($value)); |
| 56 | + } |
| 57 | + |
| 58 | + public static function provideStringsToFilter(): array |
| 59 | + { |
| 60 | + return [ |
| 61 | + '"Hello, World!"' => [ |
| 62 | + '"Hello, World!"', |
| 63 | + Result::noResult('hello-world'), |
| 64 | + ], |
| 65 | + 'camelCase' => [ |
| 66 | + 'camelCase', |
| 67 | + Result::noResult('camelcase'), |
| 68 | + ], |
| 69 | + 'kebab-case' => [ |
| 70 | + 'kebab-case', |
| 71 | + Result::noResult('kebab-case'), |
| 72 | + ], |
| 73 | + 'kebabber------case' => [ |
| 74 | + 'kebabber------case', |
| 75 | + Result::noResult('kebabber-case'), |
| 76 | + ], |
| 77 | + 'snake_case' => [ |
| 78 | + 'snake_case', |
| 79 | + Result::noResult('snake-case'), |
| 80 | + ], |
| 81 | + 'snakiest_____case' => [ |
| 82 | + 'snakiest_____case', |
| 83 | + Result::noResult('snakiest-case'), |
| 84 | + ], |
| 85 | + 'pets/{id}' => [ |
| 86 | + 'pets/{id}', |
| 87 | + Result::noResult('pets-id'), |
| 88 | + ], |
| 89 | + 'http://petstore.swagger.io/{version}/pets/{id}' => [ |
| 90 | + 'http://petstore.swagger.io/{version}/pets/{id}', |
| 91 | + Result::noResult('http-petstore-swagger-io-version-pets-id'), |
| 92 | + ], |
| 93 | + 'plain text' => [ |
| 94 | + 'plain text', |
| 95 | + Result::noResult('plain-text'), |
| 96 | + ], |
| 97 | + 'plain 1text' => [ |
| 98 | + 'plain 1text', |
| 99 | + Result::noResult('plain-1text'), |
| 100 | + ], |
| 101 | + 'plain 1 text' => [ |
| 102 | + 'plain 1 text', |
| 103 | + Result::noResult('plain-1-text'), |
| 104 | + ], |
| 105 | + 'sTuPiD-_-cAsE' => [ |
| 106 | + 'sTuPiD-_-cAsE', |
| 107 | + Result::noResult('stupid-case'), |
| 108 | + ], |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | +} |
0 commit comments