|
2 | 2 |
|
3 | 3 | namespace Incenteev\AsyncAmazonIncentives\Tests\Integration;
|
4 | 4 |
|
5 |
| -use AsyncAws\Core\Credentials\NullProvider; |
| 5 | +use AsyncAws\Core\Credentials\Credentials; |
6 | 6 | use AsyncAws\Core\Test\TestCase;
|
7 | 7 | use Incenteev\AsyncAmazonIncentives\AmazonIncentivesClient;
|
8 | 8 | use Incenteev\AsyncAmazonIncentives\Enum\CurrencyCode;
|
9 | 9 | use Incenteev\AsyncAmazonIncentives\Enum\Status;
|
| 10 | +use Incenteev\AsyncAmazonIncentives\Exception\InsufficientFundsException; |
| 11 | +use Incenteev\AsyncAmazonIncentives\Exception\ThrottlingException; |
10 | 12 | use Incenteev\AsyncAmazonIncentives\Input\CancelGiftCardRequest;
|
11 | 13 | use Incenteev\AsyncAmazonIncentives\Input\CreateGiftCardRequest;
|
12 | 14 | use Incenteev\AsyncAmazonIncentives\Input\GetAvailableFundsRequest;
|
13 | 15 | use Incenteev\AsyncAmazonIncentives\Region;
|
14 | 16 | use Incenteev\AsyncAmazonIncentives\ValueObject\MoneyAmount;
|
| 17 | +use PHPUnit\Framework\Attributes\Group; |
15 | 18 |
|
| 19 | +#[Group('integration')] |
16 | 20 | class AmazonIncentivesClientTest extends TestCase
|
17 | 21 | {
|
18 | 22 | public function testCancelGiftCard(): void
|
19 | 23 | {
|
20 | 24 | $client = $this->getClient();
|
21 | 25 |
|
22 | 26 | $input = new CancelGiftCardRequest([
|
23 |
| - 'creationRequestId' => 'change me', |
24 |
| - 'partnerId' => 'change me', |
25 |
| - 'gcId' => 'change me', |
| 27 | + 'creationRequestId' => 'F0000', |
| 28 | + 'partnerId' => $this->getPartnerId(), |
| 29 | + 'gcId' => 'AMAZONGCID', |
26 | 30 | ]);
|
27 |
| - $result = $client->cancelGiftCard($input); |
28 | 31 |
|
29 |
| - $result->resolve(); |
| 32 | + try { |
| 33 | + $result = $client->cancelGiftCard($input); |
30 | 34 |
|
31 |
| - self::assertSame('changeIt', $result->getCreationRequestId()); |
32 |
| - self::assertSame('changeIt', $result->getStatus()); |
| 35 | + $result->resolve(); |
| 36 | + } catch (ThrottlingException $e) { |
| 37 | + self::markTestSkipped('Could not run the integration tests: ' . $e->getMessage()); |
| 38 | + } |
| 39 | + |
| 40 | + self::assertSame('F0000', $result->getCreationRequestId()); |
| 41 | + self::assertSame(Status::SUCCESS, $result->getStatus()); |
33 | 42 | }
|
34 | 43 |
|
35 | 44 | public function testCreateGiftCard(): void
|
36 | 45 | {
|
37 | 46 | $client = $this->getClient();
|
38 | 47 |
|
39 | 48 | $input = new CreateGiftCardRequest([
|
40 |
| - 'creationRequestId' => 'change me', |
41 |
| - 'partnerId' => 'change me', |
| 49 | + 'creationRequestId' => 'F0000', |
| 50 | + 'partnerId' => $this->getPartnerId(), |
42 | 51 | 'value' => new MoneyAmount([
|
43 | 52 | 'amount' => 10,
|
44 | 53 | 'currencyCode' => CurrencyCode::EUR,
|
45 | 54 | ]),
|
46 | 55 | ]);
|
47 |
| - $result = $client->createGiftCard($input); |
48 | 56 |
|
49 |
| - $result->resolve(); |
| 57 | + try { |
| 58 | + $result = $client->createGiftCard($input); |
| 59 | + |
| 60 | + $result->resolve(); |
| 61 | + } catch (ThrottlingException $e) { |
| 62 | + self::markTestSkipped('Could not run the integration tests: ' . $e->getMessage()); |
| 63 | + } |
50 | 64 |
|
51 | 65 | self::assertSame(10.0, $result->getCardInfo()->getValue()->getAmount());
|
52 |
| - self::assertSame('changeIt', $result->getCreationRequestId()); |
| 66 | + self::assertSame('F0000', $result->getCreationRequestId()); |
53 | 67 | self::assertSame(Status::SUCCESS, $result->getStatus());
|
| 68 | + self::assertNotEmpty($result->getGcClaimCode()); |
| 69 | + self::assertNotEmpty($result->getGcId()); |
| 70 | + } |
| 71 | + |
| 72 | + public function testCreateGiftCardFailure(): void |
| 73 | + { |
| 74 | + $client = $this->getClient(); |
| 75 | + |
| 76 | + $input = new CreateGiftCardRequest([ |
| 77 | + 'creationRequestId' => 'F3003', |
| 78 | + 'partnerId' => $this->getPartnerId(), |
| 79 | + 'value' => new MoneyAmount([ |
| 80 | + 'amount' => 10, |
| 81 | + 'currencyCode' => CurrencyCode::EUR, |
| 82 | + ]), |
| 83 | + ]); |
| 84 | + |
| 85 | + $this->expectException(InsufficientFundsException::class); |
| 86 | + |
| 87 | + try { |
| 88 | + $result = $client->createGiftCard($input); |
| 89 | + |
| 90 | + $result->resolve(); |
| 91 | + } catch (ThrottlingException $e) { |
| 92 | + self::markTestSkipped('Could not run the integration tests: ' . $e->getMessage()); |
| 93 | + } |
54 | 94 | }
|
55 | 95 |
|
56 | 96 | public function testGetAvailableFunds(): void
|
57 | 97 | {
|
58 | 98 | $client = $this->getClient();
|
59 | 99 |
|
60 | 100 | $input = new GetAvailableFundsRequest([
|
61 |
| - 'partnerId' => 'change me', |
| 101 | + 'partnerId' => $this->getPartnerId(), |
62 | 102 | ]);
|
63 |
| - $result = $client->getAvailableFunds($input); |
64 | 103 |
|
65 |
| - $result->resolve(); |
| 104 | + try { |
| 105 | + $result = $client->getAvailableFunds($input); |
| 106 | + |
| 107 | + $result->resolve(); |
| 108 | + } catch (ThrottlingException $e) { |
| 109 | + self::markTestSkipped('Could not run the integration tests: ' . $e->getMessage()); |
| 110 | + } |
66 | 111 |
|
67 |
| - // self::assertTODO(expected, $result->getAvailableFunds()); |
68 |
| - self::assertSame('changeIt', $result->getStatus()); |
69 |
| - // self::assertTODO(expected, $result->getTimestamp()); |
| 112 | + self::assertSame(0.0, $result->getAvailableFunds()->getAmount()); // the balance is always 0 for a sandbox |
| 113 | + self::assertSame(Status::SUCCESS, $result->getStatus()); |
70 | 114 | }
|
71 | 115 |
|
72 | 116 | private function getClient(): AmazonIncentivesClient
|
73 | 117 | {
|
74 |
| - self::markTestIncomplete('Not implemented'); |
| 118 | + if (!isset($_SERVER['AMAZON_INCENTIVES_ACCESS_KEY'], $_SERVER['AMAZON_INCENTIVES_SECRET_KEY']) || $_SERVER['AMAZON_INCENTIVES_ACCESS_KEY'] === '' || $_SERVER['AMAZON_INCENTIVES_SECRET_KEY'] === '') { |
| 119 | + self::markTestSkipped('Test credentials are not provided.'); |
| 120 | + } |
75 | 121 |
|
76 |
| - // @phpstan-ignore-next-line |
77 | 122 | return new AmazonIncentivesClient([
|
78 | 123 | 'region' => Region::EUROPE_AND_ASIA_SANDBOX,
|
79 |
| - ], new NullProvider()); |
| 124 | + ], new Credentials($_SERVER['AMAZON_INCENTIVES_ACCESS_KEY'], $_SERVER['AMAZON_INCENTIVES_SECRET_KEY'])); |
| 125 | + } |
| 126 | + |
| 127 | + private function getPartnerId(): string |
| 128 | + { |
| 129 | + if (!isset($_SERVER['AMAZON_INCENTIVES_PARTNER_ID']) || $_SERVER['AMAZON_INCENTIVES_PARTNER_ID'] === '') { |
| 130 | + self::markTestSkipped('Test credentials are not provided.'); |
| 131 | + } |
| 132 | + |
| 133 | + return $_SERVER['AMAZON_INCENTIVES_PARTNER_ID']; |
80 | 134 | }
|
81 | 135 | }
|
0 commit comments