Skip to content

Commit

Permalink
Merge pull request #207 from MyParcelCOM/qa/MP-6434-update-collection…
Browse files Browse the repository at this point in the history
…-errors

MP-6434 Update collection errors
  • Loading branch information
M4tini authored Feb 27, 2024
2 parents 9bc9438 + 28c7839 commit 33e1e09
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 76 deletions.
19 changes: 12 additions & 7 deletions src/MyParcelComApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,10 @@ public function createCollection(CollectionInterface $collection): CollectionInt
return $this->postResource($collection);
}

/**
* Updates the collection with allowed attributes and relationships.
* @see https://api-specification.myparcel.com/#tag/Collections/paths/~1collections~1%7Bcollection_id%7D/patch
*/
public function updateCollection(CollectionInterface $collection): CollectionInterface
{
if (!$collection->getId()) {
Expand All @@ -677,16 +681,17 @@ public function updateCollection(CollectionInterface $collection): CollectionInt
);
}

$validator = new CollectionValidator($collection);
if (!$validator->isValid()) {
$message = 'This collection contains invalid data. ' . implode('. ', $validator->getErrors()) . '.';
$exception = new InvalidResourceException($message);
$exception->setErrors($validator->getErrors());
$collectionToUpdate = (new Collection())
->setId($collection->getId())
->setDescription($collection->getDescription())
->setRegister($collection->getRegister());

throw $exception;
// Only the `collection-collected` status can be manually assigned by users.
if ($collection->getStatus()?->getCode() === 'collection-collected') {
$collectionToUpdate->setStatus($collection->getStatus());
}

return $this->patchResource($collection);
return $this->patchResource($collectionToUpdate);
}

public function registerCollection(CollectionInterface|string $collectionId): CollectionInterface
Expand Down
69 changes: 0 additions & 69 deletions tests/Feature/MyParcelComApi/CollectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,6 @@ public function testItUpdatesACollection(): void
{
$collection = new Collection();
$collection->setId('65ddc1c8-8e16-41e3-a383-c8a5eac68caa');
$collection->setShop(
(new Shop())->setId('1ebabb0e-9036-4259-b58e-2b42742bb86a')
);
$collection->setCollectionTime(
(new CollectionTime())->setFrom(1708085160)->setTo(1708096680)
);
$collection->setAddress(
(new Address())
->setStreet1('Updated street 1')
->setCity('Updated city')
->setCountryCode('NL')
);
$collection->setDescription('Updated description');

$updatedCollection = $this->api->updateCollection($collection);
Expand Down Expand Up @@ -175,63 +163,6 @@ public function testItCannotUpdateACollectionWithoutAnId(): void
$this->api->updateCollection($collection);
}

public function testItFailsToUpdateWhenNoCollectionTimeIsSet(): void
{
$collectionToUpdate = new Collection();
$collectionToUpdate->setId('65ddc1c8-8e16-41e3-a383-c8a5eac68caa');

$collectionToUpdate->setAddress(
(new Address())
->setStreet1('Test street 1')
->setCity('Test city')
->setCountryCode('NL')
);

$collectionToUpdate->setShop(
(new Shop())->setId('1ebabb0e-9036-4259-b58e-2b42742bb86a')
);

$this->expectException(InvalidResourceException::class);
$this->expectExceptionMessage('This collection contains invalid data. Attribute collection_time.from is required. Attribute collection_time.to is required.');
$this->api->updateCollection($collectionToUpdate);
}

public function testItFailsToUpdateWhenNoShopIsSet(): void
{
$collectionToUpdate = new Collection();
$collectionToUpdate->setId('65ddc1c8-8e16-41e3-a383-c8a5eac68caa');
$collectionToUpdate->setAddress(
(new Address())
->setStreet1('Test street 1')
->setCity('Test city')
->setCountryCode('NL')
);

$collectionToUpdate->setCollectionTime(
(new CollectionTime())->setFrom(1708085160)->setTo(1708096680)
);

$this->expectException(InvalidResourceException::class);
$this->expectExceptionMessage('This collection contains invalid data. Attribute shop.id is required.');
$this->api->updateCollection($collectionToUpdate);
}

public function testItFailsToUpdateWhenNoAddressIsSet(): void
{
$collectionToUpdate = new Collection();
$collectionToUpdate->setId('65ddc1c8-8e16-41e3-a383-c8a5eac68caa');
$collectionToUpdate->setShop(
(new Shop())->setId('1ebabb0e-9036-4259-b58e-2b42742bb86a')
);
$collectionToUpdate->setCollectionTime(
(new CollectionTime())->setFrom(123456789)->setTo(123456800)
);

$this->expectException(InvalidResourceException::class);
$this->expectExceptionMessage('This collection contains invalid data. Attribute address.street_1 is required. Attribute address.city is required. Attribute address.country_code is required.');
$this->api->updateCollection($collectionToUpdate);
}

public function testItRegistersACollection(): void
{
$collection = new Collection();
Expand Down

0 comments on commit 33e1e09

Please sign in to comment.