Skip to content

Commit

Permalink
♻️ Add 201 as allowed status for update
Browse files Browse the repository at this point in the history
  • Loading branch information
mschadegg committed Feb 29, 2024
1 parent c79b00b commit bc9c8f0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Traits/Resources/Updatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function save(): static

$response = EconomicApiService::put(static::getEndpoint(Update::class, $this), $this->toArray(true));

if ($response->getStatusCode() !== 200) {
EconomicLoggerService::error('Economic API Service returned a non 200 status code when updating a resource', [
if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) { // 201 is for created, 200 is for updated -> both are valid here
EconomicLoggerService::error('Economic API Service returned a non 200 or 201 status code when updating a resource', [
'status_code' => $response->getStatusCode(),
'response_body' => $response->getBody(),
'resource' => static::class,
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@
->name->toBe('John Doe Renamed');
});

it('can create a customer through update', function () {
$this->driver->expects()->put()
->withArgs(function (string $url, array $body) {
return $url === 'https://restapi.e-conomic.com/customers/1'
&& $body === [
'customerNumber' => 1,
'name' => 'John Doe Renamed',
'self' => 'https://restapi.e-conomic.com/customers/1',
];
})
->once()
->andReturn(new EconomicResponse(201, fixture('Customers/update')));

$customer = new Customer([
'customerNumber' => 1,
'name' => 'John Doe Renamed',
]);

$updatedCustomer = $customer->save();

expect($updatedCustomer)
->toBeInstanceOf(Customer::class)
->customerNumber->toBe(1)
->name->toBe('John Doe Renamed');
});

it('filters null values', function () {
$this->driver->expects()->post(
'https://restapi.e-conomic.com/customers',
Expand Down

0 comments on commit bc9c8f0

Please sign in to comment.