You are here

class DependencyTest in Drupal 8

Same name in this branch
  1. 8 core/tests/Drupal/Tests/Core/Extension/DependencyTest.php \Drupal\Tests\Core\Extension\DependencyTest
  2. 8 core/modules/system/tests/src/Functional/Module/DependencyTest.php \Drupal\Tests\system\Functional\Module\DependencyTest
Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Extension/DependencyTest.php \Drupal\Tests\Core\Extension\DependencyTest
  2. 10 core/tests/Drupal/Tests/Core/Extension/DependencyTest.php \Drupal\Tests\Core\Extension\DependencyTest

@coversDefaultClass \Drupal\Core\Extension\Dependency @group Extension

Hierarchy

Expanded class hierarchy of DependencyTest

File

core/tests/Drupal/Tests/Core/Extension/DependencyTest.php, line 13

Namespace

Drupal\Tests\Core\Extension
View source
class DependencyTest extends UnitTestCase {

  /**
   * @covers ::createFromString
   * @dataProvider providerCreateFromString
   */
  public function testCreateFromString($string, $expected_name, $expected_project, $expected_constraint) {
    $dependency = Dependency::createFromString($string);
    $this
      ->assertSame($expected_name, $dependency
      ->getName());
    $this
      ->assertSame($expected_project, $dependency
      ->getProject());
    $this
      ->assertSame($expected_constraint, $dependency
      ->getConstraintString());
  }

  /**
   * Data provider for testCreateFromString.
   */
  public function providerCreateFromString() {
    $tests = [];
    $tests['module_name_only'] = [
      'views',
      'views',
      '',
      '',
    ];
    $tests['module_and_project_names'] = [
      'drupal:views',
      'views',
      'drupal',
      '',
    ];
    $tests['module_and_constraint'] = [
      'views (<8.x-3.1)',
      'views',
      '',
      '<8.x-3.1',
    ];
    $tests['module_and_project_names_and_constraint'] = [
      'drupal:views (>8.x-1.1)',
      'views',
      'drupal',
      '>8.x-1.1',
    ];
    return $tests;
  }

  /**
   * @covers ::isCompatible
   */
  public function testIsCompatible() {
    $dependency = new Dependency('paragraphs_demo', 'paragraphs', '>8.x-1.1');
    $this
      ->assertFalse($dependency
      ->isCompatible('1.1'));
    $this
      ->assertTrue($dependency
      ->isCompatible('1.2'));
  }

  /**
   * @covers ::offsetExists
   * @group legacy
   * @expectedDeprecation Array access to Drupal\Core\Extension\Dependency properties is deprecated. Use accessor methods instead. See https://www.drupal.org/node/2756875
   */
  public function testOffsetTest() {
    $dependency = new Dependency('views', 'drupal', '>8.x-1.1');
    $this
      ->assertTrue(isset($dependency['name']));
    $this
      ->assertFalse(isset($dependency['foo']));
  }

  /**
   * @covers ::offsetGet
   * @group legacy
   * @expectedDeprecation Array access to the Drupal\Core\Extension\Dependency name property is deprecated. Use Drupal\Core\Extension\Dependency::getName() instead. See https://www.drupal.org/node/2756875
   * @expectedDeprecation Array access to the Drupal\Core\Extension\Dependency project property is deprecated. Use Drupal\Core\Extension\Dependency::getProject() instead. See https://www.drupal.org/node/2756875
   * @expectedDeprecation Array access to the Drupal\Core\Extension\Dependency original_version property is deprecated. Use Drupal\Core\Extension\Dependency::getConstraintString() instead. See https://www.drupal.org/node/2756875
   * @expectedDeprecation Array access to the Drupal\Core\Extension\Dependency versions property is deprecated. See https://www.drupal.org/node/2756875
   */
  public function testOffsetGet() {
    $dependency = new Dependency('views', 'drupal', '>8.x-1.1');
    $this
      ->assertSame('views', $dependency['name']);
    $this
      ->assertSame('drupal', $dependency['project']);
    $this
      ->assertSame(' (>8.x-1.1)', $dependency['original_version']);
    $this
      ->assertSame([
      [
        'op' => '>',
        'version' => '1.1',
      ],
    ], $dependency['versions']);
  }

  /**
   * @covers ::offsetGet
   * @group legacy
   */
  public function testOffsetGetException() {
    $dependency = new Dependency('views', 'drupal', '>8.x-1.1');
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('The does_not_exist key is not supported');
    $dependency['does_not_exist'];
  }

  /**
   * @covers ::offsetUnset
   * @group legacy
   */
  public function testOffsetUnset() {
    $dependency = new Dependency('views', 'drupal', '>8.x-1.1');
    $this
      ->expectException(\BadMethodCallException::class);
    $this
      ->expectExceptionMessage('Drupal\\Core\\Extension\\Dependency::offsetUnset() is not supported');
    unset($dependency['name']);
  }

  /**
   * @covers ::offsetSet
   * @group legacy
   */
  public function testOffsetSet() {
    $dependency = new Dependency('views', 'drupal', '>8.x-1.1');
    $this
      ->expectException(\BadMethodCallException::class);
    $this
      ->expectExceptionMessage('Drupal\\Core\\Extension\\Dependency::offsetSet() is not supported');
    $dependency['name'] = 'foo';
  }

  /**
   * Ensures that constraint objects are not serialized.
   *
   * @covers ::__sleep
   */
  public function testSerialization() {
    $dependency = new Dependency('paragraphs_demo', 'paragraphs', '>8.x-1.1');
    $this
      ->assertTrue($dependency
      ->isCompatible('1.2'));
    $this
      ->assertInstanceOf(Constraint::class, $this
      ->getObjectAttribute($dependency, 'constraint'));
    $dependency = unserialize(serialize($dependency));
    $this
      ->assertNull($this
      ->getObjectAttribute($dependency, 'constraint'));
    $this
      ->assertTrue($dependency
      ->isCompatible('1.2'));
    $this
      ->assertInstanceOf(Constraint::class, $this
      ->getObjectAttribute($dependency, 'constraint'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencyTest::providerCreateFromString public function Data provider for testCreateFromString.
DependencyTest::testCreateFromString public function @covers ::createFromString @dataProvider providerCreateFromString
DependencyTest::testIsCompatible public function @covers ::isCompatible
DependencyTest::testOffsetGet public function @covers ::offsetGet @group legacy @expectedDeprecation Array access to the Drupal\Core\Extension\Dependency name property is deprecated. Use Drupal\Core\Extension\Dependency::getName() instead. See…
DependencyTest::testOffsetGetException public function @covers ::offsetGet @group legacy
DependencyTest::testOffsetSet public function @covers ::offsetSet @group legacy
DependencyTest::testOffsetTest public function @covers ::offsetExists @group legacy @expectedDeprecation Array access to Drupal\Core\Extension\Dependency properties is deprecated. Use accessor methods instead. See https://www.drupal.org/node/2756875
DependencyTest::testOffsetUnset public function @covers ::offsetUnset @group legacy
DependencyTest::testSerialization public function Ensures that constraint objects are not serialized.
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.
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