1+ <?php
2+
3+ use Workbench \App \Enums \VolumeUnitEnum ;
4+ use Workbench \App \Enums \VolumeUnitWithoutMetaEnum ;
5+
6+ it ('can create an enum ' , function () {
7+ expect (VolumeUnitEnum::GRAMS ->value )->toEqual ('grams ' );
8+ });
9+
10+ it ('can get the label ' , function () {
11+ expect (VolumeUnitEnum::GRAMS ->label ())->toEqual ('g ' )
12+ ->and (VolumeUnitEnum::labelFor (VolumeUnitEnum::GRAMS ))->toEqual ('g ' );
13+ });
14+
15+ it ('can get the metadata ' , function () {
16+ expect (VolumeUnitWithoutMetaEnum::GRAMS ->toArray ())->toMatchArray ([
17+ 'meta ' => []
18+ ])
19+ ->and (VolumeUnitEnum::GRAMS ->toArray ())->toMatchArray ([
20+ 'meta ' => [
21+ 'background_color ' => 'bg-red-100 ' ,
22+ 'text_color ' => 'text-red-800 ' ,
23+ ]
24+ ]);
25+
26+ });
27+
28+ it ('can list all options ' , function () {
29+ expect (VolumeUnitEnum::options ())->toEqual ([
30+ [
31+ 'name ' => 'MILLIGRAMS ' ,
32+ 'value ' => 'milligrams ' ,
33+ 'label ' => 'mg ' ,
34+ 'meta ' => [
35+ 'background_color ' => 'bg-green-100 ' ,
36+ 'text_color ' => 'text-green-800 ' ,
37+ ]
38+ ],
39+ [
40+ 'name ' => 'GRAMS ' ,
41+ 'value ' => 'grams ' ,
42+ 'label ' => 'g ' ,
43+ 'meta ' => [
44+ 'background_color ' => 'bg-red-100 ' ,
45+ 'text_color ' => 'text-red-800 ' ,
46+ ]
47+ ],
48+ [
49+ 'name ' => 'KILOGRAMS ' ,
50+ 'value ' => 'kilograms ' ,
51+ 'label ' => 'kg ' ,
52+ 'meta ' => [
53+ 'background_color ' => 'bg-gray-100 ' ,
54+ 'text_color ' => 'text-gray-800 ' ,
55+ ]
56+ ],
57+ [
58+ 'name ' => 'TONNE ' ,
59+ 'value ' => 'tonne ' ,
60+ 'label ' => 't ' ,
61+ 'meta ' => [
62+ 'background_color ' => 'bg-gray-100 ' ,
63+ 'text_color ' => 'text-gray-800 ' ,
64+ ],
65+ ]
66+ ]);
67+ });
68+
69+ it ('can get the names ' , function () {
70+ expect (VolumeUnitEnum::names ())->toEqual ([
71+ 'MILLIGRAMS ' ,
72+ 'GRAMS ' ,
73+ 'KILOGRAMS ' ,
74+ 'TONNE ' ,
75+ ]);
76+ });
77+
78+ it ('can get the values ' , function () {
79+ expect (VolumeUnitEnum::values ())->toEqual ([
80+ 'milligrams ' ,
81+ 'grams ' ,
82+ 'kilograms ' ,
83+ 'tonne ' ,
84+ ]);
85+ });
86+
87+ it ('can get the labels ' , function () {
88+ expect (VolumeUnitEnum::labels ())->toEqual ([
89+ 'mg ' ,
90+ 'g ' ,
91+ 'kg ' ,
92+ 't ' ,
93+ ]);
94+ });
95+
96+ it ('can get the value => label map ' , function () {
97+ expect (VolumeUnitEnum::map ())->toEqual ([
98+ 'milligrams ' => 'mg ' ,
99+ 'grams ' => 'g ' ,
100+ 'kilograms ' => 'kg ' ,
101+ 'tonne ' => 't ' ,
102+ ]);
103+ });
104+
105+ it ('can convert a single value to an array ' , function () {
106+ expect (VolumeUnitEnum::MILLIGRAMS ->toArray ())->toEqual ([
107+ 'name ' => 'MILLIGRAMS ' ,
108+ 'value ' => 'milligrams ' ,
109+ 'label ' => 'mg ' ,
110+ 'meta ' => [
111+ 'background_color ' => 'bg-green-100 ' ,
112+ 'text_color ' => 'text-green-800 ' ,
113+ ]
114+ ]);
115+ });
116+
117+ it ('will return the label if toHtml it called ' , function () {
118+ expect (VolumeUnitEnum::MILLIGRAMS ->toHtml ())->toEqual ('mg ' );
119+ });
120+
121+ it ('will return the json string format of toArray if toJson is called ' , function () {
122+ expect (VolumeUnitEnum::MILLIGRAMS ->toJson ())->toEqual ('{"name":"MILLIGRAMS","value":"milligrams","label":"mg","meta":{"background_color":"bg-green-100","text_color":"text-green-800"}} ' );
123+ });
124+
125+ it ('can compare enums ' , function () {
126+ // is methods
127+ expect (VolumeUnitEnum::MILLIGRAMS ->is (VolumeUnitEnum::MILLIGRAMS ))->toBeTrue ()
128+ ->and (VolumeUnitEnum::MILLIGRAMS ->is (VolumeUnitEnum::GRAMS ))->toBeFalse ()
129+ ->and (VolumeUnitEnum::MILLIGRAMS ->is ('milligrams ' ))->toBeTrue ()
130+ ->and (VolumeUnitEnum::MILLIGRAMS ->is ('grams ' ))->toBeFalse ()
131+ // isA methods
132+ ->and (VolumeUnitEnum::MILLIGRAMS ->isA (VolumeUnitEnum::MILLIGRAMS ))->toBeTrue ()
133+ ->and (VolumeUnitEnum::MILLIGRAMS ->isA (VolumeUnitEnum::GRAMS ))->toBeFalse ()
134+ ->and (VolumeUnitEnum::MILLIGRAMS ->isA ('milligrams ' ))->toBeTrue ()
135+ ->and (VolumeUnitEnum::MILLIGRAMS ->isA ('grams ' ))->toBeFalse ()
136+ // isAn methods
137+ ->and (VolumeUnitEnum::MILLIGRAMS ->isAn (VolumeUnitEnum::MILLIGRAMS ))->toBeTrue ()
138+ ->and (VolumeUnitEnum::MILLIGRAMS ->isAn (VolumeUnitEnum::GRAMS ))->toBeFalse ()
139+ ->and (VolumeUnitEnum::MILLIGRAMS ->isAn ('milligrams ' ))->toBeTrue ()
140+ ->and (VolumeUnitEnum::MILLIGRAMS ->isAn ('grams ' ))->toBeFalse ()
141+ // isAny methods
142+ ->and (VolumeUnitEnum::MILLIGRAMS ->isAny ([VolumeUnitEnum::MILLIGRAMS , VolumeUnitEnum::TONNE ]))->toBeTrue ()
143+ ->and (VolumeUnitEnum::MILLIGRAMS ->isAny ([VolumeUnitEnum::GRAMS , VolumeUnitEnum::TONNE ]))->toBeFalse ()
144+ ->and (VolumeUnitEnum::MILLIGRAMS ->isAny (['milligrams ' , 'tonne ' ]))->toBeTrue ()
145+ ->and (VolumeUnitEnum::MILLIGRAMS ->isAny (['grams ' , 'tonne ' ]))->toBeFalse ();
146+ });
147+
148+ it ('can negative compare enums ' , function () {
149+ expect (VolumeUnitEnum::MILLIGRAMS ->isNot (VolumeUnitEnum::MILLIGRAMS ))->toBeFalse ()
150+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNot (VolumeUnitEnum::GRAMS ))->toBeTrue ()
151+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNot ('milligrams ' ))->toBeFalse ()
152+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNot ('grams ' ))->toBeTrue ()
153+ //
154+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNotA (VolumeUnitEnum::MILLIGRAMS ))->toBeFalse ()
155+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNotA (VolumeUnitEnum::GRAMS ))->toBeTrue ()
156+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNotA ('milligrams ' ))->toBeFalse ()
157+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNotA ('grams ' ))->toBeTrue ()
158+ //
159+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNotAn (VolumeUnitEnum::MILLIGRAMS ))->toBeFalse ()
160+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNotAn (VolumeUnitEnum::GRAMS ))->toBeTrue ()
161+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNotAn ('milligrams ' ))->toBeFalse ()
162+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNotAn ('grams ' ))->toBeTrue ()
163+ // isNotAny methods
164+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNotAny ([VolumeUnitEnum::MILLIGRAMS , VolumeUnitEnum::TONNE ]))->toBeFalse ()
165+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNotAny ([VolumeUnitEnum::GRAMS , VolumeUnitEnum::TONNE ]))->toBeTrue ()
166+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNotAny (['milligrams ' , 'tonne ' ]))->toBeFalse ()
167+ ->and (VolumeUnitEnum::MILLIGRAMS ->isNotAny (['grams ' , 'tonne ' ]))->toBeTrue ();
168+ });
169+
170+ it ('throws an exception when comparing against an invalid value ' , function () {
171+ expect (fn () => VolumeUnitEnum::MILLIGRAMS ->is ('invalid ' ))
172+ ->toThrow ('"invalid" is not a valid backing value for enum Workbench\App\Enums\VolumeUnitEnum ' );
173+ });
174+
175+ it ('can get the validation rule ' , function () {
176+ expect (VolumeUnitEnum::rule ())->toBeInstanceOf (\Illuminate \Validation \Rules \Enum::class);
177+ });
0 commit comments