Skip to content

Commit 57ee841

Browse files
committed
Fix up Upsert Response Handling + Test Coverage
1 parent 19bd6ae commit 57ee841

File tree

9 files changed

+140
-12
lines changed

9 files changed

+140
-12
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: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,15 @@ 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+
}
187191
$data->reset();
188192
$data->set($this->toArray());
193+
if (!empty($fields)) {
194+
$data[AbstractSugarBeanCollectionEndpoint::SUGAR_FIELDS_DATA_PROPERTY] = $fields;
195+
}
189196
$syncKeyField = $this->getSyncKeyField();
190197
if (!empty($syncKeyField)) {
191198
$data[Integrate::DATA_SYNC_KEY_FIELD] = $syncKeyField;
@@ -215,7 +222,7 @@ protected function configurePayload(): mixed
215222
protected function parseResponse(Response $response): void
216223
{
217224
$this->resetUploads();
218-
if ($response->getStatusCode() == 200) {
225+
if (in_array($response->getStatusCode(), [200,201])) {
219226
$this->getData()->reset();
220227
switch ($this->getCurrentAction()) {
221228
case self::BEAN_ACTION_TEMP_FILE_UPLOAD:
@@ -236,7 +243,15 @@ protected function parseResponse(Response $response): void
236243
return;
237244
case self::BEAN_ACTION_UPSERT:
238245
$body = $this->getResponseContent($response);
239-
$this->syncFromApi($this->parseResponseBodyToArray($body, Integrate::INTEGRATE_RESPONSE_PROP));
246+
$body = $this->parseResponseBodyToArray($body);
247+
if (!empty($body[Integrate::INTEGRATE_RESPONSE_PROP])) {
248+
if (is_string($body[Integrate::INTEGRATE_RESPONSE_PROP])) {
249+
$model = ['id' => $body[Integrate::INTEGRATE_RESPONSE_PROP]];
250+
} else {
251+
$model = $body[Integrate::INTEGRATE_RESPONSE_PROP];
252+
}
253+
$this->syncFromApi($model);
254+
}
240255
return;
241256
}
242257
}

src/Endpoint/Integrate.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ protected function configurePayload(): mixed
159159

160160
protected function parseResponse(Response $response): void
161161
{
162-
if ($response->getStatusCode() == 200) {
162+
if (in_array($response->getStatusCode(), [200,201])) {
163163
switch ($this->getCurrentAction()) {
164164
case self::INTEGRATE_ACTION_DELETE:
165165
case self::MODEL_ACTION_DELETE:
@@ -174,16 +174,24 @@ protected function parseResponse(Response $response): void
174174
$syncKeyField = $this->getSyncKeyField();
175175
$syncKey = $this->getSyncKey();
176176
$this->_sugarBean->set($syncKeyField ?: self::SYNC_KEY, $syncKey);
177-
if ($syncKeyField !== '' && $syncKeyField !== '0') {
177+
if ($syncKeyField !== '' && $syncKeyField !== self::SYNC_KEY) {
178178
$this->_sugarBean->setSyncKeyField($syncKeyField);
179179
}
180180
}
181181

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

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ 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
}
44-
45+
//@codeCoverageIgnoreEnd
4546
return $key;
4647
}
4748

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: 60 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,14 @@ 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+
$Bean->sync_key = '67890';
59+
$Request = $Bean->compileRequest();
60+
$this->assertEquals("PATCH", $Request->getMethod());
61+
$this->assertEquals('http://localhost/rest/v11/Accounts/sync_key/67890', $Request->getUri()->__toString());
62+
$this->assertNotEmpty($Request->getBody()->getContents());
5463
}
5564

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

tests/Endpoint/IntegrateTest.php

Lines changed: 36 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

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)