View source
<?php
namespace Drupal\Tests\Core\Extension;
use Drupal\Component\Version\Constraint;
use Drupal\Core\Extension\Dependency;
use Drupal\Tests\UnitTestCase;
class DependencyTest extends UnitTestCase {
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());
}
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;
}
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'));
}
public function testOffsetTest() {
$dependency = new Dependency('views', 'drupal', '>8.x-1.1');
$this
->assertTrue(isset($dependency['name']));
$this
->assertFalse(isset($dependency['foo']));
}
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']);
}
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'];
}
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']);
}
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';
}
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'));
}
}