Skip to content

Commit 1da53bd

Browse files
authored
Merge pull request #506 from defuse/phpunit-compat2
Make PHPUnit tests pass in PHP 8.0, PHP 8.1 and PHP 8.2 thanks to @boenrobot
2 parents 9006087 + 90c263c commit 1da53bd

20 files changed

Lines changed: 224 additions & 334 deletions

.github/workflows/ci.yml

Lines changed: 0 additions & 107 deletions
This file was deleted.

.travis.yml

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,44 @@ matrix:
99
- php: "7.0"
1010
env: USE_PSALM=0
1111
- php: "7.1"
12-
env: USE_PSALM=1
12+
env: USE_PSALM=0
1313
- php: "7.2"
14-
env: USE_PSALM=1
14+
env: USE_PSALM=0
1515
- php: "7.3"
16-
env: USE_PSALM=1
16+
env: USE_PSALM=0
1717
- php: "7.4"
1818
env: USE_PSALM=1
1919
- php: "8.0"
2020
env: USE_PSALM=1
21+
- php: "8.1"
22+
env: USE_PSALM=1
23+
- php: "8.2"
24+
env: USE_PSALM=1
25+
dist: focal
2126
- php: "nightly"
2227
env: USE_PSALM=1
2328
- php: "hhvm"
2429
env: USE_PSALM=1
2530
allow_failures:
2631
- php: "nightly"
2732
- php: "hhvm"
28-
- php: "8.0"
33+
# Travis-CI's 8.2 is currently broken, see:
34+
# https://github.com/defuse/php-encryption/pull/506#issuecomment-1594084107
35+
#- php: "8.2"
2936
install:
3037
- composer install
31-
- curl -LSs https://box-project.github.io/box2/installer.php | php
32-
- mkdir ~/box
33-
- mv box.phar ~/box/box
3438
before_script:
3539
- echo "xdebug.mode = coverage" > extra_php_config.ini
3640
- phpenv config-add extra_php_config.ini
3741
script:
3842
- ./test.sh
39-
- PATH=$PATH:~/box/ make -C dist/ build-phar
40-
- ./test.sh dist/defuse-crypto.phar
43+
# - mkdir /tmp/box
44+
# - chmod 755 /tmp/box
45+
# - curl -LSs https://github.com/box-project/box/releases/download/4.3.8/box.phar -o /tmp/box/box
46+
# - chmod 755 /tmp/box/box
47+
# - PATH="$PATH:/tmp/box/" which box
48+
# - PATH="$PATH:/tmp/box/" make -C dist/ build-phar
49+
# - ./test.sh dist/defuse-crypto.phar
4150
- if [[ $USE_PSALM -eq 1 ]]; then composer require --with-all-dependencies --dev "vimeo/psalm:dev-master"; fi
4251
- if [[ $USE_PSALM -eq 1 ]]; then composer install; fi
4352
- if [[ $USE_PSALM -eq 1 ]]; then vendor/bin/psalm; fi

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"php": ">=5.6.0"
2727
},
2828
"require-dev": {
29-
"phpunit/phpunit": "^4|^5|^6|^7|^8|^9"
29+
"phpunit/phpunit": "^5|^6|^7|^8|^9|^10",
30+
"yoast/phpunit-polyfills": "^2.0.0"
3031
},
3132
"bin": [
3233
"bin/generate-defuse-key"

src/Core.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,14 @@ public static function incrementCounter($ctr, $inc)
9898
*/
9999
public static function secureRandom($octets)
100100
{
101+
if ($octets <= 0) {
102+
throw new Ex\CryptoException(
103+
'A zero or negative amount of random bytes was requested.'
104+
);
105+
}
101106
self::ensureFunctionExists('random_bytes');
102107
try {
103-
return \random_bytes($octets);
108+
return \random_bytes(max(1, $octets));
104109
} catch (\Exception $ex) {
105110
throw new Ex\EnvironmentIsBrokenException(
106111
'Your system does not have a secure random number generator.'

src/File.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ private static function encryptFileInternal($inputFilename, $outputFilename, Key
196196
}
197197

198198
/* Open the input file. */
199+
self::removePHPUnitErrorHandler();
199200
$if = @\fopen($inputFilename, 'rb');
201+
self::restorePHPUnitErrorHandler();
200202
if ($if === false) {
201203
throw new Ex\IOException(
202204
'Cannot open input file for encrypting: ' .
@@ -209,7 +211,9 @@ private static function encryptFileInternal($inputFilename, $outputFilename, Key
209211
}
210212

211213
/* Open the output file. */
214+
self::removePHPUnitErrorHandler();
212215
$of = @\fopen($outputFilename, 'wb');
216+
self::restorePHPUnitErrorHandler();
213217
if ($of === false) {
214218
\fclose($if);
215219
throw new Ex\IOException(
@@ -265,7 +269,9 @@ private static function decryptFileInternal($inputFilename, $outputFilename, Key
265269
}
266270

267271
/* Open the input file. */
272+
self::removePHPUnitErrorHandler();
268273
$if = @\fopen($inputFilename, 'rb');
274+
self::restorePHPUnitErrorHandler();
269275
if ($if === false) {
270276
throw new Ex\IOException(
271277
'Cannot open input file for decrypting: ' .
@@ -279,7 +285,9 @@ private static function decryptFileInternal($inputFilename, $outputFilename, Key
279285
}
280286

281287
/* Open the output file. */
288+
self::removePHPUnitErrorHandler();
282289
$of = @\fopen($outputFilename, 'wb');
290+
self::restorePHPUnitErrorHandler();
283291
if ($of === false) {
284292
\fclose($if);
285293
throw new Ex\IOException(
@@ -770,9 +778,38 @@ private static function getLastErrorMessage()
770778
{
771779
$error = error_get_last();
772780
if ($error === null) {
773-
return '[no PHP error]';
781+
return '[no PHP error, or you have a custom error handler set]';
774782
} else {
775783
return $error['message'];
776784
}
777785
}
786+
787+
/**
788+
* PHPUnit sets an error handler, which prevents getLastErrorMessage() from working,
789+
* because error_get_last does not work when custom handlers are set.
790+
*
791+
* This is a workaround, which should be a no-op in production deployments, to make
792+
* getLastErrorMessage() return the error messages that the PHPUnit tests expect.
793+
*
794+
* If, in a production deployment, a custom error handler is set, the exception
795+
* handling will still work as usual, but the error messages will be confusing.
796+
*
797+
* @return void
798+
*/
799+
private static function removePHPUnitErrorHandler() {
800+
if (defined('PHPUNIT_COMPOSER_INSTALL') || defined('__PHPUNIT_PHAR__')) {
801+
set_error_handler(null);
802+
}
803+
}
804+
805+
/**
806+
* Undoes what removePHPUnitErrorHandler did.
807+
*
808+
* @return void
809+
*/
810+
private static function restorePHPUnitErrorHandler() {
811+
if (defined('PHPUNIT_COMPOSER_INSTALL') || defined('__PHPUNIT_PHAR__')) {
812+
restore_error_handler();
813+
}
814+
}
778815
}

test/phpunit-10.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<phpunit beStrictAboutTestsThatDoNotTestAnything="false">
2+
<source>
3+
<include>
4+
<directory suffix=".php">../src</directory>
5+
</include>
6+
</source>
7+
<coverage includeUncoveredFiles="true">
8+
</coverage>
9+
</phpunit>

test/phpunit-5.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit>
2+
<filter>
3+
<whitelist processUncoveredFilesFromWhitelist="true">
4+
<directory suffix=".php">../src</directory>
5+
</whitelist>
6+
</filter>
7+
</phpunit>

test/phpunit-8.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit>
2+
<filter>
3+
<whitelist processUncoveredFilesFromWhitelist="true">
4+
<directory suffix=".php">../src</directory>
5+
</whitelist>
6+
</filter>
7+
</phpunit>

test/phpunit.sh

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,12 @@ if [ "$clean" -eq 1 ]; then
3333
fi
3434

3535
# Let's grab the latest release and its signature
36+
phpunitversion=$([ $PHP_VERSION -ge 80100 ] && echo "10" || ([ $PHP_VERSION -ge 70200 ] && echo "8" || echo "5"))
3637
if [ ! -f phpunit.phar ]; then
37-
if [[ $PHP_VERSION -ge 50600 ]]; then
38-
wget -O phpunit.phar https://phar.phpunit.de/phpunit-5.7.phar
39-
else
40-
wget -O phpunit.phar https://phar.phpunit.de/phpunit-4.8.phar
41-
fi
38+
wget -O phpunit.phar https://phar.phpunit.de/phpunit-$phpunitversion.phar
4239
fi
4340
if [ ! -f phpunit.phar.asc ]; then
44-
if [[ $PHP_VERSION -ge 50600 ]]; then
45-
wget -O phpunit.phar.asc https://phar.phpunit.de/phpunit-5.7.phar.asc
46-
else
47-
wget -O phpunit.phar.asc https://phar.phpunit.de/phpunit-4.8.phar.asc
48-
fi
41+
wget -O phpunit.phar.asc https://phar.phpunit.de/phpunit-$phpunitversion.phar.asc
4942
fi
5043

5144
# What are the major/minor versions?
@@ -56,19 +49,19 @@ gpg --verify phpunit.phar.asc phpunit.phar
5649
if [ $? -eq 0 ]; then
5750
echo
5851
if [ "$2" -eq "1" ]; then
59-
COVERAGE1_ARGS="--coverage-clover=$parentdir/coverage1.xml -c $parentdir/test/phpunit.xml"
60-
COVERAGE2_ARGS="--coverage-clover=$parentdir/coverage2.xml -c $parentdir/test/phpunit.xml"
52+
COVERAGE1_ARGS="--coverage-clover=$parentdir/coverage1.xml"
53+
COVERAGE2_ARGS="--coverage-clover=$parentdir/coverage2.xml"
6154
else
6255
COVERAGE1_ARGS=""
6356
COVERAGE2_ARGS=""
6457
fi
6558
echo -e "\033[33mBegin Unit Testing\033[0m"
6659
# Run the test suite with normal func_overload.
67-
php -d mbstring.func_overload=0 phpunit.phar $COVERAGE1_ARGS --bootstrap "$parentdir/$1" "$parentdir/test/unit" && \
60+
php -d mbstring.func_overload=0 phpunit.phar -c "$parentdir/test/phpunit-$phpunitversion.xml" $COVERAGE1_ARGS --bootstrap "$parentdir/$1" "$parentdir/test/unit" && \
6861
# Run the test suite again with funky func_overload.
6962
# This is deprecated in PHP 7 and PHPUnit is no longer compatible with the options.
7063
if [[ $PHP_VERSION -le 50600 ]]; then
71-
php -d mbstring.func_overload=7 phpunit.phar $COVERAGE2_ARGS --bootstrap "$parentdir/$1" "$parentdir/test/unit"
64+
php -d mbstring.func_overload=7 phpunit.phar -c "$parentdir/test/phpunit-$phpunitversion.xml" $COVERAGE2_ARGS --bootstrap "$parentdir/$1" "$parentdir/test/unit"
7265
fi
7366
EXITCODE=$?
7467
# Cleanup

test/phpunit.xml

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)