Completed
Pull Request — master (#27)
by Harry
02:14
created

testAddingFormParamsOptionsWillBeIgnored()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 13
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\Gigya;
18
use Graze\Gigya\Response\ResponseFactoryInterface;
19
use Graze\Gigya\Test\TestCase;
20
use Graze\Gigya\Test\TestFixtures;
21
use Graze\Gigya\Validation\ResponseValidatorInterface;
22
use GuzzleHttp\HandlerStack;
23
use GuzzleHttp\Psr7\Response;
24
use Mockery as m;
25
26
/**
27
 * @runTestsInSeparateProcesses
28
 * @preserveGlobalState disabled
29
 */
30
class GigyaTest extends TestCase
31
{
32
    /** @var mixed */
33
    private $factory;
34
    /** @var string */
35
    private $certPath;
36
    /** @var mixed */
37
    private $handlerStack;
38
    /** @var mixed */
39
    private $guzzleClient;
40
41
    public function setUp()
42
    {
43
        $this->guzzleClient = m::mock('overload:GuzzleHttp\Client');
44
        $this->handlerStack = m::mock(new HandlerStack())->makePartial();
45
        $this->factory = m::mock(ResponseFactoryInterface::class);
46
        $this->certPath = realpath(__DIR__ . '/../../src/' . Gigya::CERTIFICATE_FILE);
47
    }
48
49
    /**
50
     * @param string      $key
51
     * @param string      $secret
52
     * @param string      $dc
53
     * @param string|null $userKey
54
     *
55
     * @return Gigya
56
     */
57
    public function createClient($key = 'key', $secret = 'secret', $dc = Gigya::DC_EU, $userKey = null)
58
    {
59
        $options = [
60
            'uidValidator' => false,
61
            'factory'      => $this->factory,
62
            'guzzle'       => ['handler' => $this->handlerStack],
63
        ];
64
65
        return new Gigya($key, $secret, $dc, $userKey, $options);
66
    }
67
68
    /**
69
     * @param string $fixtureName
70
     * @param string $uri
71
     * @param array  $getOptions
72
     *
73
     * @return mixed MockInterface and ResponseInterface
74
     */
75
    private function setupCall($fixtureName, $uri, array $getOptions)
76
    {
77
        $response = new Response(200, [], TestFixtures::getFixture($fixtureName));
78
79
        $this->guzzleClient->shouldReceive('post')
80
                           ->with(
81
                               $uri,
82
                               $getOptions
83
                           )
84
                           ->andReturn($response);
85
86
        $gigyaResponse = m::mock('Graze\Gigya\Response\ResponseInterface');
87
88
        $this->factory->shouldReceive('getResponse')
89
                      ->with($response)
90
                      ->andReturn($gigyaResponse);
91
92
        return $gigyaResponse;
93
    }
94
95
    public function testDefaultConstructor()
96
    {
97
        $client = new Gigya('key', 'secret');
98
        static::assertInstanceOf(Gigya::class, $client);
99
    }
100
101 View Code Duplication
    public function testConstructorConfig()
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...
102
    {
103
        $this->guzzleClient->shouldReceive('__construct')
104
                           ->once()
105
                           ->with(
106
                               [
107
                                   'handler' => $this->handlerStack,
108
                               ]
109
                           )
110
                           ->andReturn($this->guzzleClient);
111
112
        $response = new Response(200, [], TestFixtures::getFixture('account.getAccountInfo'));
113
114
        $this->guzzleClient->shouldReceive('post')
115
                           ->with(
116
                               'https://accounts.au1.gigya.com/accounts.getAccountInfo',
117
                               [
118
                                   'cert'        => 'some_cert.pem',
119
                                   'auth'        => 'oauth',
120
                                   'verify'      => $this->certPath,
121
                                   'form_params' => [],
122
                               ]
123
                           )
124
                           ->andReturn($response);
125
126
        $gigyaResponse = m::mock('Graze\Gigya\Response\ResponseInterface');
127
128
        $this->factory->shouldReceive('getResponse')
129
                      ->with($response)
130
                      ->andReturn($gigyaResponse);
131
132
        $config = [
133
            'auth'         => 'oauth',
134
            'uidValidator' => false,
135
            'factory'      => $this->factory,
136
            'guzzle'       => [
137
                'handler' => $this->handlerStack,
138
            ],
139
            'options'      => [
140
                'cert' => 'some_cert.pem',
141
            ],
142
        ];
143
        $client = new Gigya('key', 'secret', Gigya::DC_AU, null, $config);
144
145
        static::assertSame($gigyaResponse, $client->accounts()->getAccountInfo());
146
    }
147
148 View Code Duplication
    public function testOAuth2AuthConstructor()
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
        $this->guzzleClient->shouldReceive('__construct')
151
                           ->once()
152
                           ->with(
153
                               [
154
                                   'handler' => $this->handlerStack,
155
                               ]
156
                           )
157
                           ->andReturn($this->guzzleClient);
158
159
        $response = new Response(200, [], TestFixtures::getFixture('account.getAccountInfo'));
160
161
        $this->guzzleClient->shouldReceive('post')
162
                           ->with(
163
                               'https://accounts.au1.gigya.com/accounts.getAccountInfo',
164
                               [
165
                                   'cert'        => 'some_cert.pem',
166
                                   'auth'        => 'gigya-oauth2',
167
                                   'verify'      => $this->certPath,
168
                                   'form_params' => [],
169
                               ]
170
                           )
171
                           ->andReturn($response);
172
173
        $gigyaResponse = m::mock('Graze\Gigya\Response\ResponseInterface');
174
175
        $this->factory->shouldReceive('getResponse')
176
                      ->with($response)
177
                      ->andReturn($gigyaResponse);
178
179
        $config = [
180
            'auth'         => 'gigya-oauth2',
181
            'uidValidator' => false,
182
            'factory'      => $this->factory,
183
            'guzzle'       => [
184
                'handler' => $this->handlerStack,
185
            ],
186
            'options'      => [
187
                'cert' => 'some_cert.pem',
188
            ],
189
        ];
190
        $client = new Gigya('key', 'secret', Gigya::DC_AU, null, $config);
191
192
        static::assertSame($gigyaResponse, $client->accounts()->getAccountInfo());
193
    }
194
195 View Code Duplication
    public function testCredentialsAuthConstructor()
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...
196
    {
197
        $this->guzzleClient->shouldReceive('__construct')
198
                           ->with(
199
                               [
200
                                   'handler' => $this->handlerStack,
201
                               ]
202
                           )
203
                           ->once()
204
                           ->andReturn($this->guzzleClient);
205
206
        $response = new Response(200, [], TestFixtures::getFixture('account.getAccountInfo'));
207
208
        $this->guzzleClient->shouldReceive('post')
209
                           ->with(
210
                               'https://accounts.au1.gigya.com/accounts.getAccountInfo',
211
                               [
212
                                   'cert'        => 'some_cert.pem',
213
                                   'auth'        => 'credentials',
214
                                   'verify'      => $this->certPath,
215
                                   'form_params' => [],
216
                               ]
217
                           )
218
                           ->once()
219
                           ->andReturn($response);
220
221
        $gigyaResponse = m::mock('Graze\Gigya\Response\ResponseInterface');
222
223
        $this->factory->shouldReceive('getResponse')
224
                      ->with($response)
225
                      ->andReturn($gigyaResponse);
226
227
        $config = [
228
            'auth'         => 'credentials',
229
            'uidValidator' => false,
230
            'factory'      => $this->factory,
231
            'guzzle'       => [
232
                'handler' => $this->handlerStack,
233
            ],
234
            'options'      => [
235
                'cert' => 'some_cert.pem',
236
            ],
237
        ];
238
        $client = new Gigya('key', 'secret', Gigya::DC_AU, null, $config);
239
240
        static::assertSame($gigyaResponse, $client->accounts()->getAccountInfo());
241
    }
242
243
    public function testSettingKeyAndSecretWillPassToGuzzleClient()
244
    {
245
        $key = 'key' . rand(1, 1000);
246
        $secret = 'secret' . rand(1001, 2000002);
247
248
        $gigyaResponse = $this->setupCall(
249
            'accounts.getAccountInfo',
250
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
251
            [
252
                'auth'        => 'gigya',
253
                'verify'      => $this->certPath,
254
                'form_params' => [],
255
            ]
256
        );
257
        $client = $this->createClient($key, $secret, Gigya::DC_EU, null);
258
        $client->setFactory($this->factory);
259
260
        $result = $client->accounts()->getAccountInfo([]);
261
262
        static::assertSame($gigyaResponse, $result);
263
    }
264
265 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...
266
    {
267
        $gigyaResponse = $this->setupCall(
268
            'accounts.getAccountInfo',
269
            'https://accounts.au1.gigya.com/accounts.getAccountInfo',
270
            [
271
                'auth'        => 'gigya',
272
                'verify'      => $this->certPath,
273
                'form_params' => [],
274
            ]
275
        );
276
        $client = $this->createClient('key', 'secret', Gigya::DC_AU);
277
278
        $result = $client->accounts()->getAccountInfo([]);
279
280
        static::assertSame($gigyaResponse, $result);
281
    }
282
283 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...
284
    {
285
        $gigyaResponse = $this->setupCall(
286
            'accounts.getAccountInfo',
287
            'https://accounts.us1.gigya.com/accounts.getAccountInfo',
288
            [
289
                'auth'        => 'gigya',
290
                'verify'      => $this->certPath,
291
                'form_params' => [],
292
            ]
293
        );
294
        $client = $this->createClient('key', 'secret', Gigya::DC_US);
295
296
        $result = $client->accounts()->getAccountInfo([]);
297
298
        static::assertSame($gigyaResponse, $result);
299
    }
300
301 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...
302
    {
303
        $gigyaResponse = $this->setupCall(
304
            'accounts.getAccountInfo',
305
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
306
            [
307
                'auth'        => 'gigya',
308
                'verify'      => $this->certPath,
309
                'form_params' => [],
310
            ]
311
        );
312
        $client = $this->createClient('key', 'userSecret', Gigya::DC_EU, 'userKey');
313
        $client->setFactory($this->factory);
314
315
        $result = $client->accounts()->getAccountInfo([]);
316
317
        static::assertSame($gigyaResponse, $result);
318
    }
319
320 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...
321
    {
322
        $gigyaResponse = $this->setupCall(
323
            'accounts.getAccountInfo',
324
            'https://socialize.eu1.gigya.com/socialize.notifyLogin',
325
            [
326
                'auth'        => 'gigya',
327
                'verify'      => $this->certPath,
328
                'form_params' => [
329
                    'param' => 'passedThrough',
330
                ],
331
            ]
332
        );
333
        $client = $this->createClient();
334
335
        $result = $client->socialize()->notifyLogin(['param' => 'passedThrough']);
336
337
        static::assertSame($gigyaResponse, $result);
338
    }
339
340 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...
341
    {
342
        $gigyaResponse = $this->setupCall(
343
            'accounts.getAccountInfo',
344
            'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig',
345
            [
346
                'auth'        => 'gigya',
347
                'verify'      => $this->certPath,
348
                'form_params' => [
349
                    'params' => 'passedThrough',
350
                ],
351
            ]
352
        );
353
        $client = $this->createClient();
354
355
        $result = $client->saml()->idp()->getConfig(['params' => 'passedThrough']);
356
357
        static::assertSame($gigyaResponse, $result);
358
    }
359
360 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...
361
    {
362
        $gigyaResponse = $this->setupCall(
363
            'accounts.getAccountInfo',
364
            'https://accounts.eu1.gigya.com/accounts.tfa.getCertificate',
365
            [
366
                'auth'        => 'gigya',
367
                'verify'      => $this->certPath,
368
                'form_params' => [
369
                    'params' => 'passedThrough',
370
                ],
371
            ]
372
        );
373
        $client = $this->createClient();
374
375
        $result = $client->accounts()->tfa()->getCertificate(['params' => 'passedThrough']);
376
377
        static::assertSame($gigyaResponse, $result);
378
    }
379
380
    /**
381
     * @dataProvider clientCallDataProvider
382
     *
383
     * @param string $namespace
384
     * @param string $method
385
     * @param string $expectedUri
386
     */
387
    public function testClientCalls($namespace, $method, $expectedUri)
388
    {
389
        $gigyaResponse = $this->setupCall(
390
            'accounts.getAccountInfo',
391
            $expectedUri,
392
            [
393
                'auth'        => 'gigya',
394
                'verify'      => $this->certPath,
395
                'form_params' => [
396
                    'params' => 'passedThrough',
397
                ],
398
            ]
399
        );
400
        $client = $this->createClient();
401
402
        $result = $client->{$namespace}()->{$method}(['params' => 'passedThrough']);
403
404
        static::assertSame($gigyaResponse, $result);
405
    }
406
407
    public function testCallingMagicMethodWithArgumentsThrowsAnException()
408
    {
409
        static::expectException('BadMethodCallException');
410
        static::expectExceptionMessage('No Arguments should be supplied for Gigya call');
411
412
        $client = $this->createClient();
413
        $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...
414
    }
415
416 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...
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
                'form_params' => [
425
                    'params' => 'passedThrough',
426
                ],
427
                'option1'     => 'value1',
428
                'option2'     => false,
429
            ]
430
        );
431
        $client = $this->createClient();
432
433
        $client->addOption('option1', 'value1');
434
        $client->addOption('option2', false);
435
436
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
437
438
        static::assertSame($gigyaResponse, $result);
439
    }
440
441 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...
442
    {
443
        $gigyaResponse = $this->setupCall(
444
            'accounts.getAccountInfo',
445
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
446
            [
447
                'auth'        => 'gigya',
448
                'verify'      => $this->certPath,
449
                'form_params' => [
450
                    'params' => 'passedThrough',
451
                ],
452
                'option1'     => 'value1',
453
                'option2'     => true,
454
            ]
455
        );
456
        $client = $this->createClient();
457
458
        $client->addOptions(
459
            [
460
                'option1' => 'value1',
461
                'option2' => true,
462
            ]
463
        );
464
465
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
466
467
        static::assertSame($gigyaResponse, $result);
468
    }
469
470 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...
471
    {
472
        $gigyaResponse = $this->setupCall(
473
            'accounts.getAccountInfo',
474
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
475
            [
476
                'auth'        => 'gigya',
477
                'verify'      => $this->certPath,
478
                'form_params' => [
479
                    'params' => 'passedThrough',
480
                ],
481
                'option1'     => false,
482
            ]
483
        );
484
        $client = $this->createClient();
485
486
        $client->addOption('option1', 'value1');
487
        $client->addOption('option1', false);
488
489
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
490
491
        static::assertSame($gigyaResponse, $result);
492
    }
493
494 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...
495
    {
496
        $gigyaResponse = $this->setupCall(
497
            'accounts.getAccountInfo',
498
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
499
            [
500
                'auth'        => 'gigya',
501
                'verify'      => $this->certPath,
502
                'form_params' => [
503
                    'params' => 'passedThrough',
504
                ],
505
                'option1'     => true,
506
            ]
507
        );
508
        $client = $this->createClient();
509
510
        $client->addOption('option1', 'value1');
511
        $client->addOptions(['option1' => true]);
512
513
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
514
515
        static::assertSame($gigyaResponse, $result);
516
    }
517
518
    public function testAddingFormParamsOptionsWillBeIgnored()
519
    {
520
        $gigyaResponse = $this->setupCall(
521
            'accounts.getAccountInfo',
522
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
523
            [
524
                'auth'        => 'gigya',
525
                'verify'      => 'notAFile',
526
                'form_params' => [
527
                    'params' => 'passedThrough',
528
                ],
529
            ]
530
        );
531
        $client = $this->createClient();
532
533
        $client->addOption('form_params', 'random');
534
        $client->addOption('verify', 'notAFile');
535
536
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
537
538
        static::assertSame($gigyaResponse, $result);
539
    }
540
541 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...
542
    {
543
        $gigyaResponse = $this->setupCall(
544
            'accounts.getAccountInfo',
545
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
546
            [
547
                'auth'        => 'gigya',
548
                'verify'      => $this->certPath,
549
                'form_params' => [
550
                    'params' => 'passedThrough',
551
                ],
552
                'custom'      => 'value',
553
            ]
554
        );
555
        $client = $this->createClient();
556
557
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough'], ['custom' => 'value']);
558
559
        static::assertSame($gigyaResponse, $result);
560
    }
561
562 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...
563
    {
564
        $gigyaResponse = $this->setupCall(
565
            'accounts.getAccountInfo',
566
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
567
            [
568
                'auth'        => 'gigya',
569
                'verify'      => $this->certPath,
570
                'form_params' => [
571
                    'params' => 'passedThrough',
572
                ],
573
                'custom'      => 'value',
574
            ]
575
        );
576
        $client = $this->createClient();
577
578
        $client->addOption('custom', 'notUsed');
579
580
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough'], ['custom' => 'value']);
581
582
        static::assertSame($gigyaResponse, $result);
583
    }
584
585
    public function testSettingRequestOptionsDoOverrideTheParams()
586
    {
587
        $gigyaResponse = $this->setupCall(
588
            'accounts.getAccountInfo',
589
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
590
            [
591
                'auth'        => 'gigya',
592
                'verify'      => false,
593
                'form_params' => [
594
                    'params' => 'passedThrough',
595
                ],
596
            ]
597
        );
598
        $client = $this->createClient();
599
600
        $result = $client->accounts()->getAccountInfo(
601
            ['params' => 'passedThrough'],
602
            ['form_params' => 'value', 'verify' => false]
603
        );
604
605
        static::assertSame($gigyaResponse, $result);
606
    }
607
608 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...
609
    {
610
        $gigyaResponse = $this->setupCall(
611
            'accounts.getAccountInfo',
612
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
613
            [
614
                'auth'        => 'gigya',
615
                'verify'      => $this->certPath,
616
                'form_params' => [
617
                    'secret' => 'newSecret',
618
                ],
619
            ]
620
        );
621
        $client = $this->createClient();
622
623
        $result = $client->accounts()->getAccountInfo(
624
            ['secret' => 'newSecret']
625
        );
626
627
        static::assertSame($gigyaResponse, $result);
628
    }
629
630 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...
631
    {
632
        $gigyaResponse = $this->setupCall(
633
            'accounts.getAccountInfo',
634
            'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig',
635
            [
636
                'auth'        => 'gigya',
637
                'verify'      => $this->certPath,
638
                'form_params' => [
639
                    'secret' => 'newSecret',
640
                ],
641
            ]
642
        );
643
        $client = $this->createClient();
644
645
        $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...
646
            ['secret' => 'newSecret']
647
        );
648
649
        static::assertSame($gigyaResponse, $result);
650
    }
651
652
    public function testWillCallAValidator()
653
    {
654
        $response = $this->setupCall(
655
            'accounts.getAccountInfo',
656
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
657
            [
658
                'auth'        => 'gigya',
659
                'verify'      => $this->certPath,
660
                'form_params' => [],
661
            ]
662
        );
663
        $client = $this->createClient();
664
665
        $validator = m::mock(ResponseValidatorInterface::class);
666
        $client->addValidator($validator);
667
        $validator->shouldReceive('canValidate')
668
                  ->with($response)
669
                  ->andReturn(true);
670
        $validator->shouldReceive('assert')
671
                  ->with($response)
672
                  ->andReturn(true);
673
674
        static::assertSame($response, $client->accounts()->getAccountInfo());
675
    }
676
677
    public function tesWillCallMultipleValidators()
678
    {
679
        $response = $this->setupCall(
680
            'accounts.getAccountInfo',
681
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
682
            [
683
                'auth'        => 'gigya',
684
                'verify'      => $this->certPath,
685
                'form_params' => [],
686
            ]
687
        );
688
        $client = $this->createClient();
689
690
        $validator = m::mock(ResponseValidatorInterface::class);
691
        $client->addValidator($validator);
692
        $validator->shouldReceive('canValidate')
693
                  ->with($response)
694
                  ->andReturn(true);
695
        $validator->shouldReceive('assert')
696
                  ->with($response)
697
                  ->andReturn(true);
698
        $validator2 = m::mock(ResponseValidatorInterface::class);
699
        $client->addValidator($validator2);
700
        $validator2->shouldReceive('canValidate')
701
                   ->with($response)
702
                   ->andReturn(true);
703
        $validator2->shouldReceive('assert')
704
                   ->with($response)
705
                   ->andReturn(true);
706
707
        static::assertSame($response, $client->accounts()->getAccountInfo());
708
    }
709
710
    public function testTheValidatorThrowingAnExceptionWillPassthrough()
711
    {
712
        $response = $this->setupCall(
713
            'accounts.getAccountInfo',
714
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
715
            [
716
                'auth'        => 'gigya',
717
                'verify'      => $this->certPath,
718
                'form_params' => [],
719
            ]
720
        );
721
        $client = $this->createClient();
722
723
        $validator = m::mock(ResponseValidatorInterface::class);
724
        $validator2 = m::mock(ResponseValidatorInterface::class);
725
        $client->addValidator($validator);
726
        $client->addValidator($validator2);
727
        $exception = new Exception();
728
        $validator->shouldReceive('canValidate')
729
                  ->with($response)
730
                  ->andReturn(true);
731
        $validator->shouldReceive('assert')
732
                  ->with($response)
733
                  ->andThrow($exception);
734
735
        static::expectException(Exception::class);
736
737
        $client->accounts()->getAccountInfo();
738
    }
739
740
    public function testTheValidatorWillOnlyCallAssertWhenItCanValidate()
741
    {
742
        $response = $this->setupCall(
743
            'accounts.getAccountInfo',
744
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
745
            [
746
                'auth'        => 'gigya',
747
                'verify'      => $this->certPath,
748
                'form_params' => [],
749
            ]
750
        );
751
        $client = $this->createClient();
752
753
        $validator = m::mock(ResponseValidatorInterface::class);
754
        $validator2 = m::mock(ResponseValidatorInterface::class);
755
        $client->addValidator($validator);
756
        $client->addValidator($validator2);
757
        $validator->shouldReceive('canValidate')
758
                  ->with($response)
759
                  ->andReturn(true);
760
        $validator->shouldReceive('assert')
761
                  ->with($response)
762
                  ->andReturn(true);
763
        $validator2->shouldReceive('canValidate')
764
                   ->with($response)
765
                   ->andReturn(false);
766
767
        static::assertSame($response, $client->accounts()->getAccountInfo());
768
    }
769
770
    public function testRemoveSubscriberWillDetachFromEmitter()
771
    {
772
        $response = $this->setupCall(
773
            'accounts.getAccountInfo',
774
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
775
            [
776
                'auth'        => 'gigya',
777
                'verify'      => $this->certPath,
778
                'form_params' => [],
779
            ]
780
        );
781
        $client = $this->createClient();
782
783
        static::assertSame($response, $client->accounts()->getAccountInfo());
784
785
        $fn = function (callable $handler) {
786
            return $handler;
787
        };
788
        $this->handlerStack->shouldReceive('push')
789
                           ->with($fn)
790
                           ->once();
791
792
        $client->addHandler($fn);
793
794
        $this->handlerStack->shouldReceive('remove')
795
                           ->with($fn)
796
                           ->once();
797
798
        $client->removeHandler($fn);
799
    }
800
801
    /**
802
     * @return array
803
     */
804
    public function clientCallDataProvider()
805
    {
806
        return [
807
            ['accounts', 'getAccountInfo', 'https://accounts.eu1.gigya.com/accounts.getAccountInfo'],
808
            ['accounts', 'tfa.getCertificate', 'https://accounts.eu1.gigya.com/accounts.tfa.getCertificate'],
809
            ['audit', 'search', 'https://audit.eu1.gigya.com/audit.search'],
810
            ['comments', 'analyzeMediaItem', 'https://comments.eu1.gigya.com/comments.analyzeMediaItem'],
811
            ['dataStore', 'get', 'https://ds.eu1.gigya.com/ds.get'],
812
            ['ds', 'get', 'https://ds.eu1.gigya.com/ds.get'],
813
            ['gameMechanics', 'getChallengeStatus', 'https://gm.eu1.gigya.com/gm.getChallengeStatus'],
814
            ['gm', 'getChallengeStatus', 'https://gm.eu1.gigya.com/gm.getChallengeStatus'],
815
            ['identityStorage', 'getSchema', 'https://ids.eu1.gigya.com/ids.getSchema'],
816
            ['ids', 'getSchema', 'https://ids.eu1.gigya.com/ids.getSchema'],
817
            ['reports', 'getGMStats', 'https://reports.eu1.gigya.com/reports.getGMStats'],
818
            ['saml', 'setConfig', 'https://fidm.eu1.gigya.com/fidm.saml.setConfig'],
819
            ['fidm', 'saml.setConfig', 'https://fidm.eu1.gigya.com/fidm.saml.setConfig'],
820
            ['saml', 'idp.getConfig', 'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig'],
821
            ['fidm', 'saml.idp.getConfig', 'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig'],
822
            ['socialize', 'checkin', 'https://socialize.eu1.gigya.com/socialize.checkin'],
823
        ];
824
    }
825
}
826