Skip to content

Commit

Permalink
Add paymentWasAlreadyBooked to FailureResponse
Browse files Browse the repository at this point in the history
Add a new static constructor and a boolean getter to check if the
FailureResponse is caused by an already booked payment. This will help
upstream code to check against this condition without using hard-coded
string messages (that might change).

Ticket: https://phabricator.wikimedia.org/T321346
  • Loading branch information
gbirke committed Nov 23, 2022
1 parent 9272279 commit d5cb83f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/UseCases/BookPayment/BookPaymentUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function bookPayment( int $paymentId, array $transactionData ): SuccessRe
}

if ( $this->paymentWasAlreadyBooked( $payment, $transactionData ) ) {
return new FailureResponse( 'Payment is already completed' );
return FailureResponse::newAlreadyCompletedResponse();
}

$verificationResponse = $this->validateWithExternalService( $payment, $transactionData );
Expand Down
11 changes: 11 additions & 0 deletions src/UseCases/BookPayment/FailureResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@
namespace WMDE\Fundraising\PaymentContext\UseCases\BookPayment;

class FailureResponse {

private const ALREADY_COMPLETED = 'Payment is already completed';

public function __construct(
public readonly string $message
) {
}

public static function newAlreadyCompletedResponse(): self {
return new self( self::ALREADY_COMPLETED );
}

public function paymentWasAlreadyCompleted(): bool {
return $this->message === self::ALREADY_COMPLETED;
}
}
24 changes: 24 additions & 0 deletions tests/Unit/UseCases/BookPayment/FailureResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare( strict_types=1 );

namespace WMDE\Fundraising\PaymentContext\Tests\Unit\UseCases\BookPayment;

use PHPUnit\Framework\TestCase;
use WMDE\Fundraising\PaymentContext\UseCases\BookPayment\FailureResponse;

/**
* @covers \WMDE\Fundraising\PaymentContext\UseCases\BookPayment\FailureResponse
*/
class FailureResponseTest extends TestCase {
public function testWhenUsingAlreadyCompletedConstructor_isAlreadyCompletedReturnsTrue(): void {
$response = FailureResponse::newAlreadyCompletedResponse();

$this->assertTrue( $response->paymentWasAlreadyCompleted() );
}

public function testWhenStringConstructor_isAlreadyCompletedReturnsFalse(): void {
$response = new FailureResponse( 'Could not book payment for ... reasons.' );

$this->assertFalse( $response->paymentWasAlreadyCompleted() );
}
}

0 comments on commit d5cb83f

Please sign in to comment.