Completed
Push — master ( 919a96...568a86 )
by wiese
73:55 queued 09:12
created

SofortUrlGeneratorTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 8
dl 0
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testWhenClientReturnsSuccessResponseAUrlIsReturned() 0 30 1
A testWhenApiReturnsErrorAnExceptionWithApiErrorMessageIsThrown() 0 18 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Tests\Unit\Presentation;
6
7
use RuntimeException;
8
use PHPUnit\Framework\TestCase;
9
use WMDE\Fundraising\Frontend\Infrastructure\Sofort\Transfer\Client;
10
use WMDE\Euro\Euro;
11
use WMDE\Fundraising\Frontend\Infrastructure\Sofort\Transfer\Request;
12
use WMDE\Fundraising\Frontend\Infrastructure\Sofort\Transfer\Response;
13
use WMDE\Fundraising\Frontend\Presentation\SofortUrlConfig;
14
use WMDE\Fundraising\Frontend\Presentation\SofortUrlGenerator;
15
16
/**
17
 * @covers \WMDE\Fundraising\Frontend\Presentation\SofortUrlGenerator
18
 */
19
class SofortUrlGeneratorTest extends TestCase {
20
21
	public function testWhenClientReturnsSuccessResponseAUrlIsReturned(): void {
22
		$config = new SofortUrlConfig( 'Donation', 'https://us.org/yes', 'https://us.org/no' );
23
24
		$amount = Euro::newFromCents( 600 );
25
26
		$request = new Request();
27
		$request->setAmount( $amount );
28
		$request->setCurrencyCode( 'EUR' );
29
		$request->setReasons( [ 'Donation', 'wx529836' ] );
30
		$request->setSuccessUrl( 'https://us.org/yes?id=44&accessToken=letmein' );
31
		$request->setAbortUrl( 'https://us.org/no' );
32
		$request->setNotificationUrl( '' );
33
34
		$response = new Response();
35
		$response->setTransactionId( '500m1l35' );
36
		$response->setPaymentUrl( 'https://awsomepaymentprovider.tld/784trhhrf4' );
37
38
		$client = $this->createMock( Client::class );
39
		$client
40
			->expects( $this->once() )
41
			->method( 'get' )
42
			->with( $request )
43
			->willReturn( $response );
44
45
		$urlGenerator = new SofortUrlGenerator( $config, $client );
46
		$this->assertSame(
47
			'https://awsomepaymentprovider.tld/784trhhrf4',
48
			$urlGenerator->generateUrl( 44, 'wx529836', $amount, 'letmein' )
49
		);
50
	}
51
52
	public function testWhenApiReturnsErrorAnExceptionWithApiErrorMessageIsThrown(): void {
53
		$config = new SofortUrlConfig( 'Your purchase', 'https://irreleva.nt', 'http://irreleva.nt' );
54
55
		$client = $this->createMock( Client::class );
56
57
		$client
58
			->expects( $this->once() )
59
			->method( 'get' )
60
			->withAnyParameters()
61
			->willThrowException( new RuntimeException( 'boo boo' ) );
62
63
		$urlGenerator = new SofortUrlGenerator( $config, $client );
64
65
		$this->expectException( RuntimeException::class );
66
		$this->expectExceptionMessage( 'Could not generate Sofort URL: boo boo' );
67
68
		$urlGenerator->generateUrl( 23, 'dq529837', Euro::newFromCents( 300 ), 'letmein' );
69
	}
70
}
71