Completed
Pull Request — master (#18)
by Harry
03:17
created

testAddingOptionsWithASingleCallPassesThroughTheOptionsToGuzzle()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 18

Duplication

Lines 28
Ratio 100 %

Importance

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

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

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