diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..bf88843 --- /dev/null +++ b/.github/workflows/tests.yml @@ -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 diff --git a/.gitignore b/.gitignore index b33fdab..399b9fb 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ composer.lock /tools/*/composer.lock /tools/*/vendor +/.phpunit.cache diff --git a/composer.json b/composer.json index e3b5e92..a8d3d94 100644 --- a/composer.json +++ b/composer.json @@ -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" + } } diff --git a/phpstan.neon b/phpstan.neon index f5ab67f..8edd531 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,3 +5,4 @@ parameters: level: 5 paths: - src + - tests diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..5f79d00 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,21 @@ + + + + + tests + + + + + + src + + + diff --git a/tests/Functional/Service/CheckoutIntentServiceTest.php b/tests/Functional/Service/CheckoutIntentServiceTest.php new file mode 100644 index 0000000..1eedcd9 --- /dev/null +++ b/tests/Functional/Service/CheckoutIntentServiceTest.php @@ -0,0 +1,40 @@ +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); + } +} diff --git a/tests/Functional/Service/ServiceTestCase.php b/tests/Functional/Service/ServiceTestCase.php new file mode 100644 index 0000000..131940d --- /dev/null +++ b/tests/Functional/Service/ServiceTestCase.php @@ -0,0 +1,31 @@ + $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); + } +}