You are here

class PhpunitCompatibilityTraitTest in Drupal 8

Tests the PHPUnit forward compatibility trait.

@coversDefaultClass \Drupal\Tests\PhpunitCompatibilityTrait @group Tests

Hierarchy

Expanded class hierarchy of PhpunitCompatibilityTraitTest

File

core/tests/Drupal/Tests/PhpunitCompatibilityTraitTest.php, line 13

Namespace

Drupal\Tests
View source
class PhpunitCompatibilityTraitTest extends UnitTestCase {

  /**
   * Tests that getMock is available.
   *
   * @covers ::getMock
   * @group legacy
   * @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
   */
  public function testGetMock() {
    $this
      ->assertInstanceOf('\\Drupal\\Tests\\MockTestClassInterface', $this
      ->getMock(MockTestClassInterface::class));
  }

  /**
   * Tests that setExpectedException is available.
   *
   * @covers ::setExpectedException
   * @group legacy
   * @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
   */
  public function testSetExpectedException() {
    $expectedMessage = "Expected message";
    $expectedCode = 100;
    $this
      ->setExpectedException(\Exception::class, $expectedMessage, $expectedCode);
    throw new \Exception($expectedMessage, $expectedCode);
  }

  /**
   * Tests that assert*StringContainsString* methods are available.
   *
   * @covers ::assertStringContainsString
   * @covers ::assertStringContainsStringIgnoringCase
   * @covers ::assertStringNotContainsString
   * @covers ::assertStringNotContainsStringIgnoringCase
   */
  public function testAssertStringContainsString() {
    $this
      ->assertStringContainsString("bingo", "foobarbingobongo");
    $this
      ->assertStringContainsStringIgnoringCase("bingo", "foobarBiNgObongo");
    $this
      ->assertStringNotContainsString("buzzer", "BUZZERbingobongo");
    $this
      ->assertStringNotContainsStringIgnoringCase("buzzer", "foobarBiNgObongo");

    // Test with stringable objects.
    $this
      ->assertStringContainsString(new FormattableMarkup("bingo", []), new FormattableMarkup("foobarbingobongo", []));
    $this
      ->assertStringContainsStringIgnoringCase(new FormattableMarkup("bingo", []), new FormattableMarkup("foobarBiNgObongo", []));
    $this
      ->assertStringNotContainsString(new FormattableMarkup("buzzer", []), new FormattableMarkup("BUZZERbingobongo", []));
    $this
      ->assertStringNotContainsStringIgnoringCase(new FormattableMarkup("buzzer", []), new FormattableMarkup("foobarBiNgObongo", []));
  }

  /**
   * Tests that assert(Not)EqualsCanonicalizing methods are available.
   *
   * @covers ::assertEqualsCanonicalizing
   * @covers ::assertNotEqualsCanonicalizing
   */
  public function testAssertEqualsCanonicalizing() {
    $this
      ->assertEqualsCanonicalizing([
      3,
      2,
      1,
    ], [
      2,
      3,
      1,
    ]);
    $this
      ->assertNotEqualsCanonicalizing([
      3,
      2,
      1,
    ], [
      2,
      3,
      0,
      1,
    ]);
  }

  /**
   * Tests that assertIs(Not)* methods are available.
   *
   * @covers ::assertIsArray
   * @covers ::assertIsBool
   * @covers ::assertIsFloat
   * @covers ::assertIsInt
   * @covers ::assertIsNumeric
   * @covers ::assertIsObject
   * @covers ::assertIsResource
   * @covers ::assertIsString
   * @covers ::assertIsScalar
   * @covers ::assertIsCallable
   * @covers ::assertIsNotArray
   * @covers ::assertIsNotBool
   * @covers ::assertIsNotFloat
   * @covers ::assertIsNotInt
   * @covers ::assertIsNotNumeric
   * @covers ::assertIsNotObject
   * @covers ::assertIsNotResource
   * @covers ::assertIsNotString
   * @covers ::assertIsNotScalar
   * @covers ::assertIsNotCallable
   */
  public function testAssertIs() {
    $this
      ->assertIsArray([]);
    $this
      ->assertIsBool(TRUE);
    $this
      ->assertIsFloat(1.0);
    $this
      ->assertIsInt(1);
    $this
      ->assertIsNumeric(1);
    $this
      ->assertIsObject(new class {

    });
    $this
      ->assertIsResource(fopen(__FILE__, 'rb'));
    $this
      ->assertIsString('');
    $this
      ->assertIsScalar(1);
    $this
      ->assertIsCallable(function () {
    });
    $this
      ->assertIsNotArray(1);
    $this
      ->assertIsNotBool([]);
    $this
      ->assertIsNotFloat(1);
    $this
      ->assertIsNotInt(1.0);
    $this
      ->assertIsNotNumeric('');
    $this
      ->assertIsNotObject('');
    $this
      ->assertIsNotResource('');
    $this
      ->assertIsNotString(1);
    $this
      ->assertIsNotScalar([]);
    $this
      ->assertIsNotCallable(1);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
PhpunitCompatibilityTraitTest::testAssertEqualsCanonicalizing public function Tests that assert(Not)EqualsCanonicalizing methods are available.
PhpunitCompatibilityTraitTest::testAssertIs public function Tests that assertIs(Not)* methods are available.
PhpunitCompatibilityTraitTest::testAssertStringContainsString public function Tests that assert*StringContainsString* methods are available.
PhpunitCompatibilityTraitTest::testGetMock public function Tests that getMock is available.
PhpunitCompatibilityTraitTest::testSetExpectedException public function Tests that setExpectedException is available.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340