File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
lib/internal/Magento/Framework/TestFramework/Unit/Helper Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * Copyright 2025 Adobe
4+ * All Rights Reserved.
5+ */
6+ declare (strict_types=1 );
7+
8+ namespace Magento \Framework \TestFramework \Unit \Helper ;
9+
10+ use PHPUnit \Framework \MockObject \MockObject ;
11+ use ReflectionClass ;
12+
13+ /**
14+ * Trait for creating partial mocks with reflection for PHPUnit 12 migration
15+ *
16+ * Provides helper methods for creating mocks when standard PHPUnit methods are insufficient
17+ */
18+ trait MockCreationTrait
19+ {
20+ /**
21+ * Create a partial mock with reflection.
22+ *
23+ * Use this when you need to mock methods that don't exist in the class/interface
24+ * and cannot use standard createPartialMock() which would throw CannotUseOnlyMethodsException.
25+ *
26+ * @param string $className
27+ * @param array $methods Methods to mock
28+ * @return MockObject
29+ */
30+ protected function createPartialMockWithReflection (string $ className , array $ methods ): MockObject
31+ {
32+ $ reflection = new ReflectionClass ($ this );
33+ $ getMockBuilderMethod = $ reflection ->getMethod ('getMockBuilder ' );
34+ $ getMockBuilderMethod ->setAccessible (true );
35+ $ mockBuilder = $ getMockBuilderMethod ->invoke ($ this , $ className );
36+
37+ $ builderReflection = new ReflectionClass ($ mockBuilder );
38+ $ methodsProperty = $ builderReflection ->getProperty ('methods ' );
39+ $ methodsProperty ->setAccessible (true );
40+ $ methodsProperty ->setValue ($ mockBuilder , $ methods );
41+
42+ $ mockBuilder ->disableOriginalConstructor ();
43+ return $ mockBuilder ->getMock ();
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments