Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
}
},
"require-dev": {
"guzzlehttp/psr7": "^1",
"ipl/stdlib": "dev-main",
"ipl/i18n": "dev-main"
}
Expand Down
6 changes: 3 additions & 3 deletions src/ValidatorChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
$this->validators->insert($validator, $priority);

if ($breakChainOnFailure) {
$this->validatorsThatBreakTheChain->attach($validator);
$this->validatorsThatBreakTheChain->offsetSet($validator);
}

return $this;
Expand Down Expand Up @@ -123,7 +123,7 @@

unset($validator['options']);

$validator = array_merge($validator, $options);

Check failure on line 126 in src/ValidatorChain.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.2) / PHPStan 8.2

Parameter #2 ...$arrays of function array_merge expects array, mixed given.

Check failure on line 126 in src/ValidatorChain.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.5) / PHPStan 8.5

Parameter #2 ...$arrays of function array_merge expects array, mixed given.

Check failure on line 126 in src/ValidatorChain.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.4) / PHPStan 8.4

Parameter #2 ...$arrays of function array_merge expects array, mixed given.

Check failure on line 126 in src/ValidatorChain.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.3) / PHPStan 8.3

Parameter #2 ...$arrays of function array_merge expects array, mixed given.
}

if (isset($validator['break_chain_on_failure'])) {
Expand Down Expand Up @@ -227,7 +227,7 @@
* @var Validator $validator
*/
foreach ($validatorChain->validators->yieldAll() as $priority => $validator) {
$this->add($validator, $validatorsThatBreakTheChain->contains($validator), $priority);
$this->add($validator, $validatorsThatBreakTheChain->offsetExists($validator), $priority);
}

return $this;
Expand Down Expand Up @@ -282,7 +282,7 @@

$this->addMessages($validator->getMessages());

if ($this->validatorsThatBreakTheChain->contains($validator)) {
if ($this->validatorsThatBreakTheChain->offsetExists($validator)) {
break;
}
}
Expand Down
69 changes: 58 additions & 11 deletions tests/FileValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,69 @@

namespace ipl\Tests\Validator;

use GuzzleHttp\Psr7\UploadedFile;
use ipl\I18n\NoopTranslator;
use ipl\I18n\StaticTranslator;
use ipl\Validator\FileValidator;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use RuntimeException;

class FileValidatorTest extends TestCase
{
public function createUploadedFileObject($mimeType = 'application/pdf'): UploadedFile
{
return new UploadedFile(
'test/test.pdf',
500,
0,
'test.pdf',
$mimeType
);
public function createUploadedFileObject(
string $filename = 'test.pdf',
int $size = 500,
int $error = UPLOAD_ERR_OK,
string $mimeType = 'application/pdf'
): UploadedFileInterface {
return new class ($filename, $size, $error, $mimeType) implements UploadedFileInterface {
private readonly int $size;
private readonly int $error;
private readonly string $clientFilename;
private readonly string $clientMediaType;

public function __construct(
string $clientFilename,
int $size,
int $error,
string $clientMediaType
) {
$this->size = $size;
$this->error = $error;
$this->clientFilename = $clientFilename;
$this->clientMediaType = $clientMediaType;
}

public function getStream(): StreamInterface
{
throw new RuntimeException('not implemented');
}

public function moveTo(string $targetPath): void
{
throw new RuntimeException('not implemented');
}

public function getSize(): ?int
{
return $this->size;
}

public function getError(): int
{
return $this->error;
}

public function getClientFilename(): ?string
{
return $this->clientFilename;
}

public function getClientMediaType(): ?string
{
return $this->clientMediaType;
}
};
}

public function testValidValue(): void
Expand Down Expand Up @@ -89,7 +136,7 @@ public function testMimeTypeOption(): void
$this->assertTrue($validator->isValid($uploadedFile));

$validator->setAllowedMimeTypes(['image/gif', 'image/jpeg']);
$uploadedFile = $this->createUploadedFileObject('image/png');
$uploadedFile = $this->createUploadedFileObject(mimeType: 'image/png');

$this->assertFalse($validator->isValid($uploadedFile));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ValidatorChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public function testMerge()
$this->assertSame($callbackValidator, $validatorsAsArray[0]);
$this->assertInstanceOf(TestValidator::class, $validatorsAsArray[1]);
$this->assertInstanceOf(CallbackValidator::class, $validatorsAsArray[2]);
$this->assertTrue($validators->getValidatorsThatBreakTheChain()->contains($callbackValidator));
$this->assertTrue($validators->getValidatorsThatBreakTheChain()->offsetExists($callbackValidator));
}

public function testArraySpecExceptionIfNameIsMissing()
Expand Down
Loading