Passed
Push — master ( fa975d...67ba18 )
by Radosław
02:17
created

ClientTest::testExceptionRethrownFromWebapi()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Radowoj\Yaah;
4
5
use PHPUnit\Framework\TestCase;
6
use Radowoj\Yaah\Config;
7
use Radowoj\Yaah\Client;
8
9
use SoapClient;
10
use SoapFault;
11
12
class ClientTest extends TestCase
13
{
14
15
    protected $defaultConfigParams = [
16
        'apiKey' => 'some api key',
17
        'login' => 'someLogin',
18
        'passwordHash' => 'passwordHash',
19
        'isSandbox' => true,
20
        'countryCode' => 1,
21
    ];
22
23
24
    protected function getConfig()
25
    {
26
        return new Config($this->defaultConfigParams);
27
    }
28
29
    protected function getPlainPasswordConfig()
30
    {
31
        $params = $this->defaultConfigParams;
32
        $params['passwordHash'] = '';
33
34
        $config = new Config($params);
35
        $config->setPasswordPlain('somePasswordPlain');
36
        return $config;
37
    }
38
39
40
    public function testLoginEncFlow()
41
    {
42
        $config = $this->getConfig();
43
44
        $soapClient = $this->getMockBuilder(SoapClient::class)
45
            ->disableOriginalConstructor()
46
            ->setMethods(['doLoginEnc', 'doQuerySysStatus', 'doSomethingAfterLogin'])
47
            ->getMock();
48
49
        //Allegro version key should be requested for first login per request
50
        $soapClient->expects($this->once())
51
            ->method('doQuerySysStatus')
52
            ->willReturn((object)['verKey' => 'someVersionKey']);
53
54
        //login
55
        $soapClient->expects($this->once())
56
            ->method('doLoginEnc')
57
            ->willReturn((object)['sessionHandlePart' => 'foo', 'userId' => 'bar']);
58
59
        $client = new Client($config, $soapClient);
60
61
        //check session handle
62
        $soapClient->expects($this->once())
63
            ->method('doSomethingAfterLogin')
64
            ->with($this->equalTo([
65
                'webapiKey' => $this->defaultConfigParams['apiKey'],
66
                'localVersion' => 'someVersionKey',
67
                'countryId' => $this->defaultConfigParams['countryCode'],
68
                'sessionId' => 'foo',
69
                'countryCode' => $this->defaultConfigParams['countryCode'],
70
                'sessionHandle' => 'foo',
71
                'someFakeRequestParam' => 'someFakeRequestValue'
72
            ]));
73
74
        $client->doSomethingAfterLogin(['someFakeRequestParam' => 'someFakeRequestValue']);
75
    }
76
77
78
    public function testLoginFlow()
79
    {
80
        $config = $this->getPlainPasswordConfig();
81
82
        $soapClient = $this->getMockBuilder(SoapClient::class)
83
            ->disableOriginalConstructor()
84
            ->setMethods(['doLogin', 'doQuerySysStatus', 'doSomethingAfterLogin'])
85
            ->getMock();
86
87
        //Allegro version key should be requested for first login per request
88
        $soapClient->expects($this->once())
89
            ->method('doQuerySysStatus')
90
            ->willReturn((object)['verKey' => 'someVersionKey']);
91
92
        //login
93
        $soapClient->expects($this->once())
94
            ->method('doLogin')
95
            ->willReturn((object)['sessionHandlePart' => 'foo', 'userId' => 'bar']);
96
97
        $client = new Client($config, $soapClient);
98
99
        //check session handle
100
        $soapClient->expects($this->once())
101
            ->method('doSomethingAfterLogin')
102
            ->with($this->equalTo([
103
                'webapiKey' => $this->defaultConfigParams['apiKey'],
104
                'localVersion' => 'someVersionKey',
105
                'countryId' => $this->defaultConfigParams['countryCode'],
106
                'sessionId' => 'foo',
107
                'countryCode' => $this->defaultConfigParams['countryCode'],
108
                'sessionHandle' => 'foo',
109
                'someFakeRequestParam' => 'someFakeRequestValue'
110
            ]));
111
112
        $client->doSomethingAfterLogin(['someFakeRequestParam' => 'someFakeRequestValue']);
113
    }
114
115
116
    /**
117
     * @expectedException Radowoj\Yaah\Exception
118
     * @expectedExceptionMessage Invalid WebAPI doLogin[Enc]() response
119
     */
120
    public function testExceptionOnInvalidLoginResponseMissingUserId()
121
    {
122
        $config = $this->getConfig();
123
124
        $soapClient = $this->getMockBuilder(SoapClient::class)
125
            ->disableOriginalConstructor()
126
            ->setMethods(['doLoginEnc', 'doQuerySysStatus', 'doSomethingAfterLogin'])
127
            ->getMock();
128
129
        //Allegro version key should be requested for first login per request
130
        $soapClient->expects($this->once())
131
            ->method('doQuerySysStatus')
132
            ->willReturn((object)['verKey' => 'someVersionKey']);
133
134
        //login
135
        $soapClient->expects($this->once())
136
            ->method('doLoginEnc')
137
            ->willReturn((object)['sessionHandlePart' => 'foo']);
138
139
        $client = new Client($config, $soapClient);
140
        $client->login();
141
    }
142
143
144
    /**
145
     * @expectedException Radowoj\Yaah\Exception
146
     * @expectedExceptionMessage Invalid WebAPI doLogin[Enc]() response
147
     */
148
    public function testExceptionOnInvalidLoginResponseMissingSessionHandlePart()
149
    {
150
        $config = $this->getConfig();
151
152
        $soapClient = $this->getMockBuilder(SoapClient::class)
153
            ->disableOriginalConstructor()
154
            ->setMethods(['doLoginEnc', 'doQuerySysStatus', 'doSomethingAfterLogin'])
155
            ->getMock();
156
157
        //Allegro version key should be requested for first login per request
158
        $soapClient->expects($this->once())
159
            ->method('doQuerySysStatus')
160
            ->willReturn((object)['verKey' => 'someVersionKey']);
161
162
        //login
163
        $soapClient->expects($this->once())
164
            ->method('doLoginEnc')
165
            ->willReturn((object)['userId' => 'bar']);
166
167
        $client = new Client($config, $soapClient);
168
        $client->login();
169
    }
170
171
172
    /**
173
     * @expectedException Radowoj\Yaah\Exception
174
     * @expectedExceptionMessage Invalid WebAPI doQuerySysStatus() response
175
     */
176
    public function testExceptionOnInvalidQuerySysStatusResponse()
177
    {
178
        $config = $this->getConfig();
179
180
        $soapClient = $this->getMockBuilder(SoapClient::class)
181
            ->disableOriginalConstructor()
182
            ->setMethods(['doQuerySysStatus', 'doSomethingAfterLogin'])
183
            ->getMock();
184
185
        //Allegro version key should be requested for first login per request
186
        $soapClient->expects($this->once())
187
            ->method('doQuerySysStatus')
188
            ->willReturn((object)['trololo' => 'thisIsNotAVersionKey']);
189
190
        $client = new Client($config, $soapClient);
191
        $client->login();
192
    }
193
194
195
    /**
196
     * @expectedException Radowoj\Yaah\Exception
197
     * @expectedExceptionMessage Trying to call: doSomething(); WebAPI exception: someException
198
     */
199
    public function testExceptionRethrownFromWebapi()
200
    {
201
        $config = $this->getConfig();
202
203
        $soapClient = $this->getMockBuilder(SoapClient::class)
204
            ->disableOriginalConstructor()
205
            ->setMethods(['doQuerySysStatus', 'doSomethingAfterLogin', 'doSomething', 'doLoginEnc'])
206
            ->getMock();
207
208
        //Allegro version key should be requested for first login per request
209
        $soapClient->expects($this->once())
210
            ->method('doQuerySysStatus')
211
            ->willReturn((object)['verKey' => 'someVersionKey']);
212
213
        $soapClient->expects($this->once())
214
            ->method('doLoginEnc')
215
            ->willReturn((object)['userId' => 12312, 'sessionHandlePart' => 'someSessionHandle']);
216
217
        $soapClient->expects($this->once())
218
            ->method('doSomething')
219
            ->will($this->throwException(new SoapFault('code', 'someException')));
220
221
        $client = new Client($config, $soapClient);
222
        $client->doSomething();
223
    }
224
225
226
227
}
228