@@ -200,44 +200,47 @@ public function testGetVariantsIncludeDisabledParameter(): void
200200 */
201201 public function testGetVariantsNullableIncludeDisabled (?bool $ includeDisabled , bool $ useNestedElementsController , int $ expectedCount ): void
202202 {
203- if ($ useNestedElementsController ) {
204- $ mockController = $ this ->getMockBuilder (NestedElementsController::class)
205- ->disableOriginalConstructor ()
206- ->getMock ();
207- Craft::$ app ->controller = $ mockController ;
208- } else {
209- Craft::$ app ->controller = null ;
210- }
203+ $ originalController = Craft::$ app ->controller ;
204+
205+ try {
206+ if ($ useNestedElementsController ) {
207+ $ mockController = $ this ->getMockBuilder (NestedElementsController::class)
208+ ->disableOriginalConstructor ()
209+ ->getMock ();
210+ Craft::$ app ->controller = $ mockController ;
211+ }
211212
212- $ product = new Product ();
213- $ product ->typeId = 2000 ;
213+ $ product = new Product ();
214+ $ product ->typeId = 2000 ;
214215
215- $ enabled = new Variant ();
216- $ enabled ->enabled = true ;
217- $ enabled ->sku = 'enabled-sku ' ;
216+ $ enabled = new Variant ();
217+ $ enabled ->enabled = true ;
218+ $ enabled ->sku = 'enabled-sku ' ;
218219
219- $ disabled = new Variant ();
220- $ disabled ->enabled = false ;
221- $ disabled ->sku = 'disabled-sku ' ;
220+ $ disabled = new Variant ();
221+ $ disabled ->enabled = false ;
222+ $ disabled ->sku = 'disabled-sku ' ;
222223
223- $ product ->setVariants ([$ enabled , $ disabled ]);
224+ $ product ->setVariants ([$ enabled , $ disabled ]);
224225
225- $ result = $ product ->getVariants ($ includeDisabled );
226- self ::assertCount ($ expectedCount , $ result );
226+ $ result = $ product ->getVariants ($ includeDisabled );
227+ self ::assertCount ($ expectedCount , $ result );
227228
228- // The internal collection must never be mutated by the filter —
229- // regardless of which parameter was passed, all set variants must be retained.
230- $ reflection = new ReflectionClass ($ product );
231- $ variantsProperty = $ reflection ->getProperty ('_variants ' );
232- $ variantsProperty ->setAccessible (true );
229+ // The internal collection must never be mutated by the filter —
230+ // regardless of which parameter was passed, all set variants must be retained.
231+ $ reflection = new ReflectionClass ($ product );
232+ $ variantsProperty = $ reflection ->getProperty ('_variants ' );
233+ $ variantsProperty ->setAccessible (true );
233234
234- /** @var VariantCollection $internalVariants */
235- $ internalVariants = $ variantsProperty ->getValue ($ product );
236- self ::assertInstanceOf (VariantCollection::class, $ internalVariants );
237- self ::assertCount (2 , $ internalVariants , '_variants must retain all variants regardless of the filter applied ' );
235+ /** @var VariantCollection $internalVariants */
236+ $ internalVariants = $ variantsProperty ->getValue ($ product );
237+ self ::assertInstanceOf (VariantCollection::class, $ internalVariants );
238+ self ::assertCount (2 , $ internalVariants , '_variants must retain all variants regardless of the filter applied ' );
238239
239- // Clean up so the controller state does not leak into subsequent tests
240- Craft::$ app ->controller = null ;
240+ } finally {
241+ // Clean up so the controller state does not leak into subsequent tests
242+ Craft::$ app ->controller = $ originalController ;
243+ }
241244 }
242245
243246 /**
0 commit comments