Skip to content

Commit

Permalink
Tests updated
Browse files Browse the repository at this point in the history
  • Loading branch information
filisko committed Feb 20, 2020
1 parent 8ea04da commit 04ccb21
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 40 deletions.
72 changes: 36 additions & 36 deletions gump.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public function validate(array $input, array $ruleset, $rules_delimiter='|', $pa

if (isset($input[$field])) {
if (is_array($input[$field]) && in_array('required_file', $ruleset)) {
$input_array = $input[$field];
$input_array = array($input[$field]);
} else {
$input_array = array($input[$field]);
}
Expand Down Expand Up @@ -1972,33 +1972,33 @@ protected function validate_starts($field, $input, $param = null)
}
}

/**
* Checks if a file was uploaded.
*
* Usage: '<index>' => 'required_file'
*
* @param string $field
* @param array $input
*
* @return mixed
*/
protected function validate_required_file($field, $input, $param = null)
{
if (!isset($input[$field])) {
return;
}

if (is_array($input[$field]) && $input[$field]['error'] !== 4) {
return;
}

return array(
'field' => $field,
'value' => $input[$field],
'rule' => __FUNCTION__,
'param' => $param,
);
}
/**
* Checks if a file was uploaded.
*
* Usage: '<index>' => 'required_file'
*
* @param string $field
* @param array $input
*
* @return mixed
*/
protected function validate_required_file($field, $input, $param = null)
{
if (!isset($input[$field])) {
return;
}

if (is_array($input[$field]) && $input[$field]['error'] === 0) {
return;
}

return array(
'field' => $field,
'value' => $input[$field],
'rule' => __FUNCTION__,
'param' => $param,
);
}

/**
* Check the uploaded file for extension for now
Expand All @@ -2017,7 +2017,7 @@ protected function validate_extension($field, $input, $param = null)
return;
}

if (is_array($input[$field]) && $input[$field]['error'] !== 4) {
if (is_array($input[$field]) && $input[$field]['error'] === 0) {
$param = trim(strtolower($param));
$allowed_extensions = explode(';', $param);

Expand All @@ -2027,14 +2027,14 @@ protected function validate_extension($field, $input, $param = null)
if ($extension && in_array(strtolower($extension), $allowed_extensions)) {
return;
}

return array(
'field' => $field,
'value' => $input[$field],
'rule' => __FUNCTION__,
'param' => $param,
);
}

return array(
'field' => $field,
'value' => $input[$field],
'rule' => __FUNCTION__,
'param' => $param,
);
}

/**
Expand Down
9 changes: 5 additions & 4 deletions tests/ValidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,15 @@ public function testCustomValidatorsReturnRightErrorStructure()
]], $result);
}

public function testRequired()
public function testRequiredAndRequiredFile()
{
$result = $this->gump->validate([
'test' => 'failing'
'field_without_validation_rules' => '123'
], [
'nonexistent' => 'required'
'some_field' => 'required',
'file_field' => 'required_file',
]);

$this->assertTrue(count($result) ===1);
$this->assertTrue(count($result) === 2);
}
}
57 changes: 57 additions & 0 deletions tests/Validators/ExtensionValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Tests\Validators;

use GUMP;
use Exception;
use Tests\BaseTestCase;

/**
* Class ExtensionValidatorTest
*
* @package Tests
*/
class ExtensionValidatorTest extends BaseTestCase
{
private const RULE = 'extension,png;jpg;gif';

public function testItSuccessesWhenExtensionMatches()
{
$input = [
'name' => 'screenshot.png',
'type' => 'image/png',
'tmp_name' => '/tmp/phphjatI9',
'error' => 0,
'size' => 22068
];

$this->assertTrue($this->validate(self::RULE, $input));
}

public function testItFailsWhenExtensionDoesntMatch()
{
$input = [
'name' => 'document.pdf',
'type' => 'application/pdf',
'tmp_name' => '/tmp/phphjatI9',
'error' => 0,
'size' => 22068
];

$this->assertNotTrue($this->validate(self::RULE, $input));
}

public function testItFailsWhenFileWasNotSuccessfullyUploaded()
{
$input = [
'name' => 'screenshot.png',
'type' => 'image/png',
'tmp_name' => '/tmp/phphjatI9',
'error' => 4,
'size' => 22068
];

$this->assertNotTrue($this->validate(self::RULE, $input));
}

}
52 changes: 52 additions & 0 deletions tests/Validators/RequiredFileValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Tests\Validators;

use GUMP;
use Exception;
use Tests\BaseTestCase;

/**
* Class RequiredFileValidatorTest
*
* @package Tests
*/
class RequiredFileValidatorTest extends BaseTestCase
{
private const RULE = 'required_file';

public function testItFailsWhenThereIsNoInputFile()
{
$result = $this->gump->validate([], [
'test' => self::RULE
]);

$this->assertNotTrue($result);
}

public function testWhenFileIsSuccessfullyUploadedItSuccesses()
{
$input = [
'name' => 'screenshot.png',
'type' => 'image/png',
'tmp_name' => '/tmp/phphjatI9',
'error' => 0,
'size' => 22068
];

$this->assertTrue($this->validate(self::RULE, $input));
}

public function testWhenFileIsNotSuccessfullyUploadedItFails()
{
$input = [
'name' => 'document.pdf',
'type' => 'application/pdf',
'tmp_name' => '/tmp/phphjatI9',
'error' => 4,
'size' => 22068
];

$this->assertNotTrue($this->validate(self::RULE, $input));
}
}

0 comments on commit 04ccb21

Please sign in to comment.