diff --git a/composer.json b/composer.json index 260b7d9..97048ae 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,6 @@ } }, "require-dev": { - "guzzlehttp/psr7": "^1", "ipl/stdlib": "dev-main", "ipl/i18n": "dev-main" } diff --git a/src/ValidatorChain.php b/src/ValidatorChain.php index 1044498..3917d2b 100644 --- a/src/ValidatorChain.php +++ b/src/ValidatorChain.php @@ -68,7 +68,7 @@ public function add(Validator $validator, $breakChainOnFailure = false, $priorit $this->validators->insert($validator, $priority); if ($breakChainOnFailure) { - $this->validatorsThatBreakTheChain->attach($validator); + $this->validatorsThatBreakTheChain->offsetSet($validator); } return $this; @@ -227,7 +227,7 @@ public function merge(ValidatorChain $validatorChain) * @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; @@ -282,7 +282,7 @@ public function isValid($value): bool $this->addMessages($validator->getMessages()); - if ($this->validatorsThatBreakTheChain->contains($validator)) { + if ($this->validatorsThatBreakTheChain->offsetExists($validator)) { break; } } diff --git a/tests/FileValidatorTest.php b/tests/FileValidatorTest.php index 92d0f9c..bb3e5ca 100644 --- a/tests/FileValidatorTest.php +++ b/tests/FileValidatorTest.php @@ -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 @@ -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)); } diff --git a/tests/ValidatorChainTest.php b/tests/ValidatorChainTest.php index 59224ac..f2544e2 100644 --- a/tests/ValidatorChainTest.php +++ b/tests/ValidatorChainTest.php @@ -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()