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

SubscriptionServiceTest::testChargeShouldDoReturnChargeResponse()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 8.8571
cc 1
eloc 27
nc 1
nop 0
1
<?php
2
namespace PHPSC\PagSeguro\Purchases\Subscriptions;
3
4
use PHPSC\PagSeguro\Purchases\SubscriptionService as SubscriptionServiceInterface;
5
use PHPSC\PagSeguro\Service;
6
use PHPSC\PagSeguro\Credentials;
7
use PHPSC\PagSeguro\Client\Client;
8
use DateTime;
9
use SimpleXMLElement;
10
11
/**
12
 * @author Renato Moura <[email protected]>
13
 */
14
class SubscriptionServiceTest extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * @var Credentials|\PHPUnit_Framework_MockObject_MockObject
18
     */
19
    private $credentials;
20
21
    /**
22
     * @var Client|\PHPUnit_Framework_MockObject_MockObject
23
     */
24
    private $client;
25
26
    /**
27
     * @var ChargeSerializer|\PHPUnit_Framework_MockObject_MockObject
28
     */
29
    private $serializer;
30
31
    protected function setUp()
32
    {
33
        $this->credentials = $this->createMock(Credentials::class);
34
        $this->client      = $this->createMock(Client::class);
35
    }
36
37
    public function testConstructShouldSettersDecoder()
38
    {
39
        $service = new SubscriptionService($this->credentials, $this->client);
40
41
        $this->assertInstanceOf(SubscriptionServiceInterface::class, $service);
42
        $this->assertInstanceOf(Service::class, $service);
43
    }
44
45
    public function testCreateChargeBuilderShouldReturnObjectBuilder()
46
    {
47
        $service = new SubscriptionService($this->credentials, $this->client);
48
49
        $this->assertEquals(new ChargeBuilder('ABCDEF'), $service->createChargeBuilder('ABCDEF'));
50
    }
51
52
    public function testCancelShouldDoReturnCancellationResponse()
53
    {
54
        $wsUrl = 'https://ws.test.com/v2/transactions?token=zzzzz';
55
        $this->credentials
56
            ->expects($this->once())
57
            ->method('getWsUrl')
58
            ->with('/v2/pre-approvals/cancel/ABCDEF', [])
59
            ->willReturn($wsUrl);
60
61
        $response  = '<?xml version="1.0" encoding="UTF-8"?><transaction>';
62
        $response .= '<status>6</status><date>2015-11-19T11:33:54.000-03:00</date>';
63
        $response .= '</transaction>';
64
        $xmlResponse = new SimpleXMLElement($response);
65
        $this->client
66
            ->expects($this->once())
67
            ->method('get')
68
            ->with($wsUrl)
69
            ->willReturn($xmlResponse);
70
71
        $service = new SubscriptionService($this->credentials, $this->client);
72
73
        $expected = new CancellationResponse('6', new DateTime('2015-11-19T11:33:54.000-03:00'));
74
        $this->assertEquals($expected, $service->cancel('ABCDEF'));
75
    }
76
77
    public function testChargeShouldDoReturnChargeResponse()
78
    {
79
        $charge = $this->createMock(Charge::class);
80
81
        $request  = '<?xml version="1.0" encoding="UTF-8"?><payment/>';
82
        $xmlRequest = new SimpleXMLElement($request);
83
        $charge
84
            ->expects($this->once())
85
            ->method('xmlSerialize')
86
            ->willReturn($xmlRequest);
87
88
        $wsUrl = 'https://ws.test.com/v2/transactions?token=zzzzz';
89
        $this->credentials
90
            ->expects($this->once())
91
            ->method('getWsUrl')
92
            ->with('/v2/pre-approvals/payment')
93
            ->willReturn($wsUrl);
94
95
        $response  = '<?xml version="1.0" encoding="UTF-8"?><transaction>';
96
        $response .= '<transactionCode>123456</transactionCode><date>2015-11-19T11:33:54.000-03:00</date>';
97
        $response .= '</transaction>';
98
        $xmlResponse = new SimpleXMLElement($response);
99
        $this->client
100
            ->expects($this->once())
101
            ->method('post')
102
            ->with($wsUrl, $xmlRequest)
103
            ->willReturn($xmlResponse);
104
105
        $service = new SubscriptionService($this->credentials, $this->client);
106
107
        $expected = new ChargeResponse('123456', new DateTime('2015-11-19T11:33:54.000-03:00'));
108
        $this->assertEquals($expected, $service->charge($charge));
109
    }
110
}
111