Skip to content

Commit

Permalink
Add a test for listing create with inventory data (#11)
Browse files Browse the repository at this point in the history
* Add a test for listing create with inventory data

* Add comments
  • Loading branch information
sfreytag authored Jul 24, 2024
1 parent c998fc6 commit 39d7949
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Modules/ListingModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
58 changes: 58 additions & 0 deletions tests/api/ListingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Tests\Api;

use Tests\TestBase;
use CloudForest\ApiClientPhp\Dto\ListingDto;
use CloudForest\ApiClientPhp\Schema\StandardCompartment;
use CloudForest\ApiClientPhp\Schema\StandardSubCompartment;

final class ListingTest extends TestBase
{
public function testCreate(): 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);

// 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']);
}
}

0 comments on commit 39d7949

Please sign in to comment.