MarkupInterfaceComparatorTest.php 4.2 KB

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