|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sugarcrm\REST\Tests\Endpoint; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use GuzzleHttp\Psr7\Response; |
| 7 | +use GuzzleHttp\Psr7\Request; |
| 8 | +use Sugarcrm\REST\Endpoint\Rest; |
| 9 | +use Sugarcrm\REST\Tests\Stubs\Client\Client; |
| 10 | + |
| 11 | +class RestTest extends TestCase |
| 12 | +{ |
| 13 | + protected Client $client; |
| 14 | + |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + $this->client = new Client(); |
| 18 | + parent::setUp(); |
| 19 | + } |
| 20 | + |
| 21 | + public function testGetRequestToCustomEndpoint() |
| 22 | + { |
| 23 | + $this->client->mockResponses->append(new Response(200, [], 'OK')); |
| 24 | + $rest = $this->client->rest('custom/endpoint'); |
| 25 | + $rest->setBaseUrl('http://localhost/rest/v11'); |
| 26 | + $rest->get(['foo' => 'bar']); |
| 27 | + $request = $this->client->mockResponses->getLastRequest(); |
| 28 | + $this->assertEquals('GET', $request->getMethod()); |
| 29 | + $this->assertEquals('http://localhost/rest/v11/custom/endpoint?foo=bar', (string) $request->getUri()); |
| 30 | + $this->assertStringContainsString('foo=bar', (string) $request->getUri()->getQuery()); |
| 31 | + } |
| 32 | + |
| 33 | + public function testPostRequestWithData() |
| 34 | + { |
| 35 | + $this->client->mockResponses->append(new Response(200, [], 'OK')); |
| 36 | + $rest = new Rest([], ['custom/endpoint']); |
| 37 | + $rest->setClient($this->client); |
| 38 | + $rest->setBaseUrl('http://localhost/rest/v11'); |
| 39 | + $data = ['foo' => 'bar', 'baz' => 'qux']; |
| 40 | + $rest->post($data); |
| 41 | + $request = $this->client->mockResponses->getLastRequest(); |
| 42 | + $this->assertEquals('POST', $request->getMethod()); |
| 43 | + $this->assertEquals('http://localhost/rest/v11/custom/endpoint', (string) $request->getUri()); |
| 44 | + $body = json_decode($request->getBody()->getContents(), true); |
| 45 | + $this->assertEquals($data, $body); |
| 46 | + } |
| 47 | + |
| 48 | + public function testPutRequestWithData() |
| 49 | + { |
| 50 | + $this->client->mockResponses->append(new Response(200, [], 'OK')); |
| 51 | + $rest = new Rest([], ['custom/endpoint']); |
| 52 | + $rest->setClient($this->client); |
| 53 | + $rest->setBaseUrl('http://localhost/rest/v11'); |
| 54 | + $data = ['foo' => 'bar']; |
| 55 | + $rest->put($data); |
| 56 | + $request = $this->client->mockResponses->getLastRequest(); |
| 57 | + $this->assertEquals('PUT', $request->getMethod()); |
| 58 | + $body = json_decode($request->getBody()->getContents(), true); |
| 59 | + $this->assertEquals($data, $body); |
| 60 | + } |
| 61 | + |
| 62 | + public function testPatchRequestWithData() |
| 63 | + { |
| 64 | + $this->client->mockResponses->append(new Response(200, [], 'OK')); |
| 65 | + $rest = new Rest([], ['custom/endpoint']); |
| 66 | + $rest->setClient($this->client); |
| 67 | + $rest->setBaseUrl('http://localhost/rest/v11'); |
| 68 | + $data = ['foo' => 'bar']; |
| 69 | + $rest->patch($data); |
| 70 | + $request = $this->client->mockResponses->getLastRequest(); |
| 71 | + $this->assertEquals('PATCH', $request->getMethod()); |
| 72 | + $body = json_decode($request->getBody()->getContents(), true); |
| 73 | + $this->assertEquals($data, $body); |
| 74 | + } |
| 75 | + |
| 76 | + public function testDeleteRequest() |
| 77 | + { |
| 78 | + $this->client->mockResponses->append(new Response(200, [], 'OK')); |
| 79 | + $rest = $this->client->rest('custom/endpoint'); |
| 80 | + $rest->setBaseUrl('http://localhost/rest/v11'); |
| 81 | + $rest->delete(); |
| 82 | + $request = $this->client->mockResponses->getLastRequest(); |
| 83 | + $this->assertEquals('DELETE', $request->getMethod()); |
| 84 | + $this->assertEquals('http://localhost/rest/v11/custom/endpoint', (string) $request->getUri()); |
| 85 | + } |
| 86 | + |
| 87 | + public function testAddCustomHeader() |
| 88 | + { |
| 89 | + $this->client->mockResponses->append(new Response(200, [], 'OK')); |
| 90 | + $rest = $this->client->rest('custom/endpoint'); |
| 91 | + $rest->setBaseUrl('http://localhost/rest/v11'); |
| 92 | + $rest->addCustomHeader('X-Test-Header', 'value1'); |
| 93 | + $rest->get(); |
| 94 | + $request = $this->client->mockResponses->getLastRequest(); |
| 95 | + $this->assertTrue($request->hasHeader('X-Test-Header')); |
| 96 | + $this->assertEquals(['value1'], $request->getHeader('X-Test-Header')); |
| 97 | + } |
| 98 | + |
| 99 | + public function testRemoveCustomHeader() |
| 100 | + { |
| 101 | + $this->client->mockResponses->append(new Response(200, [], 'OK')); |
| 102 | + $rest = $this->client->rest('custom/endpoint'); |
| 103 | + $rest->setBaseUrl('http://localhost/rest/v11'); |
| 104 | + $rest->addCustomHeader('X-Test-Header', 'value1'); |
| 105 | + $rest->removeCustomHeader('X-Test-Header'); |
| 106 | + $rest->get(); |
| 107 | + $request = $this->client->mockResponses->getLastRequest(); |
| 108 | + $this->assertFalse($request->hasHeader('X-Test-Header')); |
| 109 | + } |
| 110 | + |
| 111 | + public function testMultipleCustomHeaders() |
| 112 | + { |
| 113 | + $this->client->mockResponses->append(new Response(200, [], 'OK')); |
| 114 | + $rest = $this->client->rest('custom/endpoint'); |
| 115 | + $rest->setBaseUrl('http://localhost/rest/v11'); |
| 116 | + $rest->addCustomHeader('X-Test-Header', 'value1'); |
| 117 | + $rest->addCustomHeader('Another-Header', 'value2'); |
| 118 | + $rest->get(); |
| 119 | + $request = $this->client->mockResponses->getLastRequest(); |
| 120 | + $this->assertTrue($request->hasHeader('X-Test-Header')); |
| 121 | + $this->assertEquals(['value1'], $request->getHeader('X-Test-Header')); |
| 122 | + $this->assertTrue($request->hasHeader('Another-Header')); |
| 123 | + $this->assertEquals(['value2'], $request->getHeader('Another-Header')); |
| 124 | + } |
| 125 | +} |
0 commit comments