2
2
3
3
namespace OnrampLab \CustomFields \Tests \Unit \Concerns ;
4
4
5
+ use Illuminate \Validation \ValidationException ;
5
6
use OnrampLab \CustomFields \Models \CustomField ;
6
7
use OnrampLab \CustomFields \Tests \Classes \Account ;
7
8
use OnrampLab \CustomFields \Tests \Classes \User ;
8
9
use OnrampLab \CustomFields \Tests \TestCase ;
10
+ use OnrampLab \CustomFields \ValueObjects \AvailableOption ;
9
11
10
12
class CustomizableTest extends TestCase
11
13
{
@@ -18,7 +20,7 @@ protected function setUp(): void
18
20
'type ' => 'text ' ,
19
21
'model_class ' => User::class,
20
22
'contextable_id ' => $ this ->account ->id ,
21
- 'contextable_type ' => get_class ( $ this ->account )
23
+ 'contextable_type ' => $ this ->account -> getMorphClass ( )
22
24
];
23
25
$ this ->customField = CustomField::factory ()->create ($ attributes );
24
26
$ 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
34
36
$ this ->assertEquals ($ this ->user ->id , $ customFieldValues ->first ()->customizable_id );
35
37
$ this ->assertEquals (get_class ($ this ->user ), $ customFieldValues ->first ()->customizable_type );
36
38
}
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
+ }
37
92
}
0 commit comments