GigyaTest::testConstructorConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 28
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 45
rs 9.472
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
        parent::setUp();
44
        $this->guzzleClient = m::mock('overload:GuzzleHttp\Client');
45
        $this->handlerStack = m::mock(new HandlerStack())->makePartial();
46
        $this->factory = m::mock(ResponseFactoryInterface::class);
47
        $this->certPath = realpath(__DIR__ . '/../../src/' . Gigya::CERTIFICATE_FILE);
48
    }
49
50
    /**
51
     * @param string      $key
52
     * @param string      $secret
53
     * @param string      $dc
54
     * @param string|null $userKey
55
     *
56
     * @return Gigya
57
     */
58
    public function createClient($key = 'key', $secret = 'secret', $dc = Gigya::DC_EU, $userKey = null)
59
    {
60
        $options = [
61
            'uidValidator' => false,
62
            'factory'      => $this->factory,
63
            'guzzle'       => ['handler' => $this->handlerStack],
64
        ];
65
66
        return new Gigya($key, $secret, $dc, $userKey, $options);
67
    }
68
69
    /**
70
     * @param string $fixtureName
71
     * @param string $uri
72
     * @param array  $getOptions
73
     *
74
     * @return mixed MockInterface and ResponseInterface
75
     */
76
    private function setupCall($fixtureName, $uri, array $getOptions)
77
    {
78
        $response = new Response(200, [], TestFixtures::getFixture($fixtureName));
79
80
        $this->guzzleClient->shouldReceive('post')
81
                           ->with(
82
                               $uri,
83
                               $getOptions
84
                           )
85
                           ->andReturn($response);
86
87
        $gigyaResponse = m::mock('Graze\Gigya\Response\ResponseInterface');
88
89
        $this->factory->shouldReceive('getResponse')
90
                      ->with($response)
91
                      ->andReturn($gigyaResponse);
92
93
        return $gigyaResponse;
94
    }
95
96
    public function testDefaultConstructor()
97
    {
98
        $client = new Gigya('key', 'secret');
99
        static::assertInstanceOf(Gigya::class, $client);
100
    }
101
102
    public function testConstructorConfig()
103
    {
104
        $this->guzzleClient->shouldReceive('__construct')
105
                           ->once()
106
                           ->with(
107
                               [
108
                                   'handler' => $this->handlerStack,
109
                               ]
110
                           )
111
                           ->andReturn($this->guzzleClient);
112
113
        $response = new Response(200, [], TestFixtures::getFixture('account.getAccountInfo'));
114
115
        $this->guzzleClient->shouldReceive('post')
116
                           ->with(
117
                               'https://accounts.au1.gigya.com/accounts.getAccountInfo',
118
                               [
119
                                   'cert'        => 'some_cert.pem',
120
                                   'auth'        => 'oauth',
121
                                   'verify'      => $this->certPath,
122
                                   'form_params' => [],
123
                               ]
124
                           )
125
                           ->andReturn($response);
126
127
        $gigyaResponse = m::mock('Graze\Gigya\Response\ResponseInterface');
128
129
        $this->factory->shouldReceive('getResponse')
130
                      ->with($response)
131
                      ->andReturn($gigyaResponse);
132
133
        $config = [
134
            'auth'         => 'oauth',
135
            'uidValidator' => false,
136
            'factory'      => $this->factory,
137
            'guzzle'       => [
138
                'handler' => $this->handlerStack,
139
            ],
140
            'options'      => [
141
                'cert' => 'some_cert.pem',
142
            ],
143
        ];
144
        $client = new Gigya('key', 'secret', Gigya::DC_AU, null, $config);
145
146
        static::assertSame($gigyaResponse, $client->accounts()->getAccountInfo());
147
    }
148
149
    public function testOAuth2AuthConstructor()
150
    {
151
        $this->guzzleClient->shouldReceive('__construct')
152
                           ->once()
153
                           ->with(
154
                               [
155
                                   'handler' => $this->handlerStack,
156
                               ]
157
                           )
158
                           ->andReturn($this->guzzleClient);
159
160
        $response = new Response(200, [], TestFixtures::getFixture('account.getAccountInfo'));
161
162
        $this->guzzleClient->shouldReceive('post')
163
                           ->with(
164
                               'https://accounts.au1.gigya.com/accounts.getAccountInfo',
165
                               [
166
                                   'cert'        => 'some_cert.pem',
167
                                   'auth'        => 'gigya-oauth2',
168
                                   'verify'      => $this->certPath,
169
                                   'form_params' => [],
170
                               ]
171
                           )
172
                           ->andReturn($response);
173
174
        $gigyaResponse = m::mock('Graze\Gigya\Response\ResponseInterface');
175
176
        $this->factory->shouldReceive('getResponse')
177
                      ->with($response)
178
                      ->andReturn($gigyaResponse);
179
180
        $config = [
181
            'auth'         => 'gigya-oauth2',
182
            'uidValidator' => false,
183
            'factory'      => $this->factory,
184
            'guzzle'       => [
185
                'handler' => $this->handlerStack,
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 testCredentialsAuthConstructor()
197
    {
198
        $this->guzzleClient->shouldReceive('__construct')
199
                           ->with(
200
                               [
201
                                   'handler' => $this->handlerStack,
202
                               ]
203
                           )
204
                           ->once()
205
                           ->andReturn($this->guzzleClient);
206
207
        $response = new Response(200, [], TestFixtures::getFixture('account.getAccountInfo'));
208
209
        $this->guzzleClient->shouldReceive('post')
210
                           ->with(
211
                               'https://accounts.au1.gigya.com/accounts.getAccountInfo',
212
                               [
213
                                   'cert'        => 'some_cert.pem',
214
                                   'auth'        => 'credentials',
215
                                   'verify'      => $this->certPath,
216
                                   'form_params' => [],
217
                               ]
218
                           )
219
                           ->once()
220
                           ->andReturn($response);
221
222
        $gigyaResponse = m::mock('Graze\Gigya\Response\ResponseInterface');
223
224
        $this->factory->shouldReceive('getResponse')
225
                      ->with($response)
226
                      ->andReturn($gigyaResponse);
227
228
        $config = [
229
            'auth'         => 'credentials',
230
            'uidValidator' => false,
231
            'factory'      => $this->factory,
232
            'guzzle'       => [
233
                'handler' => $this->handlerStack,
234
            ],
235
            'options'      => [
236
                'cert' => 'some_cert.pem',
237
            ],
238
        ];
239
        $client = new Gigya('key', 'secret', Gigya::DC_AU, null, $config);
240
241
        static::assertSame($gigyaResponse, $client->accounts()->getAccountInfo());
242
    }
243
244
    public function testSettingKeyAndSecretWillPassToGuzzleClient()
245
    {
246
        $key = 'key' . rand(1, 1000);
247
        $secret = 'secret' . rand(1001, 2000002);
248
249
        $gigyaResponse = $this->setupCall(
250
            'accounts.getAccountInfo',
251
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
252
            [
253
                'auth'        => 'gigya',
254
                'verify'      => $this->certPath,
255
                'form_params' => [],
256
            ]
257
        );
258
        $client = $this->createClient($key, $secret, Gigya::DC_EU, null);
259
        $client->setFactory($this->factory);
260
261
        $result = $client->accounts()->getAccountInfo([]);
262
263
        static::assertSame($gigyaResponse, $result);
264
    }
265
266
    public function testSettingDataCenterToAuWillCallAuUri()
267
    {
268
        $gigyaResponse = $this->setupCall(
269
            'accounts.getAccountInfo',
270
            'https://accounts.au1.gigya.com/accounts.getAccountInfo',
271
            [
272
                'auth'        => 'gigya',
273
                'verify'      => $this->certPath,
274
                'form_params' => [],
275
            ]
276
        );
277
        $client = $this->createClient('key', 'secret', Gigya::DC_AU);
278
279
        $result = $client->accounts()->getAccountInfo([]);
280
281
        static::assertSame($gigyaResponse, $result);
282
    }
283
284
    public function testSettingDataCenterToUsWillCallUsUri()
285
    {
286
        $gigyaResponse = $this->setupCall(
287
            'accounts.getAccountInfo',
288
            'https://accounts.us1.gigya.com/accounts.getAccountInfo',
289
            [
290
                'auth'        => 'gigya',
291
                'verify'      => $this->certPath,
292
                'form_params' => [],
293
            ]
294
        );
295
        $client = $this->createClient('key', 'secret', Gigya::DC_US);
296
297
        $result = $client->accounts()->getAccountInfo([]);
298
299
        static::assertSame($gigyaResponse, $result);
300
    }
301
302
    public function testSettingTheUserKeyWillPassItThroughToGuzzle()
303
    {
304
        $gigyaResponse = $this->setupCall(
305
            'accounts.getAccountInfo',
306
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
307
            [
308
                'auth'        => 'gigya',
309
                'verify'      => $this->certPath,
310
                'form_params' => [],
311
            ]
312
        );
313
        $client = $this->createClient('key', 'userSecret', Gigya::DC_EU, 'userKey');
314
        $client->setFactory($this->factory);
315
316
        $result = $client->accounts()->getAccountInfo([]);
317
318
        static::assertSame($gigyaResponse, $result);
319
    }
320
321
    public function testPassingParamsThroughToTheMethodWillPassThroughToGuzzle()
322
    {
323
        $gigyaResponse = $this->setupCall(
324
            'accounts.getAccountInfo',
325
            'https://socialize.eu1.gigya.com/socialize.notifyLogin',
326
            [
327
                'auth'        => 'gigya',
328
                'verify'      => $this->certPath,
329
                'form_params' => [
330
                    'param' => 'passedThrough',
331
                ],
332
            ]
333
        );
334
        $client = $this->createClient();
335
336
        $result = $client->socialize()->notifyLogin(['param' => 'passedThrough']);
337
338
        static::assertSame($gigyaResponse, $result);
339
    }
340
341
    public function testCallingChildMethodsCallTheCorrectUri()
342
    {
343
        $gigyaResponse = $this->setupCall(
344
            'accounts.getAccountInfo',
345
            'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig',
346
            [
347
                'auth'        => 'gigya',
348
                'verify'      => $this->certPath,
349
                'form_params' => [
350
                    'params' => 'passedThrough',
351
                ],
352
            ]
353
        );
354
        $client = $this->createClient();
355
356
        $result = $client->saml()->idp()->getConfig(['params' => 'passedThrough']);
357
358
        static::assertSame($gigyaResponse, $result);
359
    }
360
361
    public function testTfaCallingChildMethodsCallTheCorrectUri()
362
    {
363
        $gigyaResponse = $this->setupCall(
364
            'accounts.getAccountInfo',
365
            'https://accounts.eu1.gigya.com/accounts.tfa.getCertificate',
366
            [
367
                'auth'        => 'gigya',
368
                'verify'      => $this->certPath,
369
                'form_params' => [
370
                    'params' => 'passedThrough',
371
                ],
372
            ]
373
        );
374
        $client = $this->createClient();
375
376
        $result = $client->accounts()->tfa()->getCertificate(['params' => 'passedThrough']);
377
378
        static::assertSame($gigyaResponse, $result);
379
    }
380
381
    /**
382
     * @dataProvider clientCallDataProvider
383
     *
384
     * @param string $namespace
385
     * @param string $method
386
     * @param string $expectedUri
387
     */
388
    public function testClientCalls($namespace, $method, $expectedUri)
389
    {
390
        $gigyaResponse = $this->setupCall(
391
            'accounts.getAccountInfo',
392
            $expectedUri,
393
            [
394
                'auth'        => 'gigya',
395
                'verify'      => $this->certPath,
396
                'form_params' => [
397
                    'params' => 'passedThrough',
398
                ],
399
            ]
400
        );
401
        $client = $this->createClient();
402
403
        $result = $client->{$namespace}()->{$method}(['params' => 'passedThrough']);
404
405
        static::assertSame($gigyaResponse, $result);
406
    }
407
408
    /**
409
     * @expectedException \BadMethodCallException
410
     * @expectedExceptionMessage No Arguments should be supplied for Gigya call
411
     */
412
    public function testCallingMagicMethodWithArgumentsThrowsAnException()
413
    {
414
        $client = $this->createClient();
415
        $client->custom('params');
0 ignored issues
show
Bug introduced by
The method custom() does not exist on Graze\Gigya\Gigya. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

415
        $client->/** @scrutinizer ignore-call */ 
416
                 custom('params');
Loading history...
416
    }
417
418
    public function testAddingOptionsPassesThroughTheOptionsToGuzzle()
419
    {
420
        $gigyaResponse = $this->setupCall(
421
            'accounts.getAccountInfo',
422
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
423
            [
424
                'auth'        => 'gigya',
425
                'verify'      => $this->certPath,
426
                'form_params' => [
427
                    'params' => 'passedThrough',
428
                ],
429
                'option1'     => 'value1',
430
                'option2'     => false,
431
            ]
432
        );
433
        $client = $this->createClient();
434
435
        $client->addOption('option1', 'value1');
436
        $client->addOption('option2', false);
437
438
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
439
440
        static::assertSame($gigyaResponse, $result);
441
    }
442
443
    public function testAddingOptionsWithASingleCallPassesThroughTheOptionsToGuzzle()
444
    {
445
        $gigyaResponse = $this->setupCall(
446
            'accounts.getAccountInfo',
447
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
448
            [
449
                'auth'        => 'gigya',
450
                'verify'      => $this->certPath,
451
                'form_params' => [
452
                    'params' => 'passedThrough',
453
                ],
454
                'option1'     => 'value1',
455
                'option2'     => true,
456
            ]
457
        );
458
        $client = $this->createClient();
459
460
        $client->addOptions(
461
            [
462
                'option1' => 'value1',
463
                'option2' => true,
464
            ]
465
        );
466
467
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
468
469
        static::assertSame($gigyaResponse, $result);
470
    }
471
472
    public function testAddingTheSameOptionAgainWillTakeTheLastValueSet()
473
    {
474
        $gigyaResponse = $this->setupCall(
475
            'accounts.getAccountInfo',
476
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
477
            [
478
                'auth'        => 'gigya',
479
                'verify'      => $this->certPath,
480
                'form_params' => [
481
                    'params' => 'passedThrough',
482
                ],
483
                'option1'     => false,
484
            ]
485
        );
486
        $client = $this->createClient();
487
488
        $client->addOption('option1', 'value1');
489
        $client->addOption('option1', false);
490
491
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
492
493
        static::assertSame($gigyaResponse, $result);
494
    }
495
496
    public function testAddingTheSameOptionAgainWithAddOptionsWillTakeTheLastValueSet()
497
    {
498
        $gigyaResponse = $this->setupCall(
499
            'accounts.getAccountInfo',
500
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
501
            [
502
                'auth'        => 'gigya',
503
                'verify'      => $this->certPath,
504
                'form_params' => [
505
                    'params' => 'passedThrough',
506
                ],
507
                'option1'     => true,
508
            ]
509
        );
510
        $client = $this->createClient();
511
512
        $client->addOption('option1', 'value1');
513
        $client->addOptions(['option1' => true]);
514
515
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
516
517
        static::assertSame($gigyaResponse, $result);
518
    }
519
520
    public function testAddingFormParamsOptionsWillBeIgnored()
521
    {
522
        $gigyaResponse = $this->setupCall(
523
            'accounts.getAccountInfo',
524
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
525
            [
526
                'auth'        => 'gigya',
527
                'verify'      => 'notAFile',
528
                'form_params' => [
529
                    'params' => 'passedThrough',
530
                ],
531
            ]
532
        );
533
        $client = $this->createClient();
534
535
        $client->addOption('form_params', 'random');
536
        $client->addOption('verify', 'notAFile');
537
538
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough']);
539
540
        static::assertSame($gigyaResponse, $result);
541
    }
542
543
    public function testSettingOptionsAsPartOfTheQuery()
544
    {
545
        $gigyaResponse = $this->setupCall(
546
            'accounts.getAccountInfo',
547
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
548
            [
549
                'auth'        => 'gigya',
550
                'verify'      => $this->certPath,
551
                'form_params' => [
552
                    'params' => 'passedThrough',
553
                ],
554
                'custom'      => 'value',
555
            ]
556
        );
557
        $client = $this->createClient();
558
559
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough'], ['custom' => 'value']);
560
561
        static::assertSame($gigyaResponse, $result);
562
    }
563
564
    public function testSettingGlobalAndRequestOptionsTheRequestOptionsOverrideGlobalOptions()
565
    {
566
        $gigyaResponse = $this->setupCall(
567
            'accounts.getAccountInfo',
568
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
569
            [
570
                'auth'        => 'gigya',
571
                'verify'      => $this->certPath,
572
                'form_params' => [
573
                    'params' => 'passedThrough',
574
                ],
575
                'custom'      => 'value',
576
            ]
577
        );
578
        $client = $this->createClient();
579
580
        $client->addOption('custom', 'notUsed');
581
582
        $result = $client->accounts()->getAccountInfo(['params' => 'passedThrough'], ['custom' => 'value']);
583
584
        static::assertSame($gigyaResponse, $result);
585
    }
586
587
    public function testSettingRequestOptionsDoOverrideTheParams()
588
    {
589
        $gigyaResponse = $this->setupCall(
590
            'accounts.getAccountInfo',
591
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
592
            [
593
                'auth'        => 'gigya',
594
                'verify'      => false,
595
                'form_params' => [
596
                    'params' => 'passedThrough',
597
                ],
598
            ]
599
        );
600
        $client = $this->createClient();
601
602
        $result = $client->accounts()->getAccountInfo(
603
            ['params' => 'passedThrough'],
604
            ['form_params' => 'value', 'verify' => false]
605
        );
606
607
        static::assertSame($gigyaResponse, $result);
608
    }
609
610
    public function testSettingParamsWillNotOverwriteTheDefaultParams()
611
    {
612
        $gigyaResponse = $this->setupCall(
613
            'accounts.getAccountInfo',
614
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
615
            [
616
                'auth'        => 'gigya',
617
                'verify'      => $this->certPath,
618
                'form_params' => [
619
                    'secret' => 'newSecret',
620
                ],
621
            ]
622
        );
623
        $client = $this->createClient();
624
625
        $result = $client->accounts()->getAccountInfo(
626
            ['secret' => 'newSecret']
627
        );
628
629
        static::assertSame($gigyaResponse, $result);
630
    }
631
632
    public function testCallingAChainWillCallThatChain()
633
    {
634
        $gigyaResponse = $this->setupCall(
635
            'accounts.getAccountInfo',
636
            'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig',
637
            [
638
                'auth'        => 'gigya',
639
                'verify'      => $this->certPath,
640
                'form_params' => [
641
                    'secret' => 'newSecret',
642
                ],
643
            ]
644
        );
645
        $client = $this->createClient();
646
647
        $result = $client->fidm()->{'saml.idp.getConfig'}(
0 ignored issues
show
Bug introduced by
The method fidm() does not exist on Graze\Gigya\Gigya. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

647
        $result = $client->/** @scrutinizer ignore-call */ fidm()->{'saml.idp.getConfig'}(
Loading history...
648
            ['secret' => 'newSecret']
649
        );
650
651
        static::assertSame($gigyaResponse, $result);
652
    }
653
654
    public function testWillCallAValidator()
655
    {
656
        $response = $this->setupCall(
657
            'accounts.getAccountInfo',
658
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
659
            [
660
                'auth'        => 'gigya',
661
                'verify'      => $this->certPath,
662
                'form_params' => [],
663
            ]
664
        );
665
        $client = $this->createClient();
666
667
        $validator = m::mock(ResponseValidatorInterface::class);
668
        $client->addValidator($validator);
0 ignored issues
show
Bug introduced by
$validator of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Validation\ResponseValidatorInterface expected by parameter $validator of Graze\Gigya\Gigya::addValidator(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

668
        $client->addValidator(/** @scrutinizer ignore-type */ $validator);
Loading history...
669
        $validator->shouldReceive('canValidate')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'canValidate'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

669
        $validator->/** @scrutinizer ignore-call */ 
670
                    shouldReceive('canValidate')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
670
                  ->with($response)
671
                  ->andReturn(true);
672
        $validator->shouldReceive('assert')
673
                  ->with($response)
674
                  ->andReturn(true);
675
676
        static::assertSame($response, $client->accounts()->getAccountInfo());
677
    }
678
679
    public function tesWillCallMultipleValidators()
680
    {
681
        $response = $this->setupCall(
682
            'accounts.getAccountInfo',
683
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
684
            [
685
                'auth'        => 'gigya',
686
                'verify'      => $this->certPath,
687
                'form_params' => [],
688
            ]
689
        );
690
        $client = $this->createClient();
691
692
        $validator = m::mock(ResponseValidatorInterface::class);
693
        $client->addValidator($validator);
0 ignored issues
show
Bug introduced by
$validator of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Validation\ResponseValidatorInterface expected by parameter $validator of Graze\Gigya\Gigya::addValidator(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

693
        $client->addValidator(/** @scrutinizer ignore-type */ $validator);
Loading history...
694
        $validator->shouldReceive('canValidate')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'canValidate'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

694
        $validator->/** @scrutinizer ignore-call */ 
695
                    shouldReceive('canValidate')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
695
                  ->with($response)
696
                  ->andReturn(true);
697
        $validator->shouldReceive('assert')
698
                  ->with($response)
699
                  ->andReturn(true);
700
        $validator2 = m::mock(ResponseValidatorInterface::class);
701
        $client->addValidator($validator2);
702
        $validator2->shouldReceive('canValidate')
703
                   ->with($response)
704
                   ->andReturn(true);
705
        $validator2->shouldReceive('assert')
706
                   ->with($response)
707
                   ->andReturn(true);
708
709
        static::assertSame($response, $client->accounts()->getAccountInfo());
710
    }
711
712
    /**
713
     * @expectedException \Exception
714
     */
715
    public function testTheValidatorThrowingAnExceptionWillPassthrough()
716
    {
717
        $response = $this->setupCall(
718
            'accounts.getAccountInfo',
719
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
720
            [
721
                'auth'        => 'gigya',
722
                'verify'      => $this->certPath,
723
                'form_params' => [],
724
            ]
725
        );
726
        $client = $this->createClient();
727
728
        $validator = m::mock(ResponseValidatorInterface::class);
729
        $validator2 = m::mock(ResponseValidatorInterface::class);
730
        $client->addValidator($validator);
0 ignored issues
show
Bug introduced by
$validator of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Validation\ResponseValidatorInterface expected by parameter $validator of Graze\Gigya\Gigya::addValidator(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

730
        $client->addValidator(/** @scrutinizer ignore-type */ $validator);
Loading history...
731
        $client->addValidator($validator2);
732
        $exception = new Exception();
733
        $validator->shouldReceive('canValidate')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'canValidate'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

733
        $validator->/** @scrutinizer ignore-call */ 
734
                    shouldReceive('canValidate')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
734
                  ->with($response)
735
                  ->andReturn(true);
736
        $validator->shouldReceive('assert')
737
                  ->with($response)
738
                  ->andThrow($exception);
739
740
        $client->accounts()->getAccountInfo();
741
    }
742
743
    public function testTheValidatorWillOnlyCallAssertWhenItCanValidate()
744
    {
745
        $response = $this->setupCall(
746
            'accounts.getAccountInfo',
747
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
748
            [
749
                'auth'        => 'gigya',
750
                'verify'      => $this->certPath,
751
                'form_params' => [],
752
            ]
753
        );
754
        $client = $this->createClient();
755
756
        $validator = m::mock(ResponseValidatorInterface::class);
757
        $validator2 = m::mock(ResponseValidatorInterface::class);
758
        $client->addValidator($validator);
0 ignored issues
show
Bug introduced by
$validator of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Validation\ResponseValidatorInterface expected by parameter $validator of Graze\Gigya\Gigya::addValidator(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

758
        $client->addValidator(/** @scrutinizer ignore-type */ $validator);
Loading history...
759
        $client->addValidator($validator2);
760
        $validator->shouldReceive('canValidate')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'canValidate'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

760
        $validator->/** @scrutinizer ignore-call */ 
761
                    shouldReceive('canValidate')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
761
                  ->with($response)
762
                  ->andReturn(true);
763
        $validator->shouldReceive('assert')
764
                  ->with($response)
765
                  ->andReturn(true);
766
        $validator2->shouldReceive('canValidate')
767
                   ->with($response)
768
                   ->andReturn(false);
769
770
        static::assertSame($response, $client->accounts()->getAccountInfo());
771
    }
772
773
    public function testRemoveSubscriberWillDetachFromEmitter()
774
    {
775
        $response = $this->setupCall(
776
            'accounts.getAccountInfo',
777
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
778
            [
779
                'auth'        => 'gigya',
780
                'verify'      => $this->certPath,
781
                'form_params' => [],
782
            ]
783
        );
784
        $client = $this->createClient();
785
786
        static::assertSame($response, $client->accounts()->getAccountInfo());
787
788
        $fn = function (callable $handler) {
789
            return $handler;
790
        };
791
        $this->handlerStack->shouldReceive('push')
792
                           ->with($fn)
793
                           ->once();
794
795
        $client->addHandler($fn);
796
797
        $this->handlerStack->shouldReceive('remove')
798
                           ->with($fn)
799
                           ->once();
800
801
        $client->removeHandler($fn);
802
    }
803
804
    /**
805
     * @return array
806
     */
807
    public function clientCallDataProvider()
808
    {
809
        return [
810
            ['accounts', 'getAccountInfo', 'https://accounts.eu1.gigya.com/accounts.getAccountInfo'],
811
            ['accounts', 'tfa.getCertificate', 'https://accounts.eu1.gigya.com/accounts.tfa.getCertificate'],
812
            ['audit', 'search', 'https://audit.eu1.gigya.com/audit.search'],
813
            ['comments', 'analyzeMediaItem', 'https://comments.eu1.gigya.com/comments.analyzeMediaItem'],
814
            ['dataStore', 'get', 'https://ds.eu1.gigya.com/ds.get'],
815
            ['ds', 'get', 'https://ds.eu1.gigya.com/ds.get'],
816
            ['gameMechanics', 'getChallengeStatus', 'https://gm.eu1.gigya.com/gm.getChallengeStatus'],
817
            ['gm', 'getChallengeStatus', 'https://gm.eu1.gigya.com/gm.getChallengeStatus'],
818
            ['identityStorage', 'getSchema', 'https://ids.eu1.gigya.com/ids.getSchema'],
819
            ['ids', 'getSchema', 'https://ids.eu1.gigya.com/ids.getSchema'],
820
            ['reports', 'getGMStats', 'https://reports.eu1.gigya.com/reports.getGMStats'],
821
            ['saml', 'setConfig', 'https://fidm.eu1.gigya.com/fidm.saml.setConfig'],
822
            ['fidm', 'saml.setConfig', 'https://fidm.eu1.gigya.com/fidm.saml.setConfig'],
823
            ['saml', 'idp.getConfig', 'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig'],
824
            ['fidm', 'saml.idp.getConfig', 'https://fidm.eu1.gigya.com/fidm.saml.idp.getConfig'],
825
            ['socialize', 'checkin', 'https://socialize.eu1.gigya.com/socialize.checkin'],
826
        ];
827
    }
828
}
829