1<?php 2 3namespace Drupal\KernelTests\Core\Test\Comparator; 4 5use Drupal\Component\Render\FormattableMarkup; 6use Drupal\Core\StringTranslation\TranslatableMarkup; 7use Drupal\KernelTests\KernelTestBase; 8use Drupal\TestTools\Comparator\MarkupInterfaceComparator; 9use PHPUnit\Framework\Error\Notice; 10use PHPUnit\Framework\Error\Warning; 11use SebastianBergmann\Comparator\Factory; 12use SebastianBergmann\Comparator\ComparisonFailure; 13 14/** 15 * Tests \Drupal\TestTools\Comparator\MarkupInterfaceComparator. 16 * 17 * We need to test the class with a kernel test since casting MarkupInterface 18 * objects to strings can require an initialized container. 19 * 20 * @group Test 21 * 22 * @coversDefaultClass \Drupal\TestTools\Comparator\MarkupInterfaceComparator 23 */ 24class MarkupInterfaceComparatorTest extends KernelTestBase { 25 26 /** 27 * @var \Drupal\TestTools\Comparator\MarkupInterfaceComparator 28 */ 29 protected $comparator; 30 31 /** 32 * @var \SebastianBergmann\Comparator\Factory 33 */ 34 protected $factory; 35 36 /** 37 * {@inheritdoc} 38 */ 39 protected function setUp(): void { 40 parent::setUp(); 41 $this->factory = new Factory(); 42 $this->comparator = new MarkupInterfaceComparator(); 43 $this->comparator->setFactory($this->factory); 44 } 45 46 /** 47 * Provides test data for the comparator. 48 * 49 * @return array 50 * Each array entry has: 51 * - test expected value, 52 * - test actual value, 53 * - a bool indicating the expected return value of ::accepts, 54 * - a value indicating the expected result of ::assertEquals, TRUE if 55 * comparison should match, FALSE if error, or a class name of an object 56 * thrown. 57 */ 58 public function dataSetProvider() { 59 return [ 60 'FormattableMarkup vs FormattableMarkup, equal' => [ 61 new FormattableMarkup('goldfinger', []), 62 new FormattableMarkup('goldfinger', []), 63 TRUE, 64 TRUE, 65 ], 66 'FormattableMarkup vs FormattableMarkup, not equal' => [ 67 new FormattableMarkup('goldfinger', []), 68 new FormattableMarkup('moonraker', []), 69 TRUE, 70 ComparisonFailure::class, 71 ], 72 'FormattableMarkup vs string, equal' => [ 73 new FormattableMarkup('goldfinger', []), 74 'goldfinger', 75 TRUE, 76 TRUE, 77 ], 78 'string vs FormattableMarkup, equal' => [ 79 'goldfinger', 80 new FormattableMarkup('goldfinger', []), 81 TRUE, 82 TRUE, 83 ], 84 'TranslatableMarkup vs FormattableMarkup, equal' => [ 85 new TranslatableMarkup('goldfinger'), 86 new FormattableMarkup('goldfinger', []), 87 TRUE, 88 TRUE, 89 ], 90 'TranslatableMarkup vs string, not equal' => [ 91 new TranslatableMarkup('goldfinger'), 92 'moonraker', 93 TRUE, 94 ComparisonFailure::class, 95 ], 96 'TranslatableMarkup vs int, equal' => [ 97 new TranslatableMarkup('1234'), 98 1234, 99 TRUE, 100 TRUE, 101 ], 102 'int vs TranslatableMarkup, equal' => [ 103 1234, 104 new TranslatableMarkup('1234'), 105 TRUE, 106 TRUE, 107 ], 108 'FormattableMarkup vs array' => [ 109 new FormattableMarkup('goldfinger', []), 110 ['goldfinger'], 111 FALSE, 112 PHP_VERSION_ID >= 80000 ? Warning::class : Notice::class, 113 ], 114 'stdClass vs TranslatableMarkup' => [ 115 (object) ['goldfinger'], 116 new TranslatableMarkup('goldfinger'), 117 FALSE, 118 FALSE, 119 ], 120 'string vs string, equal' => [ 121 'goldfinger', 122 'goldfinger', 123 FALSE, 124 TRUE, 125 ], 126 ]; 127 } 128 129 /** 130 * @covers ::accepts 131 * @dataProvider dataSetProvider 132 */ 133 public function testAccepts($expected, $actual, $accepts_result, $equals_result) { 134 if ($accepts_result) { 135 $this->assertTrue($this->comparator->accepts($expected, $actual)); 136 } 137 else { 138 $this->assertFalse($this->comparator->accepts($expected, $actual)); 139 } 140 } 141 142 /** 143 * @covers ::assertEquals 144 * @dataProvider dataSetProvider 145 */ 146 public function testAssertEquals($expected, $actual, $accepts_result, $equals_result) { 147 try { 148 $this->assertNull($this->comparator->assertEquals($expected, $actual)); 149 $this->assertTrue($equals_result); 150 } 151 catch (\Throwable $e) { 152 if ($equals_result === FALSE) { 153 $this->assertNotNull($e->getMessage()); 154 } 155 else { 156 $this->assertInstanceOf($equals_result, $e); 157 } 158 } 159 } 160 161} 162