class MarkupInterfaceComparatorTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/KernelTests/Core/Test/Comparator/MarkupInterfaceComparatorTest.php \Drupal\KernelTests\Core\Test\Comparator\MarkupInterfaceComparatorTest
- 10 core/tests/Drupal/KernelTests/Core/Test/Comparator/MarkupInterfaceComparatorTest.php \Drupal\KernelTests\Core\Test\Comparator\MarkupInterfaceComparatorTest
- 8.9.x core/tests/Drupal/KernelTests/Core/Test/Comparator/MarkupInterfaceComparatorTest.php \Drupal\KernelTests\Core\Test\Comparator\MarkupInterfaceComparatorTest
Tests \Drupal\TestTools\Comparator\MarkupInterfaceComparator.
We need to test the class with a kernel test since casting MarkupInterface objects to strings can require an initialized container.
@group Test
@coversDefaultClass \Drupal\TestTools\Comparator\MarkupInterfaceComparator
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\KernelTests\Core\Test\Comparator\MarkupInterfaceComparatorTest extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of MarkupInterfaceComparatorTest
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Test/ Comparator/ MarkupInterfaceComparatorTest.php, line 24
Namespace
Drupal\KernelTests\Core\Test\ComparatorView source
class MarkupInterfaceComparatorTest extends KernelTestBase {
/**
* @var \Drupal\TestTools\Comparator\MarkupInterfaceComparator
*/
protected $comparator;
/**
* @var \SebastianBergmann\Comparator\Factory
*/
protected $factory;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->factory = new Factory();
$this->comparator = new MarkupInterfaceComparator();
$this->comparator
->setFactory($this->factory);
}
/**
* Provides test data for the comparator.
*
* @return array
* Each array entry has:
* - test expected value,
* - test actual value,
* - a bool indicating the expected return value of ::accepts,
* - a value indicating the expected result of ::assertEquals, TRUE if
* comparison should match, FALSE if error, or a class name of an object
* thrown.
*/
public function dataSetProvider() {
return [
'FormattableMarkup vs FormattableMarkup, equal' => [
new FormattableMarkup('goldfinger', []),
new FormattableMarkup('goldfinger', []),
TRUE,
TRUE,
],
'FormattableMarkup vs FormattableMarkup, not equal' => [
new FormattableMarkup('goldfinger', []),
new FormattableMarkup('moonraker', []),
TRUE,
ComparisonFailure::class,
],
'FormattableMarkup vs string, equal' => [
new FormattableMarkup('goldfinger', []),
'goldfinger',
TRUE,
TRUE,
],
'string vs FormattableMarkup, equal' => [
'goldfinger',
new FormattableMarkup('goldfinger', []),
TRUE,
TRUE,
],
'TranslatableMarkup vs FormattableMarkup, equal' => [
new TranslatableMarkup('goldfinger'),
new FormattableMarkup('goldfinger', []),
TRUE,
TRUE,
],
'TranslatableMarkup vs string, not equal' => [
new TranslatableMarkup('goldfinger'),
'moonraker',
TRUE,
ComparisonFailure::class,
],
'TranslatableMarkup vs int, equal' => [
new TranslatableMarkup('1234'),
1234,
TRUE,
TRUE,
],
'int vs TranslatableMarkup, equal' => [
1234,
new TranslatableMarkup('1234'),
TRUE,
TRUE,
],
'FormattableMarkup vs array' => [
new FormattableMarkup('goldfinger', []),
[
'goldfinger',
],
FALSE,
PHP_VERSION_ID >= 80000 ? Warning::class : Notice::class,
],
'stdClass vs TranslatableMarkup' => [
(object) [
'goldfinger',
],
new TranslatableMarkup('goldfinger'),
FALSE,
FALSE,
],
'string vs string, equal' => [
'goldfinger',
'goldfinger',
FALSE,
TRUE,
],
];
}
/**
* @covers ::accepts
* @dataProvider dataSetProvider
*/
public function testAccepts($expected, $actual, $accepts_result, $equals_result) {
if ($accepts_result) {
$this->assertTrue($this->comparator
->accepts($expected, $actual));
}
else {
$this->assertFalse($this->comparator
->accepts($expected, $actual));
}
}
/**
* @covers ::assertEquals
* @dataProvider dataSetProvider
*/
public function testAssertEquals($expected, $actual, $accepts_result, $equals_result) {
try {
$this->assertNull($this->comparator
->assertEquals($expected, $actual));
$this->assertTrue($equals_result);
} catch (\Throwable $e) {
if ($equals_result === FALSE) {
$this->assertNotNull($e->getMessage());
}
else {
$this->assertInstanceOf($equals_result, $e);
}
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.