diff --git a/src/MyParcelComApi.php b/src/MyParcelComApi.php index 47fd5edb..a2f76822 100644 --- a/src/MyParcelComApi.php +++ b/src/MyParcelComApi.php @@ -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. /** diff --git a/src/MyParcelComApiInterface.php b/src/MyParcelComApiInterface.php index 2444cf4a..e134e020 100644 --- a/src/MyParcelComApiInterface.php +++ b/src/MyParcelComApiInterface.php @@ -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. /** diff --git a/tests/Feature/MyParcelComApi/CollectionsTest.php b/tests/Feature/MyParcelComApi/CollectionsTest.php index 71a62c97..5fd35581 100644 --- a/tests/Feature/MyParcelComApi/CollectionsTest.php +++ b/tests/Feature/MyParcelComApi/CollectionsTest.php @@ -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()) @@ -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); + } } diff --git a/tests/Stubs/patch/https---api-collections-65ddc1c8-8e16-41e3-a383-c8a5eac68caa.json b/tests/Stubs/patch/https---api-collections-65ddc1c8-8e16-41e3-a383-c8a5eac68caa.json new file mode 100644 index 00000000..111e13dd --- /dev/null +++ b/tests/Stubs/patch/https---api-collections-65ddc1c8-8e16-41e3-a383-c8a5eac68caa.json @@ -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" + } + } + } + } +} diff --git a/tests/Unit/CollectionTest.php b/tests/Unit/CollectionTest.php index d9661f62..ef72946d 100644 --- a/tests/Unit/CollectionTest.php +++ b/tests/Unit/CollectionTest.php @@ -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