Skip to content

Commit 59922b6

Browse files
authored
feat: setup tests (#68)
1 parent f279890 commit 59922b6

File tree

8 files changed

+222
-2
lines changed

8 files changed

+222
-2
lines changed

.github/workflows/ci.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
name: CI
22

3+
permissions:
4+
contents: read
5+
36
on:
47
workflow_dispatch: {}
58
push:
@@ -22,3 +25,26 @@ jobs:
2225
uses: docker://oskarstark/php-cs-fixer-ga:3.4.0
2326
with:
2427
args: --format=txt --diff --dry-run --using-cache=no --verbose
28+
29+
tests:
30+
name: Tests (PHP ${{ matrix.php }})
31+
runs-on: ubuntu-latest
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
php: ['7.4', '8.2']
36+
steps:
37+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
38+
39+
- name: Set up PHP
40+
uses: shivammathur/setup-php@bf6b4fbd49ca58e4608c9c89fba0b8d90bd2a39f # v2.35.5
41+
with:
42+
php-version: ${{ matrix.php }}
43+
coverage: none
44+
tools: composer:v2
45+
46+
- name: Install dependencies
47+
run: composer install --no-interaction --prefer-dist
48+
49+
- name: Run tests
50+
run: composer test

.phpunit.result.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":[],"times":{"SumUp\\Tests\\ApplicationConfigurationTest::testCustomScopesAreMergedWithDefaults":0.001,"SumUp\\Tests\\ApplicationConfigurationTest::testInvalidGrantTypeThrowsException":0,"SumUp\\Tests\\AuthorizationTest::testApiKeyShortCircuitsHttpCalls":0,"SumUp\\Tests\\AuthorizationTest::testClientCredentialsFlowMakesHttpRequest":0}}

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ fmt: vendor ## Format code using php-cs-fixer
1717
.PHONY: fmtcheck
1818
fmtcheck: vendor ## Check code formatting
1919
PHP_CS_FIXER_IGNORE_ENV=true vendor/bin/php-cs-fixer fix -v --using-cache=no --dry-run
20+
21+
.PHONY: test
22+
test: vendor ## Run PHPUnit test suite
23+
composer test

composer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
}
2222
],
2323
"scripts": {
24-
"cs": "php-cs-fixer fix --dry-run --diff"
24+
"cs": "php-cs-fixer fix --dry-run --diff",
25+
"test": "phpunit"
2526
},
2627
"require": {
2728
"php": "^5.6|^7.0|^8.0"
2829
},
2930
"require-dev": {
30-
"friendsofphp/php-cs-fixer": "3.5.0"
31+
"friendsofphp/php-cs-fixer": "3.5.0",
32+
"phpunit/phpunit": "^9.6"
3133
},
3234
"suggest": {
3335
"guzzlehttp/guzzle": "Allows for implementation of the Guzzle HTTP client"
@@ -36,5 +38,10 @@
3638
"psr-4": {
3739
"SumUp\\": "src/SumUp/"
3840
}
41+
},
42+
"autoload-dev": {
43+
"psr-4": {
44+
"SumUp\\Tests\\": "tests/"
45+
}
3946
}
4047
}

phpunit.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
bootstrap="vendor/autoload.php"
4+
colors="true"
5+
beStrictAboutTestsThatDoNotTestAnything="true"
6+
beStrictAboutOutputDuringTests="true"
7+
failOnWarning="true"
8+
failOnRisky="true"
9+
>
10+
<testsuites>
11+
<testsuite name="SumUp SDK">
12+
<directory suffix="Test.php">tests</directory>
13+
</testsuite>
14+
</testsuites>
15+
<coverage>
16+
<include>
17+
<directory suffix=".php">src</directory>
18+
</include>
19+
</coverage>
20+
</phpunit>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace SumUp\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use SumUp\Application\ApplicationConfiguration;
7+
use SumUp\Exceptions\SumUpConfigurationException;
8+
9+
class ApplicationConfigurationTest extends TestCase
10+
{
11+
public function testCustomScopesAreMergedWithDefaults()
12+
{
13+
$config = new ApplicationConfiguration([
14+
'api_key' => 'test-api-key',
15+
'scopes' => ['transactions.history', 'custom.scope'],
16+
]);
17+
18+
$scopes = $config->getScopes();
19+
20+
foreach (ApplicationConfiguration::DEFAULT_SCOPES as $scope) {
21+
$this->assertContains($scope, $scopes);
22+
}
23+
24+
$this->assertContains('custom.scope', $scopes);
25+
$this->assertStringContainsString('custom.scope', $config->getFormattedScopes());
26+
}
27+
28+
public function testInvalidGrantTypeThrowsException()
29+
{
30+
$this->expectException(SumUpConfigurationException::class);
31+
32+
new ApplicationConfiguration([
33+
'api_key' => 'test-api-key',
34+
'grant_type' => 'invalid',
35+
]);
36+
}
37+
}

tests/AuthorizationTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace SumUp\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use SumUp\Application\ApplicationConfiguration;
7+
use SumUp\HttpClients\Response;
8+
use SumUp\Services\Authorization;
9+
use SumUp\Tests\Doubles\FakeHttpClient;
10+
11+
class AuthorizationTest extends TestCase
12+
{
13+
public function testApiKeyShortCircuitsHttpCalls()
14+
{
15+
$config = new ApplicationConfiguration([
16+
'api_key' => 'secret-api-key',
17+
]);
18+
19+
$httpClient = new FakeHttpClient($this->createResponse([]), true);
20+
$authorization = new Authorization($httpClient, $config);
21+
22+
$token = $authorization->getToken();
23+
24+
$this->assertSame('secret-api-key', $token->getValue());
25+
$this->assertSame('Bearer', $token->getType());
26+
}
27+
28+
public function testClientCredentialsFlowMakesHttpRequest()
29+
{
30+
$config = new ApplicationConfiguration([
31+
'app_id' => 'app-id',
32+
'app_secret' => 'app-secret',
33+
'grant_type' => 'client_credentials',
34+
]);
35+
36+
$httpClient = new FakeHttpClient($this->createResponse([
37+
'access_token' => 'token-from-http',
38+
'token_type' => 'Bearer',
39+
'expires_in' => 3600,
40+
]));
41+
42+
$authorization = new Authorization($httpClient, $config);
43+
$token = $authorization->getToken();
44+
45+
$this->assertSame('token-from-http', $token->getValue());
46+
$this->assertSame('Bearer', $token->getType());
47+
$this->assertSame(3600, $token->getExpiresIn());
48+
49+
$requests = $httpClient->getRequests();
50+
$this->assertCount(1, $requests);
51+
$this->assertSame('POST', $requests[0]['method']);
52+
$this->assertSame('/token', $requests[0]['url']);
53+
$this->assertSame('client_credentials', $requests[0]['body']['grant_type']);
54+
$this->assertSame('app-id', $requests[0]['body']['client_id']);
55+
$this->assertSame('app-secret', $requests[0]['body']['client_secret']);
56+
$this->assertArrayHasKey('Content-Type', $requests[0]['headers']);
57+
}
58+
59+
private function createResponse(array $body)
60+
{
61+
return new Response(200, json_decode(json_encode($body)));
62+
}
63+
}

tests/Doubles/FakeHttpClient.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace SumUp\Tests\Doubles;
4+
5+
use RuntimeException;
6+
use SumUp\HttpClients\Response;
7+
use SumUp\HttpClients\SumUpHttpClientInterface;
8+
9+
/**
10+
* Lightweight test double that records outgoing requests and can optionally
11+
* fail when an HTTP call is not expected.
12+
*/
13+
class FakeHttpClient implements SumUpHttpClientInterface
14+
{
15+
/**
16+
* @var array<int, array<string, mixed>>
17+
*/
18+
private $requests = [];
19+
20+
/**
21+
* @var Response
22+
*/
23+
private $response;
24+
25+
/**
26+
* @var bool
27+
*/
28+
private $failOnCall;
29+
30+
public function __construct(Response $response, $failOnCall = false)
31+
{
32+
$this->response = $response;
33+
$this->failOnCall = $failOnCall;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function send($method, $url, $body, $headers)
40+
{
41+
if ($this->failOnCall) {
42+
throw new RuntimeException('HTTP client should not have been called.');
43+
}
44+
45+
$this->requests[] = [
46+
'method' => $method,
47+
'url' => $url,
48+
'body' => $body,
49+
'headers' => $headers,
50+
];
51+
52+
return $this->response;
53+
}
54+
55+
/**
56+
* @return array<int, array<string, mixed>>
57+
*/
58+
public function getRequests()
59+
{
60+
return $this->requests;
61+
}
62+
}

0 commit comments

Comments
 (0)