Passed
Push — master ( 51d123...d86252 )
by Laurens
01:30
created

ClientTest::getTestSoapExceptionData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
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
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();
109
110
        return $timeHelperMock;
111
    }
112
113
    /**
114
     * @param OauthTokenService $oauthTokenService
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 $oauthTokenService, 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($oauthTokenService, $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();
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