Skip to content

Commit

Permalink
Merge pull request #11361 from creative-commoners/pulls/6/phpunit11
Browse files Browse the repository at this point in the history
DEP Use PHPUnit 11
  • Loading branch information
GuySartorelli authored Sep 18, 2024
2 parents 68fb4b0 + 9a92488 commit 7793664
Show file tree
Hide file tree
Showing 135 changed files with 1,364 additions and 1,649 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
"m1/env": "^2.2.0",
"masterminds/html5": "^2.7.6",
"monolog/monolog": "^3.2.0",
"nikic/php-parser": "^4.15.0",
"nikic/php-parser": "^5.1.0",
"psr/container": "^1.1 || ^2.0",
"psr/http-message": "^1",
"sebastian/diff": "^4.0",
"sebastian/diff": "^6.0",
"silverstripe/config": "^3",
"silverstripe/assets": "^3",
"silverstripe/vendor-plugin": "^2",
Expand Down Expand Up @@ -63,7 +63,7 @@
},
"require-dev": {
"composer/semver": "^3.4",
"phpunit/phpunit": "^9.6",
"phpunit/phpunit": "^11.3",
"silverstripe/versioned": "^3",
"squizlabs/php_codesniffer": "^3.7",
"silverstripe/standards": "^1",
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Manifest/ClassManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public function init($includeTests = false, $forceRegen = false)
public function getParser()
{
if (!$this->parser) {
$this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$this->parser = (new ParserFactory)->createForHostVersion();
}

return $this->parser;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Manifest/ClassManifestErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct($pathname)
$this->pathname = $pathname;
}

public function handleError(Error $error)
public function handleError(Error $error): void
{
$newMessage = sprintf('%s in %s', $error->getRawMessage(), $this->pathname);
$error->setRawMessage($newMessage);
Expand Down
5 changes: 0 additions & 5 deletions src/Dev/Constraint/SSListContains.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;
use SilverStripe\Dev\SSListExporter;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\SS_List;
use SilverStripe\View\ViewableData;
Expand All @@ -20,8 +19,6 @@ class SSListContains extends Constraint implements TestOnly
*/
protected $matches = [];

protected SSListExporter $exporter;

/**
* Check if the list has left over items that don't match
*
Expand All @@ -31,8 +28,6 @@ class SSListContains extends Constraint implements TestOnly

public function __construct(array $matches)
{
$this->exporter = new SSListExporter();

$this->matches = $matches;
}

Expand Down
5 changes: 0 additions & 5 deletions src/Dev/Constraint/SSListContainsOnlyMatchingItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;
use SilverStripe\Dev\SSListExporter;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\SS_List;

Expand All @@ -19,17 +18,13 @@ class SSListContainsOnlyMatchingItems extends Constraint implements TestOnly
*/
private $match;

protected SSListExporter $exporter;

/**
* @var ViewableDataContains
*/
private $constraint;

public function __construct($match)
{
$this->exporter = new SSListExporter();

$this->constraint = new ViewableDataContains($match);
$this->match = $match;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Dev/Exceptions/ExpectedErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace SilverStripe\Dev\Exceptions;

use Exception;

class ExpectedErrorException extends Exception
{
}
9 changes: 9 additions & 0 deletions src/Dev/Exceptions/ExpectedNoticeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace SilverStripe\Dev\Exceptions;

use Exception;

class ExpectedNoticeException extends Exception
{
}
9 changes: 9 additions & 0 deletions src/Dev/Exceptions/ExpectedWarningException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace SilverStripe\Dev\Exceptions;

use Exception;

class ExpectedWarningException extends Exception
{
}
2 changes: 0 additions & 2 deletions src/Dev/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ protected function withBaseFolder($folder, $callback)

/**
* Submit a get request
* @uses Director::test()
*
* @param string $url
* @param Session $session
Expand All @@ -155,7 +154,6 @@ public function get($url, $session = null, $headers = null, $cookies = null)
/**
* Submit a post request
*
* @uses Director::test()
* @param string $url
* @param array $data
* @param array $headers
Expand Down
99 changes: 0 additions & 99 deletions src/Dev/SSListExporter.php

This file was deleted.

73 changes: 66 additions & 7 deletions src/Dev/SapphireTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace SilverStripe\Dev;

use Exception;
use Facebook\WebDriver\Exception\UnknownErrorException;
use InvalidArgumentException;
use LogicException;
use PHPUnit\Framework\Constraint\LogicalNot;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Exception as PHPUnitFrameworkException;
use PHPUnit\Framework\TestSize\Unknown;
use PHPUnit\Util\Test as TestUtil;
use SilverStripe\CMS\Controllers\RootURLController;
use SilverStripe\Control\CLIRequestBuilder;
Expand Down Expand Up @@ -41,6 +43,12 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Transport\NullTransport;
use ReflectionMethod;
use ReflectionClass;
use SilverStripe\Dev\Exceptions\ExpectedErrorException;
use SilverStripe\Dev\Exceptions\ExpectedNoticeException;
use SilverStripe\Dev\Exceptions\ExpectedWarningException;
use SilverStripe\Dev\Exceptions\UnexpectedErrorException;

/**
* Test case class for the Silverstripe framework.
Expand Down Expand Up @@ -334,6 +342,37 @@ protected function setUp(): void
Email::config()->remove('bcc_all_emails_to');
}

/**
* @param callable|null $oldHandler
*/
private $oldErrorHandler = null;

/**
* Setup a custom error handler to throw exceptions on errors
*/
protected function enableErrorHandler()
{
// If changing this method, ensure that the corresponding table on 00_Unit_Testing.md
// in silverstripe/deveoper-docs is also updated
$this->oldErrorHandler = set_error_handler(
function (int $errno, string $errstr, string $errfile, int $errline) {
// E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
// cannot be handled in set_error_handler()
// https://www.php.net/manual/en/function.set-error-handler.php
// https://www.php.net/manual/en/errorfunc.constants.php
if (in_array($errno, [E_USER_ERROR, E_RECOVERABLE_ERROR])) {
throw new ExpectedErrorException($errstr);
} elseif (in_array($errno, [E_NOTICE, E_USER_NOTICE])) {
throw new ExpectedNoticeException($errstr);
} elseif (in_array($errno, [E_WARNING, E_USER_WARNING])) {
throw new ExpectedWarningException($errstr);
}
// Use the standard PHP error handler
return false;
}
);
}

/**
* Helper method to determine if the current test should enable a test database
*
Expand Down Expand Up @@ -563,6 +602,12 @@ protected function tearDown(): void

// Call state helpers
static::$state->tearDown($this);

// Reset custom error handler
if ($this->oldErrorHandler) {
restore_error_handler();
$this->oldErrorHandler = null;
}
}

/**
Expand Down Expand Up @@ -1212,16 +1257,30 @@ public static function createInvalidArgumentException($argument, $type, $value =
}

/**
* Returns the annotations for this test.
*
* @return array
* Returns the annotations for this test
*/
public function getAnnotations(): array
{
return TestUtil::parseTestMethodAnnotations(
get_class($this),
$this->getName(false)
);
$class = get_class($this);
$method = $this->name();
$ret = [];
foreach (['method', 'class'] as $what) {
if ($what === 'method') {
$reflection = new ReflectionMethod($class, $method);
} else {
$reflection = new ReflectionClass($class);
}
preg_match_all('#@(.*?)\n#s', $reflection->getDocComment(), $annotations);
$ret[$what] = [];
foreach ($annotations[1] as $annotation) {
$parts = explode(' ', $annotation);
$key = array_shift($parts);
$value = implode(' ', $parts);
$ret[$what][$key] ??= [];
$ret[$what][$key][] = $value;
}
}
return $ret;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/FieldType/DBClassNameTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace SilverStripe\ORM\FieldType;

use RuntimeException;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\ORM\DataObject;
use RuntimeException;
use SilverStripe\View\ViewableData;

trait DBClassNameTrait
Expand Down
3 changes: 2 additions & 1 deletion src/View/Parsers/HtmlDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SebastianBergmann\Diff\Differ;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector;
use SebastianBergmann\Diff\Output\DiffOnlyOutputBuilder;

/**
* Class representing a 'diff' between two sequences of HTML strings.
Expand Down Expand Up @@ -285,7 +286,7 @@ private static function cleanHTML(string $content, ?HTMLCleaner $cleaner = null)
private static function getDiffer(): Differ
{
if (!HtmlDiff::$differ) {
HtmlDiff::$differ = new Differ();
HtmlDiff::$differ = new Differ(new DiffOnlyOutputBuilder());
}
return HtmlDiff::$differ;
}
Expand Down
Loading

0 comments on commit 7793664

Please sign in to comment.