|
| 1 | +<?php |
| 2 | +namespace PHPForm\Unit\Fields; |
| 3 | + |
| 4 | +use PHPUnit\Framework\TestCase; |
| 5 | + |
| 6 | +use PHPForm\Exceptions\ValidationError; |
| 7 | +use PHPForm\Fields\FileField; |
| 8 | +use PHPForm\Widgets\FileInput; |
| 9 | + |
| 10 | +class FileFieldTest extends TestCase |
| 11 | +{ |
| 12 | + public function setUp() |
| 13 | + { |
| 14 | + $this->field = new FileField(["max_size" => 20]); |
| 15 | + } |
| 16 | + |
| 17 | + public function testConstruct() |
| 18 | + { |
| 19 | + $this->assertInstanceOf(FileInput::class, $this->field->getWidget()); |
| 20 | + $this->assertAttributeEquals(20, "max_size", $this->field); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @expectedException PHPForm\Exceptions\ValidationError |
| 25 | + * @expectedExceptionMessage The submitted file is empty. |
| 26 | + */ |
| 27 | + public function testValidateEmpty() |
| 28 | + { |
| 29 | + $data = array('size' => 0); |
| 30 | + $this->field->validate((object) $data); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @expectedException PHPForm\Exceptions\ValidationError |
| 35 | + * @expectedExceptionMessage This field is required. |
| 36 | + */ |
| 37 | + public function testValidateEmptyRequired() |
| 38 | + { |
| 39 | + $data = array('size' => 0); |
| 40 | + |
| 41 | + $field = new FileField(["required" => true]); |
| 42 | + $field->validate((object) $data); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + /** |
| 47 | + * @expectedException PHPForm\Exceptions\ValidationError |
| 48 | + * @expectedExceptionMessage Ensure the file has at most 20 bytes (it has 100 bytes). |
| 49 | + */ |
| 50 | + public function testValidateMaxSize() |
| 51 | + { |
| 52 | + $data = array('size' => 100); |
| 53 | + |
| 54 | + $this->field->validate((object) $data); |
| 55 | + } |
| 56 | + |
| 57 | + public function testToNative() |
| 58 | + { |
| 59 | + $data = array( |
| 60 | + 'name' => 'mine_small.jpg', |
| 61 | + 'type' => 'image/jpeg', |
| 62 | + 'tmp_name' => '/data/tmp/php/uploads/phpscF9Uz', |
| 63 | + 'size' => 13481, |
| 64 | + ); |
| 65 | + |
| 66 | + $result = $this->field->toNative($data); |
| 67 | + |
| 68 | + $this->assertEquals($data['name'], $result->name); |
| 69 | + $this->assertEquals($data['type'], $result->type); |
| 70 | + $this->assertEquals($data['tmp_name'], $result->tmp_name); |
| 71 | + $this->assertEquals($data['size'], $result->size); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @expectedException PHPForm\Exceptions\ValidationError |
| 76 | + * @expectedExceptionMessage Invalid file submitted. |
| 77 | + */ |
| 78 | + public function testToNativeInvalidValue() |
| 79 | + { |
| 80 | + $this->field->toNative("string"); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @expectedException PHPForm\Exceptions\ValidationError |
| 85 | + */ |
| 86 | + public function testFileTypeValidatorCalled() |
| 87 | + { |
| 88 | + $data = array('size' => 10, 'type' => 'image/jpeg',); |
| 89 | + |
| 90 | + $field = new FileField(["valid_filetypes" => ['image/png']]); |
| 91 | + $field->clean($data); |
| 92 | + } |
| 93 | +} |
0 commit comments