|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hypernode\Api\Service; |
| 6 | + |
| 7 | +use GuzzleHttp\Psr7\Response; |
| 8 | +use Hypernode\Api\Exception\HypernodeApiClientException; |
| 9 | +use Hypernode\Api\Exception\HypernodeApiServerException; |
| 10 | +use Hypernode\Api\HypernodeClientTestCase; |
| 11 | + |
| 12 | +class InsightsAnnotationTest extends HypernodeClientTestCase |
| 13 | +{ |
| 14 | + public function testGetList() |
| 15 | + { |
| 16 | + $annotations = [ |
| 17 | + [ |
| 18 | + 'name' => 'Deployed release 1.2.3', |
| 19 | + 'x_axis' => 1756384957, |
| 20 | + 'app_name' => 'johndoe', |
| 21 | + 'metrics_list' => ['memory_usage'], |
| 22 | + 'metadata' => [], |
| 23 | + ], |
| 24 | + ]; |
| 25 | + $this->responses->append( |
| 26 | + new Response(200, [], json_encode([ |
| 27 | + 'count' => 1, |
| 28 | + 'next' => null, |
| 29 | + 'previous' => null, |
| 30 | + 'results' => $annotations, |
| 31 | + ])), |
| 32 | + ); |
| 33 | + |
| 34 | + $result = $this->client->insightsAnnotation->getList(); |
| 35 | + |
| 36 | + $request = $this->responses->getLastRequest(); |
| 37 | + $this->assertEquals('GET', $request->getMethod()); |
| 38 | + $this->assertEquals('/v2/insights-annotation/', $request->getUri()); |
| 39 | + $this->assertEquals($annotations, $result); |
| 40 | + } |
| 41 | + |
| 42 | + public function testGetListAcceptsParams() |
| 43 | + { |
| 44 | + $this->responses->append( |
| 45 | + new Response(200, [], json_encode([ |
| 46 | + 'count' => 0, |
| 47 | + 'next' => null, |
| 48 | + 'previous' => null, |
| 49 | + 'results' => [], |
| 50 | + ])), |
| 51 | + ); |
| 52 | + |
| 53 | + $result = $this->client->insightsAnnotation->getList(['limit' => 10, 'offset' => 20]); |
| 54 | + |
| 55 | + $request = $this->responses->getLastRequest(); |
| 56 | + $this->assertEquals('GET', $request->getMethod()); |
| 57 | + $this->assertEquals('/v2/insights-annotation/?limit=10&offset=20', $request->getUri()); |
| 58 | + $this->assertEquals([], $result); |
| 59 | + } |
| 60 | + |
| 61 | + public function testGetListPaginates() |
| 62 | + { |
| 63 | + $this->responses->append( |
| 64 | + new Response(200, [], json_encode([ |
| 65 | + 'count' => 2, |
| 66 | + 'next' => 'https://api.hypernode.com/v2/insights-annotation/?limit=1&offset=1', |
| 67 | + 'previous' => null, |
| 68 | + 'results' => [ |
| 69 | + [ |
| 70 | + 'name' => 'Deployed release 1.2.3', |
| 71 | + 'x_axis' => 1756384957, |
| 72 | + 'app_name' => 'johndoe', |
| 73 | + ], |
| 74 | + ], |
| 75 | + ])), |
| 76 | + new Response(200, [], json_encode([ |
| 77 | + 'count' => 2, |
| 78 | + 'next' => null, |
| 79 | + 'previous' => null, |
| 80 | + 'results' => [ |
| 81 | + [ |
| 82 | + 'name' => 'Deployed release 1.2.4', |
| 83 | + 'x_axis' => 1756395957, |
| 84 | + 'app_name' => 'johndoe', |
| 85 | + ], |
| 86 | + ], |
| 87 | + ])), |
| 88 | + ); |
| 89 | + |
| 90 | + $result = $this->client->insightsAnnotation->getList(); |
| 91 | + |
| 92 | + $this->assertCount(2, $result); |
| 93 | + $this->assertEquals('Deployed release 1.2.3', $result[0]['name']); |
| 94 | + $this->assertEquals('Deployed release 1.2.4', $result[1]['name']); |
| 95 | + |
| 96 | + $request = $this->responses->getLastRequest(); |
| 97 | + $this->assertEquals('GET', $request->getMethod()); |
| 98 | + $this->assertEquals( |
| 99 | + 'https://api.hypernode.com/v2/insights-annotation/?limit=1&offset=1', |
| 100 | + $request->getUri() |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + public function testGetListRaisesClientExceptions() |
| 105 | + { |
| 106 | + $badRequestResponse = new Response(400, [], json_encode([ |
| 107 | + 'non_field_errors' => ['Your request was invalid.'] |
| 108 | + ])); |
| 109 | + $this->responses->append($badRequestResponse); |
| 110 | + |
| 111 | + $this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse)); |
| 112 | + |
| 113 | + $this->client->insightsAnnotation->getList(); |
| 114 | + } |
| 115 | + |
| 116 | + public function testGetListRaisesServerExceptions() |
| 117 | + { |
| 118 | + $badRequestResponse = new Response(500, [], json_encode([ |
| 119 | + 'non_field_errors' => ['Something went wrong processing your request.'] |
| 120 | + ])); |
| 121 | + $this->responses->append($badRequestResponse); |
| 122 | + |
| 123 | + $this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse)); |
| 124 | + |
| 125 | + $this->client->insightsAnnotation->getList(); |
| 126 | + } |
| 127 | +} |
0 commit comments