From d4487bab58c72021f5a3d8998c8a5dd4a5ff479f Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 21 Dec 2022 16:46:54 +0000 Subject: [PATCH 01/48] Apply fixes from StyleCI --- .../Features/Publications/Models/PublicationField.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index ddc28872ca7..93e0e88e743 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -49,7 +49,8 @@ public function toArray(): array } /** - * @param \Hyde\Framework\Features\Publications\Models\PublicationType|null $publicationType Required when using the "image" type. + * @param \Hyde\Framework\Features\Publications\Models\PublicationType|null $publicationType Required when using the "image" type. + * * @see \Hyde\Framework\Testing\Unit\PublicationFieldTypeValidationRulesTest * @see https://laravel.com/docs/9.x/validation#available-validation-rules */ From 1e347da7effdeea90161e1aa86aa763d871276f3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 18:20:14 +0100 Subject: [PATCH 02/48] Add $rules array to the publication field class --- .../Framework/Features/Publications/Models/PublicationField.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 93e0e88e743..e342c283aa8 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -26,6 +26,7 @@ class PublicationField implements SerializableContract public readonly PublicationFieldTypes $type; public readonly string $name; public readonly ?string $tagGroup; + public readonly array $rules; public static function fromArray(array $array): static { From 113b297ffcc5c15ece9b41f9a0464f5f0e6f0ff5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:29:28 +0100 Subject: [PATCH 03/48] Add the rules array to the constructor parameters --- .../Features/Publications/Models/PublicationField.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index cfbb19dafd4..4f7e0a195c0 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -34,11 +34,12 @@ public static function fromArray(array $array): static return new static(...$array); } - public function __construct(PublicationFieldTypes|string $type, string $name, ?string $tagGroup = null) + public function __construct(PublicationFieldTypes|string $type, string $name, ?string $tagGroup = null, array $rules = []) { $this->type = $type instanceof PublicationFieldTypes ? $type : PublicationFieldTypes::from(strtolower($type)); $this->name = Str::kebab($name); $this->tagGroup = $tagGroup; + $this->rules = $rules; } public function toArray(): array From d57bf42294b45f826f32fc85865bd673927e6585 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:30:58 +0100 Subject: [PATCH 04/48] Add validation rules to the array representation --- .../Features/Publications/Models/PublicationField.php | 1 + .../framework/tests/Feature/PublicationFieldTest.php | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 4f7e0a195c0..b3c1a08bd6f 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -48,6 +48,7 @@ public function toArray(): array 'type' => $this->type->value, 'name' => $this->name, 'tagGroup' => $this->tagGroup, + 'rules' => $this->rules, ]); } diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php index 09fd93be487..b6056707497 100644 --- a/packages/framework/tests/Feature/PublicationFieldTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -44,13 +44,14 @@ public function test_can_get_field_as_array() ], (new PublicationField('string', 'test'))->toArray()); } - public function test_can_get_field_with_tag_group_as_array() + public function test_can_get_field_with_optional_properties_as_array() { $this->assertSame([ 'type' => 'string', 'name' => 'test', 'tagGroup' => 'foo', - ], (new PublicationField('string', 'test', 'foo'))->toArray()); + 'rules' => ['required'], + ], (new PublicationField('string', 'test', 'foo', ['required']))->toArray()); } public function test_can_encode_field_as_json() @@ -58,9 +59,9 @@ public function test_can_encode_field_as_json() $this->assertSame('{"type":"string","name":"test"}', json_encode(new PublicationField('string', 'test'))); } - public function test_can_encode_field_with_tag_group_as_json() + public function test_can_get_field_with_optional_properties_as_json() { - $this->assertSame('{"type":"string","name":"test","tagGroup":"foo"}', json_encode(new PublicationField('string', 'test', 'foo'))); + $this->assertSame('{"type":"string","name":"test","tagGroup":"foo","rules":["required"]}', json_encode(new PublicationField('string', 'test', 'foo', ['required']))); } public function test_can_construct_type_using_enum_case() From fb968f61877622e9b43a2dec7cacc547cad4b408 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:31:48 +0100 Subject: [PATCH 05/48] Update test link annotation --- .../Framework/Features/Publications/Models/PublicationField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index b3c1a08bd6f..8d60fcf2eaf 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -55,7 +55,7 @@ public function toArray(): array /** * @param \Hyde\Framework\Features\Publications\Models\PublicationType|null $publicationType Required only when using the 'image' type. * - * @see \Hyde\Framework\Testing\Unit\PublicationFieldTypeValidationRulesTest + * @see \Hyde\Framework\Testing\Unit\PublicationFieldValidationRulesTest * @see https://laravel.com/docs/9.x/validation#available-validation-rules */ public function getValidationRules(?PublicationType $publicationType = null): Collection From bc339201670348ae4ff82e63d94b18fc7dfc24a0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:33:09 +0100 Subject: [PATCH 06/48] Rename local variable --- .../Features/Publications/Models/PublicationField.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 8d60fcf2eaf..eaae619d2c9 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -60,8 +60,8 @@ public function toArray(): array */ public function getValidationRules(?PublicationType $publicationType = null): Collection { - $defaultRules = Collection::create(PublicationFieldTypes::values()); - $fieldRules = Collection::create($defaultRules->get($this->type->value)); + $types = Collection::create(PublicationFieldTypes::values()); + $fieldRules = Collection::create($types->get($this->type->value)); switch ($this->type->value) { case 'array': From 1d93c2aef4070238e9e5320a34b629a6d7349f10 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 21 Dec 2022 19:35:38 +0000 Subject: [PATCH 07/48] Apply fixes from StyleCI --- packages/framework/tests/Feature/PublicationFieldTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php index b6056707497..40118939943 100644 --- a/packages/framework/tests/Feature/PublicationFieldTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -59,7 +59,7 @@ public function test_can_encode_field_as_json() $this->assertSame('{"type":"string","name":"test"}', json_encode(new PublicationField('string', 'test'))); } - public function test_can_get_field_with_optional_properties_as_json() + public function test_can_get_field_with_optional_properties_as_json() { $this->assertSame('{"type":"string","name":"test","tagGroup":"foo","rules":["required"]}', json_encode(new PublicationField('string', 'test', 'foo', ['required']))); } From 04972610ae4efb1b28bb17ba000b2705b31a0cd6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:36:06 +0100 Subject: [PATCH 08/48] Actually get the default field rules --- .../Features/Publications/Models/PublicationField.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index eaae619d2c9..e8b787086f4 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -60,8 +60,7 @@ public function toArray(): array */ public function getValidationRules(?PublicationType $publicationType = null): Collection { - $types = Collection::create(PublicationFieldTypes::values()); - $fieldRules = Collection::create($types->get($this->type->value)); + $fieldRules = Collection::create(PublicationFieldTypes::getRules($this->type)); switch ($this->type->value) { case 'array': From acfe602e661ad2e9cbf50f6c87387948cf5e6ffa Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:36:46 +0100 Subject: [PATCH 09/48] Group together related tests --- .../Feature/PublicationFieldTypesEnumTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index fe5b248de49..7795772ec7a 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -38,6 +38,13 @@ public function testCanGetRulesForEnum() ], PublicationFieldTypes::String->rules()); } + public function testCanGetRulesForEnumWithNoRules() + { + $this->expectException(BadMethodCallException::class); + $this->expectExceptionMessage('This type has no validation rules'); + PublicationFieldTypes::Tag->rules(); + } + public function testCollectCreatesCollectionOfCases() { $this->assertEquals(collect(PublicationFieldTypes::cases()), PublicationFieldTypes::collect()); @@ -58,11 +65,4 @@ public function testValuesReturnsArrayOfCaseValues() 9 => 'tag', ], PublicationFieldTypes::values()); } - - public function testCanGetRulesForEnumWithNoRules() - { - $this->expectException(BadMethodCallException::class); - $this->expectExceptionMessage('This type has no validation rules'); - PublicationFieldTypes::Tag->rules(); - } } From 10f31cb3a7b35f168cc6d0e5fb2cea984c680742 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:37:43 +0100 Subject: [PATCH 10/48] Return empty array instead of throwing exception for default validation rules --- .../Features/Publications/PublicationFieldTypes.php | 6 +++--- .../tests/Feature/PublicationFieldTypesEnumTest.php | 4 +--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index 605d43e9ed6..3767b6bb0eb 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -52,9 +52,9 @@ public static function getRules(self $type): array self::Datetime => ['required', 'datetime', 'between'], self::Url => ['required', 'url'], self::Text => ['required', 'string', 'between'], - self::Array => throw new BadMethodCallException('This type has no validation rules'), - self::Image => throw new BadMethodCallException('This type has no validation rules'), - self::Tag => throw new BadMethodCallException('This type has no validation rules'), + self::Array => [], + self::Image => [], + self::Tag => [], }; } } diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index 7795772ec7a..3bfc43b6bbc 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -40,9 +40,7 @@ public function testCanGetRulesForEnum() public function testCanGetRulesForEnumWithNoRules() { - $this->expectException(BadMethodCallException::class); - $this->expectExceptionMessage('This type has no validation rules'); - PublicationFieldTypes::Tag->rules(); + $this->assertSame([], PublicationFieldTypes::Tag->rules()); } public function testCollectCreatesCollectionOfCases() From f32cddaa9e1ef2edf9a149119bac72a80c13c9da Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:38:43 +0100 Subject: [PATCH 11/48] Remove 'required' from default types validation rules --- .../Publications/PublicationFieldTypes.php | 14 +++++++------- .../Feature/PublicationFieldTypesEnumTest.php | 1 - 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index 3767b6bb0eb..5c04f658542 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -45,13 +45,13 @@ public static function getRules(self $type): array { /** @noinspection PhpDuplicateMatchArmBodyInspection */ return match ($type) { - self::String => ['required', 'string', 'between'], - self::Boolean => ['required', 'boolean'], - self::Integer => ['required', 'integer', 'between'], - self::Float => ['required', 'numeric', 'between'], - self::Datetime => ['required', 'datetime', 'between'], - self::Url => ['required', 'url'], - self::Text => ['required', 'string', 'between'], + self::String => ['string', 'between'], + self::Boolean => ['boolean'], + self::Integer => ['integer', 'between'], + self::Float => ['numeric', 'between'], + self::Datetime => ['datetime', 'between'], + self::Url => ['url'], + self::Text => ['string', 'between'], self::Array => [], self::Image => [], self::Tag => [], diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index 3bfc43b6bbc..06efdb6461e 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -32,7 +32,6 @@ public function testCases() public function testCanGetRulesForEnum() { $this->assertSame([ - 'required', 'string', 'between', ], PublicationFieldTypes::String->rules()); From 9cf859e9d94350e7e914fbf4b887de87f3c0bf8a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:39:18 +0100 Subject: [PATCH 12/48] Add todo --- .../Framework/Features/Publications/Models/PublicationField.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index e8b787086f4..881b072defc 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -62,6 +62,8 @@ public function getValidationRules(?PublicationType $publicationType = null): Co { $fieldRules = Collection::create(PublicationFieldTypes::getRules($this->type)); + // Here we could check for a "strict" mode type of thing and add 'required' to the rules if we wanted to. + switch ($this->type->value) { case 'array': $fieldRules->add('array'); From 5261fe581a544c58122bef50e0e65b0027955a70 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:39:27 +0100 Subject: [PATCH 13/48] Join comma-separated values into a single line --- .../tests/Feature/PublicationFieldTypesEnumTest.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index 06efdb6461e..56e8bb67a41 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -31,10 +31,7 @@ public function testCases() public function testCanGetRulesForEnum() { - $this->assertSame([ - 'string', - 'between', - ], PublicationFieldTypes::String->rules()); + $this->assertSame(['string', 'between'], PublicationFieldTypes::String->rules()); } public function testCanGetRulesForEnumWithNoRules() From d7781653e81e1fd3526111f3e7d17350e1326354 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:40:24 +0100 Subject: [PATCH 14/48] Remove 'between' from default types validation rules --- .../Features/Publications/PublicationFieldTypes.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index 5c04f658542..3482b07c0d3 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -45,13 +45,13 @@ public static function getRules(self $type): array { /** @noinspection PhpDuplicateMatchArmBodyInspection */ return match ($type) { - self::String => ['string', 'between'], + self::String => ['string'], self::Boolean => ['boolean'], - self::Integer => ['integer', 'between'], - self::Float => ['numeric', 'between'], - self::Datetime => ['datetime', 'between'], + self::Integer => ['integer'], + self::Float => ['numeric'], + self::Datetime => ['datetime'], self::Url => ['url'], - self::Text => ['string', 'between'], + self::Text => ['string'], self::Array => [], self::Image => [], self::Tag => [], From 2e48fe26bbc6d6efddd5ff8c6b298c201e9dcaed Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:41:37 +0100 Subject: [PATCH 15/48] Change default validation rule to date as datetime is not a valid rule --- .../Framework/Features/Publications/PublicationFieldTypes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index 3482b07c0d3..a0fc1b4f8db 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -49,7 +49,7 @@ public static function getRules(self $type): array self::Boolean => ['boolean'], self::Integer => ['integer'], self::Float => ['numeric'], - self::Datetime => ['datetime'], + self::Datetime => ['date'], self::Url => ['url'], self::Text => ['string'], self::Array => [], From a90a0104a6878ed88f49f3d6aa2fc0671e0fc841 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:45:25 +0100 Subject: [PATCH 16/48] Expect rules to contain the default rules --- .../tests/Unit/PublicationFieldValidationRulesTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index 20425a0217a..2cca0a22a1a 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -59,19 +59,19 @@ public function testGetRulesForFloat() public function testGetRulesForInteger() { $rules = (new PublicationField('integer', 'myInteger'))->getValidationRules(); - $this->assertSame([], $rules->toArray()); + $this->assertSame(['integer'], $rules->toArray()); } public function testGetRulesForString() { $rules = (new PublicationField('string', 'myString'))->getValidationRules(); - $this->assertSame([], $rules->toArray()); + $this->assertSame(['string'], $rules->toArray()); } public function testGetRulesForText() { $rules = (new PublicationField('text', 'myText'))->getValidationRules(); - $this->assertSame([], $rules->toArray()); + $this->assertSame(['string'], $rules->toArray()); } public function testGetRulesForImage() From 55f105d9a6342a1f3fb749c17bf01f8bf6a05737 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:45:40 +0100 Subject: [PATCH 17/48] Remove duplicated rules added by the default rules --- .../Features/Publications/Models/PublicationField.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 881b072defc..2d8408a50f6 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -69,11 +69,8 @@ public function getValidationRules(?PublicationType $publicationType = null): Co $fieldRules->add('array'); break; case 'datetime': - $fieldRules->add('date'); - break; case 'float': - $fieldRules->add('numeric'); break; case 'integer': case 'string': @@ -90,7 +87,6 @@ public function getValidationRules(?PublicationType $publicationType = null): Co $fieldRules->add("in:$valueList"); break; case 'url': - $fieldRules->add('url'); break; } From de8590475ccf734f19a09a4f91a8ee309891ef7a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:46:54 +0100 Subject: [PATCH 18/48] Merge duplicate switch branches --- .../Publications/Models/PublicationField.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 2d8408a50f6..6d9e482b043 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -68,14 +68,6 @@ public function getValidationRules(?PublicationType $publicationType = null): Co case 'array': $fieldRules->add('array'); break; - case 'datetime': - break; - case 'float': - break; - case 'integer': - case 'string': - case 'text': - break; case 'image': $mediaFiles = PublicationService::getMediaForPubType($publicationType); $valueList = $mediaFiles->implode(','); @@ -86,7 +78,12 @@ public function getValidationRules(?PublicationType $publicationType = null): Co $valueList = $tagValues->implode(','); $fieldRules->add("in:$valueList"); break; + case 'float': + case 'text': + case 'datetime': + case 'integer': case 'url': + case 'string': break; } From b34593ebeb37382882f618551a31d91df5643067 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:47:29 +0100 Subject: [PATCH 19/48] Shift default value location to enum to only use dynamic rules in switch --- .../Features/Publications/Models/PublicationField.php | 4 +--- .../Framework/Features/Publications/PublicationFieldTypes.php | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 6d9e482b043..2089fa04fca 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -65,9 +65,6 @@ public function getValidationRules(?PublicationType $publicationType = null): Co // Here we could check for a "strict" mode type of thing and add 'required' to the rules if we wanted to. switch ($this->type->value) { - case 'array': - $fieldRules->add('array'); - break; case 'image': $mediaFiles = PublicationService::getMediaForPubType($publicationType); $valueList = $mediaFiles->implode(','); @@ -78,6 +75,7 @@ public function getValidationRules(?PublicationType $publicationType = null): Co $valueList = $tagValues->implode(','); $fieldRules->add("in:$valueList"); break; + case 'array': case 'float': case 'text': case 'datetime': diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index a0fc1b4f8db..831054280f0 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -52,7 +52,7 @@ public static function getRules(self $type): array self::Datetime => ['date'], self::Url => ['url'], self::Text => ['string'], - self::Array => [], + self::Array => ['array'], self::Image => [], self::Tag => [], }; From 16514e5ffcc65532b97821513dd9b1d471014dc6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:47:42 +0100 Subject: [PATCH 20/48] Add code comment --- .../Framework/Features/Publications/Models/PublicationField.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 2089fa04fca..6c0f83c61b6 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -64,6 +64,7 @@ public function getValidationRules(?PublicationType $publicationType = null): Co // Here we could check for a "strict" mode type of thing and add 'required' to the rules if we wanted to. + // Apply any dynamic rules. switch ($this->type->value) { case 'image': $mediaFiles = PublicationService::getMediaForPubType($publicationType); From 1882f4bf726382c01f45298ad83b0e800bdf6c9c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:48:14 +0100 Subject: [PATCH 21/48] Remove unnecessary switch cases --- .../Features/Publications/Models/PublicationField.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 6c0f83c61b6..2fbe19900c9 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -76,14 +76,6 @@ public function getValidationRules(?PublicationType $publicationType = null): Co $valueList = $tagValues->implode(','); $fieldRules->add("in:$valueList"); break; - case 'array': - case 'float': - case 'text': - case 'datetime': - case 'integer': - case 'url': - case 'string': - break; } return $fieldRules; From 9fe9e1aae8da3e7d12c266170912092b4ae3a894 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 21 Dec 2022 19:48:28 +0000 Subject: [PATCH 22/48] Apply fixes from StyleCI --- .../Framework/Features/Publications/PublicationFieldTypes.php | 1 - .../framework/tests/Feature/PublicationFieldTypesEnumTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index 831054280f0..50bf84f8e56 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -4,7 +4,6 @@ namespace Hyde\Framework\Features\Publications; -use BadMethodCallException; use Illuminate\Support\Collection; /** diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index 56e8bb67a41..edb320310fd 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -4,7 +4,6 @@ namespace Hyde\Framework\Testing\Feature; -use BadMethodCallException; use Hyde\Framework\Features\Publications\PublicationFieldTypes; use Hyde\Testing\TestCase; From 49c64516c101e5783b7429570788528226fd33e2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:49:09 +0100 Subject: [PATCH 23/48] Add 'numeric' rule to integer type See https://laravel.com/docs/9.x/validation#rule-integer --- .../Framework/Features/Publications/PublicationFieldTypes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index 831054280f0..d892b44c708 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -47,7 +47,7 @@ public static function getRules(self $type): array return match ($type) { self::String => ['string'], self::Boolean => ['boolean'], - self::Integer => ['integer'], + self::Integer => ['integer', 'numeric'], self::Float => ['numeric'], self::Datetime => ['date'], self::Url => ['url'], From b58d657572b9bdd5bc93a0e8d4318575a16f2b37 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:49:38 +0100 Subject: [PATCH 24/48] Expect integer to be numeric --- .../tests/Unit/PublicationFieldValidationRulesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index 2cca0a22a1a..f8825c5013a 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -59,7 +59,7 @@ public function testGetRulesForFloat() public function testGetRulesForInteger() { $rules = (new PublicationField('integer', 'myInteger'))->getValidationRules(); - $this->assertSame(['integer'], $rules->toArray()); + $this->assertSame(['integer', 'numeric'], $rules->toArray()); } public function testGetRulesForString() From 702d161252a07b18cc5bc90aa55d5eff5efeabf0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:50:19 +0100 Subject: [PATCH 25/48] Update PublicationFieldTypesEnumTest.php --- .../framework/tests/Feature/PublicationFieldTypesEnumTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index edb320310fd..94524ca6aa2 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -30,7 +30,7 @@ public function testCases() public function testCanGetRulesForEnum() { - $this->assertSame(['string', 'between'], PublicationFieldTypes::String->rules()); + $this->assertSame(['string'], PublicationFieldTypes::String->rules()); } public function testCanGetRulesForEnumWithNoRules() From bb7e309fce549e546601be4bc6c9487b7fb6fb4f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:52:17 +0100 Subject: [PATCH 26/48] Test all default rules to guard against unintentional BC breaks --- .../Feature/PublicationFieldTypesEnumTest.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index 94524ca6aa2..2c21d478c8f 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -28,13 +28,17 @@ public function testCases() $this->assertSame('tag', PublicationFieldTypes::Tag->value); } - public function testCanGetRulesForEnum() + public function testRules() { $this->assertSame(['string'], PublicationFieldTypes::String->rules()); - } - - public function testCanGetRulesForEnumWithNoRules() - { + $this->assertSame(['boolean'], PublicationFieldTypes::Boolean->rules()); + $this->assertSame(['integer', 'numeric'], PublicationFieldTypes::Integer->rules()); + $this->assertSame(['numeric'], PublicationFieldTypes::Float->rules()); + $this->assertSame(['date'], PublicationFieldTypes::Datetime->rules()); + $this->assertSame(['url'], PublicationFieldTypes::Url->rules()); + $this->assertSame(['string'], PublicationFieldTypes::Text->rules()); + $this->assertSame(['array'], PublicationFieldTypes::Array->rules()); + $this->assertSame([], PublicationFieldTypes::Image->rules()); $this->assertSame([], PublicationFieldTypes::Tag->rules()); } From 82063e418611b9c87c4fc0205276f758d9591034 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:52:28 +0100 Subject: [PATCH 27/48] Update test method name --- .../framework/tests/Feature/PublicationFieldTypesEnumTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index 2c21d478c8f..5c0e2a51908 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -28,7 +28,7 @@ public function testCases() $this->assertSame('tag', PublicationFieldTypes::Tag->value); } - public function testRules() + public function testGetRules() { $this->assertSame(['string'], PublicationFieldTypes::String->rules()); $this->assertSame(['boolean'], PublicationFieldTypes::Boolean->rules()); From 15d064995dcab3de166d197eab80eacc0aad63c6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:53:17 +0100 Subject: [PATCH 28/48] Expect default rule --- packages/framework/tests/Feature/PublicationTypeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/PublicationTypeTest.php b/packages/framework/tests/Feature/PublicationTypeTest.php index c40a25be672..8e8a5780af3 100644 --- a/packages/framework/tests/Feature/PublicationTypeTest.php +++ b/packages/framework/tests/Feature/PublicationTypeTest.php @@ -169,7 +169,7 @@ public function test_get_field_rules() { $publicationType = new PublicationType(...$this->getTestData()); $this->assertEquals([ - 'title' => [], + 'title' => ['string'], ], $publicationType->getFieldRules()->toArray()); } From 5c3ae0ed691aafbcaa1693f102fa04849d4d1b4b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 20:59:42 +0100 Subject: [PATCH 29/48] Merge unit test into feature test --- .../Publications/Models/PublicationField.php | 1 - .../tests/Feature/PublicationFieldTest.php | 88 ++++++++++++++- .../PublicationFieldValidationRulesTest.php | 103 ------------------ 3 files changed, 86 insertions(+), 106 deletions(-) delete mode 100644 packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 2fbe19900c9..9857d69ee1f 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -55,7 +55,6 @@ public function toArray(): array /** * @param \Hyde\Framework\Features\Publications\Models\PublicationType|null $publicationType Required only when using the 'image' type. * - * @see \Hyde\Framework\Testing\Unit\PublicationFieldValidationRulesTest * @see https://laravel.com/docs/9.x/validation#available-validation-rules */ public function getValidationRules(?PublicationType $publicationType = null): Collection diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php index 40118939943..c614a5229dc 100644 --- a/packages/framework/tests/Feature/PublicationFieldTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -5,8 +5,10 @@ namespace Hyde\Framework\Testing\Feature; use Hyde\Framework\Features\Publications\Models\PublicationField; +use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Framework\Features\Publications\PublicationFieldTypes; use Hyde\Testing\TestCase; +use Illuminate\Validation\ValidationException; use ValueError; /** @@ -95,8 +97,90 @@ public function test_name_gets_stored_as_kebab_case() $this->assertSame('test-field', $field->name); } - public function test_validate_input_against_rules() + public function testGetRulesForArray() { - $this->markTestIncomplete('TODO: Implement this method.'); + $rules = (new PublicationField('array', 'myArray'))->getValidationRules(); + $this->assertSame(['array'], $rules->toArray()); + } + + public function testValidateArrayPasses() + { + $validated = (new PublicationField('array', 'myArray'))->validate(['foo', 'bar', 'baz']); + $this->assertSame(['my-array' => ['foo', 'bar', 'baz']], $validated); + } + + public function testValidateArrayFails() + { + $this->expectValidationException('The my-array must be an array.'); + (new PublicationField('array', 'myArray'))->validate('foo'); + } + + public function testGetRulesForDatetime() + { + $rules = (new PublicationField('datetime', 'myDatetime'))->getValidationRules(); + $this->assertSame(['date'], $rules->toArray()); + } + + public function testValidateDatetimePasses() + { + $validated = (new PublicationField('datetime', 'myDatetime'))->validate('2021-01-01'); + $this->assertSame(['my-datetime' => '2021-01-01'], $validated); + } + + public function testValidateDatetimeFailsForInvalidType() + { + $this->expectValidationException('The my-datetime is not a valid date.'); + (new PublicationField('datetime', 'myDatetime'))->validate('string'); + } + + public function testGetRulesForFloat() + { + $rules = (new PublicationField('float', 'myFloat'))->getValidationRules(); + $this->assertSame(['numeric'], $rules->toArray()); + } + + public function testGetRulesForInteger() + { + $rules = (new PublicationField('integer', 'myInteger'))->getValidationRules(); + $this->assertSame(['integer', 'numeric'], $rules->toArray()); + } + + public function testGetRulesForString() + { + $rules = (new PublicationField('string', 'myString'))->getValidationRules(); + $this->assertSame(['string'], $rules->toArray()); + } + + public function testGetRulesForText() + { + $rules = (new PublicationField('text', 'myText'))->getValidationRules(); + $this->assertSame(['string'], $rules->toArray()); + } + + public function testGetRulesForImage() + { + $this->directory('_media/foo'); + $this->file('_media/foo/bar.jpg'); + $this->file('_media/foo/baz.png'); + $rules = (new PublicationField('image', 'myImage'))->getValidationRules(publicationType: new PublicationType('foo')); + $this->assertSame(['in:_media/foo/bar.jpg,_media/foo/baz.png'], $rules->toArray()); + } + + public function testGetRulesForTag() + { + $rules = (new PublicationField('tag', 'myTag', tagGroup: 'foo'))->getValidationRules(); + $this->assertSame(['in:'], $rules->toArray()); + } + + public function testGetRulesForUrl() + { + $rules = (new PublicationField('url', 'myUrl'))->getValidationRules(); + $this->assertSame(['url'], $rules->toArray()); + } + + protected function expectValidationException(string $message): void + { + $this->expectException(ValidationException::class); + $this->expectExceptionMessage($message); } } diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php deleted file mode 100644 index f8825c5013a..00000000000 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ /dev/null @@ -1,103 +0,0 @@ -getValidationRules(); - $this->assertSame(['array'], $rules->toArray()); - } - - public function testValidateArrayPasses() - { - $validated = (new PublicationField('array', 'myArray'))->validate(['foo', 'bar', 'baz']); - $this->assertSame(['my-array' => ['foo', 'bar', 'baz']], $validated); - } - - public function testValidateArrayFails() - { - $this->expectValidationException('The my-array must be an array.'); - (new PublicationField('array', 'myArray'))->validate('foo'); - } - - public function testGetRulesForDatetime() - { - $rules = (new PublicationField('datetime', 'myDatetime'))->getValidationRules(); - $this->assertSame(['date'], $rules->toArray()); - } - - public function testValidateDatetimePasses() - { - $validated = (new PublicationField('datetime', 'myDatetime'))->validate('2021-01-01'); - $this->assertSame(['my-datetime' => '2021-01-01'], $validated); - } - - public function testValidateDatetimeFailsForInvalidType() - { - $this->expectValidationException('The my-datetime is not a valid date.'); - (new PublicationField('datetime', 'myDatetime'))->validate('string'); - } - - public function testGetRulesForFloat() - { - $rules = (new PublicationField('float', 'myFloat'))->getValidationRules(); - $this->assertSame(['numeric'], $rules->toArray()); - } - - public function testGetRulesForInteger() - { - $rules = (new PublicationField('integer', 'myInteger'))->getValidationRules(); - $this->assertSame(['integer', 'numeric'], $rules->toArray()); - } - - public function testGetRulesForString() - { - $rules = (new PublicationField('string', 'myString'))->getValidationRules(); - $this->assertSame(['string'], $rules->toArray()); - } - - public function testGetRulesForText() - { - $rules = (new PublicationField('text', 'myText'))->getValidationRules(); - $this->assertSame(['string'], $rules->toArray()); - } - - public function testGetRulesForImage() - { - $this->directory('_media/foo'); - $this->file('_media/foo/bar.jpg'); - $this->file('_media/foo/baz.png'); - $rules = (new PublicationField('image', 'myImage'))->getValidationRules(publicationType: new PublicationType('foo')); - $this->assertSame(['in:_media/foo/bar.jpg,_media/foo/baz.png'], $rules->toArray()); - } - - public function testGetRulesForTag() - { - $rules = (new PublicationField('tag', 'myTag', tagGroup: 'foo'))->getValidationRules(); - $this->assertSame(['in:'], $rules->toArray()); - } - - public function testGetRulesForUrl() - { - $rules = (new PublicationField('url', 'myUrl'))->getValidationRules(); - $this->assertSame(['url'], $rules->toArray()); - } - - protected function expectValidationException(string $message): void - { - $this->expectException(ValidationException::class); - $this->expectExceptionMessage($message); - } -} From d046b472f8e0d4152333993991129e9a76a5ced3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:05:39 +0100 Subject: [PATCH 30/48] Add base test for the validate method --- .../framework/tests/Feature/PublicationFieldTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php index c614a5229dc..290ebcc40de 100644 --- a/packages/framework/tests/Feature/PublicationFieldTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -97,6 +97,15 @@ public function test_name_gets_stored_as_kebab_case() $this->assertSame('test-field', $field->name); } + public function testValidate() + { + $validated = (new PublicationField('string', 'myString'))->validate('foo'); + $this->assertSame(['my-string' => 'foo'], $validated); + + $this->expectValidationException('The my-string must be a string.'); + (new PublicationField('string', 'myString'))->validate(1); + } + public function testGetRulesForArray() { $rules = (new PublicationField('array', 'myArray'))->getValidationRules(); From 19b5b964a7577b1cb10bf5bad280cb059f3af0ab Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:16:05 +0100 Subject: [PATCH 31/48] Test with custom validation rules --- .../tests/Feature/PublicationFieldTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php index 290ebcc40de..9eacfc58731 100644 --- a/packages/framework/tests/Feature/PublicationFieldTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -9,6 +9,7 @@ use Hyde\Framework\Features\Publications\PublicationFieldTypes; use Hyde\Testing\TestCase; use Illuminate\Validation\ValidationException; +use Rgasch\Collection\Collection; use ValueError; /** @@ -106,6 +107,21 @@ public function testValidate() (new PublicationField('string', 'myString'))->validate(1); } + public function testValidateWithCustomRuleCollection() + { + $validated = (new PublicationField('string', 'myString'))->validate('foo', Collection::create(['min:3'])); + $this->assertSame(['my-string' => 'foo'], $validated); + + $this->expectValidationException('The my-string must be at least 5 characters.'); + (new PublicationField('string', 'myString'))->validate('foo', Collection::create(['min:5'])); + } + + public function testValidateWithCustomRuleCollectionOverridesDefaultRules() + { + $this->expectValidationException('The my-string must be a number.'); + (new PublicationField('string', 'myString'))->validate("foo", Collection::create(['numeric'])); + } + public function testGetRulesForArray() { $rules = (new PublicationField('array', 'myArray'))->getValidationRules(); From dbfaa91c7dcd6ea6344fe38b62f3ad41aef9f2f5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:16:50 +0100 Subject: [PATCH 32/48] Replace if with ??= --- .../Features/Publications/Models/PublicationField.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 9857d69ee1f..d3083b2520d 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -83,9 +83,7 @@ public function getValidationRules(?PublicationType $publicationType = null): Co /** @param \Hyde\Framework\Features\Publications\Models\PublicationType|null $publicationType Required only when using the 'image' type. */ public function validate(mixed $input = null, Collection $fieldRules = null, ?PublicationType $publicationType = null): array { - if (! $fieldRules) { - $fieldRules = $this->getValidationRules($publicationType); - } + $fieldRules ??= $this->getValidationRules($publicationType); $validator = validator([$this->name => $input], [$this->name => $fieldRules->toArray()]); From 6fba7e550badd2c0dd9f941e4994dbbacba386b8 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 22 Dec 2022 10:17:58 +0000 Subject: [PATCH 33/48] Apply fixes from StyleCI --- packages/framework/tests/Feature/PublicationFieldTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php index 9eacfc58731..8ea0d5febd6 100644 --- a/packages/framework/tests/Feature/PublicationFieldTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -119,7 +119,7 @@ public function testValidateWithCustomRuleCollection() public function testValidateWithCustomRuleCollectionOverridesDefaultRules() { $this->expectValidationException('The my-string must be a number.'); - (new PublicationField('string', 'myString'))->validate("foo", Collection::create(['numeric'])); + (new PublicationField('string', 'myString'))->validate('foo', Collection::create(['numeric'])); } public function testGetRulesForArray() From c4ffb986189f5fa60492dee5508a2cbb6aa42c0c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:19:48 +0100 Subject: [PATCH 34/48] Revert "Replace if with ??=" This reverts commit dbfaa91c7dcd6ea6344fe38b62f3ad41aef9f2f5. --- .../Features/Publications/Models/PublicationField.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index d3083b2520d..9857d69ee1f 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -83,7 +83,9 @@ public function getValidationRules(?PublicationType $publicationType = null): Co /** @param \Hyde\Framework\Features\Publications\Models\PublicationType|null $publicationType Required only when using the 'image' type. */ public function validate(mixed $input = null, Collection $fieldRules = null, ?PublicationType $publicationType = null): array { - $fieldRules ??= $this->getValidationRules($publicationType); + if (! $fieldRules) { + $fieldRules = $this->getValidationRules($publicationType); + } $validator = validator([$this->name => $input], [$this->name => $fieldRules->toArray()]); From fd65b41960d884efe22d2718534d82d54dc892f6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:20:27 +0100 Subject: [PATCH 35/48] Use strict comparison --- .../Framework/Features/Publications/Models/PublicationField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 9857d69ee1f..86af7bc066d 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -83,7 +83,7 @@ public function getValidationRules(?PublicationType $publicationType = null): Co /** @param \Hyde\Framework\Features\Publications\Models\PublicationType|null $publicationType Required only when using the 'image' type. */ public function validate(mixed $input = null, Collection $fieldRules = null, ?PublicationType $publicationType = null): array { - if (! $fieldRules) { + if ($fieldRules === null) { $fieldRules = $this->getValidationRules($publicationType); } From 9510bdb9c9f599bfdab28ecdb3820c7ed43135e4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:22:20 +0100 Subject: [PATCH 36/48] Support both arrays and Arrayables to be passed for custom rules --- .../Publications/Models/PublicationField.php | 9 +++++++-- .../tests/Feature/PublicationFieldTest.php | 19 +++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 86af7bc066d..d63dc99800f 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Features\Publications\Models; +use Illuminate\Contracts\Support\Arrayable; use function array_filter; use function collect; use Hyde\Framework\Features\Publications\PublicationFieldTypes; @@ -81,13 +82,17 @@ public function getValidationRules(?PublicationType $publicationType = null): Co } /** @param \Hyde\Framework\Features\Publications\Models\PublicationType|null $publicationType Required only when using the 'image' type. */ - public function validate(mixed $input = null, Collection $fieldRules = null, ?PublicationType $publicationType = null): array + public function validate(mixed $input = null, Array|Arrayable $fieldRules = null, ?PublicationType $publicationType = null): array { if ($fieldRules === null) { $fieldRules = $this->getValidationRules($publicationType); + } else { + if ($fieldRules instanceof Arrayable) { + $fieldRules = $fieldRules->toArray(); + } } - $validator = validator([$this->name => $input], [$this->name => $fieldRules->toArray()]); + $validator = validator([$this->name => $input], [$this->name => $fieldRules]); return $validator->validate(); } diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php index 9eacfc58731..89360980548 100644 --- a/packages/framework/tests/Feature/PublicationFieldTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -9,7 +9,6 @@ use Hyde\Framework\Features\Publications\PublicationFieldTypes; use Hyde\Testing\TestCase; use Illuminate\Validation\ValidationException; -use Rgasch\Collection\Collection; use ValueError; /** @@ -109,17 +108,29 @@ public function testValidate() public function testValidateWithCustomRuleCollection() { - $validated = (new PublicationField('string', 'myString'))->validate('foo', Collection::create(['min:3'])); + $validated = (new PublicationField('string', 'myString'))->validate('foo', ['min:3']); $this->assertSame(['my-string' => 'foo'], $validated); $this->expectValidationException('The my-string must be at least 5 characters.'); - (new PublicationField('string', 'myString'))->validate('foo', Collection::create(['min:5'])); + (new PublicationField('string', 'myString'))->validate('foo', ['min:5']); } public function testValidateWithCustomRuleCollectionOverridesDefaultRules() { $this->expectValidationException('The my-string must be a number.'); - (new PublicationField('string', 'myString'))->validate("foo", Collection::create(['numeric'])); + (new PublicationField('string', 'myString'))->validate("foo", ['numeric']); + } + + public function testValidateMethodAcceptsArrayOfRules() + { + $validated = (new PublicationField('string', 'myString'))->validate('foo', ['min:3']); + $this->assertSame(['my-string' => 'foo'], $validated); + } + + public function testValidateMethodAcceptsArrayableOfRules() + { + $validated = (new PublicationField('string', 'myString'))->validate('foo', collect(['min:3'])); + $this->assertSame(['my-string' => 'foo'], $validated); } public function testGetRulesForArray() From 20f5d498582a95c4bd9222c525fa00075db6ec4c Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 22 Dec 2022 10:23:41 +0000 Subject: [PATCH 37/48] Apply fixes from StyleCI --- .../Features/Publications/Models/PublicationField.php | 4 ++-- packages/framework/tests/Feature/PublicationFieldTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index d63dc99800f..4ff6a397b67 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -4,13 +4,13 @@ namespace Hyde\Framework\Features\Publications\Models; -use Illuminate\Contracts\Support\Arrayable; use function array_filter; use function collect; use Hyde\Framework\Features\Publications\PublicationFieldTypes; use Hyde\Framework\Features\Publications\PublicationService; use Hyde\Support\Concerns\Serializable; use Hyde\Support\Contracts\SerializableContract; +use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Str; use Rgasch\Collection\Collection; use function strtolower; @@ -82,7 +82,7 @@ public function getValidationRules(?PublicationType $publicationType = null): Co } /** @param \Hyde\Framework\Features\Publications\Models\PublicationType|null $publicationType Required only when using the 'image' type. */ - public function validate(mixed $input = null, Array|Arrayable $fieldRules = null, ?PublicationType $publicationType = null): array + public function validate(mixed $input = null, array|Arrayable $fieldRules = null, ?PublicationType $publicationType = null): array { if ($fieldRules === null) { $fieldRules = $this->getValidationRules($publicationType); diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php index 89360980548..fd416a5f6c6 100644 --- a/packages/framework/tests/Feature/PublicationFieldTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -118,7 +118,7 @@ public function testValidateWithCustomRuleCollection() public function testValidateWithCustomRuleCollectionOverridesDefaultRules() { $this->expectValidationException('The my-string must be a number.'); - (new PublicationField('string', 'myString'))->validate("foo", ['numeric']); + (new PublicationField('string', 'myString'))->validate('foo', ['numeric']); } public function testValidateMethodAcceptsArrayOfRules() From f1e057343a2a8f0cc7dd7f33886dd8f381f52919 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:24:21 +0100 Subject: [PATCH 38/48] Merge into 'elseif' --- .../Features/Publications/Models/PublicationField.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 4ff6a397b67..f09dc391e69 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -86,10 +86,8 @@ public function validate(mixed $input = null, array|Arrayable $fieldRules = null { if ($fieldRules === null) { $fieldRules = $this->getValidationRules($publicationType); - } else { - if ($fieldRules instanceof Arrayable) { - $fieldRules = $fieldRules->toArray(); - } + } elseif ($fieldRules instanceof Arrayable) { + $fieldRules = $fieldRules->toArray(); } $validator = validator([$this->name => $input], [$this->name => $fieldRules]); From 63250850bb20897dfa63cdbae77e8394e97bff29 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:31:05 +0100 Subject: [PATCH 39/48] Refactor method --- .../Publications/Models/PublicationField.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index f09dc391e69..3d3f79d3c74 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -82,16 +82,16 @@ public function getValidationRules(?PublicationType $publicationType = null): Co } /** @param \Hyde\Framework\Features\Publications\Models\PublicationType|null $publicationType Required only when using the 'image' type. */ - public function validate(mixed $input = null, array|Arrayable $fieldRules = null, ?PublicationType $publicationType = null): array + public function validate(mixed $input = null, Arrayable|array|null $fieldRules = null, ?PublicationType $publicationType = null): array { - if ($fieldRules === null) { - $fieldRules = $this->getValidationRules($publicationType); - } elseif ($fieldRules instanceof Arrayable) { - $fieldRules = $fieldRules->toArray(); - } - - $validator = validator([$this->name => $input], [$this->name => $fieldRules]); + return validator([$this->name => $input], + [$this->name => $this->evaluateArrayable( + $fieldRules ?? $this->getValidationRules($publicationType) + )])->validate(); + } - return $validator->validate(); + protected function evaluateArrayable(array|Arrayable $fieldRules): array + { + return $fieldRules instanceof Arrayable ? $fieldRules->toArray() : $fieldRules; } } From db5688152d8d01bca583c3ae28c2a70584d1ed45 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:32:21 +0100 Subject: [PATCH 40/48] Use generic parameter for helper method --- .../Features/Publications/Models/PublicationField.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 3d3f79d3c74..4e867e39bdb 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -90,8 +90,8 @@ public function validate(mixed $input = null, Arrayable|array|null $fieldRules = )])->validate(); } - protected function evaluateArrayable(array|Arrayable $fieldRules): array + protected function evaluateArrayable(array|Arrayable $array): array { - return $fieldRules instanceof Arrayable ? $fieldRules->toArray() : $fieldRules; + return $array instanceof Arrayable ? $array->toArray() : $array; } } From 03e6e64c115404fef30ea566097b306be0da9f70 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:33:25 +0100 Subject: [PATCH 41/48] Introduce local variable --- .../Features/Publications/Models/PublicationField.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 4e867e39bdb..070063ac1a0 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -84,10 +84,9 @@ public function getValidationRules(?PublicationType $publicationType = null): Co /** @param \Hyde\Framework\Features\Publications\Models\PublicationType|null $publicationType Required only when using the 'image' type. */ public function validate(mixed $input = null, Arrayable|array|null $fieldRules = null, ?PublicationType $publicationType = null): array { - return validator([$this->name => $input], - [$this->name => $this->evaluateArrayable( - $fieldRules ?? $this->getValidationRules($publicationType) - )])->validate(); + $rules = $this->evaluateArrayable($fieldRules ?? $this->getValidationRules($publicationType)); + + return validator([$this->name => $input], [$this->name => $rules])->validate(); } protected function evaluateArrayable(array|Arrayable $array): array From 362db37df49c9db5394934e5f6bff506d3f95d24 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 22 Dec 2022 10:34:26 +0000 Subject: [PATCH 42/48] Apply fixes from StyleCI --- .../Framework/Features/Publications/Models/PublicationField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 070063ac1a0..cf1337f5a81 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -85,7 +85,7 @@ public function getValidationRules(?PublicationType $publicationType = null): Co public function validate(mixed $input = null, Arrayable|array|null $fieldRules = null, ?PublicationType $publicationType = null): array { $rules = $this->evaluateArrayable($fieldRules ?? $this->getValidationRules($publicationType)); - + return validator([$this->name => $input], [$this->name => $rules])->validate(); } From 26b1e8865d69f7f3c604bd933c0380714c09b9df Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:42:06 +0100 Subject: [PATCH 43/48] Apply the custom field rules --- .../Publications/Models/PublicationField.php | 5 +++++ .../tests/Feature/PublicationFieldTest.php | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index cf1337f5a81..fea3ffe8b35 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -64,6 +64,11 @@ public function getValidationRules(?PublicationType $publicationType = null): Co // Here we could check for a "strict" mode type of thing and add 'required' to the rules if we wanted to. + // Apply any field rules. + foreach ($this->rules as $rule) { + $fieldRules->add($rule); + } + // Apply any dynamic rules. switch ($this->type->value) { case 'image': diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php index fd416a5f6c6..e68b8cc2f4f 100644 --- a/packages/framework/tests/Feature/PublicationFieldTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -106,6 +106,15 @@ public function testValidate() (new PublicationField('string', 'myString'))->validate(1); } + public function testValidateWithCustomTypeRules() + { + $validated = (new PublicationField('string', 'myString', rules: ['min:3']))->validate('foo'); + $this->assertSame(['my-string' => 'foo'], $validated); + + $this->expectValidationException('The my-string must be at least 5 characters.'); + (new PublicationField('string', 'myString', rules: ['min:5']))->validate('foo'); + } + public function testValidateWithCustomRuleCollection() { $validated = (new PublicationField('string', 'myString'))->validate('foo', ['min:3']); @@ -133,6 +142,12 @@ public function testValidateMethodAcceptsArrayableOfRules() $this->assertSame(['my-string' => 'foo'], $validated); } + public function testGetRulesWithCustomTypeRules() + { + $rules = (new PublicationField('string', 'myString', rules: ['foo', 'bar']))->getValidationRules(); + $this->assertSame(['string', 'foo', 'bar'], $rules->toArray()); + } + public function testGetRulesForArray() { $rules = (new PublicationField('array', 'myArray'))->getValidationRules(); From ff820a8cd84ade4b3609bbf405e34333a5f5c7aa Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:43:29 +0100 Subject: [PATCH 44/48] Use built in push helper instead of foreach loop --- .../Features/Publications/Models/PublicationField.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index fea3ffe8b35..cdd191bde3d 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -65,9 +65,7 @@ public function getValidationRules(?PublicationType $publicationType = null): Co // Here we could check for a "strict" mode type of thing and add 'required' to the rules if we wanted to. // Apply any field rules. - foreach ($this->rules as $rule) { - $fieldRules->add($rule); - } + $fieldRules->push(...$this->rules); // Apply any dynamic rules. switch ($this->type->value) { From a18f00d216354c922635cb784d54518a18d76127 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:45:32 +0100 Subject: [PATCH 45/48] Add baseline test --- packages/framework/tests/Feature/PublicationFieldTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php index e68b8cc2f4f..cb25990fef5 100644 --- a/packages/framework/tests/Feature/PublicationFieldTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -142,6 +142,12 @@ public function testValidateMethodAcceptsArrayableOfRules() $this->assertSame(['my-string' => 'foo'], $validated); } + public function testGetRules() + { + $rules = (new PublicationField('string', 'myString'))->getValidationRules(); + $this->assertSame(['string'], $rules->toArray()); + } + public function testGetRulesWithCustomTypeRules() { $rules = (new PublicationField('string', 'myString', rules: ['foo', 'bar']))->getValidationRules(); From 68f09c6c9c5640f629e180dcbd071a893fefa05a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:50:53 +0100 Subject: [PATCH 46/48] Add testing helper to merge data --- .../tests/Feature/PublicationTypeTest.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/framework/tests/Feature/PublicationTypeTest.php b/packages/framework/tests/Feature/PublicationTypeTest.php index 8e8a5780af3..874692a09fd 100644 --- a/packages/framework/tests/Feature/PublicationTypeTest.php +++ b/packages/framework/tests/Feature/PublicationTypeTest.php @@ -173,26 +173,26 @@ public function test_get_field_rules() ], $publicationType->getFieldRules()->toArray()); } - protected function getTestData(): array + protected function getTestData(array $mergeData = []): array { - return [ - 'name' => 'Test Publication', + return array_merge([ + 'name' => 'Test Publication', 'canonicalField' => 'title', 'detailTemplate' => 'test-publication_detail', - 'listTemplate' => 'test-publication_list', + 'listTemplate' => 'test-publication_list', 'pagination' => [ - 'sortField' => '__createdAt', - 'sortAscending' => true, - 'prevNextLinks' => true, - 'pageSize' => 25, + 'sortField' => '__createdAt', + 'sortAscending' => true, + 'prevNextLinks' => true, + 'pageSize' => 25, ], - 'fields' => [ + 'fields' => [ [ 'name' => 'title', 'type' => 'string', ], ], - ]; + ], $mergeData); } protected function getTestDataWithPathInformation(): array From 087b92250fc49ca3ba7d5f7ade6805795cdf7383 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 22 Dec 2022 11:52:42 +0100 Subject: [PATCH 47/48] Test custom field rules can be deserialized for publication type schemas --- .../tests/Feature/PublicationTypeTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/framework/tests/Feature/PublicationTypeTest.php b/packages/framework/tests/Feature/PublicationTypeTest.php index 874692a09fd..5c8848a026a 100644 --- a/packages/framework/tests/Feature/PublicationTypeTest.php +++ b/packages/framework/tests/Feature/PublicationTypeTest.php @@ -173,6 +173,21 @@ public function test_get_field_rules() ], $publicationType->getFieldRules()->toArray()); } + public function test_get_field_rules_with_custom_type_rules() + { + $publicationType = new PublicationType(...$this->getTestData(['fields' => [ + 'title' => [ + 'name' => 'title', + 'type' => 'string', + 'rules' => ['required', 'foo'], + ], + ]])); + + $this->assertEquals([ + 'title' => ['string', 'required', 'foo'], + ], $publicationType->getFieldRules()->toArray()); + } + protected function getTestData(array $mergeData = []): array { return array_merge([ From 165da4986e6d5eb3ffdb91b4e93f98b09e52ecbd Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 22 Dec 2022 10:53:03 +0000 Subject: [PATCH 48/48] Apply fixes from StyleCI --- .../framework/tests/Feature/PublicationTypeTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/framework/tests/Feature/PublicationTypeTest.php b/packages/framework/tests/Feature/PublicationTypeTest.php index 5c8848a026a..e37d7c80583 100644 --- a/packages/framework/tests/Feature/PublicationTypeTest.php +++ b/packages/framework/tests/Feature/PublicationTypeTest.php @@ -176,11 +176,11 @@ public function test_get_field_rules() public function test_get_field_rules_with_custom_type_rules() { $publicationType = new PublicationType(...$this->getTestData(['fields' => [ - 'title' => [ - 'name' => 'title', - 'type' => 'string', - 'rules' => ['required', 'foo'], - ], + 'title' => [ + 'name' => 'title', + 'type' => 'string', + 'rules' => ['required', 'foo'], + ], ]])); $this->assertEquals([