Skip to content

Commit

Permalink
Add PHP 8.2 support and PHPUnit 10 (#319)
Browse files Browse the repository at this point in the history
* allow running tests via composer script

* Update all dev-dependencies

* fix covers warnings

* set all dataProvider as static

* Improve github Actions, update services, add tests with PHP 8.2

* Run tests with phpunit 10.2, replace deprecated withConsecutive()

* catch and tests warnings thrown by SimpleXMLElement

* Create composer command to create HTML code-coverage report

* Update CHANGELOG.md
  • Loading branch information
Art4 authored Sep 14, 2023
1 parent a2e59e7 commit 006e065
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 208 deletions.
23 changes: 6 additions & 17 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
fail-fast: false
matrix:
operating-system: ['ubuntu-latest']
php-versions: ['7.4', '8.0', '8.1']
php-versions: ['7.4', '8.0', '8.1', '8.2']

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 2

Expand All @@ -32,21 +32,10 @@ jobs:
extensions: mbstring, xml, ctype, iconv, intl, pdo_sqlite
coverage: xdebug

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
# Use composer.json for key, if composer.lock is not committed.
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Composer dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader
# Install composer dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
- name: "Install Composer dependencies"
uses: "ramsey/composer-install@v2"

- name: Run tests
run: vendor/bin/phpunit --coverage-text
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.php-cs-fixer.cache
.phpunit.result.cache
.phpunit.cache
composer.lock
composer.phar
coverage.clover
phpunit.xml
vendor
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/kbsali/php-redmine-api/compare/v2.2.0...v2.x)

### Added

- Added support for PHP 8.2

### Deprecated

- `Redmine\Api\AbstractApi::attachCustomFieldXML()` is deprecated
Expand Down Expand Up @@ -37,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added support for PHP 8.1
- New interface `Redmine\Exception` that is implemented by every library-related exception
- New exception `Redmine\Exception\ClientException` for client related exceptions
- New exception `Redmine\Exception\InvalidApiNameException` if an invalid API instance is requested
Expand Down
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
"psr/http-factory": "^1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "3.11.0",
"phpunit/phpunit": "9.5.10",
"guzzlehttp/psr7": "2.1.0",
"php-mock/php-mock-phpunit": "2.6.0"
"friendsofphp/php-cs-fixer": "^3",
"phpunit/phpunit": "^9 || 10.2.*",
"guzzlehttp/psr7": "^2",
"php-mock/php-mock-phpunit": "^2.6"
},
"autoload": {
"psr-4": {
Expand All @@ -38,5 +38,9 @@
"psr-4": {
"Redmine\\Tests\\": "tests/"
}
},
"scripts": {
"coverage": "phpunit --coverage-html=\".phpunit.cache/code-coverage\"",
"test": "phpunit"
}
}
29 changes: 20 additions & 9 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/Redmine/</directory>
</include>
<report>
<clover outputFile="coverage.clover"/>
</report>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
colors="true"
beStrictAboutCoverageMetadata="false"
beStrictAboutOutputDuringTests="true"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
>
<coverage />
<testsuites>
<testsuite name="all">
<directory suffix="Test.php">tests/Unit/</directory>
Expand All @@ -20,4 +26,9 @@
</exclude>
</groups>
<logging/>
<source>
<include>
<directory suffix=".php">src/Redmine/</directory>
</include>
</source>
</phpunit>
29 changes: 27 additions & 2 deletions src/Redmine/Serializer/XmlSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Redmine\Serializer;

use JsonException;
use Redmine\Exception\SerializerException;
use SimpleXMLElement;
use Throwable;
Expand Down Expand Up @@ -64,14 +65,26 @@ private function deserialize(string $encoded): void
{
$this->encoded = $encoded;

$prevSetting = libxml_use_internal_errors(true);

try {
$this->deserialized = new SimpleXMLElement($encoded);
} catch (Throwable $e) {
$errors = [];

foreach (libxml_get_errors() as $error) {
$errors[] = $error->message;
}

libxml_clear_errors();

throw new SerializerException(
'Catched error "' . $e->getMessage() . '" while decoding XML: ' . $encoded,
'Catched errors: "' . implode('", "', $errors) . '" while decoding XML: ' . $encoded,
$e->getCode(),
$e
);
} finally {
libxml_use_internal_errors($prevSetting);
}

$this->normalize($this->deserialized);
Expand All @@ -94,14 +107,26 @@ private function denormalize(array $normalized): void

$rootElementName = array_key_first($this->normalized);

$prevSetting = libxml_use_internal_errors(true);

try {
$this->deserialized = $this->createXmlElement($rootElementName, $this->normalized[$rootElementName]);
} catch (Throwable $e) {
$errors = [];

foreach (libxml_get_errors() as $error) {
$errors[] = $error->message;
}

libxml_clear_errors();

throw new SerializerException(
'Could not create XML from array: ' . $e->getMessage(),
'Could not create XML from array: "' . implode('", "', $errors) . '"',
$e->getCode(),
$e
);
} finally {
libxml_use_internal_errors($prevSetting);
}

$this->encoded = $this->deserialized->asXml();
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Psr18ClientRequestGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function createStreamFromResource($resource): StreamInterface
$client->$method($path, $data);
}

public function createdGetRequestsData()
public static function createdGetRequestsData(): array
{
return [
[
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Api/AbstractApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testIsNotNullReturnsCorrectBoolean(bool $expected, $value)
$this->assertSame($expected, $method->invoke($api, $value));
}

public function getIsNotNullReturnsCorrectBooleanData()
public static function getIsNotNullReturnsCorrectBooleanData(): array
{
return [
[false, null],
Expand Down Expand Up @@ -67,7 +67,7 @@ public function testLastCallFailedReturnsCorrectBoolean($statusCode, $expectedBo
$this->assertSame($expectedBoolean, $api->lastCallFailed());
}

public function getLastCallFailedData()
public static function getLastCallFailedData(): array
{
return [
[100, true],
Expand Down Expand Up @@ -161,7 +161,7 @@ public function testJsonDecodingFromGetMethod($response, $decode, $expected)
}
}

public function getJsonDecodingFromGetMethodData()
public static function getJsonDecodingFromGetMethodData(): array
{
return [
['{"foo_bar": 12345}', null, ['foo_bar' => 12345]], // test decode by default
Expand Down Expand Up @@ -217,7 +217,7 @@ public function testXmlDecodingFromRequestMethods($methodName, $response, $decod
}
}

public function getXmlDecodingFromGetMethodData()
public static function getXmlDecodingFromGetMethodData(): array
{
return [
['get', '<?xml version="1.0"?><issue/>', null, '<?xml version="1.0"?><issue/>'], // test decode by default
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/AttachmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testLastCallFailedTrue($responseCode, $hasFailed)
*
* @return array[]
*/
public function responseCodeProvider()
public static function responseCodeProvider(): array
{
return [
[199, true],
Expand Down
7 changes: 1 addition & 6 deletions tests/Unit/Api/IssueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
class IssueTest extends TestCase
{
public function getPriorityConstantsData()
public static function getPriorityConstantsData(): array
{
return [
[1, Issue::PRIO_LOW],
Expand Down Expand Up @@ -475,7 +475,6 @@ public function testCreateCleansParameters()
* Test create() and buildXML().
*
* @covers ::create
* @covers ::buildXML
* @covers ::attachCustomFieldXML
* @test
*/
Expand Down Expand Up @@ -719,7 +718,6 @@ public function testAddNoteToIssue()
/**
* Test buildXML().
*
* @covers ::buildXML
* @test
*/
public function testBuildXmlWithCustomFields()
Expand Down Expand Up @@ -760,7 +758,6 @@ public function testBuildXmlWithCustomFields()
/**
* Test buildXML().
*
* @covers ::buildXML
* @test
*/
public function testBuildXmlWithWatchers()
Expand Down Expand Up @@ -796,7 +793,6 @@ public function testBuildXmlWithWatchers()
/**
* Test buildXML().
*
* @covers ::buildXML
* @test
*/
public function testBuildXmlWithUploads()
Expand Down Expand Up @@ -857,7 +853,6 @@ public function testBuildXmlWithUploads()
/**
* Test buildXML().
*
* @covers ::buildXML
* @test
*/
public function testBuildXmlWithWatcherAndUploadAndCustomFieldAndStandard()
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/Api/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ public function testCreateCallsPost()
* Test create().
*
* @covers ::create
* @covers ::buildXML
* @test
*/
public function testCreateBuildsXml()
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public function testCreateThrowsExceptionIfValueIsMissingInParameters($parameter
*
* @return array[]
*/
public function incompleteCreateParameterProvider()
public static function incompleteCreateParameterProvider(): array
{
return [
// Missing Login
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Api/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ public function testUpdateThrowsExceptionWithInvalidSharing($sharingValue)
*
* @return array[]
*/
public function validSharingProvider()
public static function validSharingProvider(): array
{
return [
['none', '<sharing>none</sharing>'],
Expand All @@ -925,7 +925,7 @@ public function validSharingProvider()
*
* @return array[]
*/
public function validEmptySharingProvider()
public static function validEmptySharingProvider(): array
{
return [
[null],
Expand All @@ -939,7 +939,7 @@ public function validEmptySharingProvider()
*
* @return array[]
*/
public function invalidSharingProvider()
public static function invalidSharingProvider(): array
{
return [
['all'],
Expand Down
Loading

0 comments on commit 006e065

Please sign in to comment.