Skip to content

Commit

Permalink
Made tests be able to run on all supported PHP versions, and run succ…
Browse files Browse the repository at this point in the history
…essfully.

As part of accomplishing this, the following was done:

- Added "yoast/phpunit-polyfills" as a dev dependency.
- Modified fixtures to use the polyfilled methods (as they are cross version compatible).
- Added a custom test runner (that is compatible across PHPUnit versions).
- Modified PHPUnit version to be "^7|^8|^9", since each is supported by different PHP versions.
- Modified the rector version to "^0.12.19", as that is the last one still supporting PHP 7.1.
- Changed expectNotice() and expectError() to expecting the equivalent PHPUnit exception, to avoid deprecation notice and support PHPUnit 7, where this feature is absent.
- Removed the requires annotation in Zend_Filter_Encrypt_OpensslTest
- Explicitly added "RC4" as the value for the algo to appease supported PHP versions. Why RC4? Because that is the default, and has been the default, and ZF has had no option to override it. Such option could be introduced in a separate PR maybe.
- Fixed a type related warning in OpenSSL related tests, by casting array keys to strings before the remaining checks that assume a string.
- Added an openssl.conf file that enables the legacy provider, in addition to the default, in order to enable the tests to pass in newer OpenSSL versions that even older PHP versions are now being compiled against.

Also provided higher test coverage overall. To accomplish that part:

- Added "symfony/polyfill-ctype" as a runtime dependency, as it can be disabled (while the test tooling does not require it), but is required by many components.
- Made PHPUnit run twice - once with the bare minimum of extensions needed to get it going, and a second time with all extensions that can be enabled for the given PHP version. Zend_Barcode_AllTests in particular benefits of this, as it has both tests that work only if GD is disabled, as well as tests that run only if GD is enabled.
- Moved the PHP extensions from the env into a matrix, and adjusted the set of extensions to be one that the PHP version would support.
- Adjusted the JSON encoder and decoder to explicitly use "UTF-16BE" instead of just "UTF-16", as the mbstring polyfill uses iconv, which in turn aliases "UTF-16" to "UTF-16LE", while the true mbstring extension aliases "UTF-16" to "UTF-16BE". The explicit notation works with both.
- Adjusted the testDisablingTryCommonMagicFilesIgnoresCommonLocations() test to call setMagicFile(false) when the environment is not good, i.e. when fileinfo is not present.
- Added some requires annotations where PDO was assumed to be present.
  • Loading branch information
boenrobot committed Jun 15, 2023
1 parent 7fa9c5f commit b4e595f
Show file tree
Hide file tree
Showing 1,265 changed files with 3,792 additions and 3,659 deletions.
63 changes: 53 additions & 10 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: PHPUnit-9
name: PHPUnit

on: [push, pull_request]

Expand All @@ -9,7 +9,6 @@ jobs:
runs-on: ubuntu-22.04

env:
PHP_EXTENSIONS: none, posix, curl, dom, json, libxml, mbstring, openssl, tokenizer, xml, xmlwriter, ctype, iconv, simplexml, pdo_sqlite, pdo_mysql, fileinfo, json, zip, sqlite, soap, bcmath, mcrypt, igbinary, gd, bz2, lzf, rar, memcached, memcache
PHP_INI_VALUES: memory_limit=-1, assert.exception=1, zend.assertions=1, error_reporting=-1, log_errors_max_len=0, display_errors=On

TESTS_ZEND_DB_ADAPTER_PDO_MYSQL_ENABLED: true
Expand All @@ -34,7 +33,7 @@ jobs:
TESTS_ZEND_CACHE_LIBMEMCACHED_HOST: 127.0.0.1
TESTS_ZEND_CACHE_LIBMEMCACHED_PORT: 11211

# https://hub.docker.com/r/bitnami/openldap
# https://hub.docker.com/r/bitnami/openldap
LDAP_ROOT: "dc=example,dc=com"
LDAP_ALLOW_ANON_BINDING: false
LDAP_SKIP_DEFAULT_TREE: "yes"
Expand All @@ -46,6 +45,7 @@ jobs:
TESTS_ZEND_AUTH_ADAPTER_LDAP_ONLINE_ENABLED: true

LOCALES: "fr_FR@euro fr_FR fr_BE.UTF-8 de en_US"
OPENSSL_CONF: "./tests/openssl.conf"
services:
memcache:
image: memcached:1.6.17-alpine
Expand All @@ -60,7 +60,7 @@ jobs:
MYSQL_DATABASE: ${{ env.TESTS_ZEND_DB_ADAPTER_MYSQL_DATABASE }}
MYSQL_AUTHENTICATION_PLUGIN: mysql_native_password
ports:
- 3306:3306
- 3306:3306
options: >-
--health-cmd "mysqladmin ping"
--health-interval 10s
Expand Down Expand Up @@ -99,13 +99,28 @@ jobs:
fail-fast: false
matrix:
php-version:
- "7.1"
- "7.2"
- "7.3"
- "7.4"
- "8.0"
- "8.1"
- "8.2"
experimental:
- false
include:
#bare for PHP >=7.2
- php-extensions-bare: "none, iconv, json, libxml, xml, dom, simplexml, xmlwriter, tokenizer, mbstring"
#full for PHP <= 8.0
- php-extensions-full: "none, iconv, json, libxml, xml, dom, simplexml, xmlwriter, tokenizer, mbstring, ctype, openssl, curl, gd, posix, pdo_sqlite, pdo_mysql, fileinfo, zip, sqlite, soap, bcmath, igbinary, bz2, lzf, memcached, memcache, ldap, sqlite, mcrypt, rar"
- php-version: "7.1"
php-extensions-bare: "none, iconv, json, libxml, xml, dom, simplexml, xmlwriter, tokenizer"
- php-version: "8.1"
php-extensions-full: "none, iconv, json, libxml, xml, dom, simplexml, xmlwriter, tokenizer, mbstring, ctype, openssl, curl, gd, posix, pdo_sqlite, pdo_mysql, fileinfo, zip, sqlite, soap, bcmath, igbinary, bz2, lzf, memcached, memcache, ldap, sqlite, mcrypt"
- php-version: "8.2"
php-extensions-full: "none, iconv, json, libxml, xml, dom, simplexml, xmlwriter, tokenizer, mbstring, ctype, openssl, curl, gd, posix, pdo_sqlite, pdo_mysql, fileinfo, zip, sqlite, soap, bcmath, igbinary, bz2, lzf, memcached, memcache, ldap, sqlite, mcrypt"
- php-version: "8.3"
php-extensions-full: "none, iconv, json, libxml, xml, dom, simplexml, xmlwriter, tokenizer, mbstring, ctype, openssl, curl, gd, posix, pdo_sqlite, pdo_mysql, fileinfo, zip, sqlite, soap, bcmath, igbinary, bz2, lzf, memcached, memcache, ldap, sqlite"
experimental: true

steps:
Expand All @@ -118,32 +133,60 @@ jobs:
echo "All languages..."
locale -a
- name: Provider config base on env for intergrate test
- name: Provider config base on env for integrate test
run: cp tests/TestConfiguration.env.php tests/TestConfiguration.php

- name: Install PHP with extensions
- name: Install PHP with minimal extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: cs2pr
extensions: memcached, memcache
extensions: ${{ matrix.php-extensions-bare }}
ini-values: ${{ env.PHP_INI_VALUES }}
env:
# https:/shivammathur/setup-php/issues/407#issuecomment-773675741
fail-fast: true

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

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php-version }}-composer-

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Lint PHP source files
run: |
bin/parallel-lint --exclude vendor --exclude tests/Zend/Loader/_files/ParseError.php . --checkstyle | cs2pr
- name: "Run tests on PHP ${{ matrix.php-version }} (Experimental: ${{ matrix.experimental }}) with minimal extensions"
run: |
bin/phpunit -c tests/phpunit.xml
continue-on-error: ${{ matrix.experimental }}

- name: Setup LDAP
run: |
sudo apt-get install -y libnss-ldap libpam-ldap ldap-utils
tests/resources/openldap/docker-entrypoint-initdb.d/init.sh
- name: "Run tests on PHP ${{ matrix.php-version }} (Experimental: ${{ matrix.experimental }})"
run: bin/phpunit -c tests/phpunit.xml
continue-on-error: ${{ matrix.experimental }}
- name: Install PHP with extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: cs2pr
extensions: ${{ matrix.php-extensions-full }}
ini-values: ${{ env.PHP_INI_VALUES }}
env:
# https:/shivammathur/setup-php/issues/407#issuecomment-773675741
fail-fast: true

- name: "Run tests on PHP ${{ matrix.php-version }} (Experimental: ${{ matrix.experimental }}) with extensions"
run: |
bin/phpunit -c tests/phpunit.xml
continue-on-error: ${{ matrix.experimental }}
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"require": {
"php": ">=7.1",
"symfony/polyfill-php81": "^1.24",
"symfony/polyfill-ctype": "^1.27",
"symfony/polyfill-mbstring": "^1.26"
},
"suggest": {
Expand All @@ -33,9 +34,10 @@
}
},
"require-dev": {
"phpunit/phpunit": "^9",
"phpunit/phpunit": "^7|^8|^9",
"php-parallel-lint/php-parallel-lint": "^1.3",
"rector/rector": "^0.14.8"
"rector/rector": "^0.12.19",
"yoast/phpunit-polyfills": "2.0"
},
"archive": {
"exclude": ["/demos", "/documentation", "/tests"]
Expand Down
9 changes: 5 additions & 4 deletions library/Zend/Filter/Encrypt/Openssl.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ protected function _setKeys($keys)
}

foreach ($keys as $type => $key) {
if (ctype_print($key) && is_file(realpath($key)) && is_readable($key)) {
$key = (string)$key;
if (ctype_print($key) && is_file($key) && is_readable($key)) {
$file = fopen($key, 'r');
$cert = fread($file, 8192);
fclose($file);
Expand Down Expand Up @@ -354,7 +355,7 @@ public function setPackage($package)
*/
public function encrypt($value)
{
$encrypted = [];
$encrypted = '';
$encryptedkeys = [];

if (count($this->_keys['public']) === 0) {
Expand Down Expand Up @@ -385,7 +386,7 @@ public function encrypt($value)
$value = $compress->filter($value);
}

$crypt = openssl_seal($value, $encrypted, $encryptedkeys, $keys);
$crypt = openssl_seal($value, $encrypted, $encryptedkeys, $keys, 'RC4');
if (PHP_VERSION_ID < 80000) {
foreach ($keys as $key) {
openssl_free_key($key);
Expand Down Expand Up @@ -465,7 +466,7 @@ public function decrypt($value)
$value = substr($value, $length);
}

$crypt = openssl_open($value, $decrypted, $envelope, $keys);
$crypt = openssl_open($value, $decrypted, $envelope, $keys, 'RC4');
if (PHP_VERSION_ID < 80000) {
openssl_free_key($keys);
}
Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Json/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ protected static function _utf162utf8($utf16)
{
// Check for mb extension otherwise do by hand.
if( function_exists('mb_convert_encoding') ) {
return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16BE');
}

$bytes = (ord($utf16[0]) << 8) | ord($utf16[1]);
Expand Down
3 changes: 1 addition & 2 deletions library/Zend/Json/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ protected static function _utf82utf16($utf8)
{
// Check for mb extension otherwise do by hand.
if( function_exists('mb_convert_encoding') ) {
return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
return mb_convert_encoding($utf8, 'UTF-16BE', 'UTF-8');
}

switch (strlen($utf8)) {
Expand Down Expand Up @@ -575,4 +575,3 @@ protected static function _utf82utf16($utf8)
return '';
}
}

4 changes: 2 additions & 2 deletions tests/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function main()
// ob_end_flush();
// }

(new TestRunner())->run(self::suite(), $parameters);
(new resources_Runner())->run(self::suite(), $parameters);
}

/**
Expand Down Expand Up @@ -107,6 +107,6 @@ public static function suite()
}
}

if (PHPUnit_MAIN_METHOD == 'AllTests::main') {
if (PHPUnit_MAIN_METHOD === 'AllTests::main') {
AllTests::main();
}
2 changes: 1 addition & 1 deletion tests/TestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
require_once 'PHPUnit/Runner/Version.php';

$phpunitVersion = Version::id();
if ($phpunitVersion == '@package_version@' || version_compare($phpunitVersion, '3.5.5', '>=')) {
if ($phpunitVersion === '@package_version@' || version_compare($phpunitVersion, '3.5.5', '>=')) {
require_once 'PHPUnit/Autoload.php'; // >= PHPUnit 3.5.5
} else {
require_once 'PHPUnit/Framework.php'; // < PHPUnit 3.5.5
Expand Down
6 changes: 3 additions & 3 deletions tests/Zend/Acl/AclTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use PHPUnit\Framework\TestCase;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

/**
* Zend Framework
Expand Down Expand Up @@ -50,7 +50,7 @@ class Zend_Acl_AclTest extends TestCase
*
* @return void
*/
protected function setUp(): void
protected function set_up()
{
$this->_acl = new Zend_Acl();
}
Expand Down Expand Up @@ -1271,7 +1271,7 @@ public function testAclPassesPrivilegeToAssertClass()
*/
public function testGetRegisteredRolesIsDeprecated()
{
$this->expectNotice();
$this->expectException(\PHPUnit\Framework\Error\Notice::class);
$this->_acl->getRegisteredRoles();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Zend/Acl/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Zend_Acl_AllTests
{
public static function main()
{
(new TestRunner())->run(self::suite());
(new resources_Runner())->run(self::suite());
}

public static function suite()
Expand All @@ -55,6 +55,6 @@ public static function suite()
}
}

if (PHPUnit_MAIN_METHOD == 'Zend_Acl_AllTests::main') {
if (PHPUnit_MAIN_METHOD === 'Zend_Acl_AllTests::main') {
Zend_Acl_AllTests::main();
}
4 changes: 2 additions & 2 deletions tests/Zend/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static function main()
// ob_end_flush();
// }

(new TestRunner())->run(self::suite(), $arguments);
(new resources_Runner())->run(self::suite(), $arguments);
}

/**
Expand Down Expand Up @@ -263,6 +263,6 @@ public static function suite()
}
}

if (PHPUnit_MAIN_METHOD == 'Zend_AllTests::main') {
if (PHPUnit_MAIN_METHOD === 'Zend_AllTests::main') {
Zend_AllTests::main();
}
4 changes: 2 additions & 2 deletions tests/Zend/AllTests/StreamWrapper/PhpInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* <code>
* class ...
* {
* public function setUp()
* public function set_up()
* {
* Zend_AllTests_StreamWrapper_PhpInput::mockInput('expected string');
* }
Expand All @@ -37,7 +37,7 @@
* $this->assertSame('php://input', Zend_AllTests_StreamWrapper_PhpInput::getCurrentPath());
* }
*
* public function tearDown()
* public function tear_down()
* {
* Zend_AllTests_StreamWrapper_PhpInput::restoreDefault();
* }
Expand Down
8 changes: 4 additions & 4 deletions tests/Zend/Amf/Adobe/IntrospectorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use PHPUnit\Framework\TestCase;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use PHPUnit\Framework\TestSuite;
use PHPUnit\TextUI\TestRunner;

Expand Down Expand Up @@ -53,10 +53,10 @@ class Zend_Amf_Adobe_IntrospectorTest extends TestCase
public static function main()
{
$suite = new TestSuite(__CLASS__);
(new TestRunner())->run($suite);
(new resources_Runner())->run($suite);
}

protected function setUp(): void
protected function set_up()
{
$this->introspector = new Zend_Amf_Adobe_Introspector();
}
Expand Down Expand Up @@ -235,6 +235,6 @@ class com_zend_framework_IntrospectorTestExplicitType


// Call Zend_Amf_Adobe_IntrospectorTest::main() if this source file is executed directly.
if (PHPUnit_MAIN_METHOD == "Zend_Amf_Adobe_IntrospectorTest::main") {
if (PHPUnit_MAIN_METHOD === "Zend_Amf_Adobe_IntrospectorTest::main") {
Zend_Amf_Adobe_IntrospectorTest::main();
}
4 changes: 2 additions & 2 deletions tests/Zend/Amf/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Zend_Amf_AllTests
{
public static function main()
{
(new TestRunner())->run(self::suite());
(new resources_Runner())->run(self::suite());
}

public static function suite()
Expand All @@ -76,6 +76,6 @@ public static function suite()
}
}

if (PHPUnit_MAIN_METHOD == 'Zend_Amf_AllTests::main') {
if (PHPUnit_MAIN_METHOD === 'Zend_Amf_AllTests::main') {
Zend_Amf_AllTests::main();
}
Loading

0 comments on commit b4e595f

Please sign in to comment.