|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Bundle\MongoDBMakerBundle\Tests\MongoDB; |
| 6 | + |
| 7 | +use Doctrine\Bundle\MongoDBMakerBundle\MongoDB\Validator; |
| 8 | +use Generator; |
| 9 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; |
| 12 | + |
| 13 | +class ValidatorTest extends TestCase |
| 14 | +{ |
| 15 | + #[DataProvider('validNotBlankProvider')] |
| 16 | + public function testNotBlankWithValidValue(string $value): void |
| 17 | + { |
| 18 | + $result = Validator::notBlank($value); |
| 19 | + |
| 20 | + $this->assertSame($value, $result); |
| 21 | + } |
| 22 | + |
| 23 | + /** @return Generator<string, array{0: string}> */ |
| 24 | + public static function validNotBlankProvider(): Generator |
| 25 | + { |
| 26 | + yield 'simple string' => ['valid']; |
| 27 | + yield 'string with spaces' => ['hello world']; |
| 28 | + yield 'single character' => ['a']; |
| 29 | + yield 'whitespace only' => [' ']; |
| 30 | + } |
| 31 | + |
| 32 | + #[DataProvider('invalidNotBlankProvider')] |
| 33 | + public function testNotBlankWithInvalidValue(string|null $value): void |
| 34 | + { |
| 35 | + $this->expectException(RuntimeCommandException::class); |
| 36 | + $this->expectExceptionMessage('This value cannot be blank.'); |
| 37 | + |
| 38 | + Validator::notBlank($value); |
| 39 | + } |
| 40 | + |
| 41 | + /** @return Generator<string, array{0: string|null}> */ |
| 42 | + public static function invalidNotBlankProvider(): Generator |
| 43 | + { |
| 44 | + yield 'null' => [null]; |
| 45 | + yield 'empty string' => ['']; |
| 46 | + } |
| 47 | + |
| 48 | + #[DataProvider('validFieldNameProvider')] |
| 49 | + public function testValidateFieldNameWithValidNames(string $fieldName, string $expected): void |
| 50 | + { |
| 51 | + $result = Validator::validateFieldName($fieldName); |
| 52 | + |
| 53 | + $this->assertSame($expected, $result); |
| 54 | + } |
| 55 | + |
| 56 | + /** @return Generator<string, array{0: string, 1: string}> */ |
| 57 | + public static function validFieldNameProvider(): Generator |
| 58 | + { |
| 59 | + yield 'simple name' => ['name', 'name']; |
| 60 | + yield 'camelCase' => ['firstName', 'firstName']; |
| 61 | + yield 'with underscore prefix' => ['_private', '_private']; |
| 62 | + yield 'with numbers' => ['field1', 'field1']; |
| 63 | + yield 'snake_case' => ['user_name', 'user_name']; |
| 64 | + yield 'uppercase' => ['CONSTANT', 'CONSTANT']; |
| 65 | + yield 'mixed case with numbers' => ['myField2Test', 'myField2Test']; |
| 66 | + yield 'dollar prefix is trimmed' => ['$field', 'field']; |
| 67 | + yield 'multiple dollar prefix is trimmed' => ['$$field', 'field']; |
| 68 | + } |
| 69 | + |
| 70 | + #[DataProvider('emptyFieldNameProvider')] |
| 71 | + public function testValidateFieldNameWithEmptyValue(string $fieldName): void |
| 72 | + { |
| 73 | + $this->expectException(RuntimeCommandException::class); |
| 74 | + $this->expectExceptionMessage('Field name cannot be empty.'); |
| 75 | + |
| 76 | + Validator::validateFieldName($fieldName); |
| 77 | + } |
| 78 | + |
| 79 | + /** @return Generator<string, array{0: string|null}> */ |
| 80 | + public static function emptyFieldNameProvider(): Generator |
| 81 | + { |
| 82 | + yield 'empty string' => ['']; |
| 83 | + yield 'dollar only' => ['$']; |
| 84 | + yield 'multiple dollars only' => ['$$$']; |
| 85 | + } |
| 86 | + |
| 87 | + #[DataProvider('invalidPhpPropertyNameProvider')] |
| 88 | + public function testValidateFieldNameWithInvalidPhpPropertyNames(string $fieldName): void |
| 89 | + { |
| 90 | + $this->expectException(RuntimeCommandException::class); |
| 91 | + $this->expectExceptionMessageMatches('/is not a valid PHP property name/'); |
| 92 | + |
| 93 | + Validator::validateFieldName($fieldName); |
| 94 | + } |
| 95 | + |
| 96 | + /** @return Generator<string, array{0: string}> */ |
| 97 | + public static function invalidPhpPropertyNameProvider(): Generator |
| 98 | + { |
| 99 | + yield 'starts with number' => ['1field']; |
| 100 | + yield 'contains dash' => ['field-name']; |
| 101 | + yield 'contains space' => ['field name']; |
| 102 | + yield 'contains special char' => ['field@name']; |
| 103 | + } |
| 104 | + |
| 105 | + #[DataProvider('mongoDbRestrictedFieldNameProvider')] |
| 106 | + public function testValidateFieldNameWithMongoDbRestrictions(string $fieldName, string $expectedMessagePattern): void |
| 107 | + { |
| 108 | + $this->expectException(RuntimeCommandException::class); |
| 109 | + $this->expectExceptionMessageMatches($expectedMessagePattern); |
| 110 | + |
| 111 | + Validator::validateFieldName($fieldName); |
| 112 | + } |
| 113 | + |
| 114 | + /** @return Generator<string, array{0: string, 1: string}> */ |
| 115 | + public static function mongoDbRestrictedFieldNameProvider(): Generator |
| 116 | + { |
| 117 | + yield 'contains dot' => ['field.name', '/cannot contain a dot/']; |
| 118 | + yield 'null character' => ["field\0name", '/cannot contain null characters/']; |
| 119 | + } |
| 120 | +} |
0 commit comments