Skip to content

Commit

Permalink
Add test cases and handle exceptions for undefined rules (#61)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SmetDenis authored Mar 19, 2024
1 parent 0e3110f commit 1735c8d
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 26 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ It's random ideas and plans. No orderings and deadlines. <u>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.
Expand All @@ -594,6 +595,7 @@ It's random ideas and plans. No orderings and deadlines. <u>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?
Expand Down
9 changes: 9 additions & 0 deletions tests/Commands/ValidateCsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
}
}
7 changes: 6 additions & 1 deletion tests/Rules/Cell/AllowValuesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/Rules/Cell/DateFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
);
}
}
42 changes: 42 additions & 0 deletions tests/Rules/Cell/IsTrimmedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/

declare(strict_types=1);

namespace JBZoo\PHPUnit\Rules\Cell;

use JBZoo\CsvBlueprint\Rules\Cell\IsTrimmed;
use JBZoo\PHPUnit\Rules\AbstractCellRule;

use function JBZoo\PHPUnit\isSame;

final class IsTrimmedTest extends AbstractCellRule
{
protected string $ruleClass = IsTrimmed::class;

public function testPositive(): void
{
$rule = $this->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! '));
}
}
13 changes: 4 additions & 9 deletions tests/Rules/Cell/NotAllowValuesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions tests/Rules/Cell/RegexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
13 changes: 5 additions & 8 deletions tests/Rules/Cell/StratsWithTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '));
}
}
1 change: 1 addition & 0 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function testFindFiles(): void
])));

isSame([], $this->getFileName(Utils::findFiles([])));
isSame([], $this->getFileName(Utils::findFiles([''])));

$this->getFileName(Utils::findFiles(['*.qwerty']));

Expand Down

0 comments on commit 1735c8d

Please sign in to comment.