From fa2b30d3223a20bbe69f7a3f942028d6de4ca5df Mon Sep 17 00:00:00 2001 From: Denis Smet Date: Tue, 19 Mar 2024 15:27:10 +0400 Subject: [PATCH] Add test cases and handle exceptions for undefined rules Added additional tests for when schema file not found and when certain rules are left undefined. These tests cover cases for allowed values, date formats, disallowed values, and regex pattern. Furthermore, a new rule for checking whether a cell data is trimmed was introduced. Also fixed some formatting errors and updated the README file. --- README.md | 2 ++ tests/Commands/ValidateCsvTest.php | 9 ++++++ tests/Rules/Cell/AllowValuesTest.php | 7 +++- tests/Rules/Cell/DateFormatTest.php | 6 ++++ tests/Rules/Cell/IsTrimmedTest.php | 43 +++++++++++++++++++++++++ tests/Rules/Cell/NotAllowValuesTest.php | 13 +++----- tests/Rules/Cell/RegexTest.php | 13 +++----- tests/Rules/Cell/StratsWithTest.php | 13 +++----- tests/UtilsTest.php | 1 + 9 files changed, 81 insertions(+), 26 deletions(-) create mode 100644 tests/Rules/Cell/IsTrimmedTest.php diff --git a/README.md b/README.md index 4e04cc6e..063d80a6 100644 --- a/README.md +++ b/README.md @@ -568,6 +568,7 @@ It's random ideas and plans. No orderings and deadlines. But batch processing * [More aggregate rules](https://github.com/markrogoyski/math-php#statistics---descriptive). * [More cell rules](https://github.com/Respect/Validation). * `required` flag for the column. + * Multi values in one cell. * Custom cell rule as a callback. It's useful when you have a complex rule that can't be described in the schema file. * Custom agregate rule as a callback. It's useful when you have a complex rule that can't be described in the schema file. * Configurable keyword for null/empty values. By default, it's an empty string. But you will use `null`, `nil`, `none`, `empty`, etc. Overridable on the column level. @@ -594,6 +595,7 @@ It's random ideas and plans. No orderings and deadlines. But batch processing * **Mock data generation** * Create CSV files based on the schema (like "create 1000 rows with random data based on schema and rules"). * Use [Faker](https://github.com/FakerPHP/Faker) for random data generation. + * [ReverseRegex](https://github.com/enso-media/ReverseRegex) to generate text from regex. * **Reporting** * More report formats (like JSON, XML, etc). Any ideas? diff --git a/tests/Commands/ValidateCsvTest.php b/tests/Commands/ValidateCsvTest.php index b3f081e7..879102d3 100644 --- a/tests/Commands/ValidateCsvTest.php +++ b/tests/Commands/ValidateCsvTest.php @@ -485,4 +485,13 @@ public function testInvalidSchemaAndNoFoundCSV(): void isSame(1, $exitCode, $actual); isSame($expected, $actual); } + + public function testSchemaNotFound(): void + { + $this->expectExceptionMessage('Schema file not found: invalid_schema_path.yml'); + Tools::virtualExecution('validate:csv', [ + 'csv' => './tests/fixtures/no-found-file.csv', + 'schema' => 'invalid_schema_path.yml', + ]); + } } diff --git a/tests/Rules/Cell/AllowValuesTest.php b/tests/Rules/Cell/AllowValuesTest.php index e97d5456..ab7e508c 100644 --- a/tests/Rules/Cell/AllowValuesTest.php +++ b/tests/Rules/Cell/AllowValuesTest.php @@ -42,11 +42,16 @@ public function testPositive(): void public function testNegative(): void { $rule = $this->create(['1', '2', '3']); - isSame( 'Value "invalid" is not allowed. Allowed values: ["1", "2", "3"]', $rule->test('invalid'), ); + + $rule = $this->create([]); + isSame( + 'Allowed values are not defined', + $rule->test('invalid'), + ); } public function testInvalidOption(): void diff --git a/tests/Rules/Cell/DateFormatTest.php b/tests/Rules/Cell/DateFormatTest.php index 8c4ecd6b..7618cf51 100644 --- a/tests/Rules/Cell/DateFormatTest.php +++ b/tests/Rules/Cell/DateFormatTest.php @@ -43,5 +43,11 @@ public function testNegative(): void 'Date format of value "2000-01-02 12:34:56" is not valid. Expected format: "Y-m-d"', $rule->test('2000-01-02 12:34:56'), ); + + $rule = $this->create(''); + isSame( + 'Date format is not defined', + $rule->test('12'), + ); } } diff --git a/tests/Rules/Cell/IsTrimmedTest.php b/tests/Rules/Cell/IsTrimmedTest.php new file mode 100644 index 00000000..dd7e4fe5 --- /dev/null +++ b/tests/Rules/Cell/IsTrimmedTest.php @@ -0,0 +1,43 @@ +create(true); + isSame('', $rule->test('')); + isSame('', $rule->test('Hello world!')); + } + + public function testNegative(): void + { + $rule = $this->create(true); + isSame('Value "Hello world! " is not trimmed', $rule->test('Hello world! ')); + isSame('Value " Hello world!" is not trimmed', $rule->test(' Hello world!')); + isSame('Value " Hello world! " is not trimmed', $rule->test(' Hello world! ')); + } +} diff --git a/tests/Rules/Cell/NotAllowValuesTest.php b/tests/Rules/Cell/NotAllowValuesTest.php index 795d0193..b3cc8f3d 100644 --- a/tests/Rules/Cell/NotAllowValuesTest.php +++ b/tests/Rules/Cell/NotAllowValuesTest.php @@ -40,16 +40,11 @@ public function testPositive(): void public function testNegative(): void { $rule = $this->create(['invalid', ' ']); + isSame('Value "invalid" is not allowed', $rule->test('invalid')); + isSame('Value " " is not allowed', $rule->test(' ')); - isSame( - 'Value "invalid" is not allowed', - $rule->test('invalid'), - ); - - isSame( - 'Value " " is not allowed', - $rule->test(' '), - ); + $rule = $this->create([]); + isSame('Not allowed values are not defined', $rule->test('invalid')); } public function testInvalidOption(): void diff --git a/tests/Rules/Cell/RegexTest.php b/tests/Rules/Cell/RegexTest.php index 0a5a0e33..1dbf1517 100644 --- a/tests/Rules/Cell/RegexTest.php +++ b/tests/Rules/Cell/RegexTest.php @@ -43,15 +43,12 @@ public function testPositive(): void public function testNegative(): void { $rule = $this->create('/^a/'); - isSame( - 'Value "1bc" does not match the pattern "/^a/"', - $rule->test('1bc'), - ); + isSame('Value "1bc" does not match the pattern "/^a/"', $rule->test('1bc')); $rule = $this->create('^a'); - isSame( - 'Value "1bc" does not match the pattern "/^a/"', - $rule->test('1bc'), - ); + isSame('Value "1bc" does not match the pattern "/^a/"', $rule->test('1bc')); + + $rule = $this->create(''); + isSame('Regex pattern is not defined', $rule->test('1bc')); } } diff --git a/tests/Rules/Cell/StratsWithTest.php b/tests/Rules/Cell/StratsWithTest.php index 4f9634df..ec6df49f 100644 --- a/tests/Rules/Cell/StratsWithTest.php +++ b/tests/Rules/Cell/StratsWithTest.php @@ -32,21 +32,18 @@ public function testPositive(): void isSame('', $rule->test('a')); isSame('', $rule->test('abc')); isSame(null, $rule->validate('')); + + $rule = $this->create('a'); + isSame('', $rule->test('')); } public function testNegative(): void { $rule = $this->create('a'); - isSame( - 'Value " a" must start with "a"', - $rule->test(' a'), - ); + isSame('Value " a" must start with "a"',$rule->test(' a')); $rule = $this->create(''); - isSame( - 'Rule must contain a prefix value in schema file.', - $rule->test('a '), - ); + isSame('Rule must contain a prefix value in schema file.', $rule->test('a ')); } } diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index adf068a6..9ebff7b4 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -51,6 +51,7 @@ public function testFindFiles(): void ]))); isSame([], $this->getFileName(Utils::findFiles([]))); + isSame([], $this->getFileName(Utils::findFiles(['']))); $this->getFileName(Utils::findFiles(['*.qwerty']));