Skip to content

Commit bc8fc57

Browse files
authored
Merge pull request #3 from Incenteev/integration_tests
Implement integration tests
2 parents 9f542d0 + f839a77 commit bc8fc57

File tree

3 files changed

+99
-23
lines changed

3 files changed

+99
-23
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,25 @@ jobs:
6464
run: composer update --ansi --no-progress --prefer-dist --no-interaction ${{ matrix.composer_flags }}
6565

6666
- name: Run tests
67-
run: vendor/bin/phpunit --colors=always
67+
run: vendor/bin/phpunit --colors=always --exclude-group=integration
68+
69+
integration_tests:
70+
name: "Integration tests"
71+
runs-on: ubuntu-latest
72+
73+
steps:
74+
- uses: actions/checkout@v3
75+
- uses: shivammathur/setup-php@v2
76+
with:
77+
coverage: "none"
78+
php-version: "8.2"
79+
80+
- name: Install dependencies
81+
run: composer update --ansi --no-progress --prefer-dist --no-interaction
82+
83+
- name: Run tests
84+
run: vendor/bin/phpunit --colors=always --group=integration --display-skipped
85+
env:
86+
AMAZON_INCENTIVES_ACCESS_KEY: ${{ secrets.AMAZON_INCENTIVES_ACCESS_KEY }}
87+
AMAZON_INCENTIVES_PARTNER_ID: ${{ secrets.AMAZON_INCENTIVES_PARTNER_ID }}
88+
AMAZON_INCENTIVES_SECRET_KEY: ${{ secrets.AMAZON_INCENTIVES_SECRET_KEY }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor/
22
/composer.lock
3+
/phpunit.xml
34
*.cache

tests/Integration/AmazonIncentivesClientTest.php

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,134 @@
22

33
namespace Incenteev\AsyncAmazonIncentives\Tests\Integration;
44

5-
use AsyncAws\Core\Credentials\NullProvider;
5+
use AsyncAws\Core\Credentials\Credentials;
66
use AsyncAws\Core\Test\TestCase;
77
use Incenteev\AsyncAmazonIncentives\AmazonIncentivesClient;
88
use Incenteev\AsyncAmazonIncentives\Enum\CurrencyCode;
99
use Incenteev\AsyncAmazonIncentives\Enum\Status;
10+
use Incenteev\AsyncAmazonIncentives\Exception\InsufficientFundsException;
11+
use Incenteev\AsyncAmazonIncentives\Exception\ThrottlingException;
1012
use Incenteev\AsyncAmazonIncentives\Input\CancelGiftCardRequest;
1113
use Incenteev\AsyncAmazonIncentives\Input\CreateGiftCardRequest;
1214
use Incenteev\AsyncAmazonIncentives\Input\GetAvailableFundsRequest;
1315
use Incenteev\AsyncAmazonIncentives\Region;
1416
use Incenteev\AsyncAmazonIncentives\ValueObject\MoneyAmount;
17+
use PHPUnit\Framework\Attributes\Group;
1518

19+
#[Group('integration')]
1620
class AmazonIncentivesClientTest extends TestCase
1721
{
1822
public function testCancelGiftCard(): void
1923
{
2024
$client = $this->getClient();
2125

2226
$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',
2630
]);
27-
$result = $client->cancelGiftCard($input);
2831

29-
$result->resolve();
32+
try {
33+
$result = $client->cancelGiftCard($input);
3034

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());
3342
}
3443

3544
public function testCreateGiftCard(): void
3645
{
3746
$client = $this->getClient();
3847

3948
$input = new CreateGiftCardRequest([
40-
'creationRequestId' => 'change me',
41-
'partnerId' => 'change me',
49+
'creationRequestId' => 'F0000',
50+
'partnerId' => $this->getPartnerId(),
4251
'value' => new MoneyAmount([
4352
'amount' => 10,
4453
'currencyCode' => CurrencyCode::EUR,
4554
]),
4655
]);
47-
$result = $client->createGiftCard($input);
4856

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+
}
5064

5165
self::assertSame(10.0, $result->getCardInfo()->getValue()->getAmount());
52-
self::assertSame('changeIt', $result->getCreationRequestId());
66+
self::assertSame('F0000', $result->getCreationRequestId());
5367
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+
}
5494
}
5595

5696
public function testGetAvailableFunds(): void
5797
{
5898
$client = $this->getClient();
5999

60100
$input = new GetAvailableFundsRequest([
61-
'partnerId' => 'change me',
101+
'partnerId' => $this->getPartnerId(),
62102
]);
63-
$result = $client->getAvailableFunds($input);
64103

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+
}
66111

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());
70114
}
71115

72116
private function getClient(): AmazonIncentivesClient
73117
{
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+
}
75121

76-
// @phpstan-ignore-next-line
77122
return new AmazonIncentivesClient([
78123
'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'];
80134
}
81135
}

0 commit comments

Comments
 (0)