|
| 1 | +<?php |
| 2 | +namespace PHPForm\Unit\Fields; |
| 3 | + |
| 4 | +use PHPUnit\Framework\TestCase; |
| 5 | + |
| 6 | +use PHPForm\Exceptions\ValidationError; |
| 7 | +use PHPForm\Fields\MultipleChoiceField; |
| 8 | +use PHPForm\Widgets\SelectMultiple; |
| 9 | + |
| 10 | +class MultipleChoiceFieldTest extends TestCase |
| 11 | +{ |
| 12 | + public function setUp() |
| 13 | + { |
| 14 | + $this->choices = array( |
| 15 | + "option1" => "Option 1", |
| 16 | + "option2" => "Option 2", |
| 17 | + "option3" => "Option 3" |
| 18 | + ); |
| 19 | + |
| 20 | + $this->field = new MultipleChoiceField(["choices" => $this->choices, "required" => true]); |
| 21 | + } |
| 22 | + |
| 23 | + public function testConstruct() |
| 24 | + { |
| 25 | + $this->assertInstanceOf(SelectMultiple::class, $this->field->getWidget()); |
| 26 | + } |
| 27 | + |
| 28 | + public function testToNative() |
| 29 | + { |
| 30 | + $this->assertEquals([], $this->field->toNative(false)); |
| 31 | + $this->assertEquals(["1", "2", "3", "4"], $this->field->toNative([1, 2, 3, 4])); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @expectedException PHPForm\Exceptions\ValidationError |
| 36 | + * @expectedExceptionMessage Enter a list of values. |
| 37 | + */ |
| 38 | + public function testToNativeNonArray() |
| 39 | + { |
| 40 | + $this->field->toNative("invalid value"); |
| 41 | + } |
| 42 | + |
| 43 | + public function testValidateChoiceExistent() |
| 44 | + { |
| 45 | + $this->assertNull($this->field->validate(["option1", "option2"])); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @expectedException PHPForm\Exceptions\ValidationError |
| 50 | + * @expectedExceptionMessage This field is required. |
| 51 | + */ |
| 52 | + public function testValidateEmptyArray() |
| 53 | + { |
| 54 | + $this->field->validate([]); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @expectedException PHPForm\Exceptions\ValidationError |
| 59 | + * @expectedExceptionMessage Select a valid choice. "option5" is not one of the available choices. |
| 60 | + */ |
| 61 | + public function testValidateChoiceUnexistent() |
| 62 | + { |
| 63 | + $this->field->validate(["option1", "option5"]); |
| 64 | + } |
| 65 | +} |
0 commit comments