Skip to content

Commit

Permalink
✨ Added patch collection feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
NickVries committed Feb 16, 2024
1 parent f4e1295 commit 3f54353
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 20 deletions.
20 changes: 20 additions & 0 deletions src/MyParcelComApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,26 @@ public function createCollection(CollectionInterface $collection): CollectionInt
return $this->postResource($collection);
}

public function updateCollection(CollectionInterface $collection): CollectionInterface
{
if (!$collection->getId()) {
throw new InvalidResourceException(
'Could not update collection. This collection does not have an id, use createCollection() to save it.',
);
}

$validator = new CollectionValidator($collection);
if (!$validator->isValid()) {
$message = 'This collection contains invalid data. ' . implode('. ', $validator->getErrors()) . '.';
$exception = new InvalidResourceException($message);
$exception->setErrors($validator->getErrors());

throw $exception;
}

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

// TODO: Add other collection methods.

/**
Expand Down
4 changes: 4 additions & 0 deletions src/MyParcelComApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ public function getCollections(array $filters, int $ttl = self::TTL_10MIN): Reso

public function getCollection(string $collectionId, int $ttl = self::TTL_NO_CACHE): CollectionInterface;

public function createCollection(CollectionInterface $collection): CollectionInterface;

public function updateCollection(CollectionInterface $collection): CollectionInterface;

// TODO: Add other collection methods.

/**
Expand Down
121 changes: 102 additions & 19 deletions tests/Feature/MyParcelComApi/CollectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,6 @@ public function testItFailsWhenNoCollectionTimeIsSet(): void
$this->api->createCollection($newCollection);
}

public function testItFailsWhenNoShopIsSet(): void
{
$newCollection = new Collection();
$newCollection->setAddress(
(new Address())
->setStreet1('Test street 1')
->setCity('Test city')
->setCountryCode('NL')
);

$newCollection->setCollectionTime(
(new CollectionTime())->setFrom(123456789)->setTo(123456800)
);

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

public function testItDefaultsToShopAddressWhenNoAddressIsSet(): void
{
$shop = (new Shop())
Expand All @@ -147,4 +128,106 @@ public function testItDefaultsToShopAddressWhenNoAddressIsSet(): void
$this->assertEquals('Test city', $createdCollection->getAddress()->getCity());
$this->assertEquals('NL', $createdCollection->getAddress()->getCountryCode());
}

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);

$this->assertInstanceOf(CollectionInterface::class, $updatedCollection);
$this->assertEquals('Updated description', $updatedCollection->getDescription());
}

public function testItCannotUpdateACollectionWithoutAnId(): void
{
$collection = new Collection();
$collection->setShop(
(new Shop())->setId('1ebabb0e-9036-4259-b58e-2b42742bb86a')
);
$collection->setCollectionTime(
(new CollectionTime())->setFrom(1708085160)->setTo(1708096680)
);
$collection->setAddress(
(new Address())
->setStreet1('Some street 1')
->setCity('Some city')
->setCountryCode('GB')
);

$this->expectException(InvalidResourceException::class);
$this->expectExceptionMessage('Could not update collection. This collection does not have an id, use createCollection() to save it.');
$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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"data": {
"id": "65ddc1c8-8e16-41e3-a383-c8a5eac68caa",
"type": "collections",
"attributes": {
"description": "Updated description",
"collection_time": {
"from": 1708085160,
"to": 1708096680
},
"register": false,
"address": {
"street_1": "Updated street 1",
"city": "Updated city",
"country_code": "NL"
},
"tracking_code": "TRK-1234567890",
"created_at": 1707991769
},
"relationships": {
"shop": {
"data": {
"id": "1ebabb0e-9036-4259-b58e-2b42742bb86a",
"type": "shops"
},
"links": {
"related": "https:\/\/api.localhost.private\/shops\/1ebabb0e-9036-4259-b58e-2b42742bb86a"
}
},
"status": {
"data": {
"id": "583e311f-8f03-4b34-84dd-5a82305ce5a8",
"type": "statuses"
},
"links": {
"related": "https:\/\/api.localhost.private\/statuses\/583e311f-8f03-4b34-84dd-5a82305ce5a8"
}
},
"shipments": {
"data": [
{
"id": "65e10ca7-5e34-40da-9da5-928f8aa57f97",
"type": "shipments"
},
{
"id": "872960ef-2a2c-4ab5-9a36-01478fd20276",
"type": "shipments"
}
]
},
"files": {
"data": [
{
"id": "cbf0973b-9bf6-49b9-a8c3-fd69a51a0457",
"type": "files"
},
{
"id": "289928ba-76a7-4601-9af0-33c46c0abea4",
"type": "files"
}
]
},
"manifest": {
"data": {
"id": "59b4daab-0b08-42ca-9b13-b13fb397af89",
"type": "manifests"
},
"links": {
"related": "https:\/\/api.localhost.private\/manifests\/59b4daab-0b08-42ca-9b13-b13fb397af89"
}
},
"contract": {
"data": {
"id": "be5c82fd-e936-440a-a5d8-9a3124d86fcf",
"type": "contracts"
},
"links": {
"related": "https:\/\/api.localhost.private\/contracts\/be5c82fd-e936-440a-a5d8-9a3124d86fcf"
}
}
}
}
}
2 changes: 1 addition & 1 deletion tests/Unit/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testRegistered(): void
{
$collection = new Collection();

$this->assertTrue($collection->setRegistered(true)->getRegistered());
$this->assertTrue($collection->setRegister(true)->getRegister());
}

public function testCollectionTime(): void
Expand Down

0 comments on commit 3f54353

Please sign in to comment.