You are here

class PhpUnitWarningsTest in Drupal 9

@coversDefaultClass \Drupal\Tests\Traits\PhpUnitWarnings @group legacy

Hierarchy

Expanded class hierarchy of PhpUnitWarningsTest

File

core/tests/Drupal/Tests/PhpUnitWarningsTest.php, line 11

Namespace

Drupal\Tests
View source
class PhpUnitWarningsTest extends UnitTestCase {

  /**
   * Tests that selected PHPUnit warning is converted to deprecation.
   */
  public function testAddWarning() {
    $this
      ->expectDeprecation('Test warning for \\Drupal\\Tests\\PhpUnitWarningsTest::testAddWarning()');
    $this
      ->addWarning('Test warning for \\Drupal\\Tests\\PhpUnitWarningsTest::testAddWarning()');
  }

  /**
   * Tests assertContains.
   */
  public function testAssertContains() {
    if (RunnerVersion::getMajor() > 8) {
      $this
        ->markTestSkipped("In PHPUnit 9+, argument 2 passed to PHPUnit\\Framework\\Assert::assertContains() must be iterable.");
    }
    $this
      ->expectDeprecation('Using assertContains() with string haystacks is deprecated and will not be supported in PHPUnit 9. Refactor your test to use assertStringContainsString() or assertStringContainsStringIgnoringCase() instead.');
    $this
      ->expectDeprecation('The optional $ignoreCase parameter of assertContains() is deprecated and will be removed in PHPUnit 9.');
    $this
      ->assertContains('string', 'aaaa_string_aaa');
    $this
      ->assertContains('STRING', 'aaaa_string_aaa', '', TRUE);
  }

  /**
   * Tests assertNotContains.
   */
  public function testAssertNotContains() {
    if (RunnerVersion::getMajor() > 8) {
      $this
        ->markTestSkipped("In PHPUnit 9+, argument 2 passed to PHPUnit\\Framework\\Assert::assertNotContains() must be iterable.");
    }
    $this
      ->expectDeprecation('Using assertNotContains() with string haystacks is deprecated and will not be supported in PHPUnit 9. Refactor your test to use assertStringNotContainsString() or assertStringNotContainsStringIgnoringCase() instead.');
    $this
      ->expectDeprecation('The optional $ignoreCase parameter of assertNotContains() is deprecated and will be removed in PHPUnit 9.');
    $this
      ->assertNotContains('foo', 'bar');
    $this
      ->assertNotContains('FOO', 'bar', '', TRUE);
  }

  /**
   * Tests assertArraySubset.
   */
  public function testAssertArraySubset() {
    if (RunnerVersion::getMajor() > 8) {
      $this
        ->markTestSkipped("In PHPUnit 9+, assertArraySubset() is removed.");
    }
    $this
      ->expectDeprecation('assertArraySubset() is deprecated and will be removed in PHPUnit 9.');
    $this
      ->assertArraySubset([
      'a',
    ], [
      'a',
      'b',
    ]);
  }

  /**
   * Tests assertInternalType.
   */
  public function testAssertInternalType() {
    if (RunnerVersion::getMajor() > 8) {
      $this
        ->markTestSkipped("In PHPUnit 9+, assertInternalType() is removed.");
    }
    $this
      ->expectDeprecation('assertInternalType() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertIsString() instead.');
    $this
      ->assertInternalType('string', 'string');
  }

  /**
   * Tests assertion methods accessing class attributes.
   */
  public function testAssertAttribute() {
    if (RunnerVersion::getMajor() > 8) {
      $this
        ->markTestSkipped("In PHPUnit 9+, assertion methods accessing class attributes are removed.");
    }
    $this
      ->expectDeprecation('assertAttributeEquals() is deprecated and will be removed in PHPUnit 9.');
    $this
      ->expectDeprecation('readAttribute() is deprecated and will be removed in PHPUnit 9.');
    $this
      ->expectDeprecation('getObjectAttribute() is deprecated and will be removed in PHPUnit 9.');
    $this
      ->expectDeprecation('assertAttributeSame() is deprecated and will be removed in PHPUnit 9.');
    $this
      ->expectDeprecation('assertAttributeInstanceOf() is deprecated and will be removed in PHPUnit 9.');
    $this
      ->expectDeprecation('assertAttributeEmpty() is deprecated and will be removed in PHPUnit 9.');
    $obj = new class {
      protected $attribute = 'value';
      protected $class;
      protected $empty;
      public function __construct() {
        $this->class = new \stdClass();
      }

    };
    $this
      ->assertAttributeEquals('value', 'attribute', $obj);
    $this
      ->assertAttributeSame('value', 'attribute', $obj);
    $this
      ->assertAttributeInstanceOf(\stdClass::class, 'class', $obj);
    $this
      ->assertAttributeEmpty('empty', $obj);
  }

  /**
   * Tests assertEquals.
   */
  public function testAssertEquals() {
    if (RunnerVersion::getMajor() > 8) {
      $this
        ->markTestSkipped("In PHPUnit 9+, the \$canonicalize parameter of assertEquals() is removed.");
    }
    $this
      ->expectDeprecation('The optional $canonicalize parameter of assertEquals() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertEqualsCanonicalizing() instead.');
    $this
      ->assertEquals([
      'a',
      'b',
    ], [
      'b',
      'a',
    ], '', 0.0, 10, TRUE);
  }

  /**
   * Tests expectExceptionMessageRegExp.
   */
  public function testExpectExceptionMessageRegExp() {
    if (RunnerVersion::getMajor() > 8) {
      $this
        ->markTestSkipped("In PHPUnit 9+, expectExceptionMessageRegExp() is removed.");
    }
    $this
      ->expectDeprecation('expectExceptionMessageRegExp() is deprecated in PHPUnit 8 and will be removed in PHPUnit 9. Use expectExceptionMessageMatches() instead.');
    $this
      ->expectException(\Exception::class);
    $this
      ->expectExceptionMessageRegExp('/An exception .*/');
    throw new \Exception('An exception has been triggered');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
PhpUnitWarningsTest::testAddWarning public function Tests that selected PHPUnit warning is converted to deprecation.
PhpUnitWarningsTest::testAssertArraySubset public function Tests assertArraySubset.
PhpUnitWarningsTest::testAssertAttribute public function Tests assertion methods accessing class attributes.
PhpUnitWarningsTest::testAssertContains public function Tests assertContains.
PhpUnitWarningsTest::testAssertEquals public function Tests assertEquals.
PhpUnitWarningsTest::testAssertInternalType public function Tests assertInternalType.
PhpUnitWarningsTest::testAssertNotContains public function Tests assertNotContains.
PhpUnitWarningsTest::testExpectExceptionMessageRegExp public function Tests expectExceptionMessageRegExp.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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 308
UnitTestCase::setUpBeforeClass public static function