|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hyde\Framework\Testing\Feature; |
| 6 | + |
| 7 | +use Hyde\Framework\Features\Publications\Models\PublicationField; |
| 8 | +use Hyde\Testing\TestCase; |
| 9 | +use InvalidArgumentException; |
| 10 | + |
| 11 | +/** |
| 12 | + * @covers \Hyde\Framework\Features\Publications\Models\PublicationField |
| 13 | + */ |
| 14 | +class PublicationFieldTest extends TestCase |
| 15 | +{ |
| 16 | + public function test_can_instantiate_class() |
| 17 | + { |
| 18 | + $field = $this->makeField(); |
| 19 | + $this->assertInstanceOf(PublicationField::class, $field); |
| 20 | + |
| 21 | + $this->assertSame('string', $field->type); |
| 22 | + $this->assertSame('test', $field->name); |
| 23 | + $this->assertSame(1, $field->min); |
| 24 | + $this->assertSame(10, $field->max); |
| 25 | + } |
| 26 | + |
| 27 | + public function test_can_get_field_as_array() |
| 28 | + { |
| 29 | + $this->assertSame([ |
| 30 | + 'type' => 'string', |
| 31 | + 'name' => 'test', |
| 32 | + 'min' => 1, |
| 33 | + 'max' => 10, |
| 34 | + ], $this->makeField()->toArray()); |
| 35 | + } |
| 36 | + |
| 37 | + public function test_can_encode_field_as_json() |
| 38 | + { |
| 39 | + $this->assertSame('{"type":"string","name":"test","min":1,"max":10}', json_encode($this->makeField())); |
| 40 | + } |
| 41 | + |
| 42 | + public function test_range_values_can_be_null() |
| 43 | + { |
| 44 | + $field = new PublicationField('string', 'test', null, null); |
| 45 | + $this->assertNull($field->min); |
| 46 | + $this->assertNull($field->max); |
| 47 | + } |
| 48 | + |
| 49 | + public function test_max_value_cannot_be_less_than_min_value() |
| 50 | + { |
| 51 | + $this->expectException(InvalidArgumentException::class); |
| 52 | + $this->expectExceptionMessage("The 'max' value cannot be less than the 'min' value."); |
| 53 | + |
| 54 | + new PublicationField('string', 'test', 10, 1); |
| 55 | + } |
| 56 | + |
| 57 | + public function test_types_constant() |
| 58 | + { |
| 59 | + $this->assertSame([ |
| 60 | + 'string', |
| 61 | + 'boolean', |
| 62 | + 'integer', |
| 63 | + 'float', |
| 64 | + 'datetime', |
| 65 | + 'url', |
| 66 | + 'array', |
| 67 | + 'text', |
| 68 | + 'image', |
| 69 | + ], PublicationField::TYPES); |
| 70 | + } |
| 71 | + |
| 72 | + public function test_type_must_be_valid() |
| 73 | + { |
| 74 | + $this->expectException(InvalidArgumentException::class); |
| 75 | + $this->expectExceptionMessage("The type 'invalid' is not a valid type. Valid types are: string, boolean, integer, float, datetime, url, array, text, image."); |
| 76 | + |
| 77 | + new PublicationField('invalid', 'test', 1, 10); |
| 78 | + } |
| 79 | + |
| 80 | + public function test_type_input_is_case_insensitive() |
| 81 | + { |
| 82 | + $field = new PublicationField('STRING', 'test', 1, 10); |
| 83 | + $this->assertSame('string', $field->type); |
| 84 | + } |
| 85 | + |
| 86 | + public function test_validate_input_against_rules() |
| 87 | + { |
| 88 | + $this->markTestIncomplete('TODO: Implement this method.'); |
| 89 | + } |
| 90 | + |
| 91 | + protected function makeField(): PublicationField |
| 92 | + { |
| 93 | + return new PublicationField('string', 'test', 1, 10); |
| 94 | + } |
| 95 | +} |
0 commit comments