Skip to content

Commit e7d79a6

Browse files
author
Alex Boyce
authored
Merge pull request #56 from advisors-excel-llc/develop
curiosity26/summer-19-fixes
2 parents 56e8393 + 14564c5 commit e7d79a6

File tree

7 files changed

+298
-141
lines changed

7 files changed

+298
-141
lines changed

Tests/Bayeux/BayeuxClientTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,75 @@ function (ChannelInterface $channel, Message $message) use ($name) {
109109
$this->client->start();
110110
}
111111

112+
public function testCdc()
113+
{
114+
$rand = rand(100, 1000);
115+
$name = 'Test Account '.$rand;
116+
117+
$consumer = Consumer::create(
118+
function (ChannelInterface $channel, Message $message) use ($name, &$consumer) {
119+
$this->assertTrue($message->isSuccessful());
120+
$this->assertFalse($this->client->isDisconnected());
121+
$client = new Client(['base_uri' => $this->client->getAuthProvider()->getInstanceUrl()]);
122+
$response = $client->post(
123+
'services/data/v43.0/sobjects/Account',
124+
[
125+
'headers' => [
126+
'Content-Type' => 'application/json',
127+
'Accept' => 'application/json',
128+
'Authorization' => $this->client->getAuthProvider()->authorize(),
129+
'Sforce-Call-Options' => 'client=testing_sdk',
130+
],
131+
'json' => ['Name' => $name],
132+
]
133+
);
134+
135+
$this->assertEquals(201, $response->getStatusCode());
136+
$channel->unsubscribe($consumer);
137+
}
138+
);
139+
140+
$this->client->getChannel(ChannelInterface::META_CONNECT)->subscribe($consumer);
141+
142+
$channel = $this->client->getChannel('/data/AccountChangeEvent');
143+
$channel->subscribe(
144+
Consumer::create(
145+
function (ChannelInterface $channel, Message $message) use ($name) {
146+
$this->assertEquals("/data/AccountChangeEvent", $channel->getChannelId());
147+
$data = $message->getData();
148+
$this->assertNotNull($data);
149+
$payload = $data->getPayload();
150+
$this->assertNotNull($payload);
151+
$this->assertArrayHasKey('ChangeEventHeader', $payload);
152+
$cdcHeader = $payload['ChangeEventHeader'];
153+
$this->assertNotNull($cdcHeader['recordIds'][0]);
154+
$this->assertEquals($name, $payload['Name']);
155+
$this->assertEquals(';client=testing_sdk', substr($cdcHeader['changeOrigin'], -19));
156+
157+
if (!$this->client->isDisconnected()) {
158+
$this->client->disconnect();
159+
}
160+
161+
$client = new Client(['base_uri' => $this->client->getAuthProvider()->getInstanceUrl()]);
162+
$response = $client->delete(
163+
'services/data/v43.0/sobjects/Account/'.$cdcHeader['recordIds'][0],
164+
[
165+
'headers' => [
166+
'Content-Type' => 'application/json',
167+
'Accept' => 'application/json',
168+
'Authorization' => $this->client->getAuthProvider()->authorize(),
169+
],
170+
]
171+
);
172+
173+
$this->assertEquals(204, $response->getStatusCode());
174+
}
175+
)
176+
);
177+
178+
$this->client->start();
179+
}
180+
112181
public function testHandshakeReauth()
113182
{
114183
if (!$this->client->isDisconnected()) {

Tests/Rest/ClientTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,25 @@ public function testRetry()
7777

7878
$this->assertNotNull($limits);
7979
}
80+
81+
public function testApexGet()
82+
{
83+
$res = $this->client->apex("GET", "/S3F/v1/test");
84+
85+
$this->assertEquals(['myTest' => 'is good'], $res);
86+
}
87+
88+
public function testApexPost()
89+
{
90+
$payload = [
91+
'test' => [
92+
'name' => 'Jiminy',
93+
'value' => 'Cricket'
94+
]
95+
];
96+
97+
$res = $this->client->apex("POST", "/S3F/v1/test", $payload);
98+
99+
$this->assertEquals(['name' => 'Jiminy', 'value' => 'Cricket'], $res);
100+
}
80101
}

Tests/Rest/Composite/CompositeClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function setUp()
4141
getenv("SF_PASS")
4242
),
4343
"45.0",
44-
2000
44+
"test_sdk"
4545
);
4646

4747
$this->client = $this->restClient->getCompositeClient();

src/Rest/CallOptionsTrait.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: alex.boyce
5+
* Date: 7/2/19
6+
* Time: 10:08 AM
7+
*/
8+
9+
namespace AE\SalesforceRestSdk\Rest;
10+
11+
12+
use Psr\Http\Message\RequestInterface;
13+
14+
trait CallOptionsTrait
15+
{
16+
/**
17+
* @var array
18+
*/
19+
protected $callOptions = [];
20+
21+
protected function appendSforceCallOptions(RequestInterface $request): RequestInterface
22+
{
23+
$callOptions = array_reduce(
24+
array_keys($this->callOptions),
25+
function (array $carry, $key) {
26+
$value = $this->callOptions[$key];
27+
if (strlen($value) > 0) {
28+
$carry[] = "$key=$value";
29+
}
30+
31+
return $carry;
32+
},
33+
[]
34+
);
35+
36+
if (!empty($callOptions)) {
37+
return $request->withAddedHeader('Sforce-Call-Options', implode(" ", $callOptions));
38+
}
39+
40+
return $request;
41+
}
42+
43+
/**
44+
* @param array $options
45+
*
46+
* @return self
47+
*/
48+
public function setCallOptions(array $options): self
49+
{
50+
$this->callOptions = $options;
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* @param $name
57+
* @param string $value
58+
*
59+
* @return self
60+
*/
61+
public function setCallOption($name, string $value): self
62+
{
63+
$this->callOptions[$name] = $value;
64+
65+
return $this;
66+
}
67+
68+
/**
69+
* @param $name
70+
*
71+
* @return self
72+
*/
73+
public function removeCallOption($name): self
74+
{
75+
unset($this->callOptions[$name]);
76+
77+
return $this;
78+
}
79+
}

src/Rest/Client.php

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
class Client extends AbstractClient
2929
{
30+
use CallOptionsTrait;
31+
3032
/**
3133
* @var string
3234
*/
@@ -42,11 +44,6 @@ class Client extends AbstractClient
4244
*/
4345
protected $sObjectClient;
4446

45-
/**
46-
* @var array
47-
*/
48-
protected $callOptions = [];
49-
5047
public function __construct(AuthProviderInterface $provider, string $version = "44.0", ?string $appName = null)
5148
{
5249
if (null !== $appName) {
@@ -57,18 +54,8 @@ public function __construct(AuthProviderInterface $provider, string $version = "
5754
$this->authProvider = $provider;
5855
$this->client = $this->createHttpClient();
5956
$this->serializer = $this->createSerializer();
60-
$this->compositeClient = new CompositeClient(
61-
$this->client,
62-
$this->serializer,
63-
$this->authProvider,
64-
$this->version
65-
);
66-
$this->sObjectClient = new SObject\Client(
67-
$this->client,
68-
$this->serializer,
69-
$this->authProvider,
70-
$this->version
71-
);
57+
$this->compositeClient = new CompositeClient($this);
58+
$this->sObjectClient = new SObject\Client($this);
7259
}
7360

7461
/**
@@ -138,6 +125,18 @@ public function limits(): Limits
138125
);
139126
}
140127

128+
/**
129+
* @param string $method
130+
* @param string $path
131+
* @param null $payload
132+
* @param string $responseType
133+
* @param array $headers
134+
* @param int $expectedResponseCode
135+
*
136+
* @return array|\JMS\Serializer\scalar|null|object
137+
* @throws SessionExpiredOrInvalidException
138+
* @throws \GuzzleHttp\Exception\GuzzleException
139+
*/
141140
public function apex(
142141
string $method,
143142
string $path,
@@ -152,22 +151,8 @@ public function apex(
152151
$body = null !== $payload ? $this->serializer->serialize($payload, 'json') : null;
153152
$request = new Request($method, '/services/apexrest'.$path, $headers, $body);
154153

155-
$response = $this->client->request($request);
156-
157-
try {
158-
$this->throwErrorIfInvalidResponseCode($response, $expectedResponseCode);
159-
} catch (SessionExpiredOrInvalidException $e) {
160-
return $this->apex(
161-
$method,
162-
$path,
163-
$payload,
164-
$responseType,
165-
$headers,
166-
$expectedResponseCode
167-
);
168-
}
169-
170-
$resBody = (string)$response->getBody();
154+
$response = $this->send($request, $expectedResponseCode);
155+
$resBody = (string)$response->getBody();
171156

172157
if (null !== $resBody) {
173158
return $this->serializer->deserialize(
@@ -265,12 +250,12 @@ function (HandlerRegistry $handler) {
265250
return $builder->build();
266251
}
267252

268-
protected function authorize(RequestInterface $request): RequestInterface
253+
public function authorize(RequestInterface $request): RequestInterface
269254
{
270255
return $request->withAddedHeader('Authorization', $this->authProvider->authorize());
271256
}
272257

273-
protected function appendSforceCallOptions(RequestInterface $request): RequestInterface
258+
public function appendSforceCallOptions(RequestInterface $request): RequestInterface
274259
{
275260
$callOptions = array_reduce(
276261
array_keys($this->callOptions),
@@ -292,7 +277,7 @@ function (array $carry, $key) {
292277
return $request;
293278
}
294279

295-
protected function send(RequestInterface $request, $expectedStatusCode = 200)
280+
public function send(RequestInterface $request, $expectedStatusCode = 200)
296281
{
297282
return parent::send($this->appendSforceCallOptions($request), $expectedStatusCode);
298283
}

0 commit comments

Comments
 (0)