-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Adjustment items can be omitted when adjustment type is full #110
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,28 +22,36 @@ class CreateAdjustment implements \JsonSerializable | |
*/ | ||
public function __construct( | ||
public readonly Action $action, | ||
public readonly array|Undefined $items, | ||
public readonly array|Undefined|null $items, | ||
public readonly string $reason, | ||
public readonly string $transactionId, | ||
public readonly AdjustmentType|Undefined $type = new Undefined(), | ||
) { | ||
if ($this->type === AdjustmentType::Partial() && ($this->items instanceof Undefined || empty($this->items))) { | ||
$typeIsFull = AdjustmentType::Full()->equals($this->type); | ||
|
||
if (! $typeIsFull && ($this->items instanceof Undefined || empty($this->items))) { | ||
throw InvalidArgumentException::arrayIsEmpty('items'); | ||
} | ||
|
||
if ($typeIsFull && is_array($this->items)) { | ||
throw new InvalidArgumentException('items are not allowed when the adjustment type is full'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. API returns error "Array must have at least 1 items" when an empty array is sent in request, and "items are not allowed when the adjustment type is full" when items are provided with |
||
} | ||
} | ||
|
||
public function jsonSerialize(): array | ||
{ | ||
$items = []; | ||
if (is_array($this->items)) { | ||
$items = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved |
||
|
||
if (! $this->items instanceof Undefined) { | ||
foreach ($this->items as $item) { | ||
$items[] = [ | ||
'item_id' => $item->itemId, | ||
'type' => $item->type->getValue(), | ||
'amount' => $item->amount, | ||
]; | ||
} | ||
} else { | ||
$items = $this->items; | ||
} | ||
|
||
return $this->filterUndefined([ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"action": "refund", | ||
"type": "full", | ||
"reason": "error", | ||
"transaction_id": "txn_01h8bxpvx398a7zbawb77y0kp5" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"action": "refund", | ||
"type": "full", | ||
"items": null, | ||
"reason": "error", | ||
"transaction_id": "txn_01h8bxpvx398a7zbawb77y0kp5" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"action": "refund", | ||
"type": "partial", | ||
"items": [ | ||
{ | ||
"item_id": "txnitm_01h8bxryv3065dyh6103p3yg28", | ||
"type": "partial", | ||
"amount": "100" | ||
} | ||
], | ||
"reason": "error", | ||
"transaction_id": "txn_01h8bxpvx398a7zbawb77y0kp5" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Paddle\SDK\Tests\Unit\Resources\Adjustments\Operations; | ||
|
||
use Paddle\SDK\Entities\Adjustment\AdjustmentType; | ||
use Paddle\SDK\Entities\Shared\Action; | ||
use Paddle\SDK\Exceptions\SdkExceptions\InvalidArgumentException; | ||
use Paddle\SDK\Resources\Adjustments\Operations\CreateAdjustment; | ||
use Paddle\SDK\Undefined; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CreateAdjustmentTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* | ||
* @dataProvider invalidItemsDataProvider | ||
*/ | ||
public function it_validates_items(array|Undefined|null $items, AdjustmentType $type, string $expectedExceptionMessage): void | ||
{ | ||
self::expectException(InvalidArgumentException::class); | ||
self::expectExceptionMessage($expectedExceptionMessage); | ||
|
||
new CreateAdjustment( | ||
Action::Refund(), | ||
$items, | ||
'error', | ||
'txn_01h8bxpvx398a7zbawb77y0kp5', | ||
$type, | ||
); | ||
} | ||
|
||
public static function invalidItemsDataProvider(): \Generator | ||
{ | ||
yield 'Empty' => [ | ||
[], | ||
AdjustmentType::Partial(), | ||
'items cannot be empty', | ||
]; | ||
yield 'Undefined' => [ | ||
new Undefined(), | ||
AdjustmentType::Partial(), | ||
'items cannot be empty', | ||
]; | ||
yield 'Null' => [ | ||
null, | ||
AdjustmentType::Partial(), | ||
'items cannot be empty', | ||
]; | ||
yield 'Items for full type' => [ | ||
[], | ||
AdjustmentType::Full(), | ||
'items are not allowed when the adjustment type is full', | ||
]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->type === AdjustmentType::Partial()
strict comparison can't be used here