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

ClientTest::generateSoapFault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 14
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 14
loc 14
rs 9.4285
cc 1
eloc 11
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 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...
94
    {
95
        $oauthTokenServiceMock = Mockery::mock(OauthTokenService::class);
96
        $oauthTokenServiceMock
97
            ->shouldReceive('refreshToken')
98
            ->with('clientId', 'clientSecret', 'redirectUri', AccessToken::class)
99
            ->once()
100
            ->andReturn(new AccessToken(self::ACCESS_TOKEN, self::REFRESH_TOKEN));
101
102
        return $oauthTokenServiceMock;
103
    }
104
105
    private function getTimeHelperMock()
106
    {
107
        $timeHelperMock = Mockery::mock(Helper\Time::class);
108
        $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...
109
110
        return $timeHelperMock;
111
    }
112
113
    /**
114
     * @param OauthTokenService $requestNewAccessToken
115
     * @param ClientProxy $clientProxy
116
     * @param Helper\File $fileHelper
117
     * @param Helper\Csv $csvHelper
118
     * @param Helper\Time $timeHelper
119
     *
120
     * @return Client
121
     */
122 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...
123
    {
124
        $apiClient = new Client($requestNewAccessToken, $apiDetails, $clientProxy, $fileHelper, $csvHelper, $timeHelper);
125
        $apiClient->setConfig(['cache_dir' => '/tmp']);
126
127
        return $apiClient;
128
    }
129
130 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...
131
    {
132
        $message = "an error message {$code}";
133
        $error = new \stdClass();
134
        $error->Code = $code;
135
        $error->Message = $message;
136
        $exception = new \SoapFault('Server', '');
137
        $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...
138
        $exception->detail->AdApiFaultDetail = new \stdClass();
139
        $exception->detail->AdApiFaultDetail->Errors = new \stdClass();
140
        $exception->detail->AdApiFaultDetail->Errors->AdApiError = [$error];
141
142
        return $exception;
143
    }
144
}
145