From 923e9761efddc056f8b13afe528ea44b1b640701 Mon Sep 17 00:00:00 2001 From: sfreytag Date: Mon, 16 Dec 2024 10:22:13 +0000 Subject: [PATCH 1/2] fineOwn method, and listing lookup tests --- src/Modules/ListingModule.php | 23 ++++++++++++++++++++++ tests/api/ListingTest.php | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/Modules/ListingModule.php b/src/Modules/ListingModule.php index 9e98b0f..9335033 100644 --- a/src/Modules/ListingModule.php +++ b/src/Modules/ListingModule.php @@ -120,4 +120,27 @@ public function findOne(string $listingUuid) $data = $this->getDataAsArray($content); return $data; } + + /** + * Find all the listings belonging to the user associated with the JWT. + * @return mixed The listing data as an associative array. + */ + public function findOwn() { + try { + $response = $this->client->request( + 'GET', + '/api/listings/own', + [ + 'headers' => $this->getHeadersWithAccessBearer(), + ] + ); + } catch (GuzzleException $e) { + throw $e; + } + + $body = $response->getBody()->getContents(); + $content = json_decode($body, true); + $data = $this->getDataAsArray($content); + return $data; + } } diff --git a/tests/api/ListingTest.php b/tests/api/ListingTest.php index 5dd704b..7421bed 100644 --- a/tests/api/ListingTest.php +++ b/tests/api/ListingTest.php @@ -157,4 +157,41 @@ public function testCreate(): void $this->assertEquals('1976', $inventory['plantingYear']); $this->assertEquals('1267.13', $inventory['volumePerSubcompartment']); } + + // A test to lookup all of a user's listings and then get more information + // about one of them. + public function testLookup(): void + { + // This is a short cut for now - log in directly using the stored user/pass + // Eventually this should use an access token obtained from the OAuth exchange + $access = $this->login(); + $this->assertIsString($access); + $this->assertNotEmpty($access); + + // Get the API client and set the access token from above. + $api = $this->getCloudForestClient(); + $api->setAccess($access); + + // Get all of the user's listings + $ownListings = $api->listing->findOwn(); + + // We don't know how many users there will be are because the marketplace + // test server is out of our control here. But there should be at least + // one because we created one above. + $this->assertGreaterThan(1, count($ownListings)); + + // Get the first listing + $first = $ownListings[0]; + $title = $first['title']; + $state = $first['state']; + $this->assertIsString($title); + $this->assertIsString($state); + + // Verify that we can also get this listing by its UUID + $uuid = $first['id']; + $listing = $api->listing->findOne($uuid); + $this->assertIsArray($listing); + $this->assertEquals($listing['title'], $title); + $this->assertEquals($listing['state'], $state); + } } From fe5ce16841a8ded23141363f9df0b7098ac23cee Mon Sep 17 00:00:00 2001 From: sfreytag Date: Mon, 16 Dec 2024 10:29:29 +0000 Subject: [PATCH 2/2] cs fix --- src/Modules/ListingModule.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Modules/ListingModule.php b/src/Modules/ListingModule.php index 9335033..4cde6d1 100644 --- a/src/Modules/ListingModule.php +++ b/src/Modules/ListingModule.php @@ -125,7 +125,8 @@ public function findOne(string $listingUuid) * Find all the listings belonging to the user associated with the JWT. * @return mixed The listing data as an associative array. */ - public function findOwn() { + public function findOwn() + { try { $response = $this->client->request( 'GET', @@ -134,7 +135,7 @@ public function findOwn() { 'headers' => $this->getHeadersWithAccessBearer(), ] ); - } catch (GuzzleException $e) { + } catch (GuzzleException $e) { throw $e; }