Completed
Pull Request — develop (#48)
by Luís
02:58 queued 50s
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
        $this->serializer  = $this->createMock(ChargeSerializer::class);
36
    }
37
38
    public function testConstructShouldSettersDecoder()
39
    {
40
        $service = new SubscriptionService($this->credentials, $this->client, $this->serializer);
41
42
        $this->assertInstanceOf(SubscriptionServiceInterface::class, $service);
43
        $this->assertInstanceOf(Service::class, $service);
44
        $this->assertAttributeSame($this->serializer, 'serializer', $service);
45
    }
46
47
    public function testCreateChargeBuilderShouldReturnObjectBuilder()
48
    {
49
        $service = new SubscriptionService($this->credentials, $this->client, $this->serializer);
50
51
        $this->assertEquals(new ChargeBuilder('ABCDEF'), $service->createChargeBuilder('ABCDEF'));
52
    }
53
54
    public function testCancelShouldDoReturnCancellationResponse()
55
    {
56
        $wsUrl = 'https://ws.test.com/v2/transactions?token=zzzzz';
57
        $this->credentials
58
            ->expects($this->once())
59
            ->method('getWsUrl')
60
            ->with('/v2/pre-approvals/cancel/ABCDEF', [])
61
            ->willReturn($wsUrl);
62
63
        $response  = '<?xml version="1.0" encoding="UTF-8"?><transaction>';
64
        $response .= '<status>6</status><date>2015-11-19T11:33:54.000-03:00</date>';
65
        $response .= '</transaction>';
66
        $xmlResponse = new SimpleXMLElement($response);
67
        $this->client
68
            ->expects($this->once())
69
            ->method('get')
70
            ->with($wsUrl)
71
            ->willReturn($xmlResponse);
72
73
        $service = new SubscriptionService($this->credentials, $this->client, $this->serializer);
74
75
        $expected = new CancellationResponse('6', new DateTime('2015-11-19T11:33:54.000-03:00'));
76
        $this->assertEquals($expected, $service->cancel('ABCDEF'));
77
    }
78
79
    public function testChargeShouldDoReturnChargeResponse()
80
    {
81
        $charge = $this->createMock(Charge::class);
82
83
        $request  = '<?xml version="1.0" encoding="UTF-8"?><payment/>';
84
        $xmlRequest = new SimpleXMLElement($request);
85
        $this->serializer
86
            ->expects($this->once())
87
            ->method('serialize')
88
            ->with($charge)
89
            ->willReturn($xmlRequest);
90
91
        $wsUrl = 'https://ws.test.com/v2/transactions?token=zzzzz';
92
        $this->credentials
93
            ->expects($this->once())
94
            ->method('getWsUrl')
95
            ->with('/v2/pre-approvals/payment')
96
            ->willReturn($wsUrl);
97
98
        $response  = '<?xml version="1.0" encoding="UTF-8"?><transaction>';
99
        $response .= '<transactionCode>123456</transactionCode><date>2015-11-19T11:33:54.000-03:00</date>';
100
        $response .= '</transaction>';
101
        $xmlResponse = new SimpleXMLElement($response);
102
        $this->client
103
            ->expects($this->once())
104
            ->method('post')
105
            ->with($wsUrl, $xmlRequest)
106
            ->willReturn($xmlResponse);
107
108
        $service = new SubscriptionService($this->credentials, $this->client, $this->serializer);
109
110
        $expected = new ChargeResponse('123456', new DateTime('2015-11-19T11:33:54.000-03:00'));
111
        $this->assertEquals($expected, $service->charge($charge));
112
    }
113
}
114