PhpunitCompatibilityTraitTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Drupal\Tests;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. /**
  5. * Tests the PHPUnit forward compatibility trait.
  6. *
  7. * @coversDefaultClass \Drupal\Tests\PhpunitCompatibilityTrait
  8. * @group Tests
  9. */
  10. class PhpunitCompatibilityTraitTest extends UnitTestCase {
  11. /**
  12. * Tests that getMock is available.
  13. *
  14. * @covers ::getMock
  15. * @group legacy
  16. * @expectedDeprecation \Drupal\Tests\PhpunitCompatibilityTrait::getMock() is deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use \Drupal\Tests\PhpunitCompatibilityTrait::createMock() instead. See https://www.drupal.org/node/2907725
  17. */
  18. public function testGetMock() {
  19. $this->assertInstanceOf('\Drupal\Tests\MockTestClassInterface', $this->getMock(MockTestClassInterface::class));
  20. }
  21. /**
  22. * Tests that setExpectedException is available.
  23. *
  24. * @covers ::setExpectedException
  25. * @group legacy
  26. * @expectedDeprecation \Drupal\Tests\PhpunitCompatibilityTrait:setExpectedException() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Backward compatibility for PHPUnit 4 will no longer be supported. See https://www.drupal.org/node/3056869
  27. */
  28. public function testSetExpectedException() {
  29. $expectedMessage = "Expected message";
  30. $expectedCode = 100;
  31. $this->setExpectedException(\Exception::class, $expectedMessage, $expectedCode);
  32. throw new \Exception($expectedMessage, $expectedCode);
  33. }
  34. /**
  35. * Tests that assert*StringContainsString* methods are available.
  36. *
  37. * @covers ::assertStringContainsString
  38. * @covers ::assertStringContainsStringIgnoringCase
  39. * @covers ::assertStringNotContainsString
  40. * @covers ::assertStringNotContainsStringIgnoringCase
  41. */
  42. public function testAssertStringContainsString() {
  43. $this->assertStringContainsString("bingo", "foobarbingobongo");
  44. $this->assertStringContainsStringIgnoringCase("bingo", "foobarBiNgObongo");
  45. $this->assertStringNotContainsString("buzzer", "BUZZERbingobongo");
  46. $this->assertStringNotContainsStringIgnoringCase("buzzer", "foobarBiNgObongo");
  47. // Test with stringable objects.
  48. $this->assertStringContainsString(new FormattableMarkup("bingo", []), new FormattableMarkup("foobarbingobongo", []));
  49. $this->assertStringContainsStringIgnoringCase(new FormattableMarkup("bingo", []), new FormattableMarkup("foobarBiNgObongo", []));
  50. $this->assertStringNotContainsString(new FormattableMarkup("buzzer", []), new FormattableMarkup("BUZZERbingobongo", []));
  51. $this->assertStringNotContainsStringIgnoringCase(new FormattableMarkup("buzzer", []), new FormattableMarkup("foobarBiNgObongo", []));
  52. }
  53. /**
  54. * Tests that assert(Not)EqualsCanonicalizing methods are available.
  55. *
  56. * @covers ::assertEqualsCanonicalizing
  57. * @covers ::assertNotEqualsCanonicalizing
  58. */
  59. public function testAssertEqualsCanonicalizing() {
  60. $this->assertEqualsCanonicalizing([3, 2, 1], [2, 3, 1]);
  61. $this->assertNotEqualsCanonicalizing([3, 2, 1], [2, 3, 0, 1]);
  62. }
  63. /**
  64. * Tests that assertIs(Not)* methods are available.
  65. *
  66. * @covers ::assertIsArray
  67. * @covers ::assertIsBool
  68. * @covers ::assertIsFloat
  69. * @covers ::assertIsInt
  70. * @covers ::assertIsNumeric
  71. * @covers ::assertIsObject
  72. * @covers ::assertIsResource
  73. * @covers ::assertIsString
  74. * @covers ::assertIsScalar
  75. * @covers ::assertIsCallable
  76. * @covers ::assertIsNotArray
  77. * @covers ::assertIsNotBool
  78. * @covers ::assertIsNotFloat
  79. * @covers ::assertIsNotInt
  80. * @covers ::assertIsNotNumeric
  81. * @covers ::assertIsNotObject
  82. * @covers ::assertIsNotResource
  83. * @covers ::assertIsNotString
  84. * @covers ::assertIsNotScalar
  85. * @covers ::assertIsNotCallable
  86. */
  87. public function testAssertIs() {
  88. $this->assertIsArray([]);
  89. $this->assertIsBool(TRUE);
  90. $this->assertIsFloat(1.0);
  91. $this->assertIsInt(1);
  92. $this->assertIsNumeric(1);
  93. $this->assertIsObject(new class() {});
  94. $this->assertIsResource(fopen(__FILE__, 'rb'));
  95. $this->assertIsString('');
  96. $this->assertIsScalar(1);
  97. $this->assertIsCallable(function () {});
  98. $this->assertIsNotArray(1);
  99. $this->assertIsNotBool([]);
  100. $this->assertIsNotFloat(1);
  101. $this->assertIsNotInt(1.0);
  102. $this->assertIsNotNumeric('');
  103. $this->assertIsNotObject('');
  104. $this->assertIsNotResource('');
  105. $this->assertIsNotString(1);
  106. $this->assertIsNotScalar([]);
  107. $this->assertIsNotCallable(1);
  108. }
  109. }
  110. interface MockTestClassInterface {
  111. }