Passed
Push — master ( 33f609...c06473 )
by Radosław
02:27
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 View Code Duplication
    public function testLoginEncFlow()
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...
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']);
0 ignored issues
show
Documentation Bug introduced by
The method doSomethingAfterLogin does not exist on object<Radowoj\Yaah\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75
    }
76
77
78 View Code Duplication
    public function testLoginFlow()
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...
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']);
0 ignored issues
show
Documentation Bug introduced by
The method doSomethingAfterLogin does not exist on object<Radowoj\Yaah\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
113
    }
114
115
116
    /**
117
     * @expectedException Radowoj\Yaah\Exception
118
     * @expectedExceptionMessage Invalid WebAPI doLogin[Enc]() response
119
     */
120 View Code Duplication
    public function testExceptionOnInvalidLoginResponseMissingUserId()
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...
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 View Code Duplication
    public function testExceptionOnInvalidLoginResponseMissingSessionHandlePart()
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...
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();
0 ignored issues
show
Documentation Bug introduced by
The method doSomething does not exist on object<Radowoj\Yaah\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
223
    }
224
225
226
227
}
228