Skip to content

Commit

Permalink
Merge pull request #110 from alma/feature/ecom-1763-improve-unit-tests
Browse files Browse the repository at this point in the history
Improve unit tests
  • Loading branch information
joyet-simon authored Jun 24, 2024
2 parents 80a0ec6 + d7d57a3 commit 9481a0b
Show file tree
Hide file tree
Showing 43 changed files with 1,668 additions and 4,274 deletions.
Binary file added .DS_Store
Binary file not shown.
44 changes: 19 additions & 25 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,16 @@ jobs:
fail-fast: false
matrix:
php:
- version: 5.6
unittest-version: Legacy
- version: 7.0
unittest-version: PHP7_0
- version: 7.1
unittest-version: PHP7_0
- version: 7.2
unittest-version: PHP7_2
- version: 7.3
unittest-version: PHP7_2
- version: 7.4
unittest-version: PHP7_2
- version: 8.0
unittest-version: PHP7_2
- version: 8.1
unittest-version: PHP8_1
- version: 8.2
unittest-version: PHP8_1
- version: 8.3
unittest-version: PHP8_1
- "5.6"
- "7.0"
- "7.1"
- "7.2"
- "7.3"
- "7.4"
- "8.0"
- "8.1"
- "8.2"
- "8.3"

steps:
- name: Checkout repository
Expand All @@ -50,7 +40,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php.version }}
php-version: ${{ matrix.php }}
tools: composer:v2

- name: Validate composer.json and composer.lock
Expand All @@ -59,14 +49,18 @@ jobs:
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Prepare unittests configuration
run: sed -i 's/{MYVERSION}/${{ matrix.php.unittest-version }}/g' phpunit.ci.xml

- name: Ensure source code is linted
run: composer exec phpcs -- src/

- name: Remove uncompliant statements in legacy code
if: contains(fromJson('["5.6", "7.0"]'), matrix.php)
run: |
# Replace string in files
string=': void'
grep -r -l "$string" tests/ | xargs sed -i "s/$string//g"
- name: PHPUnit
run: composer exec phpunit -- -c phpunit.ci.xml --coverage-xml ./.coverage
run: composer exec phpunit -- --configuration phpunit.ci.xml --coverage-xml ./.coverage
env:
XDEBUG_MODE: coverage

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dist/
phpunit.xml
.phpunit.cache/
composer.lock
.DS_Store
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ARG PHP_VERSION
ARG COMPOSER_VERSION

FROM composer:2 as composer
FROM composer:${COMPOSER_VERSION} as composer
FROM php:${PHP_VERSION}-fpm

ENV DEBIAN_FRONTEND noninteractive
Expand Down
14 changes: 10 additions & 4 deletions Taskfile.php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ vars:
PHP_VERSION: '{{ .PHP_VERSION }}'
PHPUNIT_VERSION: '{{ .PHPUNIT_VERSION }}'
# Defaulted variable
COMPOSER_VERSION: '{{ .COMPOSER_VERSION | default "2" }}'
COMPOSE_SERVICE: '{{ .COMPOSE_SERVICE | default "php" }}'
PHPUNIT_FILE: '{{ .PHPUNIT_FILE | default "phpunit.dist.xml" }}'

env:
PHP_VERSION: '{{ .PHP_VERSION }}'
COMPOSER_VERSION: '{{ .COMPOSER_VERSION }}'

tasks:
docker:build:
Expand All @@ -23,21 +25,25 @@ tasks:
deps:
- docker:build
cmds:
- sed 's/{MYVERSION}/{{ .PHPUNIT_VERSION }}/g' {{ .PHPUNIT_FILE }} > phpunit.xml
- docker compose run {{ .COMPOSE_SERVICE }} composer exec phpunit --verbose -- --testsuite "Alma PHP Client Unit Test Suite"
- >-
{{ if eq .PHP_VERSION "5.6" "7.0" }}
docker compose run {{ .COMPOSE_SERVICE }} ./tests/legacy_tests.sh
{{ else }}
docker compose run {{ .COMPOSE_SERVICE }} composer exec phpunit --verbose -- --configuration phpunit.dist.xml --testsuite "Alma PHP Client Unit Test Suite"
{{ end }}
tests:integration:
desc: Execute intregration tests
deps:
- docker:build
cmds:
- sed 's/{MYVERSION}/{{ .PHPUNIT_VERSION }}/g' {{ .PHPUNIT_FILE }} > phpunit.xml
- sed 's/{MYVERSION}/{{ .PHPUNIT_VERSION }}/g' phpunit.dist.xml > phpunit.xml
- docker compose run {{ .COMPOSE_SERVICE }} composer exec phpunit --verbose -- --testsuite "Alma PHP Client Integration Test Suite"

shell:
desc: Connect to PHP container
deps:
- docker:build
cmds:
- sed 's/{MYVERSION}/{{ .PHPUNIT_VERSION }}/g' {{ .PHPUNIT_FILE }} > phpunit.xml
- sed 's/{MYVERSION}/{{ .PHPUNIT_VERSION }}/g' phpunit.dist.xml > phpunit.xml
- docker compose run {{ .COMPOSE_SERVICE }} bash
11 changes: 1 addition & 10 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,45 @@ includes:
vars:
PHP_VERSION: "5.6"
COMPOSE_SERVICE: php-legacy # Dockerfile needs specific APT configuration
PHPUNIT_VERSION: Legacy
PHPUNIT_FILE: phpunit.dist.legacy.xml
"7.0":
taskfile: Taskfile.php.yml
vars:
PHP_VERSION: "7.0"
COMPOSE_SERVICE: php-legacy # Dockerfile needs specific APT configuration
PHPUNIT_VERSION: PHP7_0
"7.1":
taskfile: Taskfile.php.yml
vars:
PHP_VERSION: "7.1"
PHPUNIT_VERSION: PHP7_0
COMPOSER_VERSION: "2.2" # Composer >2.2 deprecated PHP 7.1
"7.2":
taskfile: Taskfile.php.yml
vars:
PHP_VERSION: "7.2"
PHPUNIT_VERSION: PHP7_2
"7.3":
taskfile: Taskfile.php.yml
vars:
PHP_VERSION: "7.3"
PHPUNIT_VERSION: PHP7_2
"7.4":
taskfile: Taskfile.php.yml
vars:
PHP_VERSION: "7.4"
PHPUNIT_VERSION: PHP7_2
"8.0":
taskfile: Taskfile.php.yml
vars:
PHP_VERSION: "8.0"
PHPUNIT_VERSION: PHP7_2
"8.1":
taskfile: Taskfile.php.yml
vars:
PHP_VERSION: "8.1"
PHPUNIT_VERSION: PHP8_1
"8.2":
taskfile: Taskfile.php.yml
vars:
PHP_VERSION: "8.2"
PHPUNIT_VERSION: PHP8_1
"8.3":
taskfile: Taskfile.php.yml
vars:
PHP_VERSION: "8.3"
PHPUNIT_VERSION: PHP8_1

tasks:
default:
Expand Down
1 change: 1 addition & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
dockerfile: Dockerfile
args:
PHP_VERSION: ${PHP_VERSION}
COMPOSER_VERSION: ${COMPOSER_VERSION:-2}
ports:
- "8080:80"
volumes:
Expand Down
5 changes: 1 addition & 4 deletions phpunit.ci.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@

<testsuites>
<testsuite name="Alma PHP Client Unit Test Suite">
<directory>tests/Unit/{MYVERSION}</directory>
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/*</directory>
<exclude>
<directory suffix=".php">./src/vendor/*</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
2 changes: 1 addition & 1 deletion phpunit.dist.legacy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<testsuites>
<testsuite name="Alma PHP Client Unit Test Suite">
<directory>tests/Unit/{MYVERSION}</directory>
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Alma PHP Client Integration Test Suite">
<directory>tests/Integration/{MYVERSION}</directory>
Expand Down
4 changes: 2 additions & 2 deletions phpunit.dist.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" beStrictAboutTestsThatDoNotTestAnything="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" testdox="true" displayDetailsOnTestsThatTriggerDeprecations="true" processIsolation="false" stopOnFailure="false" beStrictAboutTestsThatDoNotTestAnything="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Alma PHP Client Unit Test Suite">
<directory>tests/Unit/{MYVERSION}</directory>
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Alma PHP Client Integration Test Suite">
<directory>tests/Integration/{MYVERSION}</directory>
Expand Down
4 changes: 2 additions & 2 deletions src/Endpoints/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function sendStatus($orderExternalId, $orderData = array())
'is_shipped' => $orderData['is_shipped'],
))->post();
}catch (AlmaException $e) {
$this->logger->error('Error sending status');
$this->logger->error('Error sending status');
throw new RequestException('Error sending status', $e);
}

Expand All @@ -150,7 +150,7 @@ public function validateStatusData($orderData = array())
try {
$this->arrayUtils->checkMandatoryKeys(['status', 'is_shipped'], $orderData);
} catch (MissingKeyException $e ) {
throw new ParametersException('Error in the required parameters (status, is_shipped) when calling orders.sendStatus',null, $e);
throw new ParametersException('Error in the required parameters (status, is_shipped) when calling orders.sendStatus',0, $e);
}

if(!is_bool($orderData['is_shipped'])) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Alma\API\Tests\Unit\PHP8_1;
namespace Alma\API\Tests\Unit;

use Alma\API\Lib\ArrayUtils;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Alma\API\Tests\Unit\PHP8_1;
namespace Alma\API\Tests\Unit;

use Alma\API\ClientContext;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Alma\API\Tests\Unit\PHP8_1;
namespace Alma\API\Tests\Unit;

use Alma\API\Lib\ClientOptionsValidator;
use Alma\API\Client;
Expand Down Expand Up @@ -95,6 +95,41 @@ public static function getClientOptions()
];
}

/**
* Return faulty options to test ClientOptionsValidator::validateOptions
* @return array
*/
public static function getInvalidClientOptions()
{
return [
'invalid api_root' => [
[
'api_root' => [
'something wrong' => Client::SANDBOX_API_URL,
'something wronger' => Client::LIVE_API_URL
],
],
],
'invalid tls' => [
[
'force_tls' => -1,
],
],
'invalid mode' => [
[
'mode' => 'sad',
],
],
'invalid user_agent_component' => [
[
'user_agent_component' => [
'I dont give a key:value pair',
]
],
],
];
}

/**
* @dataProvider getClientOptions
* @return void
Expand All @@ -107,41 +142,6 @@ public function testClientOptionsValidator($options, $expectedResult)
$this->assertEquals($expectedResult, $validatedConfig);
}

/**
* Return faulty options to test ClientOptionsValidator::validateOptions
* @return array
*/
public static function getInvalidClientOptions()
{
return [
'invalid api_root' => [
[
'api_root' => [
'something wrong' => Client::SANDBOX_API_URL,
'something wronger' => Client::LIVE_API_URL
],
],
],
'invalid tls' => [
[
'force_tls' => -1,
],
],
'invalid mode' => [
[
'mode' => 'sad',
],
],
'invalid user_agent_component' => [
[
'user_agent_component' => [
'I dont give a key:value pair',
]
],
],
];
}

/**
* @dataProvider getInvalidClientOptions
* @return void
Expand Down
Loading

0 comments on commit 9481a0b

Please sign in to comment.