Completed
Pull Request — master (#4)
by Laurens
02:48
created

ClientTest::runClientSoapException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 22
rs 9.2
cc 1
eloc 18
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 Mockery\MockInterface;
9
use PHPUnit_Framework_TestCase;
10
use Werkspot\BingAdsApiBundle\Api\Client;
11
use Werkspot\BingAdsApiBundle\Api\Exceptions;
12
use Werkspot\BingAdsApiBundle\Api\Helper;
13
use Werkspot\BingAdsApiBundle\Guzzle\OauthTokenService;
14
use Werkspot\BingAdsApiBundle\Model\AccessToken;
15
use Werkspot\BingAdsApiBundle\Model\ApiDetails;
16
17
class ClientTest extends PHPUnit_Framework_TestCase
18
{
19
    const ACCESS_TOKEN = '2ec09aeccaf634d982eec793037e37fe';
20
    const REFRESH_TOKEN = '0c59f7e609b0cc467067e39d523116ce';
21
22
    /**
23
     * @dataProvider getTestSoapExceptionData
24
     *
25
     * @param int $code
26
     * @param string $exceptionClassName
27
     */
28
    public function testSoapExceptions($code, $exceptionClassName)
29
    {
30
        $this->expectException($exceptionClassName);
31
        $this->runClientSoapException($code);
32
    }
33
34
    public function getTestSoapExceptionData()
35
    {
36
        return [
37
            0 => [
38
                'errorCode' => 0,
39
                'exceptionClassName' => Exceptions\SoapInternalErrorException::class
40
            ],
41
            105 => [
42
                'errorCode' => 105,
43
                'exceptionClassName' => Exceptions\SoapInvalidCredentialsException::class,
44
            ],
45
            106 => [
46
                'errorCode' => 106,
47
                'exceptionClassName' => Exceptions\SoapUserIsNotAuthorizedException::class,
48
            ],
49
            2004 => [
50
                'errorCode' => 2004,
51
                'exceptionClassName' => Exceptions\SoapNoCompleteDataAvailableException::class,
52
            ],
53
            2100 => [
54
                'errorCode' => 2100,
55
                'exceptionClassName' => Exceptions\SoapReportingServiceInvalidReportIdException::class,
56
            ],
57
            9999 => [
58
                'errorCode' => 9999,
59
                'exceptionClassName' => Exceptions\SoapUnknownErrorException::class,
60
            ],
61
        ];
62
    }
63
64
    /**
65
     * @return MockInterface
66
     */
67
    private function runClientSoapException($code)
68
    {
69
        $clientProxyMock = Mockery::mock(ClientProxy::class);
70
        $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...
71
            ->shouldReceive('ConstructWithCredentials')
72
            ->andReturnSelf()
73
            ->once()
74
            ->shouldReceive('GetNamespace')
75
            ->andReturn('Namespace')
76
            ->shouldReceive('GetService')
77
            ->andThrow($this->generateSoapFault($code));
78
79
        $apiClient = $this->getApiClient(
80
            $this->getOauthTokenServiceMock(),
81
            new ApiDetails('refreshToken', 'clientId', 'clientSecret', 'redirectUri', 'devToken'),
82
            $clientProxyMock,
83
            new Helper\File(),
84
            new Helper\Csv(),
85
            $this->getTimeHelperMock()
86
        );
87
        $apiClient->get([], 'GeoLocationPerformanceReport', ReportTimePeriod::LastWeek);
88
    }
89
90
    /**
91
     * @return Mockery\MockInterface
92
     */
93 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...
94
    {
95
        $clientProxyMock = Mockery::mock(ClientProxy::class);
96
        $clientProxyMock
97
            ->shouldReceive('ConstructWithCredentials')
98
            ->andReturnSelf()
99
            ->once()
100
            ->shouldReceive('GetNamespace')
101
            ->between(1, 48)
102
            ->andReturn('Namespace')
103
            ->shouldReceive('GetService')
104
            ->between(2, 49)
105
            ->andReturnSelf()
106
            ->shouldReceive('SubmitGenerateReport')
107
            ->between(1, 48)
108
            ->andReturnSelf()
109
            ->shouldReceive('PollGenerateReport')
110
            ->between(1, 48)
111
            ->andReturnSelf();
112
113
        $status = new \stdClass();
114
        $status->Status = $reportStatus;
115
        $status->ReportDownloadUrl = 'http://example.com/download.zip';
116
117
        $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...
118
        $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...
119
120
        return $clientProxyMock;
121
    }
122
123
    /**
124
     * @return Mockery\MockInterface
125
     */
126 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...
127
    {
128
        $oauthTokenServiceMock = Mockery::mock(OauthTokenService::class);
129
        $oauthTokenServiceMock
130
            ->shouldReceive('refreshToken')
131
            ->with('clientId', 'clientSecret', 'redirectUri', AccessToken::class)
132
            ->once()
133
            ->andReturn(new AccessToken(self::ACCESS_TOKEN, self::REFRESH_TOKEN));
134
135
        return $oauthTokenServiceMock;
136
    }
137
138
    private function getTimeHelperMock()
139
    {
140
        $timeHelperMock = Mockery::mock(Helper\Time::class);
141
        $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...
142
143
        return $timeHelperMock;
144
    }
145
146
    /**
147
     * @param OauthTokenService $requestNewAccessToken
148
     * @param ClientProxy $clientProxy
149
     * @param Helper\File $fileHelper
150
     * @param Helper\Csv $csvHelper
151
     * @param Helper\Time $timeHelper
152
     *
153
     * @return Client
154
     */
155 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...
156
    {
157
        $apiClient = new Client($requestNewAccessToken, $apiDetails, $clientProxy, $fileHelper, $csvHelper, $timeHelper);
158
        $apiClient->setConfig(['cache_dir' => '/tmp']);
159
160
        return $apiClient;
161
    }
162
163 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...
164
    {
165
        $message = "an error message {$code}";
166
        $error = new \stdClass();
167
        $error->Code = $code;
168
        $error->Message = $message;
169
        $exception = new \SoapFault('Server', '');
170
        $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...
171
        $exception->detail->AdApiFaultDetail = new \stdClass();
172
        $exception->detail->AdApiFaultDetail->Errors = new \stdClass();
173
        $exception->detail->AdApiFaultDetail->Errors->AdApiError = [$error];
174
175
        return $exception;
176
    }
177
}
178