Skip to content

Commit

Permalink
PhpUnit 10 Compatibility Part 3 (Last)
Browse files Browse the repository at this point in the history
Final changes for PhpUnit 10, including enabling it for testing. This finishes the work of PR PHPOffice#3523 and PR PHPOffice#3526.

The major remaining problem with PhpUnit 10 is that earlier releases converted notices and warnings to exceptions, and 10 does not. Not having the information provided by the messages seems risky to me. Fortunately, it appears that you can add an error handler to the test bootstrap for 10 and make it act like earlier versions; I have done so. In order to demonstrate the effectiveness of this handler, a new otherwise unused class Helper/Handler and tests for that class are added.

As part of the testing of this change, it became apparent that the fopen in OLE::getStream attempts to create a dynamic property $context in Shared/OLE/ChainedBlockStream, and that action is deprecated in Php8.2. Adding the property to the class eliminates that problem. No executable code is added, and this is the only change to source code.

There also seems to have been a change in assertXmlStringEqualsXmlString in PhpUnit 10. The only test which uses that method is Chart/Issue589Test, and both the places which use that method could just as easily and effectively use assertSame. They are changed to do so.
  • Loading branch information
oleibman committed Apr 21, 2023
1 parent e9cf273 commit b825c35
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"phpcompatibility/php-compatibility": "^9.3",
"phpstan/phpstan": "^1.1",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^8.5 || ^9.0",
"phpunit/phpunit": "^8.5 || ^9.0 || ^10.0",
"squizlabs/php_codesniffer": "^3.7",
"tecnickcom/tcpdf": "^6.5"
},
Expand Down
46 changes: 46 additions & 0 deletions src/PhpSpreadsheet/Helper/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace PhpOffice\PhpSpreadsheet\Helper;

class Handler
{
/** @var string */
private static $invalidHex = 'Y';

// A bunch of methods to show that we continue
// to capture messages even using PhpUnit 10.
public static function suppressed(): bool
{
return @trigger_error('hello');
}

public static function deprecated(): string
{
return (string) hexdec(self::$invalidHex);
}

public static function notice(string $value): void
{
date_default_timezone_set($value);
}

public static function warning(): bool
{
return file_get_contents(__FILE__ . 'noexist') !== false;
}

public static function userDeprecated(): bool
{
return trigger_error('hello', E_USER_DEPRECATED);
}

public static function userNotice(): bool
{
return trigger_error('userNotice', E_USER_NOTICE);
}

public static function userWarning(): bool
{
return trigger_error('userWarning', E_USER_WARNING);
}
}
3 changes: 3 additions & 0 deletions src/PhpSpreadsheet/Shared/OLE/ChainedBlockStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class ChainedBlockStream
{
/** @var mixed */
public $context;

/**
* The OLE container of the file that is being read.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/PhpSpreadsheetTests/Chart/Issue589Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function testLineChartFill(): void
if ($actualXml === false) {
self::fail('Failure saving the spPr element as xml string!');
} else {
self::assertXmlStringEqualsXmlString('<c:spPr><a:ln><a:solidFill><a:srgbClr val="98B954"/></a:solidFill></a:ln></c:spPr>', $actualXml);
self::assertSame('<c:spPr><a:ln><a:solidFill><a:srgbClr val="98B954"/></a:solidFill></a:ln></c:spPr>', $actualXml);
}
}
}
Expand Down Expand Up @@ -153,7 +153,7 @@ public function testLineChartFillIgnoresColorArray(): void
if ($actualXml === false) {
self::fail('Failure saving the spPr element as xml string!');
} else {
self::assertXmlStringEqualsXmlString('<c:spPr><a:ln/></c:spPr>', $actualXml);
self::assertSame('<c:spPr><a:ln/></c:spPr>', $actualXml);
}
}
}
Expand Down
83 changes: 83 additions & 0 deletions tests/PhpSpreadsheetTests/Helper/HandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Helper;

use PhpOffice\PhpSpreadsheet\Helper\Handler;
use PHPUnit\Framework\TestCase;
use Throwable;

class HandlerTest extends TestCase
{
public function testSuppressed(): void
{
self::assertTrue(Handler::suppressed());
}

public function testDeprecated(): void
{
if (method_exists(\PHPUnit\Framework\TestCase::class, 'setOutputCallback')) {
self::assertTrue(true);
} else {
try {
Handler::deprecated();
self::fail('Expected error/exception did not happen');
} catch (Throwable $e) {
self::assertStringContainsString('Invalid characters', $e->getMessage());
}
}
}

public function testNotice(): void
{
try {
Handler::notice('invalidtz');
self::fail('Expected error/exception did not happen');
} catch (Throwable $e) {
self::assertStringContainsString('Timezone', $e->getMessage());
}
}

public function testWarning(): void
{
try {
Handler::warning();
self::fail('Expected error/exception did not happen');
} catch (Throwable $e) {
self::assertStringContainsString('ailed to open stream', $e->getMessage());
}
}

public function testUserDeprecated(): void
{
if (method_exists(\PHPUnit\Framework\TestCase::class, 'setOutputCallback')) {
self::assertTrue(true);
} else {
try {
Handler::userDeprecated();
self::fail('Expected error/exception did not happen');
} catch (Throwable $e) {
self::assertStringContainsString('hello', $e->getMessage());
}
}
}

public function testUserNotice(): void
{
try {
Handler::userNotice();
self::fail('Expected error/exception did not happen');
} catch (Throwable $e) {
self::assertStringContainsString('userNotice', $e->getMessage());
}
}

public function testUserWarning(): void
{
try {
Handler::userWarning();
self::fail('Expected error/exception did not happen');
} catch (Throwable $e) {
self::assertStringContainsString('userWarning', $e->getMessage());
}
}
}
31 changes: 31 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,34 @@

// PHP 5.3 Compat
//date_default_timezone_set('Europe/London');

function phpunit10ErrorHandler(int $errno, string $errstr, string $filename, int $lineno): bool
{
$x = error_reporting() & $errno;
if (
in_array(
$errno,
[
E_DEPRECATED,
E_WARNING,
E_NOTICE,
E_USER_DEPRECATED,
E_USER_NOTICE,
E_USER_WARNING,
],
true
)
) {
if (0 === $x) {
return true; // message suppressed - stop error handling
}

throw new \Exception("$errstr $filename $lineno");
}

return false; // continue error handling
}

if (!method_exists(\PHPUnit\Framework\TestCase::class, 'setOutputCallback')) {
set_error_handler('phpunit10ErrorHandler');
}

0 comments on commit b825c35

Please sign in to comment.