Skip to content

Commit

Permalink
Add compatibility with PHP 8 (see #2535)
Browse files Browse the repository at this point in the history
Description
-----------

See #2263 and #2291

### TODO

* [x] Fix the tests.
* [x] Wait if sebastianbergmann/php-code-coverage#837 gets merged and then use PHPUnit 8.5 for all PHP versions.
* [ ] <del>Click through the back end and front end and fix all warnings.</del>

Commits
-------

8ad6149 Add PHP 8.0 to the CI chain
7b8729e Fix the unit and functional tests
47c14db Use 127.0.0.1 instead of localhost in the functional tests
938836c Always set the ACCEPT_LANGUAGE header in the functional tests
a4199ec Use PHPUnit 8.5 for all PHP versions
7ffbd26 Adjust the error level in PHP 8
4ce6b27 Merge branch '4.9' into fix/php8
  • Loading branch information
leofeyer committed Dec 4, 2020
1 parent 67c7b18 commit 3feda71
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 55 deletions.
76 changes: 53 additions & 23 deletions .github/workflows/ci.yml
Expand Up @@ -19,16 +19,16 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.3
php-version: 7.4
extensions: dom, fileinfo, filter, gd, hash, intl, json, mbstring, pcre, pdo, zlib
tools: prestissimo, flex
tools: flex
coverage: pcov

- name: Checkout
uses: actions/checkout@v1

- name: Install the dependencies
run: composer install --no-interaction --no-suggest
run: composer install --no-interaction

- name: Generate the coverage report
run: php -d pcov.enabled=1 vendor/bin/phpunit --testsuite=coverage --coverage-clover=clover.xml --colors=always
Expand All @@ -47,16 +47,16 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.3
php-version: 7.4
extensions: dom, fileinfo, filter, gd, hash, intl, json, mbstring, pcre, pdo, zlib
tools: prestissimo, flex
tools: flex
coverage: none

- name: Checkout
uses: actions/checkout@v1

- name: Install the dependencies
run: composer install --no-interaction --no-suggest
run: composer install --no-interaction

- name: Check the coding style
run: |
Expand Down Expand Up @@ -87,7 +87,39 @@ jobs:
with:
php-version: ${{ matrix.php }}
extensions: dom, fileinfo, filter, gd, hash, intl, json, mbstring, pcre, pdo_mysql, zlib
tools: prestissimo, flex
tools: flex
coverage: none

- name: Initialize the database
run: |
sudo /etc/init.d/mysql start
mysql -uroot -proot -e "CREATE database contao_test"
- name: Checkout
uses: actions/checkout@v1

- name: Install the dependencies
run: composer install --no-interaction

- name: Run the unit tests
run: vendor/bin/phpunit --colors=always

- name: Run the functional tests
run: vendor/bin/phpunit --testsuite=functional --colors=always
env:
DATABASE_URL: mysql://root:root@127.0.0.1:3306/contao_test

php8:
name: PHP 8.0
runs-on: ubuntu-latest
if: github.event_name != 'push'
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
extensions: dom, fileinfo, filter, gd, hash, intl, json, mbstring, pcre, pdo_mysql, zlib
tools: flex
coverage: none

- name: Initialize the database
Expand All @@ -99,15 +131,15 @@ jobs:
uses: actions/checkout@v1

- name: Install the dependencies
run: composer install --no-interaction --no-suggest
run: composer install --ignore-platform-reqs --no-interaction

- name: Run the unit tests
run: vendor/bin/phpunit --colors=always

- name: Run the functional tests
run: vendor/bin/phpunit --testsuite=functional --colors=always
env:
DATABASE_URL: mysql://root:root@localhost:3306/contao_test
DATABASE_URL: mysql://root:root@127.0.0.1:3306/contao_test

prefer-lowest:
name: Prefer Lowest
Expand All @@ -119,7 +151,7 @@ jobs:
with:
php-version: 7.3
extensions: dom, fileinfo, filter, gd, hash, intl, json, mbstring, pcre, pdo_mysql, zlib
tools: prestissimo, flex
tools: flex
coverage: none

- name: Initialize the database
Expand All @@ -131,15 +163,15 @@ jobs:
uses: actions/checkout@v1

- name: Install the dependencies
run: composer update --prefer-lowest --prefer-stable --no-interaction --no-suggest
run: composer update --prefer-lowest --prefer-stable --no-interaction

- name: Run the unit tests
run: vendor/bin/phpunit --colors=always

- name: Run the functional tests
run: vendor/bin/phpunit --testsuite=functional --colors=always
env:
DATABASE_URL: mysql://root:root@localhost:3306/contao_test
DATABASE_URL: mysql://root:root@127.0.0.1:3306/contao_test

bundles:
name: Bundles
Expand All @@ -149,9 +181,9 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.3
php-version: 7.4
extensions: dom, fileinfo, filter, gd, hash, intl, json, mbstring, pcre, pdo, zlib
tools: prestissimo, flex
tools: flex
coverage: none

- name: Checkout
Expand All @@ -174,7 +206,7 @@ jobs:
}
file_put_contents(__DIR__."/composer.json", json_encode($data, JSON_UNESCAPED_SLASHES));
'
COMPOSER_ROOT_VERSION=dev-${{ env.GITHUB_SHA }} composer install --no-interaction --no-suggest
COMPOSER_ROOT_VERSION=dev-${{ env.GITHUB_SHA }} composer install --no-interaction
vendor/bin/phpunit --colors=always
cd ..
done
Expand All @@ -187,10 +219,10 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.3
php-version: 7.4
extensions: dom, fileinfo, filter, gd, hash, intl, json, mbstring, mysqli, pcre, pdo_mysql, zlib
ini-values: memory_limit=1G
tools: prestissimo, flex
tools: flex
coverage: none

- name: Adjust the Git autocrlf setting
Expand All @@ -200,7 +232,7 @@ jobs:
uses: actions/checkout@v1

- name: Install the dependencies
run: composer install --no-interaction --no-suggest --no-progress
run: composer install --no-interaction --no-progress

- name: Run the unit tests
run: vendor/bin/phpunit.bat --colors=always
Expand All @@ -213,9 +245,8 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.3
php-version: 7.4
extensions: json, zlib
tools: prestissimo
coverage: none

- name: Checkout
Expand All @@ -224,7 +255,7 @@ jobs:
- name: Install the dependencies
run: |
composer global require contao/monorepo-tools:dev-master
composer install --no-interaction --no-suggest
composer install --no-interaction
- name: Validate the composer.json files
run: $HOME/.composer/vendor/bin/monorepo-tools composer-json --validate --ansi
Expand All @@ -237,9 +268,8 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.3
php-version: 7.4
extensions: json, zlib
tools: prestissimo
coverage: none

- name: Checkout
Expand Down
4 changes: 2 additions & 2 deletions calendar-bundle/composer.json
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": "^7.2",
"php": "^7.2 || ^8.0",
"contao/core-bundle": "self.version",
"friendsofsymfony/http-cache": "^2.4",
"patchwork/utf8": "^1.2",
Expand All @@ -32,7 +32,7 @@
"require-dev": {
"contao/manager-plugin": "^2.3.1",
"contao/test-case": "^4.0",
"phpunit/phpunit": "^8.4",
"phpunit/phpunit": "^8.5",
"symfony/http-client": "4.4.*",
"symfony/phpunit-bridge": "4.4.*"
},
Expand Down
2 changes: 1 addition & 1 deletion comments-bundle/composer.json
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": "^7.2",
"php": "^7.2 || ^8.0",
"contao/core-bundle": "self.version",
"patchwork/utf8": "^1.2",
"symfony/http-kernel": "4.4.*"
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": "^7.2",
"php": "^7.2 || ^8.0",
"ext-dom": "*",
"ext-gd": "*",
"ext-hash": "*",
Expand Down Expand Up @@ -148,7 +148,7 @@
"contao/test-case": "^4.0",
"doctrine/event-manager": "^1.0",
"monolog/monolog": "^1.24",
"phpunit/phpunit": "^8.4",
"phpunit/phpunit": "^8.5",
"psr/event-dispatcher": "^1.0",
"slam/phpstan-extensions": "^5.0",
"symfony/browser-kit": "4.4.*",
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/composer.json
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": "^7.2",
"php": "^7.2 || ^8.0",
"ext-dom": "*",
"ext-gd": "*",
"ext-hash": "*",
Expand Down Expand Up @@ -120,7 +120,7 @@
"doctrine/event-manager": "^1.0",
"lexik/maintenance-bundle": "^2.1.5",
"monolog/monolog": "^1.24",
"phpunit/phpunit": "^8.4",
"phpunit/phpunit": "^8.5",
"psr/event-dispatcher": "^1.0",
"symfony/browser-kit": "4.4.*",
"symfony/http-client": "4.4.*",
Expand Down
17 changes: 15 additions & 2 deletions core-bundle/src/DependencyInjection/Configuration.php
Expand Up @@ -57,10 +57,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultValue('%kernel.secret%')
->end()
->integerNode('error_level')
->info('The error reporting level set when the framework is initialized. Defaults to E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_USER_DEPRECATED.')
->info('The error reporting level set when the framework is initialized.')
->min(-1)
->max(32767)
->defaultValue(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_USER_DEPRECATED)
->defaultValue($this->getErrorLevel())
->end()
->variableNode('localconfig')
->info('Allows to set TL_CONFIG variables, overriding settings stored in localconfig.php. Changes in the Contao back end will not have any effect.')
Expand Down Expand Up @@ -456,6 +456,19 @@ private function canonicalize(string $value): string
return rtrim(implode('', $resolved), '\/');
}

private function getErrorLevel(): int
{
if (PHP_MAJOR_VERSION < 8) {
return E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_USER_DEPRECATED;
}

// Disable E_WARNING in PHP 8, because a number of notices have been
// converted into warnings and now cause a lot of issues with undefined
// array keys and undefined properties.
// @see https://www.php.net/manual/de/migration80.incompatible.php
return E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED & ~E_USER_DEPRECATED;
}

/**
* @return array<string>
*/
Expand Down
Expand Up @@ -406,7 +406,13 @@ protected function createExtract()
// Check whether there is a relation (see #6524)
if (isset($config['relation']))
{
$table = substr($config['foreignKey'], 0, strrpos($config['foreignKey'], '.'));
$table = null;

if (isset($config['foreignKey']))
{
$table = substr($config['foreignKey'], 0, strrpos($config['foreignKey'], '.'));
}

$arrRelations[$field] = array_merge(array('table'=>$table, 'field'=>'id'), $config['relation']);

// Store the field delimiter if the related IDs are stored in CSV format (see #257)
Expand Down
10 changes: 7 additions & 3 deletions core-bundle/src/Resources/contao/library/Contao/GdImage.php
Expand Up @@ -129,15 +129,15 @@ public function getResource()
/**
* Set the GD resource handle
*
* @param resource $gdResource The GD resource handle
* @param \GdImage|resource $gdResource The GD resource handle
*
* @return static
*
* @throws \InvalidArgumentException If $gdResource is not a GD resource
*/
public function setResource($gdResource)
{
if (!\is_resource($gdResource) || get_resource_type($gdResource) !== 'gd')
if (!$gdResource instanceof \GdImage && (!\is_resource($gdResource) || get_resource_type($gdResource) !== 'gd'))
{
throw new \InvalidArgumentException('$gdResource is not a valid GD resource');
}
Expand Down Expand Up @@ -397,4 +397,8 @@ public function __destruct()
}
}

class_alias(GdImage::class, 'GdImage');
// PHP 8.0 compatibility
if (!class_exists('GdImage'))
{
class_alias(GdImage::class, 'GdImage');
}
Expand Up @@ -105,6 +105,12 @@ protected function doReplace($strBuffer, $blnCache)
for ($_rit=0, $_cnt=\count($tags); $_rit<$_cnt; $_rit+=2)
{
$strBuffer .= $tags[$_rit];

if (!isset($tags[$_rit+1]))
{
continue;
}

$strTag = $tags[$_rit+1];

// Skip empty tags
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Resources/contao/library/Contao/Widget.php
Expand Up @@ -780,7 +780,7 @@ protected function getPost($strKey)
break;
}

$varValue = $varValue[$part];
$varValue = $varValue[$part] ?? null;
}

return $varValue;
Expand Down
7 changes: 4 additions & 3 deletions core-bundle/tests/Contao/Database/ResultTest.php
Expand Up @@ -15,6 +15,7 @@
use Contao\CoreBundle\Tests\Fixtures\Database\DoctrineArrayStatement;
use Contao\Database\Result;
use PHPUnit\Framework\Error\Notice;
use PHPUnit\Framework\Error\Warning;
use PHPUnit\Framework\TestCase;

class ResultTest extends TestCase
Expand Down Expand Up @@ -52,7 +53,7 @@ public function testEmptyResult(): void
}
}

$this->expectException(Notice::class);
$this->expectException(PHP_MAJOR_VERSION < 8 ? Notice::class : Warning::class);
$resultStatement->fetchField();
}

Expand Down Expand Up @@ -95,7 +96,7 @@ public function testSingleRow(): void
$this->assertSame('value1', $result->fetchField(0));
}

$this->expectException(Notice::class);
$this->expectException(PHP_MAJOR_VERSION < 8 ? Notice::class : Warning::class);
$result->fetchField(1);
}

Expand Down Expand Up @@ -143,7 +144,7 @@ public function testMultipleRows(): void
$this->assertSame('value2', $result->fetchField(0));
}

$this->expectException(Notice::class);
$this->expectException(PHP_MAJOR_VERSION < 8 ? Notice::class : Warning::class);
$result->fetchField(1);
}

Expand Down

0 comments on commit 3feda71

Please sign in to comment.