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