Completed
Pull Request — master (#4)
by Laurens
05:13
created

ClientTest::getClientProxyMock()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 24

Duplication

Lines 29
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 29
loc 29
rs 8.8571
cc 1
eloc 24
nc 1
nop 1
1
<?php
2
3
namespace Test\Werkspot\BingAdsApiBundle\Api;
4
5
use BingAds\Bulk\ReportTimePeriod;
6
use BingAds\Proxy\ClientProxy;
7
use Mockery;
8
use PHPUnit_Framework_TestCase;
9
use Werkspot\BingAdsApiBundle\Api\Client;
10
use Werkspot\BingAdsApiBundle\Api\Exceptions;
11
use Werkspot\BingAdsApiBundle\Api\Helper;
12
use Werkspot\BingAdsApiBundle\Guzzle\OauthTokenService;
13
use Werkspot\BingAdsApiBundle\Model\AccessToken;
14
use Werkspot\BingAdsApiBundle\Model\ApiDetails;
15
16
class ClientTest extends PHPUnit_Framework_TestCase
17
{
18
    const ACCESS_TOKEN = '2ec09aeccaf634d982eec793037e37fe';
19
    const REFRESH_TOKEN = '0c59f7e609b0cc467067e39d523116ce';
20
21
    /**
22
     * @dataProvider getTestSoapExceptionData
23
     *
24
     * @param int $code
25
     * @param string $exceptionClassName
26
     */
27
    public function testSoapExceptions($code, $exceptionClassName)
28
    {
29
        $this->expectException($exceptionClassName);
30
        $this->runClientSoapException($code);
31
    }
32
33
    public function getTestSoapExceptionData()
34
    {
35
        return [
36
            0 => [
37
                'errorCode' => 0,
38
                'exceptionClassName' => Exceptions\SoapInternalErrorException::class
39
            ],
40
            105 => [
41
                'errorCode' => 105,
42
                'exceptionClassName' => Exceptions\SoapInvalidCredentialsException::class,
43
            ],
44
            106 => [
45
                'errorCode' => 106,
46
                'exceptionClassName' => Exceptions\SoapUserIsNotAuthorizedException::class,
47
            ],
48
            2004 => [
49
                'errorCode' => 2004,
50
                'exceptionClassName' => Exceptions\SoapNoCompleteDataAvailableException::class,
51
            ],
52
            2100 => [
53
                'errorCode' => 2100,
54
                'exceptionClassName' => Exceptions\SoapReportingServiceInvalidReportIdException::class,
55
            ],
56
            9999 => [
57
                'errorCode' => 9999,
58
                'exceptionClassName' => Exceptions\SoapUnknownErrorException::class,
59
            ],
60
        ];
61
    }
62
63
    /**
64
     * @return Mockery\MockInterface
65
     */
66
    private function runClientSoapException($code)
67
    {
68
        $clientProxyMock = Mockery::mock(ClientProxy::class);
69
        $clientProxyMock
1 ignored issue
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
            ->shouldReceive('ConstructWithCredentials')
71
            ->andReturnSelf()
72
            ->once()
73
            ->shouldReceive('GetNamespace')
74
            ->andReturn('Namespace')
75
            ->shouldReceive('GetService')
76
            ->andThrow($this->generateSoapFault($code));
77
78
        $apiClient = $this->getApiClient(
79
            $this->getOauthTokenServiceMock(),
80
            new ApiDetails('refreshToken', 'clientId', 'clientSecret', 'redirectUri', 'devToken'),
81
            $clientProxyMock,
82
            new Helper\File(),
83
            new Helper\Csv(),
84
            $this->getTimeHelperMock()
85
        );
86
        $apiClient->get([], 'GeoLocationPerformanceReport', ReportTimePeriod::LastWeek);
87
    }
88
89
    /**
90
     * @return Mockery\MockInterface
91
     */
92 View Code Duplication
    private function getClientProxyMock($reportStatus = 'Success')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        $clientProxyMock = Mockery::mock(ClientProxy::class);
95
        $clientProxyMock
96
            ->shouldReceive('ConstructWithCredentials')
97
            ->andReturnSelf()
98
            ->once()
99
            ->shouldReceive('GetNamespace')
100
            ->between(1, 48)
101
            ->andReturn('Namespace')
102
            ->shouldReceive('GetService')
103
            ->between(2, 49)
104
            ->andReturnSelf()
105
            ->shouldReceive('SubmitGenerateReport')
106
            ->between(1, 48)
107
            ->andReturnSelf()
108
            ->shouldReceive('PollGenerateReport')
109
            ->between(1, 48)
110
            ->andReturnSelf();
111
112
        $status = new \stdClass();
113
        $status->Status = $reportStatus;
114
        $status->ReportDownloadUrl = 'http://example.com/download.zip';
115
116
        $clientProxyMock->ReportRequestId = 'reportID';
1 ignored issue
show
Bug introduced by
Accessing ReportRequestId on the interface Mockery\MockInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
117
        $clientProxyMock->ReportRequestStatus = $status;
1 ignored issue
show
Bug introduced by
Accessing ReportRequestStatus on the interface Mockery\MockInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
118
119
        return $clientProxyMock;
120
    }
121
122
    /**
123
     * @return Mockery\MockInterface
124
     */
125 View Code Duplication
    private function getOauthTokenServiceMock()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        $oauthTokenServiceMock = Mockery::mock(OauthTokenService::class);
128
        $oauthTokenServiceMock
129
            ->shouldReceive('refreshToken')
130
            ->with('clientId', 'clientSecret', 'redirectUri', AccessToken::class)
131
            ->once()
132
            ->andReturn(new AccessToken(self::ACCESS_TOKEN, self::REFRESH_TOKEN));
133
134
        return $oauthTokenServiceMock;
135
    }
136
137
    private function getTimeHelperMock()
138
    {
139
        $timeHelperMock = Mockery::mock(Helper\Time::class);
140
        $timeHelperMock->shouldReceive('sleep')->andReturnNull();
1 ignored issue
show
Unused Code introduced by
The call to the method Mockery\Expectation::andReturnNull() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
141
142
        return $timeHelperMock;
143
    }
144
145
    /**
146
     * @param OauthTokenService $requestNewAccessToken
147
     * @param ClientProxy $clientProxy
148
     * @param Helper\File $fileHelper
149
     * @param Helper\Csv $csvHelper
150
     * @param Helper\Time $timeHelper
151
     *
152
     * @return Client
153
     */
154 View Code Duplication
    private function getApiClient(OauthTokenService $requestNewAccessToken, ApiDetails $apiDetails, ClientProxy $clientProxy, Helper\File $fileHelper, Helper\Csv $csvHelper, Helper\Time $timeHelper)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        $apiClient = new Client($requestNewAccessToken, $apiDetails, $clientProxy, $fileHelper, $csvHelper, $timeHelper);
157
        $apiClient->setConfig(['cache_dir' => '/tmp']);
158
159
        return $apiClient;
160
    }
161
162 View Code Duplication
    private function generateSoapFault($code)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164
        $message = "an error message {$code}";
165
        $error = new \stdClass();
166
        $error->Code = $code;
167
        $error->Message = $message;
168
        $exception = new \SoapFault('Server', '');
169
        $exception->detail = new \stdClass();
1 ignored issue
show
Bug introduced by
The property detail does not seem to exist in SoapFault.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
170
        $exception->detail->AdApiFaultDetail = new \stdClass();
171
        $exception->detail->AdApiFaultDetail->Errors = new \stdClass();
172
        $exception->detail->AdApiFaultDetail->Errors->AdApiError = [$error];
173
174
        return $exception;
175
    }
176
}
177