|
7 | 7 | namespace Sugarcrm\REST\Tests\Endpoint; |
8 | 8 |
|
9 | 9 | use PHPUnit\Framework\TestCase; |
| 10 | +use Sugarcrm\REST\Endpoint\Abstracts\AbstractSugarBeanEndpoint; |
10 | 11 | use Sugarcrm\REST\Endpoint\AuditLog; |
11 | 12 | use Sugarcrm\REST\Endpoint\Data\FilterData; |
12 | 13 | use MRussell\REST\Exception\Endpoint\InvalidRequest; |
@@ -51,6 +52,15 @@ public function testCompileRequest(): void |
51 | 52 | $this->assertEquals("GET", $Request->getMethod()); |
52 | 53 | $this->assertEquals('http://localhost/rest/v11/Foo/bar', $Request->getUri()->__toString()); |
53 | 54 | $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()); |
54 | 64 | } |
55 | 65 |
|
56 | 66 | /** |
@@ -756,4 +766,55 @@ public function testDownloadFile(): void |
756 | 766 | $this->assertEquals("test", file_get_contents($Bean->getDownloadedFile())); |
757 | 767 | unlink($Bean->getDownloadedFile()); |
758 | 768 | } |
| 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 | + } |
759 | 820 | } |
0 commit comments