Skip to content

Commit f5ccbaf

Browse files
author
Harry Bragg
committed
giggedy
1 parent aa1052e commit f5ccbaf

File tree

7 files changed

+136
-45
lines changed

7 files changed

+136
-45
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
All Notable changes to `gigya-client` will be documented in this file
44

5+
## 0.2 - 2015-10-20
6+
7+
### Changed
8+
- Moved Subscribers and Validators to the Gigya object, call these directly
9+
- Constructor of Gigya takes a configuration array with properties
10+
- modification to the order of arguments
11+
- Authentication is done using a Guzzle subscriber and you can supply your own
12+
- You can supply your own response factory for custom response handling
13+
514
## 0.1 - 2015-10-03
615

716
### Added

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# gigya-client
22

3+
<img align="right" src="http://stuffpoint.com/family-guy/image/15298-family-guy-giggedy.gif" width="250" />
4+
35
[![Latest Version on Packagist](https://img.shields.io/packagist/v/graze/gigya-client.svg?style=flat-square)](https://packagist.org/packages/graze/gigya-client)
46
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
57
[![Build Status](https://img.shields.io/travis/graze/gigya-client/master.svg?style=flat-square)](https://travis-ci.org/graze/gigya-client)

src/Endpoint/Client.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
namespace Graze\Gigya\Endpoint;
44

55
use Exception;
6-
use Graze\Gigya\Auth\GigyaAuthInterface;
76
use Graze\Gigya\Response\ResponseFactory;
87
use Graze\Gigya\Response\ResponseFactoryInterface;
98
use Graze\Gigya\Response\ResponseInterface;
109
use Graze\Gigya\Validation\ResponseValidatorInterface;
1110
use GuzzleHttp\Client as GuzzleClient;
12-
use GuzzleHttp\Event\SubscriberInterface;
1311
use GuzzleHttp\Exception\RequestException;
1412

1513
// use Psr\Http\Message\ResponseInterface; Guzzle v6
@@ -40,11 +38,6 @@ class Client
4038
*/
4139
protected $namespace;
4240

43-
/**
44-
* @var GigyaAuthInterface
45-
*/
46-
protected $auth;
47-
4841
/**
4942
* @var GuzzleClient
5043
*/
@@ -55,11 +48,6 @@ class Client
5548
*/
5649
protected $config;
5750

58-
/**
59-
* @var SubscriberInterface[]
60-
*/
61-
protected $subscribers = [];
62-
6351
/**
6452
* @var ResponseValidatorInterface[]
6553
*/
@@ -151,6 +139,7 @@ public function request($method, array $params = [], array $options = [])
151139
$response = $this->factory->getResponse($guzzleResponse);
152140

153141
$this->assert($response);
142+
154143
return $response;
155144
}
156145

@@ -176,7 +165,7 @@ public function __call($method, $arguments)
176165
*
177166
* @return Client
178167
*/
179-
protected function clientFactory($className = Client::class)
168+
protected function clientFactory($className = self::class)
180169
{
181170
return new $className(
182171
$this->client,

src/Gigya.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ class Gigya
9292
* @param string $apiKey
9393
* @param string $secretKey
9494
* @param string|null $userKey
95-
* @param array $config Gigya configuration:
96-
* - dataCenter <string> (Default: DC_EU) Data Center to use
97-
* - auth <string> (Default: gigya) Type of authentication, gigya (HttpsAuth) is the
98-
* default
99-
* - uidValidator <bool> (Default: true) Include Uid Signature Validation
100-
* - factory <object> (Default: null) A ResponseFactoryInterface to use, if none is
101-
* provided ResponseFactory will be used
102-
* - guzzle <array> (Default: []) A configuration to pass to guzzle if required
103-
* - options <array> (Default: []) A set of options to pass to each request
95+
* @param array $config Gigya configuration:
96+
* - dataCenter <string> (Default: DC_EU) Data Center to use
97+
* - auth <string> (Default: gigya) Type of authentication, gigya (HttpsAuth) is the
98+
* default
99+
* - uidValidator <bool> (Default: true) Include Uid Signature Validation
100+
* - factory <object> (Default: null) A ResponseFactoryInterface to use, if none is
101+
* provided ResponseFactory will be used
102+
* - guzzle <array> (Default: []) A configuration to pass to guzzle if required
103+
* - options <array> (Default: []) A set of options to pass to each request
104104
*/
105105
public function __construct($apiKey, $secretKey, $userKey = null, array $config = [])
106106
{
@@ -188,6 +188,18 @@ public function addSubscriber(SubscriberInterface $subscriber)
188188
return $this;
189189
}
190190

191+
/**
192+
* @param SubscriberInterface $subscriber
193+
*
194+
* @return $this
195+
*/
196+
public function removeSubscriber(SubscriberInterface $subscriber)
197+
{
198+
$this->guzzle->getEmitter()->detach($subscriber);
199+
200+
return $this;
201+
}
202+
191203
/**
192204
* @param ResponseFactoryInterface $factory
193205
*

src/Response/ResponseFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function getResponse(GuzzleResponseInterface $response)
2323
} else {
2424
$result = new Response($response);
2525
}
26+
2627
return $result;
2728
}
2829
}

tests/functional/GigyaTest.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
use Graze\Gigya\Test\TestCase;
99
use Graze\Gigya\Test\TestFixtures;
1010
use Graze\Gigya\Validation\Signature;
11+
use GuzzleHttp\Event\SubscriberInterface;
1112
use GuzzleHttp\Message\Response;
1213
use GuzzleHttp\Stream\Stream;
1314
use GuzzleHttp\Subscriber\History;
1415
use GuzzleHttp\Subscriber\Mock;
16+
use Mockery as m;
1517

1618
class GigyaTest extends TestCase
1719
{
1820
/**
1921
* @param Gigya $gigya
20-
* @param string|null $body Optional body text
22+
* @param string|null $body Optional body text
2123
*
2224
* @return History
2325
*/
@@ -32,6 +34,20 @@ public function setUpGigyaHistory(Gigya $gigya, $body = null)
3234
$body ?: TestFixtures::getFixture('basic')
3335
)
3436
),
37+
new Response(
38+
'200',
39+
[],
40+
Stream::factory(
41+
$body ?: TestFixtures::getFixture('basic')
42+
)
43+
),
44+
new Response(
45+
'200',
46+
[],
47+
Stream::factory(
48+
$body ?: TestFixtures::getFixture('basic')
49+
)
50+
),
3551
]);
3652
$gigya->addSubscriber($history);
3753
$gigya->addSubscriber($mock);
@@ -197,7 +213,7 @@ public function testUidSignatureWhenInvalidSignatureThrowsAnException()
197213
$client->accounts()->getAccountInfo(['uid' => $uid]);
198214
}
199215

200-
public function testRequestWillThrowTimestampExpceptionWhenBothTimestampAndSignatureAreInvalid()
216+
public function testRequestWillThrowTimestampExceptionWhenBothTimestampAndSignatureAreInvalid()
201217
{
202218
$uid = 'diofu90ifgdf';
203219
$timestamp = time() - 181;
@@ -227,4 +243,29 @@ public function testRequestWillThrowTimestampExpceptionWhenBothTimestampAndSigna
227243

228244
$client->accounts()->getAccountInfo(['uid' => $uid]);
229245
}
246+
247+
public function testGigyaWillTriggerSubscriberOnlyWhenItIsAddedInARequest()
248+
{
249+
$client = new Gigya('key', 'secret');
250+
$this->setUpGigyaHistory($client);
251+
252+
$client->accounts()->getAccountInfo();
253+
254+
$subscriber = m::mock(SubscriberInterface::class);
255+
$subscriber->shouldReceive('getEvents')
256+
->andReturn([
257+
'complete' => ['someMethod'],
258+
]);
259+
260+
$client->addSubscriber($subscriber);
261+
262+
$subscriber->shouldReceive('someMethod')
263+
->once();
264+
265+
$client->accounts()->getAccountInfo();
266+
267+
$client->removeSubscriber($subscriber);
268+
269+
$client->accounts()->getAccountInfo();
270+
}
230271
}

tests/unit/GigyaTest.php

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,19 @@ class GigyaTest extends TestCase
3636
*/
3737
private $certPath;
3838

39+
/**
40+
* @var EmitterInterface|MockInterface
41+
*/
42+
private $emitter;
43+
3944
public function setUp()
4045
{
4146
$this->guzzleClient = m::mock('overload:GuzzleHttp\Client');
42-
$this->factory = m::mock('Graze\Gigya\Response\ResponseFactoryInterface');
43-
$this->certPath = realpath(__DIR__ . '/../../src/' . Gigya::CERTIFICATE_FILE);
47+
$this->emitter = m::mock(EmitterInterface::class);
48+
$this->guzzleClient->shouldReceive('getEmitter')
49+
->andReturn($this->emitter);
50+
$this->factory = m::mock('Graze\Gigya\Response\ResponseFactoryInterface');
51+
$this->certPath = realpath(__DIR__ . '/../../src/' . Gigya::CERTIFICATE_FILE);
4452
}
4553

4654
public function tearDown()
@@ -63,6 +71,7 @@ public function createClient($key = 'key', $secret = 'secret', $dc = Gigya::DC_E
6371
'uidValidator' => false,
6472
'factory' => $this->factory,
6573
];
74+
6675
return new Gigya($key, $secret, $userKey, $options);
6776
}
6877

@@ -75,23 +84,20 @@ public function createClient($key = 'key', $secret = 'secret', $dc = Gigya::DC_E
7584
*/
7685
private function setupSubscribers($key, $secret, $userKey = null)
7786
{
78-
$emitter = m::mock(EmitterInterface::class);
79-
$this->guzzleClient->shouldReceive('getEmitter')
80-
->andReturn($emitter);
81-
$emitter->shouldReceive('attach')
82-
->with(m::type(ValidGigyaResponseSubscriber::class))
83-
->once();
84-
$emitter->shouldReceive('attach')
85-
->with(m::on(function (SubscriberInterface $subscriber) use ($key, $secret, $userKey) {
86-
if ($subscriber instanceof GigyaAuthInterface) {
87-
static::assertEquals($key, $subscriber->getApiKey());
88-
static::assertEquals($secret, $subscriber->getSecret());
89-
static::assertEquals($userKey, $subscriber->getUserKey());
90-
}
91-
92-
return true;
93-
}))
94-
->once();
87+
$this->emitter->shouldReceive('attach')
88+
->with(m::type(ValidGigyaResponseSubscriber::class))
89+
->once();
90+
$this->emitter->shouldReceive('attach')
91+
->with(m::on(function (SubscriberInterface $subscriber) use ($key, $secret, $userKey) {
92+
if ($subscriber instanceof GigyaAuthInterface) {
93+
static::assertEquals($key, $subscriber->getApiKey());
94+
static::assertEquals($secret, $subscriber->getSecret());
95+
static::assertEquals($userKey, $subscriber->getUserKey());
96+
}
97+
98+
return true;
99+
}))
100+
->once();
95101
}
96102

97103
/**
@@ -521,7 +527,7 @@ public function testSettingRequestOptionsDoOverrideTheParams()
521527

522528
$result = $client->accounts()->getAccountInfo(
523529
['params' => 'passedThrough'],
524-
['query' => 'value', 'verify' => false]
530+
['query' => 'value', 'verify' => false]
525531
);
526532

527533
static::assertSame($gigyaResponse, $result);
@@ -568,7 +574,7 @@ public function testCallingAChainWillCallThatChain()
568574
);
569575
$client = $this->createClient();
570576

571-
$result = $client->fidm()->{"saml.idp.getConfig"}(
577+
$result = $client->fidm()->{'saml.idp.getConfig'}(
572578
['secret' => 'newSecret']
573579
);
574580

@@ -701,6 +707,37 @@ public function testTheValidatorWillOnlyCallAssertWhenItCanValidate()
701707
static::assertSame($response, $client->accounts()->getAccountInfo());
702708
}
703709

710+
public function testRemoveSubscriberWillDetachFromEmitter()
711+
{
712+
$response = $this->setupCall(
713+
'accounts.getAccountInfo',
714+
'https://accounts.eu1.gigya.com/accounts.getAccountInfo',
715+
[
716+
'auth' => 'gigya',
717+
'verify' => $this->certPath,
718+
'query' => [],
719+
],
720+
'key',
721+
'secret'
722+
);
723+
$client = $this->createClient();
724+
725+
static::assertSame($response, $client->accounts()->getAccountInfo());
726+
727+
$subscriber = m::mock(SubscriberInterface::class);
728+
$this->emitter->shouldReceive('attach')
729+
->with($subscriber)
730+
->once();
731+
732+
$client->addSubscriber($subscriber);
733+
734+
$this->emitter->shouldReceive('detach')
735+
->with($subscriber)
736+
->once();
737+
738+
$client->removeSubscriber($subscriber);
739+
}
740+
704741
public function clientCallDataProvider()
705742
{
706743
return [

0 commit comments

Comments
 (0)