/
githubmirror
/
framework
Обзор
Документация
Войти
/
githubmirror
/
framework
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
13.x
tests/Validation/ValidationImageFileRuleTest.php
188 строк
5 KB
Lucas Michot
Remove useless parent::setUp()/tearDown() calls in tests extending PHPUnit's TestCase directly (#60994)
03 авг 2026, 20:54
Не верифицирован
03 авг 2026, 20:54
14a6983
Код
Авторство
О чём код?
<?php namespace Illuminate\Tests\Validation; use Illuminate\Container\Container; use Illuminate\Http\UploadedFile; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Facade; use Illuminate\Translation\ArrayLoader; use Illuminate\Translation\Translator; use Illuminate\Validation\Rule; use Illuminate\Validation\Rules\File; use Illuminate\Validation\ValidationServiceProvider; use Illuminate\Validation\Validator; use PHPUnit\Framework\TestCase; class ValidationImageFileRuleTest extends TestCase { public function testDimensions() { $this->fails( File::image()->dimensions(Rule::dimensions()->width(100)->height(100)), UploadedFile::fake()->image('foo.png', 101, 101), ['validation.dimensions'], ); $this->passes( File::image()->dimensions(Rule::dimensions()->width(100)->height(100)), UploadedFile::fake()->image('foo.png', 100, 100), ); } public function testDimensionsWithCustomImageSizeMethod() { $stream = tmpfile(); // To prevent PHP from deleting the temp file early. $path = stream_get_meta_data($stream)['uri']; $this->fails( File::image()->dimensions(Rule::dimensions()->width(100)->height(100)), new UploadedFileWithCustomImageSizeMethod($path, 'foo.png'), ['validation.dimensions'], ); $this->passes( File::image()->dimensions(Rule::dimensions()->width(200)->height(200)), new UploadedFileWithCustomImageSizeMethod($path, 'foo.png'), ); } public function testDimensionWithTheRatioMethod() { $this->fails( File::image()->dimensions(Rule::dimensions()->ratio(1)), UploadedFile::fake()->image('foo.png', 105, 100), ['validation.dimensions'], ); $this->passes( File::image()->dimensions(Rule::dimensions()->ratio(1)), UploadedFile::fake()->image('foo.png', 100, 100), ); } public function testDimensionWithTheMinRatioMethod() { $this->passes( File::image()->dimensions(Rule::dimensions()->minRatio(1 / 2)), UploadedFile::fake()->image('foo.png', 100, 100), ); $this->passes( File::image()->dimensions(Rule::dimensions()->minRatio(2 / 3)), UploadedFile::fake()->image('foo.png', 200, 300), ); $this->fails( File::image()->dimensions(Rule::dimensions()->minRatio(1 / 2)), UploadedFile::fake()->image('foo.png', 100, 300), ['validation.dimensions'], ); } public function testDimensionWithTheMaxRatioMethod() { $this->passes( File::image()->dimensions(Rule::dimensions()->maxRatio(1 / 2)), UploadedFile::fake()->image('foo.png', 100, 300), ['validation.dimensions'], ); $this->passes( File::image()->dimensions(Rule::dimensions()->maxRatio(1 / 3)), UploadedFile::fake()->image('foo.png', 100, 300), ); $this->fails( File::image()->dimensions(Rule::dimensions()->maxRatio(1 / 2)), UploadedFile::fake()->image('foo.png', 100, 100), ['validation.dimensions'], ); } public function testDimensionWithTheRatioBetweenMethod() { $this->fails( File::image()->dimensions(Rule::dimensions()->ratioBetween(1 / 3, 1 / 2)), UploadedFile::fake()->image('foo.png', 100, 100), ['validation.dimensions'], ); $this->passes( File::image()->dimensions(Rule::dimensions()->ratioBetween(1 / 3, 1 / 2)), UploadedFile::fake()->image('foo.png', 100, 200), ); } protected function fails($rule, $values, $messages) { $this->assertValidationRules($rule, $values, false, $messages); } protected function assertValidationRules($rule, $values, $result, $messages) { $values = Arr::wrap($values); foreach ($values as $value) { $v = new Validator( resolve('translator'), ['my_file' => $value], ['my_file' => is_object($rule) ? clone $rule : $rule] ); $this->assertSame($result, $v->passes()); $this->assertSame( $result ? [] : ['my_file' => $messages], $v->messages()->toArray() ); } } protected function passes($rule, $values) { $this->assertValidationRules($rule, $values, true, []); } protected function setUp(): void { $container = Container::getInstance(); $container->bind('translator', function () { return new Translator( new ArrayLoader, 'en' ); }); Facade::setFacadeApplication($container); (new ValidationServiceProvider($container))->register(); } protected function tearDown(): void { Container::setInstance(null); Facade::clearResolvedInstances(); Facade::setFacadeApplication(null); } } class UploadedFileWithCustomImageSizeMethod extends UploadedFile { public function isValid(): bool { return true; } public function guessExtension(): string { return 'png'; } public function dimensions() { return [200, 200]; } }