Test get Mollie Customer ID for payment
remcotolsma opened this issue · comments
We currently have a test in this library with which we test whether the search for a Mollie customer ID for a Pronamic Pay payment goes well.
wp-pronamic-pay-mollie/tests/src/GatewayTest.php
Lines 238 to 316 in 141a1d5
/** | |
* Test get Mollie Customer ID for payment. | |
* | |
* @dataProvider get_customer_id_for_payment_provider | |
* | |
* @param int $user_id Payment WordPress user ID. | |
* @param string $subscription_customer_id Subscription Mollie customer ID. | |
* @param string $first_payment_customer_id First payment Mollie customer ID. | |
* @param bool $expected Expected Mollie Customer ID. | |
*/ | |
public function test_get_customer_id_for_payment( $user_id, $subscription_customer_id, $first_payment_customer_id, $expected ) { | |
// Customer. | |
$customer = new Customer(); | |
$customer->set_user_id( $user_id ); | |
// New payment. | |
$payment = new Payment(); | |
$payment->config_id = 1; | |
$payment->set_customer( $customer ); | |
// Subscription. | |
$subscription = new Subscription(); | |
$subscription->set_customer( $customer ); | |
$subscription->add_phase( | |
new SubscriptionPhase( | |
$subscription, | |
new DateTimeImmutable(), | |
new SubscriptionInterval( 'P30D' ), | |
new Money( '10' ) | |
) | |
); | |
$payment->add_period( $subscription->new_period() ); | |
pronamic_pay_plugin()->payments_data_store->create( $payment ); | |
// Set customer ID meta. | |
$payment->set_meta( 'mollie_customer_id', $first_payment_customer_id ); | |
$subscription->set_meta( 'mollie_customer_id', $subscription_customer_id ); | |
// Get customer ID for payment. | |
$this->factory->fake( 'https://api.mollie.com/v2/customers/cst_8wmqcHMN4U', __DIR__ . '/../http/api-mollie-com-v2-customers-cst_8wmqcHMN4U.http' ); | |
$this->factory->fake( 'https://api.mollie.com/v2/customers/cst_8wmqcHMN4U_first', __DIR__ . '/../http/api-mollie-com-v2-customers-cst_8wmqcHMN4U_first.http' ); | |
$this->factory->fake( 'https://api.mollie.com/v2/customers/cst_8wmqcHMN4U_subscription', __DIR__ . '/../http/api-mollie-com-v2-customers-cst_8wmqcHMN4U_subscription.http' ); | |
$customer_id = $this->gateway->get_customer_id_for_payment( $payment ); | |
$this->assertEquals( $expected, $customer_id ); | |
} | |
/** | |
* Data provider for getting Mollie Customer ID for payment. | |
* | |
* @return array | |
*/ | |
public function get_customer_id_for_payment_provider() { | |
$customer_id = 'cst_8wmqcHMN4U'; | |
$cst_subscription = sprintf( '%s_subscription', $customer_id ); | |
$cst_first = sprintf( '%s_first', $customer_id ); | |
return [ | |
[ null, null, null, false ], | |
[ true, null, null, false ], | |
[ false, null, null, false ], | |
[ 0, null, null, false ], | |
[ 10, null, null, false ], | |
[ 1, null, null, false ], | |
[ 1, null, $cst_first, $cst_first ], | |
[ 1, $cst_subscription, null, $cst_subscription ], | |
[ 1, $cst_subscription, $cst_first, $cst_subscription ], | |
[ '1', $cst_subscription, $cst_first, $cst_subscription ], | |
]; | |
} |
The test where the $user_id = 1
, $subscription_customer_id = null
, $first_payment_customer_id = 'cst_8wmqcHMN4U_first'
and $expected = 'cst_8wmqcHMN4U_first'
currently fails.
I think this behavior has changed because we changed the implementation of get_customer_id_for_subscription( Subscription $subscription )
:
wp-pronamic-pay-mollie/src/Gateway.php
Lines 554 to 576 in 29070a8
/** | |
* Get customer ID for subscription. | |
* | |
* @param Subscription $subscription Subscription. | |
* | |
* @return string|null | |
*/ | |
private function get_customer_id_for_subscription( Subscription $subscription ) { | |
$customer_id = $subscription->get_meta( 'mollie_customer_id' ); | |
// Try to get (legacy) customer ID from first payment. | |
$first_payment = $subscription->get_first_payment(); | |
if ( empty( $customer_id ) && $first_payment ) { | |
$customer_id = $first_payment->get_meta( 'mollie_customer_id' ); | |
} | |
if ( ! empty( $customer_id ) ) { | |
return $customer_id; | |
} | |
return null; | |
} |
In the current version we no longer look at the 'first' payment of a subscription, the Mollie customer ID and the Mollie mandate ID are simply stored within the subscription metadata.
Therefore, we think that this test case is no longer relevant for now, if it later turns out to be the case, we can revise this issue.
Fixed in d2b3c25.