Skip to content

Commit 4c941f1

Browse files
committed
Implementa validación de archivos (4 casos) y completa cobertura de tests (BetweenRule, LengthRule, UppercaseRule)
1 parent 02f0ee0 commit 4c941f1

4 files changed

Lines changed: 312 additions & 45 deletions

File tree

src/Rule/Validator/File/AbstractFileRule.php

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,73 @@
1414

1515
use Derafu\DataProcessor\Contract\ValidatorRuleInterface;
1616
use Derafu\DataProcessor\Exception\ValidationException;
17-
18-
// use Psr\Http\Message\UploadedFileInterface;
17+
use Psr\Http\Message\UploadedFileInterface;
1918

2019
abstract class AbstractFileRule implements ValidatorRuleInterface
2120
{
2221
protected function validateFileArray(mixed $value): void
2322
{
24-
// // Check if PSR-7 UploadedFileInterface is available.
25-
// if (class_exists(UploadedFileInterface::class)) {
26-
// // PSR-7 UploadedFileInterface (single file).
27-
// if ($value instanceof UploadedFileInterface) {
28-
// if ($value->getError() !== UPLOAD_ERR_OK) {
29-
// throw new ValidationException('Uploaded file has error.');
30-
// }
31-
// return;
32-
// }
23+
// Case 1 & 2: PSR-7 UploadedFileInterface (if available).
24+
if (interface_exists(UploadedFileInterface::class)) {
25+
// Single PSR-7 file.
26+
if ($value instanceof UploadedFileInterface) {
27+
if ($value->getError() !== UPLOAD_ERR_OK) {
28+
throw new ValidationException('Uploaded file has error.');
29+
}
30+
return;
31+
}
32+
33+
// Indexed array of PSR-7 files.
34+
if (is_array($value) && isset($value[0]) && $value[0] instanceof UploadedFileInterface) {
35+
foreach ($value as $file) {
36+
$this->validateFileArray($file);
37+
}
38+
return;
39+
}
40+
}
41+
42+
if (!is_array($value)) {
43+
throw new ValidationException('Invalid file format.');
44+
}
3345

34-
// // Array of PSR-7 UploadedFileInterface.
35-
// if (is_array($value) && isset($value[0]) && $value[0] instanceof UploadedFileInterface) {
36-
// foreach ($value as $file) {
37-
// $this->validateFileArray($file); // Recursive.
38-
// }
39-
// return;
40-
// }
41-
// }
46+
// Case 3: $_FILES simple (single file, tmp_name is a string).
47+
if (isset($value['tmp_name']) && is_string($value['tmp_name'])) {
48+
if (!isset($value['error']) || $value['error'] !== UPLOAD_ERR_OK) {
49+
throw new ValidationException('File has upload error.');
50+
}
51+
if (!file_exists($value['tmp_name'])) {
52+
throw new ValidationException('File does not exist.');
53+
}
54+
return;
55+
}
4256

43-
// // $_FILES simple.
44-
// if (is_array($value) && isset($value['tmp_name'])) {
45-
// if (!is_string($value['tmp_name']) || !file_exists($value['tmp_name'])) {
46-
// throw new ValidationException('File does not exist.');
47-
// }
48-
// if (!isset($value['error']) || $value['error'] !== UPLOAD_ERR_OK) {
49-
// throw new ValidationException('File has upload error.');
50-
// }
51-
// return;
52-
// }
57+
// Case 4a: $_FILES native multiple (tmp_name is an array of paths).
58+
if (isset($value['tmp_name']) && is_array($value['tmp_name'])) {
59+
foreach ($value['tmp_name'] as $index => $tmpName) {
60+
$error = $value['error'][$index] ?? UPLOAD_ERR_NO_FILE;
61+
if ($error !== UPLOAD_ERR_OK) {
62+
throw new ValidationException(
63+
sprintf('File at index %d has upload error.', $index)
64+
);
65+
}
66+
if (!file_exists($tmpName)) {
67+
throw new ValidationException(
68+
sprintf('File at index %d does not exist.', $index)
69+
);
70+
}
71+
}
72+
return;
73+
}
5374

54-
// // $_FILES multiple.
55-
// if (is_array($value)) {
56-
// foreach ($value as $v) {
57-
// $this->validateFileArray($v); // Recursive.
58-
// }
59-
// return;
60-
// }
75+
// Case 4b: Normalized multiple (indexed array of single $_FILES-like arrays).
76+
if (isset($value[0]) && is_array($value[0])) {
77+
foreach ($value as $v) {
78+
$this->validateFileArray($v);
79+
}
80+
return;
81+
}
6182

62-
// // Any other case.
63-
// throw new ValidationException('Invalid file format.');
83+
throw new ValidationException('Invalid file format.');
6484
}
6585

6686
protected function parseSize(string $size): int

tests/src/Rule/Transformer/UppercaseRule.php renamed to tests/src/Rule/Transformer/UppercaseRuleTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Derafu\TestsDataProcessor\Rule\Transformer;
1414

15+
use Derafu\DataProcessor\Exception\TransformationException;
1516
use Derafu\DataProcessor\Rule\Transformer\String\UppercaseRule;
1617
use PHPUnit\Framework\Attributes\CoversClass;
1718
use PHPUnit\Framework\Attributes\DataProvider;
@@ -36,9 +37,8 @@ public function testUppercase(string $input, string $expected): void
3637

3738
public function testNonStringValue(): void
3839
{
39-
$value = 123;
40-
$result = $this->rule->transform($value);
41-
$this->assertSame($value, $result);
40+
$this->expectException(TransformationException::class);
41+
$this->rule->transform(123);
4242
}
4343

4444
public static function uppercaseDataProvider(): array

tests/src/Rule/Validator/DateValidationTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Derafu\DataProcessor\Rule\Validator\Date\AfterRule;
2323
use Derafu\DataProcessor\Rule\Validator\Date\BeforeOrEqualRule;
2424
use Derafu\DataProcessor\Rule\Validator\Date\BeforeRule;
25+
use Derafu\DataProcessor\Rule\Validator\Date\BetweenRule;
2526
use Derafu\DataProcessor\Rule\Validator\Date\DateEqualsRule;
2627
use Derafu\DataProcessor\Rule\Validator\Date\DateFormatRule;
2728
use Derafu\DataProcessor\Rule\Validator\Date\WeekdayRule;
@@ -42,6 +43,7 @@
4243
#[CoversClass(DateFormatRule::class)]
4344
#[CoversClass(AfterOrEqualRule::class)]
4445
#[CoversClass(BeforeOrEqualRule::class)]
46+
#[CoversClass(BetweenRule::class)]
4547
#[CoversClass(DateEqualsRule::class)]
4648
#[CoversClass(AfterRule::class)]
4749
#[CoversClass(BeforeRule::class)]
@@ -303,4 +305,61 @@ public static function weekendWeekdayValidationProvider(): array
303305
],
304306
];
305307
}
308+
309+
#[DataProvider('betweenValidationProvider')]
310+
public function testBetweenValidation(string $input, array $rules, bool $shouldPass): void
311+
{
312+
if (!$shouldPass) {
313+
$this->expectException(ValidationException::class);
314+
}
315+
316+
$result = $this->processor->process($input, [
317+
'validate' => $rules,
318+
]);
319+
320+
if ($shouldPass) {
321+
$this->assertSame($input, $result);
322+
}
323+
}
324+
325+
public static function betweenValidationProvider(): array
326+
{
327+
return [
328+
'valid_between_middle' => [
329+
'2024-02-15',
330+
['between:2024-02-01,2024-02-29'],
331+
true,
332+
],
333+
'valid_between_lower_bound' => [
334+
'2024-02-01',
335+
['between:2024-02-01,2024-02-29'],
336+
true,
337+
],
338+
'valid_between_upper_bound' => [
339+
'2024-02-29',
340+
['between:2024-02-01,2024-02-29'],
341+
true,
342+
],
343+
'invalid_between_before_range' => [
344+
'2024-01-31',
345+
['between:2024-02-01,2024-02-29'],
346+
false,
347+
],
348+
'invalid_between_after_range' => [
349+
'2024-03-01',
350+
['between:2024-02-01,2024-02-29'],
351+
false,
352+
],
353+
'invalid_between_missing_params' => [
354+
'2024-02-15',
355+
['between:2024-02-01'],
356+
false,
357+
],
358+
'invalid_between_bad_date_value' => [
359+
'not-a-date',
360+
['between:2024-02-01,2024-02-29'],
361+
false,
362+
],
363+
];
364+
}
306365
}

0 commit comments

Comments
 (0)