Skip to content

Commit

Permalink
Merge 2ea0f69 into 0a49388
Browse files Browse the repository at this point in the history
  • Loading branch information
codisart committed Jan 2, 2021
2 parents 0a49388 + 2ea0f69 commit b02b928
Show file tree
Hide file tree
Showing 8 changed files with 351 additions and 204 deletions.
40 changes: 22 additions & 18 deletions test/DigitsTest.php
Expand Up @@ -16,9 +16,7 @@
*/
class DigitsTest extends TestCase
{
/**
* @var Digits
*/
/** @var Digits */
protected $validator;

protected function setUp() : void
Expand All @@ -29,24 +27,30 @@ protected function setUp() : void
/**
* Ensures that the validator follows expected behavior for basic input values
*
* @dataProvider basicDataProvider
* @return void
*/
public function testExpectedResultsWithBasicInputValues()
public function testExpectedResultsWithBasicInputValues(string $input, bool $expected)
{
$valuesExpected = [
'abc123' => false,
'abc 123' => false,
'abcxyz' => false,
'AZ@#4.3' => false,
'1.23' => false,
'0x9f' => false,
'123' => true,
'09' => true,
'' => false,
];
foreach ($valuesExpected as $input => $result) {
$this->assertEquals($result, $this->validator->isValid($input));
}
$this->assertSame($expected, $this->validator->isValid($input));
}

public function basicDataProvider()
{
return [
// phpcs:disable
'invalid; starts with alphabetic chars' => ['abc123', false],
'invalid; contains alphabetic chars and one whitespace' => ['abc 123', false],
'invalid; contains only alphabetic chars' => ['abcxyz', false],
'invalid; contains alphabetic and special chars' => ['AZ@#4.3', false],
'invalid; is a float' => ['1.23', false],
'invalid; is a hexa notation' => ['0x9f', false],
'invalid; is empty' => ['', false],

'valid; is a normal integer' => ['123', true],
'valid; starts with a zero' => ['09', true],
// phpcs:enable
];
}

/**
Expand Down
65 changes: 33 additions & 32 deletions test/File/CountTest.php
Expand Up @@ -21,41 +21,42 @@ class CountTest extends TestCase
/**
* Ensures that the validator follows expected behavior
*
* @dataProvider basicDataProvider
* @param array|int $options
* @return void
*/
public function testBasic()
public function testBasic($options, bool $expected1, bool $expected2, bool $expected3, bool $expected4)
{
$valuesExpected = [
[5, true, true, true, true],
[['min' => 0, 'max' => 3], true, true, true, false],
[['min' => 2, 'max' => 3], false, true, true, false],
[['min' => 2], false, true, true, true],
[['max' => 5], true, true, true, true],
];

foreach ($valuesExpected as $element) {
$validator = new File\Count($element[0]);
$this->assertEquals(
$element[1],
$validator->isValid(__DIR__ . '/_files/testsize.mo'),
'Tested with ' . var_export($element, 1)
);
$this->assertEquals(
$element[2],
$validator->isValid(__DIR__ . '/_files/testsize2.mo'),
'Tested with ' . var_export($element, 1)
);
$this->assertEquals(
$element[3],
$validator->isValid(__DIR__ . '/_files/testsize3.mo'),
'Tested with ' . var_export($element, 1)
);
$this->assertEquals(
$element[4],
$validator->isValid(__DIR__ . '/_files/testsize4.mo'),
'Tested with ' . var_export($element, 1)
);
}
$validator = new File\Count($options);
$this->assertSame(
$expected1,
$validator->isValid(__DIR__ . '/_files/testsize.mo')
);
$this->assertSame(
$expected2,
$validator->isValid(__DIR__ . '/_files/testsize2.mo')
);
$this->assertSame(
$expected3,
$validator->isValid(__DIR__ . '/_files/testsize3.mo')
);
$this->assertSame(
$expected4,
$validator->isValid(__DIR__ . '/_files/testsize4.mo')
);
}

public function basicDataProvider()
{
return [
// phpcs:disable
'no minimum; maximum: 5; integer' => [5, true, true, true, true],
'no minimum; maximum: 5; array' => [['max' => 5], true, true, true, true],
'minimum: 0; maximum: 3' => [['min' => 0, 'max' => 3], true, true, true, false],
'minimum: 2; maximum: 3' => [['min' => 2, 'max' => 3], false, true, true, false],
'minimum: 2; no maximum' => [['min' => 2], false, true, true, true],
// phpcs:enable
];
}

/**
Expand Down
74 changes: 41 additions & 33 deletions test/File/FilesSizeTest.php
Expand Up @@ -28,50 +28,58 @@ protected function setUp() : void
/**
* Ensures that the validator follows expected behavior
*
* @dataProvider basicDataProvider
* @param array|int $options
* @return void
*/
public function testBasic()
public function testBasic($options, bool $expected1, bool $expected2, bool $expected3)
{
$valuesExpected = [
[['min' => 0, 'max' => 2000], true, true, false],
[['min' => 0, 'max' => '2 MB'], true, true, true],
[['min' => 0, 'max' => '2MB'], true, true, true],
[['min' => 0, 'max' => '2 MB'], true, true, true],
[2000, true, true, false],
[['min' => 0, 'max' => 500], false, false, false],
[500, false, false, false],
];

foreach ($valuesExpected as $element) {
$validator = new File\FilesSize($element[0]);
$this->assertEquals(
$element[1],
$validator->isValid(__DIR__ . '/_files/testsize.mo'),
'Tested with ' . var_export($element, 1)
);
$this->assertEquals(
$element[2],
$validator->isValid(__DIR__ . '/_files/testsize2.mo'),
'Tested with ' . var_export($element, 1)
);
$this->assertEquals(
$element[3],
$validator->isValid(__DIR__ . '/_files/testsize3.mo'),
'Tested with ' . var_export($element, 1)
);
}
$validator = new File\FilesSize(...$options);
$this->assertSame(
$expected1,
$validator->isValid(__DIR__ . '/_files/testsize.mo')
);
$this->assertSame(
$expected2,
$validator->isValid(__DIR__ . '/_files/testsize2.mo')
);
$this->assertSame(
$expected3,
$validator->isValid(__DIR__ . '/_files/testsize3.mo')
);
}

$validator = new File\FilesSize(['min' => 0, 'max' => 200]);
$this->assertEquals(false, $validator->isValid(__DIR__ . '/_files/nofile.mo'));
$this->assertArrayHasKey('fileFilesSizeNotReadable', $validator->getMessages());
public function basicDataProvider()
{
return [
// phpcs:disable
'minimum: 0 byte; maximum: 500 bytes; integer' => [[500], false, false, false],
'minimum: 0 byte; maximum: 500 bytes; array' => [[['min' => 0, 'max' => 500]], false, false, false],
'minimum: 0 byte; maximum: 2000 bytes; integer' => [[2000], true, true, false],
'minimum: 0 byte; maximum: 2000 bytes; array' => [[['min' => 0, 'max' => 2000]], true, true, false],
'minimum: 0 byte; maximum: 500 kilobytes' => [[['min' => 0, 'max' => 500000]], true, true, true],
'minimum: 0 byte; maximum: 2 megabytes; 2 MB' => [[['min' => 0, 'max' => '2 MB']], true, true, true],
'minimum: 0 byte; maximum: 2 megabytes; 2MB' => [[['min' => 0, 'max' => '2MB']], true, true, true],
'minimum: 0 byte; maximum: 2 megabytes; 2 MB' => [[['min' => 0, 'max' => '2 MB']], true, true, true],
// phpcs:enable
];
}

public function testMultipleFiles()
{
$validator = new File\FilesSize(['min' => 0, 'max' => 500000]);
$this->assertEquals(true, $validator->isValid([
__DIR__ . '/_files/testsize.mo',
__DIR__ . '/_files/testsize.mo',
__DIR__ . '/_files/testsize2.mo',
]));
$this->assertEquals(true, $validator->isValid(__DIR__ . '/_files/testsize.mo'));
}

public function testFileDoNotExist()
{
$validator = new File\FilesSize(['min' => 0, 'max' => 200]);
$this->assertEquals(false, $validator->isValid(__DIR__ . '/_files/nofile.mo'));
$this->assertArrayHasKey('fileFilesSizeNotReadable', $validator->getMessages());
}

/**
Expand Down
146 changes: 105 additions & 41 deletions test/GreaterThanTest.php
Expand Up @@ -19,33 +19,79 @@ class GreaterThanTest extends TestCase
/**
* Ensures that the validator follows expected behavior
*
* @dataProvider basicDataProvider
* @param int|string $input
* @return void
*/
public function testBasic()
public function testBasic(array $options, $input, bool $expected)
{
/**
* The elements of each array are, in order:
* - minimum
* - expected validation result
* - array of test input values
*/
$valuesExpected = [
[0, true, [0.01, 1, 100]],
[0, false, [0, 0.00, -0.01, -1, -100]],
['a', true, ['b', 'c', 'd']],
['z', false, ['x', 'y', 'z']],
[['min' => 0, 'inclusive' => true], true, [0, 0.00, 0.01, 1, 100]],
[['min' => 0, 'inclusive' => true], false, [-0.01, -1, -100]],
[['min' => 0, 'inclusive' => false], true, [0.01, 1, 100]],
[['min' => 0, 'inclusive' => false], false, [0, 0.00, -0.01, -1, -100]],
];
$validator = new GreaterThan(...$options);
$this->assertSame($expected, $validator->isValid($input));
}

foreach ($valuesExpected as $element) {
$validator = new GreaterThan($element[0]);
foreach ($element[2] as $input) {
$this->assertEquals($element[1], $validator->isValid($input));
}
}
public function basicDataProvider()
{
return [
// phpcs:disable
'valid; non inclusive; 0 < 0.01' => [[0], 0.01, true],
'valid; non inclusive; 0 < 1' => [[0], 1, true],
'valid; non inclusive; 0 < 100' => [[0], 100, true],

'invalid; non inclusive; 0 >= 0' => [[0], 0, false],
'invalid; non inclusive; 0 >= 0.00' => [[0], 0.00, false],
'invalid; non inclusive; 0 >= -0.01' => [[0], -0.01, false],
'invalid; non inclusive; 0 >= -1' => [[0], -1, false],
'invalid; non inclusive; 0 >= -100' => [[0], -100, false],

'valid; inclusive; 0 <= 0' => [[0, true], 0, true],
'valid; inclusive; 0 <= 0.00' => [[0, true], 0.00, true],
'valid; inclusive; 0 <= 0.01' => [[0, true], 0.01, true],
'valid; inclusive; 0 <= 1' => [[0, true], 1, true],
'valid; inclusive; 0 <= 100' => [[0, true], 100, true],

'invalid; inclusive; 0 >= -0.01' => [[0, true], -0.01, false],
'invalid; inclusive; 0 >= -1' => [[0, true], -1, false],
'invalid; inclusive; 0 >= -100' => [[0, true], -100, false],

'valid; non inclusive; a < b' => [['a'], 'b', true],
'valid; non inclusive; a < c' => [['a'], 'c', true],
'valid; non inclusive; a < d' => [['a'], 'd', true],

'valid; inclusive; a <= a' => [['a', true], 'a', true],
'valid; inclusive; a <= b' => [['a', true], 'b', true],
'valid; inclusive; a <= c' => [['a', true], 'c', true],
'valid; inclusive; a <= d' => [['a', true], 'd', true],

'invalid; non-inclusive; a >= a' => [['a', false], 'a', false],

'invalid; non inclusive; z >= x' => [['z'], 'x', false],
'invalid; non inclusive; z >= y' => [['z'], 'y', false],
'invalid; non inclusive; z >= z' => [['z'], 'z', false],

'invalid; inclusive; z > x' => [['z', true], 'x', false],
'invalid; inclusive; z > y' => [['z', true], 'y', false],

'valid; inclusive; 0 <= 0; array' => [[['min' => 0, 'inclusive' => true]], 0, true],
'valid; inclusive; 0 <= 0.00; array' => [[['min' => 0, 'inclusive' => true]], 0.00, true],
'valid; inclusive; 0 <= 0.01; array' => [[['min' => 0, 'inclusive' => true]], 0.01, true],
'valid; inclusive; 0 <= 1; array' => [[['min' => 0, 'inclusive' => true]], 1, true],
'valid; inclusive; 0 <= 100; array' => [[['min' => 0, 'inclusive' => true]], 100, true],

'invalid; inclusive; 0 >= -0.01; array' => [[['min' => 0, 'inclusive' => true]], -0.01, false],
'invalid; inclusive; 0 >= -1; array' => [[['min' => 0, 'inclusive' => true]], -1, false],
'invalid; inclusive; 0 >= -100; array' => [[['min' => 0, 'inclusive' => true]], -100, false],

'valid; non inclusive; 0 < 0.01; array' => [[['min' => 0, 'inclusive' => false]], 0.01, true],
'valid; non inclusive; 0 < 1; array' => [[['min' => 0, 'inclusive' => false]], 1, true],
'valid; non inclusive; 0 < 100; array' => [[['min' => 0, 'inclusive' => false]], 100, true],

'invalid; non inclusive; 0 >= 0; array' => [[['min' => 0, 'inclusive' => false]], 0, false],
'invalid; non inclusive; 0 >= 0.00; array' => [[['min' => 0, 'inclusive' => false]], 0.00, false],
'invalid; non inclusive; 0 >= -0.01; array' => [[['min' => 0, 'inclusive' => false]], -0.01, false],
'invalid; non inclusive; 0 >= -1; array' => [[['min' => 0, 'inclusive' => false]], -1, false],
'invalid; non inclusive; 0 >= -100; array' => [[['min' => 0, 'inclusive' => false]], -100, false],
// phpcs:enable
];
}

/**
Expand Down Expand Up @@ -101,32 +147,50 @@ public function testEqualsMessageVariables()
);
}

public function testCorrectInclusiveMessageReturn()
/**
* @dataProvider correctInclusiveMessageDataProvider
*/
public function testCorrectInclusiveMessageReturn(float $input)
{
$valuesToValidate = [0, 0.5, 5, 10];
$validator = new GreaterThan(10);
$validator->isValid($input);
$message = $validator->getMessages();

foreach ($valuesToValidate as $value) {
$validator = new GreaterThan(10);
$validator->isValid($value);
$message = $validator->getMessages();
$this->assertArrayHaskey('notGreaterThan', $message);
$this->assertEquals($message['notGreaterThan'], "The input is not greater than '10'");
}

$this->assertArrayHaskey('notGreaterThan', $message);
$this->assertEquals($message['notGreaterThan'], "The input is not greater than '10'");
}
public function correctInclusiveMessageDataProvider()
{
return [
'0' => [0],
'0.5' => [0.5],
'5' => [5],
'10' => [10],
];
}

public function testCorrectNotInclusiveMessageReturn()
/**
* @dataProvider correctNotInclusiveMessageDataProvider
*/
public function testCorrectNotInclusiveMessageReturn(float $input)
{
$valuesToValidate = [0, 0.5, 5, 9];
$validator = new GreaterThan(['min' => 10, 'inclusive' => true]);
$validator->isValid($input);
$message = $validator->getMessages();

foreach ($valuesToValidate as $value) {
$validator = new GreaterThan(['min' => 10, 'inclusive' => true]);
$validator->isValid($value);
$message = $validator->getMessages();
$this->assertArrayHaskey('notGreaterThanInclusive', $message);
$this->assertEquals($message['notGreaterThanInclusive'], "The input is not greater than or equal to '10'");
}

$this->assertArrayHaskey('notGreaterThanInclusive', $message);
$this->assertEquals($message['notGreaterThanInclusive'], "The input is not greater than or equal to '10'");
}
public function correctNotInclusiveMessageDataProvider()
{
return [
'0' => [0],
'0.5' => [0.5],
'5' => [5],
'9' => [9],
];
}

public function testConstructorCanAcceptInclusiveFlagAsAnArgument()
Expand Down

0 comments on commit b02b928

Please sign in to comment.