Skip to content

Commit 04ccb21

Browse files
committed
Tests updated
1 parent 8ea04da commit 04ccb21

File tree

4 files changed

+150
-40
lines changed

4 files changed

+150
-40
lines changed

gump.class.php

+36-36
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public function validate(array $input, array $ruleset, $rules_delimiter='|', $pa
400400

401401
if (isset($input[$field])) {
402402
if (is_array($input[$field]) && in_array('required_file', $ruleset)) {
403-
$input_array = $input[$field];
403+
$input_array = array($input[$field]);
404404
} else {
405405
$input_array = array($input[$field]);
406406
}
@@ -1972,33 +1972,33 @@ protected function validate_starts($field, $input, $param = null)
19721972
}
19731973
}
19741974

1975-
/**
1976-
* Checks if a file was uploaded.
1977-
*
1978-
* Usage: '<index>' => 'required_file'
1979-
*
1980-
* @param string $field
1981-
* @param array $input
1982-
*
1983-
* @return mixed
1984-
*/
1985-
protected function validate_required_file($field, $input, $param = null)
1986-
{
1987-
if (!isset($input[$field])) {
1988-
return;
1989-
}
1990-
1991-
if (is_array($input[$field]) && $input[$field]['error'] !== 4) {
1992-
return;
1993-
}
1994-
1995-
return array(
1996-
'field' => $field,
1997-
'value' => $input[$field],
1998-
'rule' => __FUNCTION__,
1999-
'param' => $param,
2000-
);
2001-
}
1975+
/**
1976+
* Checks if a file was uploaded.
1977+
*
1978+
* Usage: '<index>' => 'required_file'
1979+
*
1980+
* @param string $field
1981+
* @param array $input
1982+
*
1983+
* @return mixed
1984+
*/
1985+
protected function validate_required_file($field, $input, $param = null)
1986+
{
1987+
if (!isset($input[$field])) {
1988+
return;
1989+
}
1990+
1991+
if (is_array($input[$field]) && $input[$field]['error'] === 0) {
1992+
return;
1993+
}
1994+
1995+
return array(
1996+
'field' => $field,
1997+
'value' => $input[$field],
1998+
'rule' => __FUNCTION__,
1999+
'param' => $param,
2000+
);
2001+
}
20022002

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

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

@@ -2027,14 +2027,14 @@ protected function validate_extension($field, $input, $param = null)
20272027
if ($extension && in_array(strtolower($extension), $allowed_extensions)) {
20282028
return;
20292029
}
2030-
2031-
return array(
2032-
'field' => $field,
2033-
'value' => $input[$field],
2034-
'rule' => __FUNCTION__,
2035-
'param' => $param,
2036-
);
20372030
}
2031+
2032+
return array(
2033+
'field' => $field,
2034+
'value' => $input[$field],
2035+
'rule' => __FUNCTION__,
2036+
'param' => $param,
2037+
);
20382038
}
20392039

20402040
/**

tests/ValidateTest.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,15 @@ public function testCustomValidatorsReturnRightErrorStructure()
174174
]], $result);
175175
}
176176

177-
public function testRequired()
177+
public function testRequiredAndRequiredFile()
178178
{
179179
$result = $this->gump->validate([
180-
'test' => 'failing'
180+
'field_without_validation_rules' => '123'
181181
], [
182-
'nonexistent' => 'required'
182+
'some_field' => 'required',
183+
'file_field' => 'required_file',
183184
]);
184185

185-
$this->assertTrue(count($result) ===1);
186+
$this->assertTrue(count($result) === 2);
186187
}
187188
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Tests\Validators;
4+
5+
use GUMP;
6+
use Exception;
7+
use Tests\BaseTestCase;
8+
9+
/**
10+
* Class ExtensionValidatorTest
11+
*
12+
* @package Tests
13+
*/
14+
class ExtensionValidatorTest extends BaseTestCase
15+
{
16+
private const RULE = 'extension,png;jpg;gif';
17+
18+
public function testItSuccessesWhenExtensionMatches()
19+
{
20+
$input = [
21+
'name' => 'screenshot.png',
22+
'type' => 'image/png',
23+
'tmp_name' => '/tmp/phphjatI9',
24+
'error' => 0,
25+
'size' => 22068
26+
];
27+
28+
$this->assertTrue($this->validate(self::RULE, $input));
29+
}
30+
31+
public function testItFailsWhenExtensionDoesntMatch()
32+
{
33+
$input = [
34+
'name' => 'document.pdf',
35+
'type' => 'application/pdf',
36+
'tmp_name' => '/tmp/phphjatI9',
37+
'error' => 0,
38+
'size' => 22068
39+
];
40+
41+
$this->assertNotTrue($this->validate(self::RULE, $input));
42+
}
43+
44+
public function testItFailsWhenFileWasNotSuccessfullyUploaded()
45+
{
46+
$input = [
47+
'name' => 'screenshot.png',
48+
'type' => 'image/png',
49+
'tmp_name' => '/tmp/phphjatI9',
50+
'error' => 4,
51+
'size' => 22068
52+
];
53+
54+
$this->assertNotTrue($this->validate(self::RULE, $input));
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Tests\Validators;
4+
5+
use GUMP;
6+
use Exception;
7+
use Tests\BaseTestCase;
8+
9+
/**
10+
* Class RequiredFileValidatorTest
11+
*
12+
* @package Tests
13+
*/
14+
class RequiredFileValidatorTest extends BaseTestCase
15+
{
16+
private const RULE = 'required_file';
17+
18+
public function testItFailsWhenThereIsNoInputFile()
19+
{
20+
$result = $this->gump->validate([], [
21+
'test' => self::RULE
22+
]);
23+
24+
$this->assertNotTrue($result);
25+
}
26+
27+
public function testWhenFileIsSuccessfullyUploadedItSuccesses()
28+
{
29+
$input = [
30+
'name' => 'screenshot.png',
31+
'type' => 'image/png',
32+
'tmp_name' => '/tmp/phphjatI9',
33+
'error' => 0,
34+
'size' => 22068
35+
];
36+
37+
$this->assertTrue($this->validate(self::RULE, $input));
38+
}
39+
40+
public function testWhenFileIsNotSuccessfullyUploadedItFails()
41+
{
42+
$input = [
43+
'name' => 'document.pdf',
44+
'type' => 'application/pdf',
45+
'tmp_name' => '/tmp/phphjatI9',
46+
'error' => 4,
47+
'size' => 22068
48+
];
49+
50+
$this->assertNotTrue($this->validate(self::RULE, $input));
51+
}
52+
}

0 commit comments

Comments
 (0)