Completed
Pull Request — develop (#48)
by Luís
02:58 queued 50s
created

LocatorTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 59
Duplicated Lines 71.19 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 2
dl 42
loc 59
rs 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace PHPSC\PagSeguro\Purchases\Subscriptions;
3
4
use PHPSC\PagSeguro\Client\Client;
5
use PHPSC\PagSeguro\Credentials;
6
use PHPSC\PagSeguro\Purchases\NotificationService;
7
use PHPSC\PagSeguro\Purchases\SearchService;
8
use PHPSC\PagSeguro\Service;
9
use PHPSC\PagSeguro\Purchases\Transactions\Transaction;
10
use SimpleXMLElement;
11
12
/**
13
 * @author Renato Moura <[email protected]>
14
 */
15
class LocatorTest extends \PHPUnit_Framework_TestCase
16
{
17
    public function testConstructShouldSettersDecoder()
18
    {
19
        $credentials = $this->createMock(Credentials::class);
20
        $client      = $this->createMock(Client::class);
21
        $decoder     = $this->createMock(Decoder::class);
22
23
        $locator = new Locator($credentials, $client, $decoder);
24
25
        $this->assertInstanceOf(NotificationService::class, $locator);
26
        $this->assertInstanceOf(SearchService::class, $locator);
27
        $this->assertInstanceOf(Service::class, $locator);
28
        $this->assertAttributeSame($decoder, 'decoder', $locator);
29
    }
30
31
    public function testGetByCodeShouldDoAGetRequestAddingCredentialsData()
32
    {
33
        $credentials = $this->createMock(Credentials::class);
34
        $client      = $this->createMock(CLient::class);
35
        $decoder     = $this->createMock(Decoder::class);
36
37
        $locator = new Locator($credentials, $client, $decoder);
38
39
        $wsUrl = 'https://ws.test.com/v2/transactions?token=zzzzz';
40
41
        $credentials->expects($this->once())
42
                    ->method('getWsUrl')
43
                    ->with('/v2/pre-approvals/123456', [])
44
                    ->willReturn($wsUrl);
45
46
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><transaction/>');
47
        $client->expects($this->once())->method('get')->with($wsUrl)->willReturn($xml);
48
49
        $transaction = $this->createMock(Transaction::class);
50
        $decoder->expects($this->once())->method('decode')->with($xml)->willReturn($transaction);
51
52
        $this->assertEquals($transaction, $locator->getByCode('123456'));
53
    }
54
55
    public function testGetByNotificationShouldDoAGetRequestAddingCredentialsData()
56
    {
57
        $credentials = $this->createMock(Credentials::class);
58
        $client      = $this->createMock(Client::class);
59
        $decoder     = $this->createMock(Decoder::class);
60
61
        $locator = new Locator($credentials, $client, $decoder);
62
63
        $wsUrl = 'https://ws.test.com/v2/transactions?token=xxxxx';
64
        $credentials->expects($this->once())
65
            ->method('getWsUrl')
66
            ->with('/v2/pre-approvals/notifications/abcd', [])
67
            ->willReturn($wsUrl);
68
69
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><transaction/>');
70
        $client->expects($this->once())->method('get')->with($wsUrl)->willReturn($xml);
71
72
        $transaction = $this->createMock(Transaction::class);
73
74
        $decoder->expects($this->once())
75
                ->method('decode')
76
                ->with($xml)
77
                ->willReturn($transaction);
78
79
        $this->assertEquals($transaction, $locator->getByNotification('abcd'));
80
    }
81
}
82