Skip to content

Commit

Permalink
Merge pull request #9 from odolbeau/first-test
Browse files Browse the repository at this point in the history
tests(checkout-intent): Create first functional test
  • Loading branch information
Doskyft authored Mar 30, 2024
2 parents abae0d7 + f96b4e7 commit e527715
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 1 deletion.
53 changes: 53 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
on: [pull_request]
name: PHPUnit

jobs:
tests:
name: Tests
runs-on: Ubuntu-20.04

strategy:
matrix:
include:
# Lowest deps (PHP 8.1 & sf 6.4)
- php: 8.1
symfony-require: 6.4.*
composer-flags: '--prefer-stable --prefer-lowest'
# Normal deps with different php versions
- php: 8.2
symfony-require: 7.0.*
composer-flags: '--prefer-stable'
- php: 8.3
symfony-require: 7.0.*
composer-flags: '--prefer-stable'
# Development release
- php: nightly
symfony-require: 7.1.*@dev
stability: dev
can-fail: true
fail-fast: false

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none

- name: Configure Composer minimum stability
if: matrix.stability
run: composer config minimum-stability ${{ matrix.stability }}

- name: Install dependencies
env:
SYMFONY_REQUIRE: ${{ matrix.symfony-require }}
run: composer update ${{ matrix.composer-flags }} --no-interaction --no-progress --optimize-autoloader

- name: Run PHPUnit
env:
HELLOASSO_CLIENT_ID: ${{ secrets.TESTS_HELLOASSO_ID }}
HELLOASSO_CLIENT_SECRET: ${{ secrets.TESTS_HELLOASSO_SECRET }}
run: vendor/bin/phpunit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ composer.lock

/tools/*/composer.lock
/tools/*/vendor
/.phpunit.cache
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@
"Helloasso\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Helloasso\\Tests\\": "tests/"
}
},
"authors": [
{
"name": "Doskyft",
"email": "doskyft@gmail.com"
}
]
],
"require-dev": {
"phpunit/phpunit": "^10.5"
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ parameters:
level: 5
paths:
- src
- tests
21 changes: 21 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
40 changes: 40 additions & 0 deletions tests/Functional/Service/CheckoutIntentServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Helloasso\Tests\Functional\Service;

use Helloasso\Models\Carts\CheckoutPayer;
use Helloasso\Models\Carts\InitCheckoutBody;
use Helloasso\Models\Carts\InitCheckoutResponse;
use Helloasso\Service\CheckoutIntentService;

final class CheckoutIntentServiceTest extends ServiceTestCase
{
public function testCreate(): void
{
$service = $this->createService(CheckoutIntentService::class);

$payer = new CheckoutPayer();
$payer
->setFirstName('Greta')
->setLastName('Thunberg')
->setDateOfBirth(new \DateTimeImmutable('-20 years'))
;

$body = new InitCheckoutBody();
$body
->setItemName('Awesome product')
->setInitialAmount(4200)
->setTotalAmount(4200)
->setBackUrl('https://my-app.fr/back_url')
->setReturnUrl('https://my-app.fr/return_url')
->setErrorUrl('https://my-app.fr/error_url')
->setPayer($payer)
;

$response = $service->create($body);

$this->assertInstanceOf(InitCheckoutResponse::class, $response);
}
}
31 changes: 31 additions & 0 deletions tests/Functional/Service/ServiceTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Helloasso\Tests\Functional\Service;

use Helloasso\Service\ApiRequest;
use PHPUnit\Framework\TestCase;

abstract class ServiceTestCase extends TestCase
{
/**
* @template T of ApiRequest
*
* @param class-string<T> $className
*
* @return T
*/
protected function createService(string $className): ApiRequest
{
$clientId = getenv('HELLOASSO_CLIENT_ID');
$clientSecret = getenv('HELLOASSO_CLIENT_SECRET');
$organisationSlug = getenv('HELLOASSO_ORGANISATION') ? getenv('HELLOASSO_ORGANISATION_SLUG') : 'helloasso-php-sdk';

if (false === $clientId || false === $clientSecret) {
throw new \RuntimeException('Some mandatory environment variables ("HELLOASSO_CLIENT_ID", "HELLOASSO_CLIENT_SECRET") have not been set. Please consider creating an account on https://www.helloasso-sandbox.com/ and run tests using HELLOASSO_CLIENT_ID=[YourClientId] HELLOASSO_CLIENT_SECRET=[YourClientSecret] HELLOASSO_ORGANISATION_SLUG=[YourOrganisation] vendor/bin/phpunit');
}

return new $className($clientId, $clientSecret, $organisationSlug, true);
}
}

0 comments on commit e527715

Please sign in to comment.