Skip to content

Commit 923e976

Browse files
committed
fineOwn method, and listing lookup tests
1 parent 1dcd0ac commit 923e976

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/Modules/ListingModule.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,27 @@ public function findOne(string $listingUuid)
120120
$data = $this->getDataAsArray($content);
121121
return $data;
122122
}
123+
124+
/**
125+
* Find all the listings belonging to the user associated with the JWT.
126+
* @return mixed The listing data as an associative array.
127+
*/
128+
public function findOwn() {
129+
try {
130+
$response = $this->client->request(
131+
'GET',
132+
'/api/listings/own',
133+
[
134+
'headers' => $this->getHeadersWithAccessBearer(),
135+
]
136+
);
137+
} catch (GuzzleException $e) {
138+
throw $e;
139+
}
140+
141+
$body = $response->getBody()->getContents();
142+
$content = json_decode($body, true);
143+
$data = $this->getDataAsArray($content);
144+
return $data;
145+
}
123146
}

tests/api/ListingTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,41 @@ public function testCreate(): void
157157
$this->assertEquals('1976', $inventory['plantingYear']);
158158
$this->assertEquals('1267.13', $inventory['volumePerSubcompartment']);
159159
}
160+
161+
// A test to lookup all of a user's listings and then get more information
162+
// about one of them.
163+
public function testLookup(): void
164+
{
165+
// This is a short cut for now - log in directly using the stored user/pass
166+
// Eventually this should use an access token obtained from the OAuth exchange
167+
$access = $this->login();
168+
$this->assertIsString($access);
169+
$this->assertNotEmpty($access);
170+
171+
// Get the API client and set the access token from above.
172+
$api = $this->getCloudForestClient();
173+
$api->setAccess($access);
174+
175+
// Get all of the user's listings
176+
$ownListings = $api->listing->findOwn();
177+
178+
// We don't know how many users there will be are because the marketplace
179+
// test server is out of our control here. But there should be at least
180+
// one because we created one above.
181+
$this->assertGreaterThan(1, count($ownListings));
182+
183+
// Get the first listing
184+
$first = $ownListings[0];
185+
$title = $first['title'];
186+
$state = $first['state'];
187+
$this->assertIsString($title);
188+
$this->assertIsString($state);
189+
190+
// Verify that we can also get this listing by its UUID
191+
$uuid = $first['id'];
192+
$listing = $api->listing->findOne($uuid);
193+
$this->assertIsArray($listing);
194+
$this->assertEquals($listing['title'], $title);
195+
$this->assertEquals($listing['state'], $state);
196+
}
160197
}

0 commit comments

Comments
 (0)