@@ -11,4 +11,172 @@ class WC_Stripe_Payment_Method_Configurations_Test extends WP_UnitTestCase {
11
11
public function test_get_parent_configuration_id () {
12
12
$ this ->assertNull ( WC_Stripe_Payment_Method_Configurations::get_parent_configuration_id () );
13
13
}
14
+
15
+ /**
16
+ * Tests the disable_payment_method_configuration_sync method.
17
+ *
18
+ * @return void
19
+ */
20
+ public function test_disable_payment_method_configuration_sync () {
21
+ // Get initial settings
22
+ $ initial_settings = WC_Stripe_Helper::get_stripe_settings ();
23
+
24
+ // Use reflection to access the private method
25
+ $ reflection = new ReflectionClass ( 'WC_Stripe_Payment_Method_Configurations ' );
26
+ $ method = $ reflection ->getMethod ( 'disable_payment_method_configuration_sync ' );
27
+ $ method ->setAccessible ( true );
28
+ // Call the method
29
+ $ method ->invoke ( null );
30
+
31
+ // Get updated settings
32
+ $ updated_settings = WC_Stripe_Helper::get_stripe_settings ();
33
+
34
+ // Verify pmc_enabled is set to 'no'
35
+ $ this ->assertEquals ( 'no ' , $ updated_settings ['pmc_enabled ' ] );
36
+
37
+ // Restore original settings
38
+ WC_Stripe_Helper::update_main_stripe_settings ( $ initial_settings );
39
+ }
40
+
41
+ /**
42
+ * Tests that disable_payment_method_configuration_sync is called when no PMC is found.
43
+ *
44
+ * @return void
45
+ */
46
+ public function test_disable_payment_method_configuration_sync_on_no_pmc () {
47
+ // Mock the Stripe API response to return no configurations
48
+ $ mock_api = $ this ->getMockBuilder ( 'WC_Stripe_API ' )
49
+ ->disableOriginalConstructor ()
50
+ ->getMock ();
51
+
52
+ $ mock_api ->expects ( $ this ->once () )
53
+ ->method ( 'get_payment_method_configurations ' )
54
+ ->willReturn ( (object ) [ 'data ' => [] ] );
55
+
56
+ // Set the mock API instance
57
+ $ reflection = new ReflectionClass ( 'WC_Stripe_API ' );
58
+ $ property = $ reflection ->getProperty ( 'instance ' );
59
+ $ property ->setAccessible ( true );
60
+ $ property ->setValue ( null , $ mock_api );
61
+
62
+ // Get initial settings
63
+ $ initial_settings = WC_Stripe_Helper::get_stripe_settings ();
64
+
65
+ // Call get_primary_configuration which should trigger disable_payment_method_configuration_sync
66
+ // Use reflection to access the private method
67
+ $ reflection = new ReflectionClass ( 'WC_Stripe_Payment_Method_Configurations ' );
68
+ $ method = $ reflection ->getMethod ( 'get_primary_configuration ' );
69
+ $ method ->setAccessible ( true );
70
+ // Call the method
71
+ $ method ->invoke ( null );
72
+
73
+ // Get updated settings
74
+ $ updated_settings = WC_Stripe_Helper::get_stripe_settings ();
75
+
76
+ // Verify pmc_enabled is set to 'no'
77
+ $ this ->assertEquals ( 'no ' , $ updated_settings ['pmc_enabled ' ] );
78
+
79
+ // Restore original settings and API instance
80
+ WC_Stripe_Helper::update_main_stripe_settings ( $ initial_settings );
81
+ $ property ->setValue ( null , null );
82
+ }
83
+
84
+ /**
85
+ * Tests that pmc_enabled is not set to 'no' when valid PMC data exists.
86
+ *
87
+ * @return void
88
+ */
89
+ public function test_pmc_enabled_not_disabled_with_valid_data () {
90
+ // Get initial settings
91
+ $ initial_settings = WC_Stripe_Helper::get_stripe_settings ();
92
+
93
+ // Mock the Stripe API response to return a valid configuration
94
+ $ mock_api = $ this ->getMockBuilder ( 'WC_Stripe_API ' )
95
+ ->disableOriginalConstructor ()
96
+ ->getMock ();
97
+
98
+ $ mock_configuration = (object ) [
99
+ 'id ' => 'test_config_id ' ,
100
+ 'parent ' => WC_Stripe_Payment_Method_Configurations::TEST_MODE_CONFIGURATION_PARENT_ID ,
101
+ ];
102
+
103
+ $ mock_api ->expects ( $ this ->once () )
104
+ ->method ( 'get_payment_method_configurations ' )
105
+ ->willReturn ( (object ) [ 'data ' => [ $ mock_configuration ] ] );
106
+
107
+ // Set the mock API instance
108
+ $ reflection = new ReflectionClass ( 'WC_Stripe_API ' );
109
+ $ property = $ reflection ->getProperty ( 'instance ' );
110
+ $ property ->setAccessible ( true );
111
+ $ property ->setValue ( null , $ mock_api );
112
+
113
+ // Call get_primary_configuration which should NOT trigger disable_payment_method_configuration_sync
114
+ // Use reflection to access the private method
115
+ $ reflection = new ReflectionClass ( 'WC_Stripe_Payment_Method_Configurations ' );
116
+ $ method = $ reflection ->getMethod ( 'get_primary_configuration ' );
117
+ $ method ->setAccessible ( true );
118
+ // Call the method
119
+ $ method ->invoke ( null );
120
+
121
+ // Get the updated settings
122
+ $ updated_settings = WC_Stripe_Helper::get_stripe_settings ();
123
+
124
+ // Verify pmc_enabled is not set to 'no'
125
+ $ this ->assertNotEquals ( 'no ' , $ updated_settings ['pmc_enabled ' ] ?? null );
126
+
127
+ // Restore original settings and API instance
128
+ WC_Stripe_Helper::update_main_stripe_settings ( $ initial_settings );
129
+ $ property ->setValue ( null , null );
130
+ }
131
+
132
+ /**
133
+ * Tests that disable_payment_method_configuration_sync is called when there is a valid PMC in the responmse but is not valid.
134
+ *
135
+ * @return void
136
+ */
137
+ public function test_disable_payment_method_configuration_sync_on_not_valid_pmc () {
138
+ // Mock the Stripe API response to return no configurations
139
+ $ mock_api = $ this ->getMockBuilder ( 'WC_Stripe_API ' )
140
+ ->disableOriginalConstructor ()
141
+ ->getMock ();
142
+
143
+ $ mock_configuration = (object ) [
144
+ 'id ' => 'test_config_id ' ,
145
+ 'parent ' => 'pmc_from_another_platform_id ' ,
146
+ ];
147
+
148
+ $ mock_api ->expects ( $ this ->once () )
149
+ ->method ( 'get_payment_method_configurations ' )
150
+ ->willReturn ( (object ) [ 'data ' => [ $ mock_configuration ] ] );
151
+
152
+ // Set the mock API instance
153
+ $ reflection = new ReflectionClass ( 'WC_Stripe_API ' );
154
+ $ property = $ reflection ->getProperty ( 'instance ' );
155
+ $ property ->setAccessible ( true );
156
+ $ property ->setValue ( null , $ mock_api );
157
+
158
+ // Get initial settings
159
+ $ initial_settings = WC_Stripe_Helper::get_stripe_settings ();
160
+
161
+ // Call get_payment_method_configuration_from_stripe which should trigger disable_payment_method_configuration_sync
162
+ // We could use get_primary_configuration, but it has a cooldown cache; we can remove the option for the test, but
163
+ // we want to test the function get_payment_method_configuration_from_stripe that is the one processing the response
164
+ // from the Stripe API call.
165
+ // Use reflection to access the private method
166
+ $ reflection = new ReflectionClass ( 'WC_Stripe_Payment_Method_Configurations ' );
167
+ $ method = $ reflection ->getMethod ( 'get_payment_method_configuration_from_stripe ' );
168
+ $ method ->setAccessible ( true );
169
+ // Call the method
170
+ $ method ->invoke ( null );
171
+
172
+ // Get updated settings
173
+ $ updated_settings = WC_Stripe_Helper::get_stripe_settings ();
174
+
175
+ // Verify pmc_enabled is set to 'no'
176
+ $ this ->assertEquals ( 'no ' , $ updated_settings ['pmc_enabled ' ] );
177
+
178
+ // Restore original settings and API instance
179
+ WC_Stripe_Helper::update_main_stripe_settings ( $ initial_settings );
180
+ $ property ->setValue ( null , null );
181
+ }
14
182
}
0 commit comments