22
33namespace OnrampLab \CustomFields \Tests \Unit \Concerns ;
44
5+ use Illuminate \Validation \ValidationException ;
56use OnrampLab \CustomFields \Models \CustomField ;
67use OnrampLab \CustomFields \Tests \Classes \Account ;
78use OnrampLab \CustomFields \Tests \Classes \User ;
89use OnrampLab \CustomFields \Tests \TestCase ;
10+ use OnrampLab \CustomFields \ValueObjects \AvailableOption ;
911
1012class CustomizableTest extends TestCase
1113{
@@ -18,7 +20,7 @@ protected function setUp(): void
1820 'type ' => 'text ' ,
1921 'model_class ' => User::class,
2022 'contextable_id ' => $ this ->account ->id ,
21- 'contextable_type ' => get_class ( $ this ->account )
23+ 'contextable_type ' => $ this ->account -> getMorphClass ( )
2224 ];
2325 $ this ->customField = CustomField::factory ()->create ($ attributes );
2426 $ this ->user = User::factory ()->create (['account_id ' => $ this ->account ->id , 'zip_code ' => '12345 ' ]);
@@ -34,4 +36,57 @@ public function custom_field_values_relationship_should_work(): void
3436 $ this ->assertEquals ($ this ->user ->id , $ customFieldValues ->first ()->customizable_id );
3537 $ this ->assertEquals (get_class ($ this ->user ), $ customFieldValues ->first ()->customizable_type );
3638 }
39+
40+ /**
41+ * @test
42+ */
43+ public function validate_custom_fields_should_work ()
44+ {
45+ $ this ->expectException (ValidationException::class);
46+ $ this ->user ->zip_code = 123 ;
47+ $ this ->user ->validateCustomFields ();
48+ }
49+
50+ /**
51+ * @test
52+ * @dataProvider customFieldDataProvider
53+ */
54+ public function custom_load_custom_field_values_should_work ($ type , $ value , $ expected ): void
55+ {
56+ $ attributes = [
57+ 'key ' => 'field ' ,
58+ 'type ' => $ type ,
59+ 'model_class ' => User::class,
60+ 'contextable_id ' => $ this ->account ->id ,
61+ 'contextable_type ' => $ this ->account ->getMorphClass ()
62+ ];
63+ if ($ type == 'select ' ) {
64+ $ attributes ['available_options ' ] = [
65+ new AvailableOption ([
66+ 'name ' => 'Option 1 ' ,
67+ 'value ' => 'Option 1 ' ,
68+ ]),
69+ new AvailableOption ([
70+ 'name ' => 'Option 2 ' ,
71+ 'value ' => 'Option 2 ' ,
72+ ])
73+ ];
74+ }
75+ $ customField = CustomField::factory ()->create ($ attributes );
76+ $ user = User::factory ()->create (['account_id ' => $ this ->account ->id , 'field ' => $ value ]);
77+ $ user ->loadCustomFieldValues ();
78+ $ this ->assertEquals ($ expected , $ user ->field );
79+ }
80+
81+ public function customFieldDataProvider (): array
82+ {
83+ return [
84+ 'Text field ' => ['text ' , 'Value ' , 'Value ' ],
85+ 'Integer field ' => ['integer ' , '42 ' , 42 ],
86+ 'Float field ' => ['float ' , '3.14 ' , 3.14 ],
87+ 'Datetime field ' => ['datetime ' , '2023-05-16 12:34:56 ' , '2023-05-16 12:34:56 ' ],
88+ 'Select field ' => ['select ' , 'Option 1 ' , 'Option 1 ' ],
89+ 'Boolean field ' => ['boolean ' , '1 ' , true ],
90+ ];
91+ }
3792}
0 commit comments