Skip to content

Commit d23af9f

Browse files
authored
Merge pull request #7 from sugarcrm-developers/develop
v1.0.2
2 parents 6c6b8a2 + 2df2241 commit d23af9f

File tree

9 files changed

+148
-11
lines changed

9 files changed

+148
-11
lines changed

examples/IntegrateApi.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
'sync_key' => 'foobar',
2020
]);
2121
$account->upsert();
22-
echo "Record: " . json_encode($account->toArray(), JSON_PRETTY_PRINT) . "\n";
22+
echo "Record Created: " . json_encode($account->toArray(), JSON_PRETTY_PRINT) . "\n";
23+
$account->account_type = 'Prospect';
24+
$account->email1 = 'test@example.com';
25+
$account->upsert();
26+
echo "Account Updated: " . json_encode($account->toArray(), JSON_PRETTY_PRINT) . "\n";
2327

2428
$contact = $SugarAPI->module('Contacts');
2529
$contact->set([

src/Endpoint/Abstracts/AbstractSugarBeanEndpoint.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,17 @@ protected function configurePayload(): mixed
184184
$data = $this->getData();
185185
switch ($this->getCurrentAction()) {
186186
case self::BEAN_ACTION_UPSERT:
187+
$fields = [];
188+
if (isset($data[AbstractSugarBeanCollectionEndpoint::SUGAR_FIELDS_DATA_PROPERTY])) {
189+
$fields = $data[AbstractSugarBeanCollectionEndpoint::SUGAR_FIELDS_DATA_PROPERTY];
190+
}
191+
187192
$data->reset();
188193
$data->set($this->toArray());
194+
if (!empty($fields)) {
195+
$data[AbstractSugarBeanCollectionEndpoint::SUGAR_FIELDS_DATA_PROPERTY] = $fields;
196+
}
197+
189198
$syncKeyField = $this->getSyncKeyField();
190199
if (!empty($syncKeyField)) {
191200
$data[Integrate::DATA_SYNC_KEY_FIELD] = $syncKeyField;
@@ -215,7 +224,7 @@ protected function configurePayload(): mixed
215224
protected function parseResponse(Response $response): void
216225
{
217226
$this->resetUploads();
218-
if ($response->getStatusCode() == 200) {
227+
if (in_array($response->getStatusCode(), [200,201])) {
219228
$this->getData()->reset();
220229
switch ($this->getCurrentAction()) {
221230
case self::BEAN_ACTION_TEMP_FILE_UPLOAD:
@@ -236,7 +245,17 @@ protected function parseResponse(Response $response): void
236245
return;
237246
case self::BEAN_ACTION_UPSERT:
238247
$body = $this->getResponseContent($response);
239-
$this->syncFromApi($this->parseResponseBodyToArray($body, Integrate::INTEGRATE_RESPONSE_PROP));
248+
$body = $this->parseResponseBodyToArray($body);
249+
if (!empty($body[Integrate::INTEGRATE_RESPONSE_PROP])) {
250+
if (is_string($body[Integrate::INTEGRATE_RESPONSE_PROP])) {
251+
$model = ['id' => $body[Integrate::INTEGRATE_RESPONSE_PROP]];
252+
} else {
253+
$model = $body[Integrate::INTEGRATE_RESPONSE_PROP];
254+
}
255+
256+
$this->syncFromApi($model);
257+
}
258+
240259
return;
241260
}
242261
}

src/Endpoint/Integrate.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ protected function configureAction(string $action, array $arguments = []): void
105105
$action = self::INTEGRATE_ACTION_RETRIEVE;
106106
break;
107107
}
108+
108109
if ($this->_action !== $action) {
109110
$this->_action = $action;
110111
}
@@ -159,7 +160,7 @@ protected function configurePayload(): mixed
159160

160161
protected function parseResponse(Response $response): void
161162
{
162-
if ($response->getStatusCode() == 200) {
163+
if (in_array($response->getStatusCode(), [200,201])) {
163164
switch ($this->getCurrentAction()) {
164165
case self::INTEGRATE_ACTION_DELETE:
165166
case self::MODEL_ACTION_DELETE:
@@ -174,16 +175,25 @@ protected function parseResponse(Response $response): void
174175
$syncKeyField = $this->getSyncKeyField();
175176
$syncKey = $this->getSyncKey();
176177
$this->_sugarBean->set($syncKeyField ?: self::SYNC_KEY, $syncKey);
177-
if ($syncKeyField !== '' && $syncKeyField !== '0') {
178+
if ($syncKeyField !== '' && $syncKeyField !== self::SYNC_KEY) {
178179
$this->_sugarBean->setSyncKeyField($syncKeyField);
179180
}
180181
}
181182

182183
if ($this->getCurrentAction() !== self::INTEGRATE_ACTION_SET_SK) {
183184
$body = $this->getResponseContent($response);
184-
$this->syncFromApi($this->parseResponseBodyToArray($body, $this->getModelResponseProp()));
185-
if (isset($this->_sugarBean)) {
186-
$this->_sugarBean->set($this->toArray());
185+
$body = $this->parseResponseBodyToArray($body);
186+
if (!empty($body[Integrate::INTEGRATE_RESPONSE_PROP])) {
187+
if (is_string($body[Integrate::INTEGRATE_RESPONSE_PROP])) {
188+
$model = ['id' => $body[Integrate::INTEGRATE_RESPONSE_PROP]];
189+
} else {
190+
$model = $body[Integrate::INTEGRATE_RESPONSE_PROP];
191+
}
192+
193+
$this->syncFromApi($model);
194+
if (isset($this->_sugarBean)) {
195+
$this->_sugarBean->set($this->toArray());
196+
}
187197
}
188198
}
189199
}

src/Endpoint/Note.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function clear(): static
9696
* Reset the attachments link to default blank values
9797
* @return $this
9898
*/
99-
public function resetAttachments(): self
99+
public function resetAttachments(): static
100100
{
101101
$this->resetAttachmentsProp();
102102
$this->getData()->offsetUnset($this->getAttachmentsLinkField());

src/Endpoint/Traits/IntegrateSyncKeyTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ public function getSyncKey(): string|int|null
3838

3939
if (method_exists($this, 'get')) {
4040
$key = $this->get($field);
41+
//@codeCoverageIgnoreStart
4142
} elseif (property_exists($this, '_attributes')) {
4243
$key = $this->_attributes[$field];
4344
}
4445

46+
//@codeCoverageIgnoreEnd
4547
return $key;
4648
}
4749

src/Endpoint/Traits/NoteAttachmentsTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
trait NoteAttachmentsTrait
1313
{
14-
private $_attachments = [
14+
private array $_attachments = [
1515
'add' => [],
1616
'delete' => [],
1717
'create' => [],
@@ -26,7 +26,7 @@ protected function getAttachmentsLinkField(): string
2626
* Reset the attachments link to default blank values
2727
* @return $this
2828
*/
29-
public function resetAttachments()
29+
public function resetAttachments(): static
3030
{
3131
$this->_attachments = [
3232
'add' => [],

tests/Endpoint/AbstractSugarBeanEndpointTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Sugarcrm\REST\Tests\Endpoint;
88

99
use PHPUnit\Framework\TestCase;
10+
use Sugarcrm\REST\Endpoint\Abstracts\AbstractSugarBeanEndpoint;
1011
use Sugarcrm\REST\Endpoint\AuditLog;
1112
use Sugarcrm\REST\Endpoint\Data\FilterData;
1213
use MRussell\REST\Exception\Endpoint\InvalidRequest;
@@ -51,6 +52,15 @@ public function testCompileRequest(): void
5152
$this->assertEquals("GET", $Request->getMethod());
5253
$this->assertEquals('http://localhost/rest/v11/Foo/bar', $Request->getUri()->__toString());
5354
$this->assertEmpty($Request->getBody()->getContents());
55+
56+
$Bean->setUrlArgs(['Accounts','12345']);
57+
$Bean->setCurrentAction(AbstractSugarBeanEndpoint::BEAN_ACTION_UPSERT);
58+
59+
$Bean->sync_key = '67890';
60+
$Request = $Bean->compileRequest();
61+
$this->assertEquals("PATCH", $Request->getMethod());
62+
$this->assertEquals('http://localhost/rest/v11/Accounts/sync_key/67890', $Request->getUri()->__toString());
63+
$this->assertNotEmpty($Request->getBody()->getContents());
5464
}
5565

5666
/**
@@ -756,4 +766,55 @@ public function testDownloadFile(): void
756766
$this->assertEquals("test", file_get_contents($Bean->getDownloadedFile()));
757767
unlink($Bean->getDownloadedFile());
758768
}
769+
770+
/**
771+
* @covers ::configureAction
772+
* @covers ::configurePayload
773+
* @covers ::parseResponse
774+
*/
775+
public function testUpsertAction(): void
776+
{
777+
$this->client->mockResponses->append(new Response(201, [], json_encode(['record' => '12345'])));
778+
$Bean = new SugarBean();
779+
$Bean->setClient($this->client);
780+
$Bean->setModule('Accounts');
781+
$Bean->set([
782+
'name' => 'Test Account',
783+
'account_type' => 'Prospect',
784+
'sync_key' => '098765',
785+
]);
786+
$Bean->upsert();
787+
788+
$request = $this->client->mockResponses->getLastRequest();
789+
$this->assertEquals('upsert', $Bean->getCurrentAction());
790+
$this->assertEquals('/rest/v11/Accounts/sync_key/098765', $request->getUri()->getPath());
791+
$this->assertEquals('PATCH', $request->getMethod());
792+
$payload = $request->getBody()->getContents();
793+
$this->assertEquals([
794+
'name' => 'Test Account',
795+
'account_type' => 'Prospect',
796+
'sync_key' => '098765',
797+
'sync_key_field_value' => '098765',
798+
], json_decode($payload, true));
799+
$this->assertEquals('12345', $Bean->id);
800+
801+
$this->client->mockResponses->append(new Response(201, [], json_encode(['record' => ['test' => 'foobar']])));
802+
$Bean->getData()['fields'] = ['test'];
803+
$Bean->upsert();
804+
805+
$request = $this->client->mockResponses->getLastRequest();
806+
$this->assertEquals('/rest/v11/Accounts/sync_key/098765', $request->getUri()->getPath());
807+
$this->assertEquals('PATCH', $request->getMethod());
808+
$payload = $request->getBody()->getContents();
809+
$this->assertEquals([
810+
'id' => '12345',
811+
'name' => 'Test Account',
812+
'account_type' => 'Prospect',
813+
'sync_key' => '098765',
814+
'sync_key_field_value' => '098765',
815+
'fields' => ['test'],
816+
], json_decode($payload, true));
817+
$this->assertEquals('12345', $Bean->id);
818+
$this->assertEquals('foobar', $Bean->test);
819+
}
759820
}

tests/Endpoint/IntegrateTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,42 @@ public function testConfigureAction(): void
6464
$this->assertEquals(Integrate::INTEGRATE_ACTION_DELETE, $endpoint->getCurrentAction());
6565
}
6666

67+
public function testUpsert(): void
68+
{
69+
$this->client->mockResponses->append(new Response('201', [], json_encode(['record' => '12345'])));
70+
$endpoint = new Integrate();
71+
$endpoint->setClient($this->client);
72+
$endpoint->setModule('Accounts');
73+
$endpoint['name'] = 'Test Account';
74+
$endpoint['sync_key'] = 'test';
75+
$endpoint->upsert();
76+
$request = $this->client->mockResponses->getLastRequest();
77+
$this->assertEquals('PATCH', $request->getMethod());
78+
$this->assertEquals('/rest/v11/integrate/Accounts', $request->getUri()->getPath());
79+
$body = json_decode($request->getBody()->getContents(), true);
80+
$this->assertNotEmpty($body);
81+
$this->assertEquals('test', $body[Integrate::DATA_SYNC_KEY_VALUE]);
82+
$this->assertEquals('test', $endpoint->getSyncKey());
83+
$this->assertEquals('12345', $endpoint->getId());
84+
85+
$this->client->mockResponses->append(new Response('201', [], json_encode(['record' => ['foobar_c' => 'test', 'account_type' => 'Prospect']])));
86+
$endpoint->setSyncKeyField('sync_key');
87+
$endpoint->getData()['fields'] = 'foobar_c,account_type';
88+
$endpoint->upsert();
89+
$request = $this->client->mockResponses->getLastRequest();
90+
$this->assertEquals('PATCH', $request->getMethod());
91+
$this->assertEquals('/rest/v11/integrate/Accounts/sync_key/test', $request->getUri()->getPath());
92+
$body = json_decode($request->getBody()->getContents(), true);
93+
$this->assertNotEmpty($body);
94+
$this->assertEquals('test', $body[Integrate::DATA_SYNC_KEY_VALUE]);
95+
$this->assertEquals('12345', $body['id']);
96+
$this->assertEquals('foobar_c,account_type', $body['fields']);
97+
$this->assertEquals('test', $endpoint->getSyncKey());
98+
$this->assertEquals('12345', $endpoint->getId());
99+
$this->assertEquals('test', $endpoint->foobar_c);
100+
$this->assertEquals('Prospect', $endpoint['account_type']);
101+
}
102+
67103
/**
68104
* @covers ::getSyncKey
69105
* @covers ::getSyncKeyField
@@ -161,6 +197,7 @@ public function testGetBySyncKey(): void
161197
$this->client->mockResponses->append(new Response('200', [], json_encode($this->responsePayload)));
162198
$endpoint->setCurrentAction(Integrate::MODEL_ACTION_RETRIEVE);
163199
$endpoint->execute();
200+
164201
$request = $this->client->mockResponses->getLastRequest();
165202
$this->assertEquals('GET', $request->getMethod());
166203
$this->assertEquals('/rest/v11/integrate/Accounts', $request->getUri()->getPath());

tests/Endpoint/NoteTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ protected function tearDown(): void
3636
}
3737

3838
/**
39+
* @covers \Sugarcrm\REST\Endpoint\Traits\NoteAttachmentsTrait::resetAttachments
40+
* @covers \Sugarcrm\REST\Endpoint\Traits\NoteAttachmentsTrait::getAttachmentsLinkField
3941
* @covers ::resetAttachments
4042
* @covers ::deleteAttachments
4143
* @covers ::clear
@@ -120,6 +122,8 @@ public function testParseFiles(): void
120122
* @covers ::multiAttach
121123
* @covers ::parseResponse
122124
* @covers ::configurePayload
125+
* @covers \Sugarcrm\REST\Endpoint\Traits\NoteAttachmentsTrait::configureAttachmentsPayload
126+
* @covers \Sugarcrm\REST\Endpoint\Traits\NoteAttachmentsTrait::parseAttachmentUploadResponse
123127
* @covers ::configureUrl
124128
*/
125129
public function testMultiattach(): void

0 commit comments

Comments
 (0)