first commit

This commit is contained in:
2020-06-08 23:57:36 +02:00
commit 6277454f7a
16057 changed files with 1715382 additions and 0 deletions
@@ -0,0 +1,34 @@
<?php
namespace Drupal\TestTools\Comparator;
use Drupal\Component\Render\MarkupInterface;
use SebastianBergmann\Comparator\Comparator;
/**
* Compares MarkupInterface objects for equality.
*/
class MarkupInterfaceComparator extends Comparator {
/**
* {@inheritdoc}
*/
public function accepts($expected, $actual) {
// If at least one argument is a MarkupInterface object, we take over and
// convert to strings before comparing.
return ($expected instanceof MarkupInterface && $actual instanceof MarkupInterface) ||
($expected instanceof MarkupInterface && is_scalar($actual)) ||
(is_scalar($expected) && $actual instanceof MarkupInterface);
}
/**
* {@inheritdoc}
*/
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = FALSE, $ignoreCase = FALSE) {
$expected_safe = (string) $expected;
$actual_safe = (string) $actual;
$comparator = $this->factory->getComparatorFor($expected_safe, $actual_safe);
$comparator->assertEquals($expected_safe, $actual_safe, $delta, $canonicalize, $ignoreCase);
}
}
@@ -0,0 +1,24 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit6;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
/**
* Listens to PHPUnit test runs.
*
* @internal
*/
class AfterSymfonyListener implements TestListener {
use TestListenerDefaultImplementation;
/**
* {@inheritdoc}
*/
public function endTest(Test $test, $time) {
restore_error_handler();
}
}
@@ -0,0 +1,40 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit6;
use Drupal\Tests\Listeners\DeprecationListenerTrait;
use Drupal\Tests\Listeners\DrupalComponentTestListenerTrait;
use Drupal\Tests\Listeners\DrupalStandardsListenerTrait;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
use PHPUnit\Framework\Test;
/**
* Listens to PHPUnit test runs.
*
* @internal
*/
class DrupalListener implements TestListener {
use TestListenerDefaultImplementation;
use DeprecationListenerTrait;
use DrupalComponentTestListenerTrait;
use DrupalStandardsListenerTrait;
/**
* {@inheritdoc}
*/
public function startTest(Test $test) {
$this->deprecationStartTest($test);
}
/**
* {@inheritdoc}
*/
public function endTest(Test $test, $time) {
$this->deprecationEndTest($test, $time);
$this->componentEndTest($test, $time);
$this->standardsEndTest($test, $time);
}
}
@@ -0,0 +1,57 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit6;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\file\FileInterface;
/**
* Makes Drupal's test API forward compatible with multiple versions of PHPUnit.
*/
trait FileFieldTestBaseTrait {
/**
* Asserts that a file exists physically on disk.
*
* Overrides PHPUnit\Framework\Assert::assertFileExists() to also work with
* file entities.
*
* @param \Drupal\File\FileInterface|string $file
* Either the file entity or the file URI.
* @param string $message
* (optional) A message to display with the assertion.
*
* @see https://www.drupal.org/node/3057326
*/
public static function assertFileExists($file, $message = NULL) {
if ($file instanceof FileInterface) {
@trigger_error('Passing a File entity as $file argument to FileFieldTestBase::assertFileExists is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Instead, pass the File entity URI via File::getFileUri(). See https://www.drupal.org/node/3057326', E_USER_DEPRECATED);
$file = $file->getFileUri();
}
$message = isset($message) ? $message : new FormattableMarkup('File %file exists on the disk.', ['%file' => $file]);
parent::assertFileExists($file, $message);
}
/**
* Asserts that a file does not exist on disk.
*
* Overrides PHPUnit\Framework\Assert::assertFileNotExists() to also work
* with file entities.
*
* @param \Drupal\File\FileInterface|string $file
* Either the file entity or the file URI.
* @param string $message
* (optional) A message to display with the assertion.
*
* @see https://www.drupal.org/node/3057326
*/
public static function assertFileNotExists($file, $message = NULL) {
if ($file instanceof FileInterface) {
@trigger_error('Passing a File entity as $file argument to FileFieldTestBase::assertFileNotExists is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Instead, pass the File entity URI via File::getFileUri(). See https://www.drupal.org/node/3057326', E_USER_DEPRECATED);
$file = $file->getFileUri();
}
$message = isset($message) ? $message : new FormattableMarkup('File %file exists on the disk.', ['%file' => $file]);
parent::assertFileNotExists($file, $message);
}
}
@@ -0,0 +1,27 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit6;
use Drupal\Tests\Listeners\HtmlOutputPrinterTrait;
use PHPUnit\Framework\TestResult;
use PHPUnit\TextUI\ResultPrinter;
/**
* Defines a class for providing html output results for functional tests.
*
* @internal
*/
class HtmlOutputPrinter extends ResultPrinter {
use HtmlOutputPrinterTrait;
/**
* {@inheritdoc}
*/
public function printResult(TestResult $result) {
parent::printResult($result);
$this->printHtmlOutput();
}
}
@@ -0,0 +1,17 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit6;
/**
* Defines a class for providing html output links in the Simpletest UI.
*/
class SimpletestUiPrinter extends HtmlOutputPrinter {
/**
* {@inheritdoc}
*/
public function write($buffer) {
$this->simpletestUiWrite($buffer);
}
}
@@ -0,0 +1,22 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit6;
/**
* Makes Drupal's test API forward compatible with multiple versions of PHPUnit.
*/
trait StubTestSuiteBaseTrait {
/**
* {@inheritdoc}
*/
public function addTestFiles($filenames) {
// We stub addTestFiles() because the parent implementation can't deal with
// vfsStream-based filesystems due to an error in
// stream_resolve_include_path(). See
// https://github.com/mikey179/vfsStream/issues/5 Here we just store the
// test file being added in $this->testFiles.
$this->testFiles = array_merge($this->testFiles, $filenames);
}
}
@@ -0,0 +1,224 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit6;
/**
* Makes Drupal's test API forward compatible with multiple versions of PHPUnit.
*/
trait TestCompatibilityTrait {
/**
* @todo deprecate this method override in
* https://www.drupal.org/project/drupal/issues/2742585
*
* @see \Drupal\simpletest\TestBase::assertTrue()
*/
public static function assertTrue($actual, $message = '') {
if (is_bool($actual)) {
parent::assertTrue($actual, $message);
}
else {
@trigger_error('Support for asserting against non-boolean values in ::assertTrue is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use a different assert method, for example, ::assertNotEmpty(). See https://www.drupal.org/node/3082086', E_USER_DEPRECATED);
parent::assertNotEmpty($actual, $message);
}
}
/**
* @todo deprecate this method override in
* https://www.drupal.org/project/drupal/issues/2742585
*
* @see \Drupal\simpletest\TestBase::assertFalse()
*/
public static function assertFalse($actual, $message = '') {
if (is_bool($actual)) {
parent::assertFalse($actual, $message);
}
else {
@trigger_error('Support for asserting against non-boolean values in ::assertFalse is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use a different assert method, for example, ::assertEmpty(). See https://www.drupal.org/node/3082086', E_USER_DEPRECATED);
parent::assertEmpty($actual, $message);
}
}
/**
* Forward compatibility for assertStringContainsString.
*/
public static function assertStringContainsString($needle, $haystack, $message = '') {
static::assertContains((string) $needle, (string) $haystack, $message);
}
/**
* Forward compatibility for assertStringContainsStringIgnoringCase.
*/
public static function assertStringContainsStringIgnoringCase($needle, $haystack, $message = '') {
static::assertContains((string) $needle, (string) $haystack, $message, TRUE);
}
/**
* Forward compatibility for assertStringNotContainsString.
*/
public static function assertStringNotContainsString($needle, $haystack, $message = '') {
static::assertNotContains((string) $needle, (string) $haystack, $message);
}
/**
* Forward compatibility for assertStringNotContainsStringIgnoringCase.
*/
public static function assertStringNotContainsStringIgnoringCase($needle, $haystack, $message = '') {
static::assertNotContains((string) $needle, (string) $haystack, $message, TRUE);
}
/**
* Forward compatibility for assertEqualsCanonicalizing.
*/
public static function assertEqualsCanonicalizing($expected, $actual, $message = '') {
static::assertEquals($expected, $actual, $message, 0.0, 10, TRUE);
}
/**
* Forward compatibility for assertNotEqualsCanonicalizing.
*/
public static function assertNotEqualsCanonicalizing($expected, $actual, $message = '') {
static::assertNotEquals($expected, $actual, $message, 0.0, 10, TRUE);
}
/**
* Provides forward-compatibility for assertIsArray().
*/
public static function assertIsArray($actual, $message = '') {
static::assertInternalType('array', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsBool().
*/
public static function assertIsBool($actual, $message = '') {
static::assertInternalType('bool', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsFloat().
*/
public static function assertIsFloat($actual, $message = '') {
static::assertInternalType('float', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsInt().
*/
public static function assertIsInt($actual, $message = '') {
static::assertInternalType('int', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNumeric().
*/
public static function assertIsNumeric($actual, $message = '') {
static::assertInternalType('numeric', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsObject().
*/
public static function assertIsObject($actual, $message = '') {
static::assertInternalType('object', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsResource().
*/
public static function assertIsResource($actual, $message = '') {
static::assertInternalType('resource', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsString().
*/
public static function assertIsString($actual, $message = '') {
static::assertInternalType('string', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsScalar().
*/
public static function assertIsScalar($actual, $message = '') {
static::assertInternalType('scalar', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsCallable().
*/
public static function assertIsCallable($actual, $message = '') {
static::assertInternalType('callable', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotArray().
*/
public static function assertIsNotArray($actual, $message = '') {
static::assertNotInternalType('array', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotBool().
*/
public static function assertIsNotBool($actual, $message = '') {
static::assertNotInternalType('bool', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotFloat().
*/
public static function assertIsNotFloat($actual, $message = '') {
static::assertNotInternalType('float', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotInt().
*/
public static function assertIsNotInt($actual, $message = '') {
static::assertNotInternalType('int', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotNumeric().
*/
public static function assertIsNotNumeric($actual, $message = '') {
static::assertNotInternalType('numeric', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotObject().
*/
public static function assertIsNotObject($actual, $message = '') {
static::assertNotInternalType('object', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotResource().
*/
public static function assertIsNotResource($actual, $message = '') {
static::assertNotInternalType('resource', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotString().
*/
public static function assertIsNotString($actual, $message = '') {
static::assertNotInternalType('string', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotScalar().
*/
public static function assertIsNotScalar($actual, $message = '') {
static::assertNotInternalType('scalar', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotCallable().
*/
public static function assertIsNotCallable($actual, $message = '') {
static::assertNotInternalType('callable', $actual, $message);
}
}
@@ -0,0 +1,24 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit7;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
/**
* Listens to PHPUnit test runs.
*
* @internal
*/
class AfterSymfonyListener implements TestListener {
use TestListenerDefaultImplementation;
/**
* {@inheritdoc}
*/
public function endTest(Test $test, float $time): void {
restore_error_handler();
}
}
@@ -0,0 +1,40 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit7;
use Drupal\Tests\Listeners\DeprecationListenerTrait;
use Drupal\Tests\Listeners\DrupalComponentTestListenerTrait;
use Drupal\Tests\Listeners\DrupalStandardsListenerTrait;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
use PHPUnit\Framework\Test;
/**
* Listens to PHPUnit test runs.
*
* @internal
*/
class DrupalListener implements TestListener {
use TestListenerDefaultImplementation;
use DeprecationListenerTrait;
use DrupalComponentTestListenerTrait;
use DrupalStandardsListenerTrait;
/**
* {@inheritdoc}
*/
public function startTest(Test $test): void {
$this->deprecationStartTest($test);
}
/**
* {@inheritdoc}
*/
public function endTest(Test $test, float $time): void {
$this->deprecationEndTest($test, $time);
$this->componentEndTest($test, $time);
$this->standardsEndTest($test, $time);
}
}
@@ -0,0 +1,12 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit7;
/**
* Makes Drupal's test API forward compatible with multiple versions of PHPUnit.
*/
trait FileFieldTestBaseTrait {
// @todo remove in Drupal 9.
}
@@ -0,0 +1,27 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit7;
use Drupal\Tests\Listeners\HtmlOutputPrinterTrait;
use PHPUnit\Framework\TestResult;
use PHPUnit\TextUI\ResultPrinter;
/**
* Defines a class for providing html output results for functional tests.
*
* @internal
*/
class HtmlOutputPrinter extends ResultPrinter {
use HtmlOutputPrinterTrait;
/**
* {@inheritdoc}
*/
public function printResult(TestResult $result): void {
parent::printResult($result);
$this->printHtmlOutput();
}
}
@@ -0,0 +1,17 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit7;
/**
* Defines a class for providing html output links in the Simpletest UI.
*/
class SimpletestUiPrinter extends HtmlOutputPrinter {
/**
* {@inheritdoc}
*/
public function write(string $buffer): void {
$this->simpletestUiWrite($buffer);
}
}
@@ -0,0 +1,22 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit7;
/**
* Makes Drupal's test API forward compatible with multiple versions of PHPUnit.
*/
trait StubTestSuiteBaseTrait {
/**
* {@inheritdoc}
*/
public function addTestFiles($filenames): void {
// We stub addTestFiles() because the parent implementation can't deal with
// vfsStream-based filesystems due to an error in
// stream_resolve_include_path(). See
// https://github.com/mikey179/vfsStream/issues/5 Here we just store the
// test file being added in $this->testFiles.
$this->testFiles = array_merge($this->testFiles, $filenames);
}
}
@@ -0,0 +1,224 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit7;
/**
* Makes Drupal's test API forward compatible with multiple versions of PHPUnit.
*/
trait TestCompatibilityTrait {
/**
* @todo deprecate this method override in
* https://www.drupal.org/project/drupal/issues/2742585
*
* @see \Drupal\simpletest\TestBase::assertTrue()
*/
public static function assertTrue($actual, string $message = ''): void {
if (is_bool($actual)) {
parent::assertTrue($actual, $message);
}
else {
@trigger_error('Support for asserting against non-boolean values in ::assertTrue is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use a different assert method, for example, ::assertNotEmpty(). See https://www.drupal.org/node/3082086', E_USER_DEPRECATED);
parent::assertNotEmpty($actual, $message);
}
}
/**
* @todo deprecate this method override in
* https://www.drupal.org/project/drupal/issues/2742585
*
* @see \Drupal\simpletest\TestBase::assertFalse()
*/
public static function assertFalse($actual, string $message = ''): void {
if (is_bool($actual)) {
parent::assertFalse($actual, $message);
}
else {
@trigger_error('Support for asserting against non-boolean values in ::assertFalse is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use a different assert method, for example, ::assertEmpty(). See https://www.drupal.org/node/3082086', E_USER_DEPRECATED);
parent::assertEmpty($actual, $message);
}
}
/**
* Forward compatibility for assertStringContainsString.
*/
public static function assertStringContainsString(string $needle, string $haystack, string $message = ''): void {
static::assertContains($needle, $haystack, $message);
}
/**
* Forward compatibility for assertStringContainsStringIgnoringCase.
*/
public static function assertStringContainsStringIgnoringCase(string $needle, string $haystack, string $message = ''): void {
static::assertContains($needle, $haystack, $message, TRUE);
}
/**
* Forward compatibility for assertStringNotContainsString.
*/
public static function assertStringNotContainsString(string $needle, string $haystack, string $message = ''): void {
static::assertNotContains($needle, $haystack, $message);
}
/**
* Forward compatibility for assertStringNotContainsStringIgnoringCase.
*/
public static function assertStringNotContainsStringIgnoringCase(string $needle, string $haystack, string $message = ''): void {
static::assertNotContains($needle, $haystack, $message, TRUE);
}
/**
* Forward compatibility for assertEqualsCanonicalizing.
*/
public static function assertEqualsCanonicalizing($expected, $actual, string $message = ''): void {
static::assertEquals($expected, $actual, $message, 0.0, 10, TRUE);
}
/**
* Forward compatibility for assertNotEqualsCanonicalizing.
*/
public static function assertNotEqualsCanonicalizing($expected, $actual, string $message = ''): void {
static::assertNotEquals($expected, $actual, $message, 0.0, 10, TRUE);
}
/**
* Provides forward-compatibility for assertIsArray().
*/
public static function assertIsArray($actual, string $message = ''): void {
static::assertInternalType('array', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsBool().
*/
public static function assertIsBool($actual, string $message = ''): void {
static::assertInternalType('bool', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsFloat().
*/
public static function assertIsFloat($actual, string $message = ''): void {
static::assertInternalType('float', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsInt().
*/
public static function assertIsInt($actual, string $message = ''): void {
static::assertInternalType('int', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNumeric().
*/
public static function assertIsNumeric($actual, string $message = ''): void {
static::assertInternalType('numeric', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsObject().
*/
public static function assertIsObject($actual, string $message = ''): void {
static::assertInternalType('object', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsResource().
*/
public static function assertIsResource($actual, string $message = ''): void {
static::assertInternalType('resource', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsString().
*/
public static function assertIsString($actual, string $message = ''): void {
static::assertInternalType('string', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsScalar().
*/
public static function assertIsScalar($actual, string $message = ''): void {
static::assertInternalType('scalar', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsCallable().
*/
public static function assertIsCallable($actual, string $message = ''): void {
static::assertInternalType('callable', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotArray().
*/
public static function assertIsNotArray($actual, string $message = ''): void {
static::assertNotInternalType('array', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotBool().
*/
public static function assertIsNotBool($actual, string $message = ''): void {
static::assertNotInternalType('bool', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotFloat().
*/
public static function assertIsNotFloat($actual, string $message = ''): void {
static::assertNotInternalType('float', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotInt().
*/
public static function assertIsNotInt($actual, string $message = ''): void {
static::assertNotInternalType('int', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotNumeric().
*/
public static function assertIsNotNumeric($actual, string $message = ''): void {
static::assertNotInternalType('numeric', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotObject().
*/
public static function assertIsNotObject($actual, string $message = ''): void {
static::assertNotInternalType('object', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotResource().
*/
public static function assertIsNotResource($actual, string $message = ''): void {
static::assertNotInternalType('resource', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotString().
*/
public static function assertIsNotString($actual, string $message = ''): void {
static::assertNotInternalType('string', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotScalar().
*/
public static function assertIsNotScalar($actual, string $message = ''): void {
static::assertNotInternalType('scalar', $actual, $message);
}
/**
* Provides forward-compatibility for assertIsNotCallable().
*/
public static function assertIsNotCallable($actual, string $message = ''): void {
static::assertNotInternalType('callable', $actual, $message);
}
}
@@ -0,0 +1,30 @@
<?php
namespace Drupal\TestTools\PhpUnitCompatibility;
use PHPUnit\Runner\Version;
/**
* Helper class to determine information about running PHPUnit version.
*
* This class contains static methods only and is not meant to be instantiated.
*/
final class RunnerVersion {
/**
* This class should not be instantiated.
*/
private function __construct() {
}
/**
* Returns the major version of the PHPUnit runner being used.
*
* @return int
* The major version of the PHPUnit runner being used.
*/
public static function getMajor() {
return (int) explode('.', Version::id())[0];
}
}