From 39d79490c61c421753219884bacbea2b4298ce0a Mon Sep 17 00:00:00 2001 From: Simon Freytag Date: Wed, 24 Jul 2024 17:47:35 +0100 Subject: [PATCH] Add a test for listing create with inventory data (#11) * Add a test for listing create with inventory data * Add comments --- src/Modules/ListingModule.php | 25 +++++++++++++++ tests/api/ListingTest.php | 58 +++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tests/api/ListingTest.php diff --git a/src/Modules/ListingModule.php b/src/Modules/ListingModule.php index 46ce6eb..9e98b0f 100644 --- a/src/Modules/ListingModule.php +++ b/src/Modules/ListingModule.php @@ -95,4 +95,29 @@ public function create(ListingDto $listing) $data = $this->getDataAsString($content); return $data; } + + /** + * Find one listing using its UUID. + * @param string $listingUuid + * @return mixed The listing data as an associative array. + */ + public function findOne(string $listingUuid) + { + try { + $response = $this->client->request( + 'GET', + '/api/listings/' . $listingUuid, + [ + '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 new file mode 100644 index 0000000..323686a --- /dev/null +++ b/tests/api/ListingTest.php @@ -0,0 +1,58 @@ +login(); + $this->assertIsString($access); + $this->assertNotEmpty($access); + + // Get the API client and set the access token from above. + $api = $this->getCloudForestClient(); + $api->setAccess($access); + + // Create an inventory structure + $compartment = new StandardCompartment('1', 'PHPUnit Forest', '', '', '', []); + $subcompartment = new StandardSubCompartment('2'); + $subcompartment->name = 'PHPUnit Forest SubComp 1A'; + $compartment->subCompartments = [$subcompartment]; + + // Create a listing and attach the inventory + $listing = new ListingDto(); + $listing->title = 'Test Listing from PHP Unit'; + $listing->description = 'This is a test Listing from PHP Unit'; + $listing->inventory = [$compartment]; + + // Use the API to create the listing in CloudForest + $listingUuid = $api->listing->create($listing); + $this->assertIsString($listingUuid); + $this->assertNotEmpty($listingUuid); + + // Fetch the listing from CloudForest using its UUID only + $newListing = $api->listing->findOne($listingUuid); + + // Verify the returned listing has the same data as above. + $this->assertEquals('Test Listing from PHP Unit', $newListing['title']); + $this->assertIsArray($newListing['inventory']); + $inventory = $newListing['inventory']; + $this->assertCount(1, $inventory); + $compartment = $inventory[0]; + $this->assertEquals('PHPUnit Forest', $compartment['name']); + $this->assertIsArray($compartment['subCompartments']); + $this->assertCount(1, $compartment['subCompartments']); + $subcompartment = $compartment['subCompartments'][0]; + $this->assertEquals('PHPUnit Forest SubComp 1A', $subcompartment['name']); + } +}