Completed
Pull Request — master (#18)
by Harry
04:11
created

testSettingDataCenterToAuWillCallAuUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 19
Ratio 100 %

Importance

Changes 5
Bugs 1 Features 2
Metric Value
c 5
b 1
f 2
dl 19
loc 19
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Test\Unit;
15
16
use Exception;
17
use Graze\Gigya\Auth\GigyaAuthInterface;
18
use Graze\Gigya\Gigya;
19
use Graze\Gigya\Response\ResponseInterface;
20
use Graze\Gigya\Test\TestCase;
21
use Graze\Gigya\Test\TestFixtures;
22
use Graze\Gigya\Validation\ResponseValidatorInterface;
23
use Graze\Gigya\Validation\ValidGigyaResponseSubscriber;
24
use GuzzleHttp\Event\EmitterInterface;
25
use GuzzleHttp\Event\SubscriberInterface;
26
use Mockery as m;
27
use Mockery\MockInterface;
28
29
/**
30
 * @runTestsInSeparateProcesses
31
 * @preserveGlobalState disabled
32
 */
33
class GigyaTest extends TestCase
34
{
35
    /**
36
     * @var MockInterface|\GuzzleHttp\Client
37
     */
38
    private $guzzleClient;
39
40
    /**
41
     * @var MockInterface|\Graze\Gigya\Response\ResponseFactoryInterface
42
     */
43
    private $factory;
44
45
    /**
46
     * @var string
47
     */
48
    private $certPath;
49
50
    /**
51
     * @var EmitterInterface|MockInterface
52
     */
53
    private $emitter;
54
55
    public function setUp()
56
    {
57
        $this->guzzleClient = m::mock('overload:GuzzleHttp\Client');
58
        $this->emitter = m::mock(EmitterInterface::class);
59
        $this->guzzleClient->shouldReceive('getEmitter')
60
                           ->andReturn($this->emitter);
61
        $this->factory = m::mock('Graze\Gigya\Response\ResponseFactoryInterface');
62
        $this->certPath = realpath(__DIR__ . '/../../src/' . Gigya::CERTIFICATE_FILE);
63
    }
64
65
    public function tearDown()
66
    {
67
        $this->guzzleClient = $this->factory = null;
68
    }
69
70
    /**
71
     * @param string      $key
72
     * @param string      $secret
73
     * @param string      $dc
74
     * @param string|null $userKey
75
     *
76
     * @return Gigya
77
     */
78
    public function createClient($key = 'key', $secret = 'secret', $dc = Gigya::DC_EU, $userKey = null)
79
    {
80
        $options = [
81
            'uidValidator' => false,
82
            'factory'      => $this->factory,
83
        ];
84
85
        return new Gigya($key, $secret, $dc, $userKey, $options);
86
    }
87
88
    /**
89
     * Setup the subscribers
90
     *
91
     * @param string      $key
92
     * @param string      $secret
93
     * @param string|null $userKey
94
     */
95
    private function setupSubscribers($key, $secret, $userKey = null)
96
    {
97
        $this->emitter->shouldReceive('attach')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in GuzzleHttp\Event\EmitterInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
98
                      ->with(m::type(ValidGigyaResponseSubscriber::class))
99
                      ->once();
100
        $this->emitter->shouldReceive('attach')
101
                      ->with(m::on(function (SubscriberInterface $subscriber) use ($key, $secret, $userKey) {
102
                        if ($subscriber instanceof GigyaAuthInterface) {
103
                            static::assertEquals($key, $subscriber->getApiKey());
104
                            static::assertEquals($secret, $subscriber->getSecret());
105
                            static::assertEquals($userKey, $subscriber->getUserKey());
106
                        }
107
108
                        return true;
109
                      }))
110
                      ->once();
111
    }
112
113
    /**
114
     * @param string      $fixtureName
115
     * @param string      $uri
116
     * @param array       $getOptions
117
     * @param string      $key
118
     * @param string      $secret
119
     * @param string|null $userKey
120
     *
121
     * @return ResponseInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be MockInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
122
     */
123
    private function setupCall($fixtureName, $uri, array $getOptions, $key, $secret, $userKey = null)
124
    {
125
        $this->setupSubscribers($key, $secret, $userKey);
126
127
        $response = m::mock('GuzzleHttp\Message\ResponseInterface');
128
        $response->shouldReceive('getBody')->andReturn(TestFixtures::getFixture($fixtureName));
129
130
        $this->guzzleClient->shouldReceive('get')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in GuzzleHttp\Client.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
131
                           ->with(
132
                               $uri,
133
                               $getOptions
134
                           )
135
                           ->andReturn($response);
136
137
        $gigyaResponse = m::mock('Graze\Gigya\Response\ResponseInterface');
138
139
        $this->factory->shouldReceive('getResponse')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Gigya\Response\ResponseFactoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
140
                      ->with($response)
141
                      ->andReturn($gigyaResponse);
142
143
        return $gigyaResponse;
144
    }
145
146
    public function testConstructorConfig()
147
    {
148
        $this->guzzleClient->shouldReceive('__construct')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in GuzzleHttp\Client.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
149
                           ->once()
150
                           ->with([
151
                               'emitter' => $this->emitter,
152
                           ])
153
                           ->andReturn($this->guzzleClient);
154
155
        $this->emitter->shouldReceive('attach')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in GuzzleHttp\Event\EmitterInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
156
                      ->with(m::type(ValidGigyaResponseSubscriber::class))
157
                      ->once();
158
159
        $response = m::mock('GuzzleHttp\Message\ResponseInterface');
160
        $response->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('account.getAccountInfo'));
161
162
        $this->guzzleClient->shouldReceive('get')
163
                           ->with(
164
                               'https://accounts.au1.gigya.com/accounts.getAccountInfo',
165
                               [
166
                                   'cert'   => 'some_cert.pem',
167
                                   'auth'   => 'oauth',
168
                                   'verify' => $this->certPath,
169
                                   'query'  => [],
170
                               ]
171
                           )
172
                           ->andReturn($response);
173
174
        $gigyaResponse = m::mock('Graze\Gigya\Response\ResponseInterface');
175
176
        $this->factory->shouldReceive('getResponse')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Gigya\Response\ResponseFactoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
177
                      ->with($response)
178
                      ->andReturn($gigyaResponse);
179
180
        $config = [
181
            'auth'         => 'oauth',
182
            'uidValidator' => false,
183
            'factory'      => $this->factory,
184
            'guzzle'       => [
185
                'emitter' => $this->emitter,
186
            ],
187
            'options'      => [
188
                'cert' => 'some_cert.pem',
189
            ],
190
        ];
191
        $client = new Gigya('key', 'secret', Gigya::DC_AU, null, $config);
192
193
        static::assertSame($gigyaResponse, $client->accounts()->getAccountInfo());
194
    }
195
196
    public function testSettingKeyAndSecretWillPassToGuzzleClient()
197
    {
198
        $key = 'key' . rand(1, 1000);
199
        $secret = 'secret' . rand(1001, 2000002);
200
201
        $gigyaResponse = $this->setupCall(
202
            'accounts.getAccountInfo',
203
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
204
            [
205
                'auth'   => 'gigya',
206
                'verify' => $this->certPath,
207
                'query'  => [],
208
            ],
209
            $key,
210
            $secret
211
        );
212
        $client = $this->createClient($key, $secret, Gigya::DC_EU, null);
213
        $client->setFactory($this->factory);
214
215
        $result = $client->accounts()->getAccountInfo([]);
216
217
        static::assertSame($gigyaResponse, $result);
218
    }
219
220 View Code Duplication
    public function testSettingDataCenterToAuWillCallAuUri()
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...
221
    {
222
        $gigyaResponse = $this->setupCall(
223
            'accounts.getAccountInfo',
224
            'https://accounts.au1.gigya.com/accounts.getAccountInfo',
225
            [
226
                'auth'   => 'gigya',
227
                'verify' => $this->certPath,
228
                'query'  => [],
229
            ],
230
            'key',
231
            'secret'
232
        );
233
        $client = $this->createClient('key', 'secret', Gigya::DC_AU);
234
235
        $result = $client->accounts()->getAccountInfo([]);
236
237
        static::assertSame($gigyaResponse, $result);
238
    }
239
240 View Code Duplication
    public function testSettingDataCenterToUsWillCallUsUri()
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...
241
    {
242
        $gigyaResponse = $this->setupCall(
243
            'accounts.getAccountInfo',
244
            'https://accounts.us1.gigya.com/accounts.getAccountInfo',
245
            [
246
                'auth'   => 'gigya',
247
                'verify' => $this->certPath,
248
                'query'  => [],
249
            ],
250
            'key',
251
            'secret'
252
        );
253
        $client = $this->createClient('key', 'secret', Gigya::DC_US);
254
255
        $result = $client->accounts()->getAccountInfo([]);
256
257
        static::assertSame($gigyaResponse, $result);
258
    }
259
260 View Code Duplication
    public function testSettingTheUserKeyWillPassItThroughToGuzzle()
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...
261
    {
262
        $gigyaResponse = $this->setupCall(
263
            'accounts.getAccountInfo',
264
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
265
            [
266
                'auth'   => 'gigya',
267
                'verify' => $this->certPath,
268
                'query'  => [],
269
            ],
270
            'key',
271
            'userSecret',
272
            'userKey'
273
        );
274
        $client = $this->createClient('key', 'userSecret', Gigya::DC_EU, 'userKey');
275
        $client->setFactory($this->factory);
276
277
        $result = $client->accounts()->getAccountInfo([]);
278
279
        static::assertSame($gigyaResponse, $result);
280
    }
281
282 View Code Duplication
    public function testPassingParamsThroughToTheMethodWillPassThroughToGuzzle()
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...
283
    {
284
        $gigyaResponse = $this->setupCall(
285
            'accounts.getAccountInfo',
286
            'https://socialize.eu1.gigya.com/socialize.notifyLogin',
287
            [
288
                'auth'   => 'gigya',
289
                'verify' => $this->certPath,
290
                'query'  => [
291
                    'param' => 'passedThrough',
292
                ],
293
            ],
294
            'key',
295
            'secret'
296
        );
297
        $client = $this->createClient();
298
299
        $result = $client->socialize()->notifyLogin(['param' => 'passedThrough']);
300
301
        static::assertSame($gigyaResponse, $result);
302
    }
303
304 View Code Duplication
    public function testCallingChildMethodsCallTheCorrectUri()
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...
305
    {
306
        $gigyaResponse = $this->setupCall(
307
            'accounts.getAccountInfo',
308
            'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig',
309
            [
310
                'auth'   => 'gigya',
311
                'verify' => $this->certPath,
312
                'query'  => [
313
                    'params' => 'passedThrough',
314
                ],
315
            ],
316
            'key',
317
            'secret'
318
        );
319
        $client = $this->createClient();
320
321
        $result = $client->saml()->idp()->getConfig(['params' => 'passedThrough']);
322
323
        static::assertSame($gigyaResponse, $result);
324
    }
325
326 View Code Duplication
    public function testTfaCallingChildMethodsCallTheCorrectUri()
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...
327
    {
328
        $gigyaResponse = $this->setupCall(
329
            'accounts.getAccountInfo',
330
            'https://accounts.eu1.gigya.com/accounts.tfa.getCertificate',
331
            [
332
                'auth'   => 'gigya',
333
                'verify' => $this->certPath,
334
                'query'  => [
335
                    'params' => 'passedThrough',
336
                ],
337
            ],
338
            'key',
339
            'secret'
340
        );
341
        $client = $this->createClient();
342
343
        $result = $client->accounts()->tfa()->getCertificate(['params' => 'passedThrough']);
344
345
        static::assertSame($gigyaResponse, $result);
346
    }
347
348
    /**
349
     * @dataProvider clientCallDataProvider
350
     *
351
     * @param string $namespace
352
     * @param string $method
353
     * @param string $expectedUri
354
     */
355
    public function testClientCalls($namespace, $method, $expectedUri)
356
    {
357
        $gigyaResponse = $this->setupCall(
358
            'accounts.getAccountInfo',
359
            $expectedUri,
360
            [
361
                'auth'   => 'gigya',
362
                'verify' => $this->certPath,
363
                'query'  => [
364
                    'params' => 'passedThrough',
365
                ],
366
            ],
367
            'key',
368
            'secret'
369
        );
370
        $client = $this->createClient();
371
372
        $result = $client->{$namespace}()->{$method}(['params' => 'passedThrough']);
373
374
        static::assertSame($gigyaResponse, $result);
375
    }
376
377
    public function testCallingMagicMethodWithArgumentsThrowsAnException()
378
    {
379
        static::setExpectedException(
380
            'BadMethodCallException',
381
            'No Arguments should be supplied for Gigya call'
382
        );
383
384
        $this->setupSubscribers('key', 'secret');
385
        $client = $this->createClient();
386
        $client->custom('params');
0 ignored issues
show
Documentation Bug introduced by
The method custom does not exist on object<Graze\Gigya\Gigya>? 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...
387
    }
388
389 View Code Duplication
    public function testAddingOptionsPassesThroughTheOptionsToGuzzle()
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...
390
    {
391
        $gigyaResponse = $this->setupCall(
392
            'accounts.getAccountInfo',
393
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
394
            [
395
                'auth'    => 'gigya',
396
                'verify'  => $this->certPath,
397
                'query'   => [
398
                    'params' => 'passedThrough',
399
                ],
400
                'option1' => 'value1',
401
                'option2' => false,
402
            ],
403
            'key',
404
            'secret'
405
        );
406
        $client = $this->createClient();
407
408
        $client->addOption('option1', 'value1');
409
        $client->addOption('option2', false);
410
411
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
412
413
        static::assertSame($gigyaResponse, $result);
414
    }
415
416 View Code Duplication
    public function testAddingOptionsWithASingleCallPassesThroughTheOptionsToGuzzle()
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...
417
    {
418
        $gigyaResponse = $this->setupCall(
419
            'accounts.getAccountInfo',
420
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
421
            [
422
                'auth'    => 'gigya',
423
                'verify'  => $this->certPath,
424
                'query'   => [
425
                    'params' => 'passedThrough',
426
                ],
427
                'option1' => 'value1',
428
                'option2' => true,
429
            ],
430
            'key',
431
            'secret'
432
        );
433
        $client = $this->createClient();
434
435
        $client->addOptions([
436
            'option1' => 'value1',
437
            'option2' => true,
438
        ]);
439
440
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
441
442
        static::assertSame($gigyaResponse, $result);
443
    }
444
445 View Code Duplication
    public function testAddingTheSameOptionAgainWillTakeTheLastValueSet()
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...
446
    {
447
        $gigyaResponse = $this->setupCall(
448
            'accounts.getAccountInfo',
449
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
450
            [
451
                'auth'    => 'gigya',
452
                'verify'  => $this->certPath,
453
                'query'   => [
454
                    'params' => 'passedThrough',
455
                ],
456
                'option1' => false,
457
            ],
458
            'key',
459
            'secret'
460
        );
461
        $client = $this->createClient();
462
463
        $client->addOption('option1', 'value1');
464
        $client->addOption('option1', false);
465
466
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
467
468
        static::assertSame($gigyaResponse, $result);
469
    }
470
471 View Code Duplication
    public function testAddingTheSameOptionAgainWithAddOptionsWillTakeTheLastValueSet()
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...
472
    {
473
        $gigyaResponse = $this->setupCall(
474
            'accounts.getAccountInfo',
475
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
476
            [
477
                'auth'    => 'gigya',
478
                'verify'  => $this->certPath,
479
                'query'   => [
480
                    'params' => 'passedThrough',
481
                ],
482
                'option1' => true,
483
            ],
484
            'key',
485
            'secret'
486
        );
487
        $client = $this->createClient();
488
489
        $client->addOption('option1', 'value1');
490
        $client->addOptions(['option1' => true]);
491
492
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
493
494
        static::assertSame($gigyaResponse, $result);
495
    }
496
497
    public function testAddingQueryOptionsWillBeIgnored()
498
    {
499
        $gigyaResponse = $this->setupCall(
500
            'accounts.getAccountInfo',
501
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
502
            [
503
                'auth'   => 'gigya',
504
                'verify' => 'notAFile',
505
                'query'  => [
506
                    'params' => 'passedThrough',
507
                ],
508
            ],
509
            'key',
510
            'secret'
511
        );
512
        $client = $this->createClient();
513
514
        $client->addOption('query', 'random');
515
        $client->addOption('verify', 'notAFile');
516
517
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
518
519
        static::assertSame($gigyaResponse, $result);
520
    }
521
522 View Code Duplication
    public function testSettingOptionsAsPartOfTheQuery()
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...
523
    {
524
        $gigyaResponse = $this->setupCall(
525
            'accounts.getAccountInfo',
526
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
527
            [
528
                'auth'   => 'gigya',
529
                'verify' => $this->certPath,
530
                'query'  => [
531
                    'params' => 'passedThrough',
532
                ],
533
                'custom' => 'value',
534
            ],
535
            'key',
536
            'secret'
537
        );
538
        $client = $this->createClient();
539
540
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough'], ['custom' => 'value']);
541
542
        static::assertSame($gigyaResponse, $result);
543
    }
544
545 View Code Duplication
    public function testSettingGlobalAndRequestOptionsTheRequestOptionsOverrideGlobalOptions()
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...
546
    {
547
        $gigyaResponse = $this->setupCall(
548
            'accounts.getAccountInfo',
549
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
550
            [
551
                'auth'   => 'gigya',
552
                'verify' => $this->certPath,
553
                'query'  => [
554
                    'params' => 'passedThrough',
555
                ],
556
                'custom' => 'value',
557
            ],
558
            'key',
559
            'secret'
560
        );
561
        $client = $this->createClient();
562
563
        $client->addOption('custom', 'notUsed');
564
565
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough'], ['custom' => 'value']);
566
567
        static::assertSame($gigyaResponse, $result);
568
    }
569
570
    public function testSettingRequestOptionsDoOverrideTheParams()
571
    {
572
        $gigyaResponse = $this->setupCall(
573
            'accounts.getAccountInfo',
574
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
575
            [
576
                'auth'   => 'gigya',
577
                'verify' => false,
578
                'query'  => [
579
                    'params' => 'passedThrough',
580
                ],
581
            ],
582
            'key',
583
            'secret'
584
        );
585
        $client = $this->createClient();
586
587
        $result = $client->accounts()->getAccountInfo(
588
            ['params' => 'passedThrough'],
589
            ['query' => 'value', 'verify' => false]
590
        );
591
592
        static::assertSame($gigyaResponse, $result);
593
    }
594
595 View Code Duplication
    public function testSettingParamsWillNotOverwriteTheDefaultParams()
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...
596
    {
597
        $gigyaResponse = $this->setupCall(
598
            'accounts.getAccountInfo',
599
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
600
            [
601
                'auth'   => 'gigya',
602
                'verify' => $this->certPath,
603
                'query'  => [
604
                    'secret' => 'newSecret',
605
                ],
606
            ],
607
            'key',
608
            'secret'
609
        );
610
        $client = $this->createClient();
611
612
        $result = $client->accounts()->getAccountInfo(
613
            ['secret' => 'newSecret']
614
        );
615
616
        static::assertSame($gigyaResponse, $result);
617
    }
618
619 View Code Duplication
    public function testCallingAChainWillCallThatChain()
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...
620
    {
621
        $gigyaResponse = $this->setupCall(
622
            'accounts.getAccountInfo',
623
            'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig',
624
            [
625
                'auth'   => 'gigya',
626
                'verify' => $this->certPath,
627
                'query'  => [
628
                    'secret' => 'newSecret',
629
                ],
630
            ],
631
            'key',
632
            'secret'
633
        );
634
        $client = $this->createClient();
635
636
        $result = $client->fidm()->{'saml.idp.getConfig'}(
0 ignored issues
show
Documentation Bug introduced by
The method fidm does not exist on object<Graze\Gigya\Gigya>? 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...
637
            ['secret' => 'newSecret']
638
        );
639
640
        static::assertSame($gigyaResponse, $result);
641
    }
642
643
    public function testWillCallAValidator()
644
    {
645
        $response = $this->setupCall(
646
            'accounts.getAccountInfo',
647
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
648
            [
649
                'auth'   => 'gigya',
650
                'verify' => $this->certPath,
651
                'query'  => [],
652
            ],
653
            'key',
654
            'secret'
655
        );
656
        $client = $this->createClient();
657
658
        $validator = m::mock(ResponseValidatorInterface::class);
659
        $client->addValidator($validator);
660
        $validator->shouldReceive('canValidate')
661
                  ->with($response)
662
                  ->andReturn(true);
663
        $validator->shouldReceive('assert')
664
                  ->with($response)
665
                  ->andReturn(true);
666
667
        static::assertSame($response, $client->accounts()->getAccountInfo());
668
    }
669
670
    public function tesWillCallMultipleValidators()
671
    {
672
        $response = $this->setupCall(
673
            'accounts.getAccountInfo',
674
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
675
            [
676
                'auth'   => 'gigya',
677
                'verify' => $this->certPath,
678
                'query'  => [],
679
            ],
680
            'key',
681
            'secret'
682
        );
683
        $client = $this->createClient();
684
685
        $validator = m::mock(ResponseValidatorInterface::class);
686
        $client->addValidator($validator);
687
        $validator->shouldReceive('canValidate')
688
                  ->with($response)
689
                  ->andReturn(true);
690
        $validator->shouldReceive('assert')
691
                  ->with($response)
692
                  ->andReturn(true);
693
        $validator2 = m::mock(ResponseValidatorInterface::class);
694
        $client->addValidator($validator2);
695
        $validator2->shouldReceive('canValidate')
696
                   ->with($response)
697
                   ->andReturn(true);
698
        $validator2->shouldReceive('assert')
699
                   ->with($response)
700
                   ->andReturn(true);
701
702
        static::assertSame($response, $client->accounts()->getAccountInfo());
703
    }
704
705
    public function testTheValidatorThrowingAnExceptionWillPassthrough()
706
    {
707
        $response = $this->setupCall(
708
            'accounts.getAccountInfo',
709
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
710
            [
711
                'auth'   => 'gigya',
712
                'verify' => $this->certPath,
713
                'query'  => [],
714
            ],
715
            'key',
716
            'secret'
717
        );
718
        $client = $this->createClient();
719
720
        $validator = m::mock(ResponseValidatorInterface::class);
721
        $validator2 = m::mock(ResponseValidatorInterface::class);
722
        $client->addValidator($validator);
723
        $client->addValidator($validator2);
724
        $exception = new Exception();
725
        $validator->shouldReceive('canValidate')
726
                  ->with($response)
727
                  ->andReturn(true);
728
        $validator->shouldReceive('assert')
729
                  ->with($response)
730
                  ->andThrow($exception);
731
732
        static::setExpectedException(Exception::class);
733
734
        $client->accounts()->getAccountInfo();
735
    }
736
737
    public function testTheValidatorWillOnlyCallAssertWhenItCanValidate()
738
    {
739
        $response = $this->setupCall(
740
            'accounts.getAccountInfo',
741
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
742
            [
743
                'auth'   => 'gigya',
744
                'verify' => $this->certPath,
745
                'query'  => [],
746
            ],
747
            'key',
748
            'secret'
749
        );
750
        $client = $this->createClient();
751
752
        $validator = m::mock(ResponseValidatorInterface::class);
753
        $validator2 = m::mock(ResponseValidatorInterface::class);
754
        $client->addValidator($validator);
755
        $client->addValidator($validator2);
756
        $validator->shouldReceive('canValidate')
757
                  ->with($response)
758
                  ->andReturn(true);
759
        $validator->shouldReceive('assert')
760
                  ->with($response)
761
                  ->andReturn(true);
762
        $validator2->shouldReceive('canValidate')
763
                   ->with($response)
764
                   ->andReturn(false);
765
766
        static::assertSame($response, $client->accounts()->getAccountInfo());
767
    }
768
769
    public function testRemoveSubscriberWillDetachFromEmitter()
770
    {
771
        $response = $this->setupCall(
772
            'accounts.getAccountInfo',
773
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
774
            [
775
                'auth'   => 'gigya',
776
                'verify' => $this->certPath,
777
                'query'  => [],
778
            ],
779
            'key',
780
            'secret'
781
        );
782
        $client = $this->createClient();
783
784
        static::assertSame($response, $client->accounts()->getAccountInfo());
785
786
        $subscriber = m::mock(SubscriberInterface::class);
787
        $this->emitter->shouldReceive('attach')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in GuzzleHttp\Event\EmitterInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
788
                      ->with($subscriber)
789
                      ->once();
790
791
        $client->addSubscriber($subscriber);
792
793
        $this->emitter->shouldReceive('detach')
794
                      ->with($subscriber)
795
                      ->once();
796
797
        $client->removeSubscriber($subscriber);
798
    }
799
800
    /**
801
     * @return array
802
     */
803
    public function clientCallDataProvider()
804
    {
805
        return [
806
            ['accounts', 'getAccountInfo', 'https://accounts.eu1.gigya.com/accounts.getAccountInfo'],
807
            ['accounts', 'tfa.getCertificate', 'https://accounts.eu1.gigya.com/accounts.tfa.getCertificate'],
808
            ['audit', 'search', 'https://audit.eu1.gigya.com/audit.search'],
809
            ['comments', 'analyzeMediaItem', 'https://comments.eu1.gigya.com/comments.analyzeMediaItem'],
810
            ['dataStore', 'get', 'https://ds.eu1.gigya.com/ds.get'],
811
            ['ds', 'get', 'https://ds.eu1.gigya.com/ds.get'],
812
            ['gameMechanics', 'getChallengeStatus', 'https://gm.eu1.gigya.com/gm.getChallengeStatus'],
813
            ['gm', 'getChallengeStatus', 'https://gm.eu1.gigya.com/gm.getChallengeStatus'],
814
            ['identityStorage', 'getSchema', 'https://ids.eu1.gigya.com/ids.getSchema'],
815
            ['ids', 'getSchema', 'https://ids.eu1.gigya.com/ids.getSchema'],
816
            ['reports', 'getGMStats', 'https://reports.eu1.gigya.com/reports.getGMStats'],
817
            ['saml', 'setConfig', 'https://fidm.eu1.gigya.com/fidm.saml.setConfig'],
818
            ['fidm', 'saml.setConfig', 'https://fidm.eu1.gigya.com/fidm.saml.setConfig'],
819
            ['saml', 'idp.getConfig', 'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig'],
820
            ['fidm', 'saml.idp.getConfig', 'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig'],
821
            ['socialize', 'checkin', 'https://socialize.eu1.gigya.com/socialize.checkin'],
822
        ];
823
    }
824
}
825