Completed
Pull Request — master (#27)
by Harry
06:09
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
    /**
408
     * @expectedException \BadMethodCallException
409
     * @expectedExceptionMessage No Arguments should be supplied for Gigya call
410
     */
411
    public function testCallingMagicMethodWithArgumentsThrowsAnException()
412
    {
413
        $client = $this->createClient();
414
        $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...
415
    }
416
417 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...
418
    {
419
        $gigyaResponse = $this->setupCall(
420
            'accounts.getAccountInfo',
421
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
422
            [
423
                'auth'        => 'gigya',
424
                'verify'      => $this->certPath,
425
                'form_params' => [
426
                    'params' => 'passedThrough',
427
                ],
428
                'option1'     => 'value1',
429
                'option2'     => false,
430
            ]
431
        );
432
        $client = $this->createClient();
433
434
        $client->addOption('option1', 'value1');
435
        $client->addOption('option2', false);
436
437
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
438
439
        static::assertSame($gigyaResponse, $result);
440
    }
441
442 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...
443
    {
444
        $gigyaResponse = $this->setupCall(
445
            'accounts.getAccountInfo',
446
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
447
            [
448
                'auth'        => 'gigya',
449
                'verify'      => $this->certPath,
450
                'form_params' => [
451
                    'params' => 'passedThrough',
452
                ],
453
                'option1'     => 'value1',
454
                'option2'     => true,
455
            ]
456
        );
457
        $client = $this->createClient();
458
459
        $client->addOptions(
460
            [
461
                'option1' => 'value1',
462
                'option2' => true,
463
            ]
464
        );
465
466
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
467
468
        static::assertSame($gigyaResponse, $result);
469
    }
470
471 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...
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
                'form_params' => [
480
                    'params' => 'passedThrough',
481
                ],
482
                'option1'     => false,
483
            ]
484
        );
485
        $client = $this->createClient();
486
487
        $client->addOption('option1', 'value1');
488
        $client->addOption('option1', false);
489
490
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
491
492
        static::assertSame($gigyaResponse, $result);
493
    }
494
495 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...
496
    {
497
        $gigyaResponse = $this->setupCall(
498
            'accounts.getAccountInfo',
499
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
500
            [
501
                'auth'        => 'gigya',
502
                'verify'      => $this->certPath,
503
                'form_params' => [
504
                    'params' => 'passedThrough',
505
                ],
506
                'option1'     => true,
507
            ]
508
        );
509
        $client = $this->createClient();
510
511
        $client->addOption('option1', 'value1');
512
        $client->addOptions(['option1' => true]);
513
514
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
515
516
        static::assertSame($gigyaResponse, $result);
517
    }
518
519
    public function testAddingFormParamsOptionsWillBeIgnored()
520
    {
521
        $gigyaResponse = $this->setupCall(
522
            'accounts.getAccountInfo',
523
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
524
            [
525
                'auth'        => 'gigya',
526
                'verify'      => 'notAFile',
527
                'form_params' => [
528
                    'params' => 'passedThrough',
529
                ],
530
            ]
531
        );
532
        $client = $this->createClient();
533
534
        $client->addOption('form_params', 'random');
535
        $client->addOption('verify', 'notAFile');
536
537
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
538
539
        static::assertSame($gigyaResponse, $result);
540
    }
541
542 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...
543
    {
544
        $gigyaResponse = $this->setupCall(
545
            'accounts.getAccountInfo',
546
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
547
            [
548
                'auth'        => 'gigya',
549
                'verify'      => $this->certPath,
550
                'form_params' => [
551
                    'params' => 'passedThrough',
552
                ],
553
                'custom'      => 'value',
554
            ]
555
        );
556
        $client = $this->createClient();
557
558
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough'], ['custom' => 'value']);
559
560
        static::assertSame($gigyaResponse, $result);
561
    }
562
563 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...
564
    {
565
        $gigyaResponse = $this->setupCall(
566
            'accounts.getAccountInfo',
567
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
568
            [
569
                'auth'        => 'gigya',
570
                'verify'      => $this->certPath,
571
                'form_params' => [
572
                    'params' => 'passedThrough',
573
                ],
574
                'custom'      => 'value',
575
            ]
576
        );
577
        $client = $this->createClient();
578
579
        $client->addOption('custom', 'notUsed');
580
581
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough'], ['custom' => 'value']);
582
583
        static::assertSame($gigyaResponse, $result);
584
    }
585
586
    public function testSettingRequestOptionsDoOverrideTheParams()
587
    {
588
        $gigyaResponse = $this->setupCall(
589
            'accounts.getAccountInfo',
590
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
591
            [
592
                'auth'        => 'gigya',
593
                'verify'      => false,
594
                'form_params' => [
595
                    'params' => 'passedThrough',
596
                ],
597
            ]
598
        );
599
        $client = $this->createClient();
600
601
        $result = $client->accounts()->getAccountInfo(
602
            ['params' => 'passedThrough'],
603
            ['form_params' => 'value', 'verify' => false]
604
        );
605
606
        static::assertSame($gigyaResponse, $result);
607
    }
608
609 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...
610
    {
611
        $gigyaResponse = $this->setupCall(
612
            'accounts.getAccountInfo',
613
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
614
            [
615
                'auth'        => 'gigya',
616
                'verify'      => $this->certPath,
617
                'form_params' => [
618
                    'secret' => 'newSecret',
619
                ],
620
            ]
621
        );
622
        $client = $this->createClient();
623
624
        $result = $client->accounts()->getAccountInfo(
625
            ['secret' => 'newSecret']
626
        );
627
628
        static::assertSame($gigyaResponse, $result);
629
    }
630
631 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...
632
    {
633
        $gigyaResponse = $this->setupCall(
634
            'accounts.getAccountInfo',
635
            'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig',
636
            [
637
                'auth'        => 'gigya',
638
                'verify'      => $this->certPath,
639
                'form_params' => [
640
                    'secret' => 'newSecret',
641
                ],
642
            ]
643
        );
644
        $client = $this->createClient();
645
646
        $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...
647
            ['secret' => 'newSecret']
648
        );
649
650
        static::assertSame($gigyaResponse, $result);
651
    }
652
653
    public function testWillCallAValidator()
654
    {
655
        $response = $this->setupCall(
656
            'accounts.getAccountInfo',
657
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
658
            [
659
                'auth'        => 'gigya',
660
                'verify'      => $this->certPath,
661
                'form_params' => [],
662
            ]
663
        );
664
        $client = $this->createClient();
665
666
        $validator = m::mock(ResponseValidatorInterface::class);
667
        $client->addValidator($validator);
668
        $validator->shouldReceive('canValidate')
669
                  ->with($response)
670
                  ->andReturn(true);
671
        $validator->shouldReceive('assert')
672
                  ->with($response)
673
                  ->andReturn(true);
674
675
        static::assertSame($response, $client->accounts()->getAccountInfo());
676
    }
677
678
    public function tesWillCallMultipleValidators()
679
    {
680
        $response = $this->setupCall(
681
            'accounts.getAccountInfo',
682
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
683
            [
684
                'auth'        => 'gigya',
685
                'verify'      => $this->certPath,
686
                'form_params' => [],
687
            ]
688
        );
689
        $client = $this->createClient();
690
691
        $validator = m::mock(ResponseValidatorInterface::class);
692
        $client->addValidator($validator);
693
        $validator->shouldReceive('canValidate')
694
                  ->with($response)
695
                  ->andReturn(true);
696
        $validator->shouldReceive('assert')
697
                  ->with($response)
698
                  ->andReturn(true);
699
        $validator2 = m::mock(ResponseValidatorInterface::class);
700
        $client->addValidator($validator2);
701
        $validator2->shouldReceive('canValidate')
702
                   ->with($response)
703
                   ->andReturn(true);
704
        $validator2->shouldReceive('assert')
705
                   ->with($response)
706
                   ->andReturn(true);
707
708
        static::assertSame($response, $client->accounts()->getAccountInfo());
709
    }
710
711
    /**
712
     * @expectedException \Exception
713
     */
714
    public function testTheValidatorThrowingAnExceptionWillPassthrough()
715
    {
716
        $response = $this->setupCall(
717
            'accounts.getAccountInfo',
718
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
719
            [
720
                'auth'        => 'gigya',
721
                'verify'      => $this->certPath,
722
                'form_params' => [],
723
            ]
724
        );
725
        $client = $this->createClient();
726
727
        $validator = m::mock(ResponseValidatorInterface::class);
728
        $validator2 = m::mock(ResponseValidatorInterface::class);
729
        $client->addValidator($validator);
730
        $client->addValidator($validator2);
731
        $exception = new Exception();
732
        $validator->shouldReceive('canValidate')
733
                  ->with($response)
734
                  ->andReturn(true);
735
        $validator->shouldReceive('assert')
736
                  ->with($response)
737
                  ->andThrow($exception);
738
739
        $client->accounts()->getAccountInfo();
740
    }
741
742
    public function testTheValidatorWillOnlyCallAssertWhenItCanValidate()
743
    {
744
        $response = $this->setupCall(
745
            'accounts.getAccountInfo',
746
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
747
            [
748
                'auth'        => 'gigya',
749
                'verify'      => $this->certPath,
750
                'form_params' => [],
751
            ]
752
        );
753
        $client = $this->createClient();
754
755
        $validator = m::mock(ResponseValidatorInterface::class);
756
        $validator2 = m::mock(ResponseValidatorInterface::class);
757
        $client->addValidator($validator);
758
        $client->addValidator($validator2);
759
        $validator->shouldReceive('canValidate')
760
                  ->with($response)
761
                  ->andReturn(true);
762
        $validator->shouldReceive('assert')
763
                  ->with($response)
764
                  ->andReturn(true);
765
        $validator2->shouldReceive('canValidate')
766
                   ->with($response)
767
                   ->andReturn(false);
768
769
        static::assertSame($response, $client->accounts()->getAccountInfo());
770
    }
771
772
    public function testRemoveSubscriberWillDetachFromEmitter()
773
    {
774
        $response = $this->setupCall(
775
            'accounts.getAccountInfo',
776
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
777
            [
778
                'auth'        => 'gigya',
779
                'verify'      => $this->certPath,
780
                'form_params' => [],
781
            ]
782
        );
783
        $client = $this->createClient();
784
785
        static::assertSame($response, $client->accounts()->getAccountInfo());
786
787
        $fn = function (callable $handler) {
788
            return $handler;
789
        };
790
        $this->handlerStack->shouldReceive('push')
791
                           ->with($fn)
792
                           ->once();
793
794
        $client->addHandler($fn);
795
796
        $this->handlerStack->shouldReceive('remove')
797
                           ->with($fn)
798
                           ->once();
799
800
        $client->removeHandler($fn);
801
    }
802
803
    /**
804
     * @return array
805
     */
806
    public function clientCallDataProvider()
807
    {
808
        return [
809
            ['accounts', 'getAccountInfo', 'https://accounts.eu1.gigya.com/accounts.getAccountInfo'],
810
            ['accounts', 'tfa.getCertificate', 'https://accounts.eu1.gigya.com/accounts.tfa.getCertificate'],
811
            ['audit', 'search', 'https://audit.eu1.gigya.com/audit.search'],
812
            ['comments', 'analyzeMediaItem', 'https://comments.eu1.gigya.com/comments.analyzeMediaItem'],
813
            ['dataStore', 'get', 'https://ds.eu1.gigya.com/ds.get'],
814
            ['ds', 'get', 'https://ds.eu1.gigya.com/ds.get'],
815
            ['gameMechanics', 'getChallengeStatus', 'https://gm.eu1.gigya.com/gm.getChallengeStatus'],
816
            ['gm', 'getChallengeStatus', 'https://gm.eu1.gigya.com/gm.getChallengeStatus'],
817
            ['identityStorage', 'getSchema', 'https://ids.eu1.gigya.com/ids.getSchema'],
818
            ['ids', 'getSchema', 'https://ids.eu1.gigya.com/ids.getSchema'],
819
            ['reports', 'getGMStats', 'https://reports.eu1.gigya.com/reports.getGMStats'],
820
            ['saml', 'setConfig', 'https://fidm.eu1.gigya.com/fidm.saml.setConfig'],
821
            ['fidm', 'saml.setConfig', 'https://fidm.eu1.gigya.com/fidm.saml.setConfig'],
822
            ['saml', 'idp.getConfig', 'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig'],
823
            ['fidm', 'saml.idp.getConfig', 'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig'],
824
            ['socialize', 'checkin', 'https://socialize.eu1.gigya.com/socialize.checkin'],
825
        ];
826
    }
827
}
828