33namespace Grayloon \Magento \Tests \Support ;
44
55use Grayloon \Magento \Jobs \UpdateProductAttributeGroup ;
6+ use Grayloon \Magento \Models \MagentoCustomAttribute ;
67use Grayloon \Magento \Models \MagentoCustomAttributeType ;
78use Grayloon \Magento \Support \HasCustomAttributes ;
89use Grayloon \Magento \Tests \TestCase ;
@@ -91,6 +92,84 @@ public function test_resolves_raw_value_from_option_not_supplied_in_options()
9192
9293 $ this ->assertEquals ('Unknown ' , $ value );
9394 }
95+
96+ public function test_updates_attribute_value_based_on_options ()
97+ {
98+ $ type = factory (MagentoCustomAttributeType::class)->create ([
99+ 'name ' => 'foo_bar ' ,
100+ 'options ' => [
101+ [
102+ 'label ' => 'New York ' ,
103+ 'value ' => '1 ' ,
104+ ],
105+ [
106+ 'label ' => 'Los Angeles ' ,
107+ 'value ' => '2 ' ,
108+ ],
109+ ],
110+ ]);
111+
112+ $ attribute = factory (MagentoCustomAttribute::class)->create ([
113+ 'attribute_type_id ' => $ type ->id ,
114+ 'value ' => '1 ' ,
115+ ]);
116+
117+ (new FakeSupportingClass )->exposedUpdateCustomAttributeTypeValues ($ type );
118+
119+ $ this ->assertEquals ('New York ' , $ attribute ->fresh ()->value );
120+ }
121+
122+ public function test_updates_multiple_attribute_value_based_on_options ()
123+ {
124+ $ type = factory (MagentoCustomAttributeType::class)->create ([
125+ 'name ' => 'foo_bar ' ,
126+ 'options ' => [
127+ [
128+ 'label ' => 'New York ' ,
129+ 'value ' => '1 ' ,
130+ ],
131+ [
132+ 'label ' => 'Los Angeles ' ,
133+ 'value ' => '2 ' ,
134+ ],
135+ ],
136+ ]);
137+
138+ $ attributes = factory (MagentoCustomAttribute::class, 10 )->create ([
139+ 'attribute_type_id ' => $ type ->id ,
140+ 'value ' => '1 ' ,
141+ ]);
142+
143+ (new FakeSupportingClass )->exposedUpdateCustomAttributeTypeValues ($ type );
144+
145+ $ this ->assertEquals (10 , $ attributes ->fresh ()->where ('value ' , 'New York ' )->count ());
146+ }
147+
148+ public function test_missing_option_keeps_raw_attribute_value ()
149+ {
150+ $ type = factory (MagentoCustomAttributeType::class)->create ([
151+ 'name ' => 'foo_bar ' ,
152+ 'options ' => [
153+ [
154+ 'label ' => 'New York ' ,
155+ 'value ' => '1 ' ,
156+ ],
157+ [
158+ 'label ' => 'Los Angeles ' ,
159+ 'value ' => '2 ' ,
160+ ],
161+ ],
162+ ]);
163+
164+ $ attribute = factory (MagentoCustomAttribute::class)->create ([
165+ 'attribute_type_id ' => $ type ->id ,
166+ 'value ' => 'Unknown ' ,
167+ ]);
168+
169+ (new FakeSupportingClass )->exposedUpdateCustomAttributeTypeValues ($ type );
170+
171+ $ this ->assertEquals ('Unknown ' , $ attribute ->fresh ()->value );
172+ }
94173}
95174
96175class FakeSupportingClass
@@ -106,4 +185,9 @@ public function exposedResolveCustomAttributeValue($type, $value)
106185 {
107186 return $ this ->resolveCustomAttributeValue ($ type , $ value );
108187 }
188+
189+ public function exposedUpdateCustomAttributeTypeValues ($ type )
190+ {
191+ return $ this ->updateCustomAttributeTypeValues ($ type );
192+ }
109193}
0 commit comments