Passed
Push — develop ( 726e4d...2a8883 )
by Luís
37s
created

CheckoutServiceTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 3
c 5
b 0
f 0
lcom 1
cbo 5
dl 0
loc 87
rs 10
1
<?php
2
namespace PHPSC\PagSeguro\Requests\Checkout;
3
4
use DateTime;
5
use PHPSC\PagSeguro\Client\Client;
6
use PHPSC\PagSeguro\Credentials;
7
use PHPSC\PagSeguro\Environment;
8
use PHPSC\PagSeguro\Requests\Redirection;
9
10
/**
11
 * @author Luís Otávio Cobucci Oblonczyk <[email protected]>
12
 */
13
class CheckoutServiceTest extends \PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @var Client|\PHPUnit_Framework_MockObject_MockObject
17
     */
18
    private $client;
19
20
    /**
21
     * @var Credentials
22
     */
23
    private $credentials;
24
25
    protected function setUp()
26
    {
27
        $environment = $this->getMockForAbstractClass(Environment::class);
28
29
        $environment->expects($this->any())
30
                    ->method('getHost')
31
                    ->willReturn('test.com');
32
33
        $environment->expects($this->any())
34
                    ->method('getWsHost')
35
                    ->willReturn('ws.test.com');
36
37
        $this->credentials = new Credentials('[email protected]', 'test', $environment);
38
        $this->client      = $this->createMock(Client::class);
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function checkoutShouldDoAPostRequestReturningTheRedirection()
45
    {
46
        $checkout = new Checkout();
47
48
        $wsUri = 'https://ws.test.com/v2/checkout?email=test%40test.com&token=test';
49
        $request = simplexml_load_string(
50
            '<?xml version="1.0" encoding="UTF-8"?>'
51
            . '<checkout><currency>BRL</currency></checkout>'
52
        );
53
54
        $response = simplexml_load_string(
55
            '<?xml version="1.0" encoding="UTF-8"?>'
56
            . '<checkout><code>123</code><date>2010-12-02T10:11:28.000-02:00</date></checkout>'
57
        );
58
59
        $this->client->expects($this->once())
60
                     ->method('post')
61
                     ->with($wsUri, $request)
62
                     ->willReturn($response);
63
64
        $service = new CheckoutService($this->credentials, $this->client);
65
        $redirection = $service->checkout($checkout);
66
67
        $redirectUri = 'https://test.com/v2/checkout/payment.html';
68
69
        $this->assertInstanceOf(Redirection::class, $redirection);
70
        $this->assertAttributeEquals($redirectUri, 'uri', $redirection);
71
        $this->assertAttributeEquals('123', 'code', $redirection);
72
        $this->assertAttributeEquals(new DateTime('2010-12-02T10:11:28.000-02:00'), 'date', $redirection);
73
    }
74
75
    /**
76
     * @test
77
     */
78
    public function createCheckoutBuilderShouldReturnANewBuilderInstance()
79
    {
80
        $service = new CheckoutService($this->credentials, $this->client);
81
82
        $this->assertInstanceOf(CheckoutBuilder::class, $service->createCheckoutBuilder());
83
    }
84
}
85