mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
Refactoring
This commit is contained in:
parent
12f1a2e40b
commit
05dcf5c4f4
3 changed files with 89 additions and 35 deletions
|
@ -107,13 +107,8 @@ class PaymentTokenRepository {
|
|||
* @param PaymentToken[] $tokens The tokens.
|
||||
* @return bool Whether tokens contains card or not.
|
||||
*/
|
||||
public function tokens_contains_card( $tokens ): bool {
|
||||
foreach ( $tokens as $token ) {
|
||||
if ( isset( $token->source()->card ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
public function tokens_contains_card( array $tokens ): bool {
|
||||
return $this->token_contains_source( $tokens, 'card' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -122,13 +117,8 @@ class PaymentTokenRepository {
|
|||
* @param PaymentToken[] $tokens The tokens.
|
||||
* @return bool Whether tokens contains card or not.
|
||||
*/
|
||||
public function tokens_contains_paypal( $tokens ): bool {
|
||||
foreach ( $tokens as $token ) {
|
||||
if ( isset( $token->source()->paypal ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
public function tokens_contains_paypal( array $tokens ): bool {
|
||||
return $this->token_contains_source( $tokens, 'paypal' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -145,4 +135,21 @@ class PaymentTokenRepository {
|
|||
update_user_meta( $id, self::USER_META, $token_array );
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if tokens has the given source.
|
||||
*
|
||||
* @param array $tokens Payment tokens.
|
||||
* @param string $source_type Payment token source type.
|
||||
* @return bool Whether tokens contains source or not.
|
||||
*/
|
||||
private function token_contains_source( array $tokens, string $source_type ): bool {
|
||||
foreach ( $tokens as $token ) {
|
||||
if ( isset( $token->source()->card ) && 'card' === $source_type || isset( $token->source()->paypal ) && 'paypal' === $source_type ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -110,27 +110,6 @@ class PaymentTokenEndpointTest extends TestCase
|
|||
$this->sut->for_user($id);
|
||||
}
|
||||
|
||||
public function testForUserFailBecauseEmptyTokens()
|
||||
{
|
||||
$id = 1;
|
||||
$token = Mockery::mock(Token::class);
|
||||
$rawResponse = ['body' => '{"payment_tokens":[]}'];
|
||||
$this->bearer->shouldReceive('bearer')
|
||||
->andReturn($token);
|
||||
$token->shouldReceive('token')
|
||||
->andReturn('bearer');
|
||||
$this->ensureRequestForUser($rawResponse, $id);
|
||||
|
||||
|
||||
expect('wp_remote_get')->andReturn($rawResponse);
|
||||
expect('is_wp_error')->with($rawResponse)->andReturn(false);
|
||||
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(200);
|
||||
$this->logger->shouldReceive('log');
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->sut->for_user($id);
|
||||
}
|
||||
|
||||
public function testDeleteToken()
|
||||
{
|
||||
$paymentToken = $paymentToken = Mockery::mock(PaymentToken::class);
|
||||
|
|
|
@ -90,4 +90,72 @@ class PaymentTokenRepositoryTest extends TestCase
|
|||
|
||||
$this->sut->delete_token($id, $paymentToken);
|
||||
}
|
||||
|
||||
public function testAllForUserId()
|
||||
{
|
||||
$id = 1;
|
||||
$tokens = [];
|
||||
|
||||
$this->endpoint->shouldReceive('for_user')
|
||||
->with($id)
|
||||
->andReturn($tokens);
|
||||
expect('update_user_meta')->with($id, $this->sut::USER_META, $tokens);
|
||||
|
||||
$result = $this->sut->all_for_user_id($id);
|
||||
$this->assertSame($tokens, $result);
|
||||
}
|
||||
|
||||
public function test_AllForUserIdReturnsEmptyArrayIfGettingTokenFails()
|
||||
{
|
||||
$id = 1;
|
||||
$tokens = [];
|
||||
|
||||
$this->endpoint
|
||||
->expects('for_user')
|
||||
->with($id)
|
||||
->andThrow(RuntimeException::class);
|
||||
|
||||
$result = $this->sut->all_for_user_id($id);
|
||||
$this->assertSame($tokens, $result);
|
||||
}
|
||||
|
||||
public function testTokensContainCardReturnsTrue()
|
||||
{
|
||||
$source = new \stdClass();
|
||||
$card = new \stdClass();
|
||||
$source->card = $card;
|
||||
$token = Mockery::mock(PaymentToken::class);
|
||||
$tokens = [$token];
|
||||
|
||||
$token->shouldReceive('source')->andReturn($source);
|
||||
|
||||
$this->assertTrue($this->sut->tokens_contains_card($tokens));
|
||||
}
|
||||
|
||||
public function testTokensContainCardReturnsFalse()
|
||||
{
|
||||
$tokens = [];
|
||||
$this->assertFalse($this->sut->tokens_contains_card($tokens));
|
||||
}
|
||||
|
||||
public function testTokensContainPayPalReturnsTrue()
|
||||
{
|
||||
$source = new \stdClass();
|
||||
$paypal = new \stdClass();
|
||||
$source->paypal = $paypal;
|
||||
$token = Mockery::mock(PaymentToken::class);
|
||||
$tokens = [$token];
|
||||
|
||||
$token->shouldReceive('source')->andReturn($source);
|
||||
|
||||
$this->assertTrue($this->sut->tokens_contains_paypal($tokens));
|
||||
}
|
||||
|
||||
public function testTokensContainPayPalReturnsFalse()
|
||||
{
|
||||
$tokens = [];
|
||||
$this->assertFalse($this->sut->tokens_contains_paypal($tokens));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue