diff --git a/src/Modules/ListingModule.php b/src/Modules/ListingModule.php index 9e98b0f..4cde6d1 100644 --- a/src/Modules/ListingModule.php +++ b/src/Modules/ListingModule.php @@ -120,4 +120,28 @@ 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); + } }