AssertLegacyTrait.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Drupal\KernelTests;
  3. /**
  4. * Translates Simpletest assertion methods to PHPUnit.
  5. *
  6. * Protected methods are custom. Public static methods override methods of
  7. * \PHPUnit\Framework\Assert.
  8. *
  9. * @deprecated Scheduled for removal in Drupal 9.0.0. Use PHPUnit's native
  10. * assert methods instead.
  11. */
  12. trait AssertLegacyTrait {
  13. /**
  14. * @see \Drupal\simpletest\TestBase::assert()
  15. *
  16. * @deprecated Scheduled for removal in Drupal 9.0.0. Use self::assertTrue()
  17. * instead.
  18. */
  19. protected function assert($actual, $message = '') {
  20. parent::assertTrue((bool) $actual, $message);
  21. }
  22. /**
  23. * @see \Drupal\simpletest\TestBase::assertTrue()
  24. */
  25. public static function assertTrue($actual, $message = '') {
  26. if (is_bool($actual)) {
  27. parent::assertTrue($actual, $message);
  28. }
  29. else {
  30. parent::assertNotEmpty($actual, $message);
  31. }
  32. }
  33. /**
  34. * @see \Drupal\simpletest\TestBase::assertFalse()
  35. */
  36. public static function assertFalse($actual, $message = '') {
  37. if (is_bool($actual)) {
  38. parent::assertFalse($actual, $message);
  39. }
  40. else {
  41. parent::assertEmpty($actual, $message);
  42. }
  43. }
  44. /**
  45. * @see \Drupal\simpletest\TestBase::assertEqual()
  46. *
  47. * @deprecated Scheduled for removal in Drupal 9.0.0. Use self::assertEquals()
  48. * instead.
  49. */
  50. protected function assertEqual($actual, $expected, $message = '') {
  51. $this->assertEquals($expected, $actual, $message);
  52. }
  53. /**
  54. * @see \Drupal\simpletest\TestBase::assertNotEqual()
  55. *
  56. * @deprecated Scheduled for removal in Drupal 9.0.0. Use
  57. * self::assertNotEquals() instead.
  58. */
  59. protected function assertNotEqual($actual, $expected, $message = '') {
  60. $this->assertNotEquals($expected, $actual, $message);
  61. }
  62. /**
  63. * @see \Drupal\simpletest\TestBase::assertIdentical()
  64. *
  65. * @deprecated Scheduled for removal in Drupal 9.0.0. Use self::assertSame()
  66. * instead.
  67. */
  68. protected function assertIdentical($actual, $expected, $message = '') {
  69. $this->assertSame($expected, $actual, $message);
  70. }
  71. /**
  72. * @see \Drupal\simpletest\TestBase::assertNotIdentical()
  73. *
  74. * @deprecated Scheduled for removal in Drupal 9.0.0. Use
  75. * self::assertNotSame() instead.
  76. */
  77. protected function assertNotIdentical($actual, $expected, $message = '') {
  78. $this->assertNotSame($expected, $actual, $message);
  79. }
  80. /**
  81. * @see \Drupal\simpletest\TestBase::assertIdenticalObject()
  82. *
  83. * @deprecated Scheduled for removal in Drupal 9.0.0. Use self::assertEquals()
  84. * instead.
  85. */
  86. protected function assertIdenticalObject($actual, $expected, $message = '') {
  87. // Note: ::assertSame checks whether its the same object. ::assertEquals
  88. // though compares
  89. $this->assertEquals($expected, $actual, $message);
  90. }
  91. /**
  92. * @see \Drupal\simpletest\TestBase::pass()
  93. *
  94. * @deprecated Scheduled for removal in Drupal 9.0.0. Use self::assertTrue()
  95. * instead.
  96. */
  97. protected function pass($message) {
  98. $this->assertTrue(TRUE, $message);
  99. }
  100. /**
  101. * @see \Drupal\simpletest\TestBase::verbose()
  102. */
  103. protected function verbose($message) {
  104. if (in_array('--debug', $_SERVER['argv'], TRUE)) {
  105. // Write directly to STDOUT to not produce unexpected test output.
  106. // The STDOUT stream does not obey output buffering.
  107. fwrite(STDOUT, $message . "\n");
  108. }
  109. }
  110. }