|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\Tests\Unit\PaymentContext\DataAccess\Sofort\Transfer; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
use Sofort\SofortLib\Sofortueberweisung; |
|
10
|
|
|
use WMDE\Euro\Euro; |
|
11
|
|
|
use WMDE\Fundraising\Frontend\PaymentContext\DataAccess\Sofort\Transfer\Client; |
|
12
|
|
|
use WMDE\Fundraising\Frontend\PaymentContext\DataAccess\Sofort\Transfer\Request; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @covers \WMDE\Fundraising\Frontend\PaymentContext\DataAccess\Sofort\Transfer\Client |
|
16
|
|
|
*/ |
|
17
|
|
|
class ClientTest extends TestCase { |
|
18
|
|
|
|
|
19
|
|
|
public function testGet(): void { |
|
20
|
|
|
$client = new Client( '47:11:00' ); |
|
21
|
|
|
|
|
22
|
|
|
$amount = Euro::newFromCents( 500 ); |
|
23
|
|
|
$amountConvertedToFloat = $amount->getEuroFloat(); |
|
24
|
|
|
|
|
25
|
|
|
$api = $this->createMock( Sofortueberweisung::class ); |
|
26
|
|
|
|
|
27
|
|
|
$api |
|
28
|
|
|
->method( 'setAmount' ) |
|
29
|
|
|
->with( $amountConvertedToFloat ); |
|
30
|
|
|
$api |
|
31
|
|
|
->method( 'setCurrencyCode' ) |
|
32
|
|
|
->with( 'EUR' ); |
|
33
|
|
|
$api |
|
34
|
|
|
->method( 'setReason' ) |
|
35
|
|
|
->with( 'Donation', '529836' ); |
|
36
|
|
|
$api |
|
37
|
|
|
->method( 'setSuccessUrl' ) |
|
38
|
|
|
->with( 'https://us.org/yes?id=529836&accessToken=letmein', true ); |
|
39
|
|
|
$api |
|
40
|
|
|
->method( 'setAbortUrl' ) |
|
41
|
|
|
->with( 'https://us.org/no' ); |
|
42
|
|
|
$api |
|
43
|
|
|
->method( 'setNotificationUrl' ) |
|
44
|
|
|
->with( 'https://us.org/callback' ); |
|
45
|
|
|
$api |
|
46
|
|
|
->method( 'sendRequest' ); |
|
47
|
|
|
$api |
|
48
|
|
|
->method( 'isError' ) |
|
49
|
|
|
->willReturn( false ); |
|
50
|
|
|
$api |
|
51
|
|
|
->expects( $this->never() ) |
|
52
|
|
|
->method( 'getError' ); |
|
53
|
|
|
$api |
|
54
|
|
|
->method( 'getTransactionId' ) |
|
55
|
|
|
->willReturn( 'tr4ns4ct10n' ); |
|
56
|
|
|
$api |
|
57
|
|
|
->method( 'getPaymentUrl' ) |
|
58
|
|
|
->willReturn( 'https://awsomepaymentprovider.tld/784trhhrf4' ); |
|
59
|
|
|
|
|
60
|
|
|
$client->setApi( $api ); |
|
61
|
|
|
|
|
62
|
|
|
$request = new Request(); |
|
63
|
|
|
$request->setAmount( $amount ); |
|
64
|
|
|
$request->setCurrencyCode( 'EUR' ); |
|
65
|
|
|
$request->setReasons( [ 'Donation', '529836' ] ); |
|
66
|
|
|
$request->setSuccessUrl( 'https://us.org/yes?id=529836&accessToken=letmein' ); |
|
67
|
|
|
$request->setAbortUrl( 'https://us.org/no' ); |
|
68
|
|
|
$request->setNotificationUrl( 'https://us.org/callback' ); |
|
69
|
|
|
$response = $client->get( $request ); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertSame( 'https://awsomepaymentprovider.tld/784trhhrf4', $response->getPaymentUrl() ); |
|
72
|
|
|
$this->assertSame( 'tr4ns4ct10n', $response->getTransactionId() ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testWhenApiReturnsErrorAnExceptionWithApiErrorMessageIsThrown(): void { |
|
76
|
|
|
$client = new Client( '47:11:00' ); |
|
77
|
|
|
|
|
78
|
|
|
$api = $this->createMock( Sofortueberweisung::class ); |
|
79
|
|
|
|
|
80
|
|
|
$api |
|
81
|
|
|
->method( 'isError' ) |
|
82
|
|
|
->willReturn( true ); |
|
83
|
|
|
$api |
|
84
|
|
|
->method( 'getError' ) |
|
85
|
|
|
->willReturn( 'boo boo' ); |
|
86
|
|
|
|
|
87
|
|
|
$client->setApi( $api ); |
|
88
|
|
|
|
|
89
|
|
|
$this->expectException( RuntimeException::class ); |
|
90
|
|
|
$this->expectExceptionMessage( 'boo boo' ); |
|
91
|
|
|
|
|
92
|
|
|
$request = new Request(); |
|
93
|
|
|
$request->setAmount( Euro::newFromCents( 500 ) ); |
|
94
|
|
|
$request->setCurrencyCode( 'EUR' ); |
|
95
|
|
|
$request->setReasons( [ 'Donation', '529836' ] ); |
|
96
|
|
|
$request->setSuccessUrl( 'https://us.org/yes?id=529836&accessToken=letmein' ); |
|
97
|
|
|
$request->setAbortUrl( 'https://us.org/no' ); |
|
98
|
|
|
$request->setNotificationUrl( 'https://us.org/callback' ); |
|
99
|
|
|
|
|
100
|
|
|
$client->get( $request ); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|