class ModuleVersionTest in Drupal 8
Same name and namespace in other branches
- 9 core/modules/update/tests/src/Unit/ModuleVersionTest.php \Drupal\Tests\update\Unit\ModuleVersionTest
@coversDefaultClass \Drupal\update\ModuleVersion
@group update
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\update\Unit\ModuleVersionTest
Expanded class hierarchy of ModuleVersionTest
File
- core/
modules/ update/ tests/ src/ Unit/ ModuleVersionTest.php, line 13
Namespace
Drupal\Tests\update\UnitView source
class ModuleVersionTest extends UnitTestCase {
/**
* @covers ::getMajorVersion
*
* @dataProvider providerVersionInfos
*
* @param string $version
* The version string to test.
* @param array $expected_version_info
* The expected version information.
*/
public function testGetMajorVersion($version, array $expected_version_info) {
$version = ModuleVersion::createFromVersionString($version);
$this
->assertSame($expected_version_info['major'], $version
->getMajorVersion());
}
/**
* @covers ::getVersionExtra
*
* @dataProvider providerVersionInfos
*
* @param string $version
* The version string to test.
* @param array $expected_version_info
* The expected version information.
*/
public function testGetVersionExtra($version, array $expected_version_info) {
$version = ModuleVersion::createFromVersionString($version);
$this
->assertSame($expected_version_info['extra'], $version
->getVersionExtra());
}
/**
* Data provider for expected version information.
*
* @return array
* Arrays of version information.
*/
public function providerVersionInfos() {
// Data provider values are:
// - The version number to test.
// - Array of expected version information with the following keys:
// -'major': The expected result from ::getMajorVersion().
// -'extra': The expected result from ::getVersionExtra().
return [
'8.x-1.3' => [
'8.x-1.3',
[
'major' => '1',
'extra' => NULL,
],
],
'8.x-1.0' => [
'8.x-1.0',
[
'major' => '1',
'extra' => NULL,
],
],
'8.x-1.0-alpha1' => [
'8.x-1.0-alpha1',
[
'major' => '1',
'extra' => 'alpha1',
],
],
'8.x-1.3-alpha1' => [
'8.x-1.3-alpha1',
[
'major' => '1',
'extra' => 'alpha1',
],
],
'0.1' => [
'0.1',
[
'major' => '0',
'extra' => NULL,
],
],
'1.0' => [
'1.0',
[
'major' => '1',
'extra' => NULL,
],
],
'1.3' => [
'1.3',
[
'major' => '1',
'extra' => NULL,
],
],
'1.0-alpha1' => [
'1.0-alpha1',
[
'major' => '1',
'extra' => 'alpha1',
],
],
'1.3-alpha1' => [
'1.3-alpha1',
[
'major' => '1',
'extra' => 'alpha1',
],
],
'0.2.0' => [
'0.2.0',
[
'major' => '0',
'extra' => NULL,
],
],
'1.2.0' => [
'1.2.0',
[
'major' => '1',
'extra' => NULL,
],
],
'1.0.3' => [
'1.0.3',
[
'major' => '1',
'extra' => NULL,
],
],
'1.2.3' => [
'1.2.3',
[
'major' => '1',
'extra' => NULL,
],
],
'1.2.0-alpha1' => [
'1.2.0-alpha1',
[
'major' => '1',
'extra' => 'alpha1',
],
],
'1.2.3-alpha1' => [
'1.2.3-alpha1',
[
'major' => '1',
'extra' => 'alpha1',
],
],
'8.x-1.x-dev' => [
'8.x-1.x-dev',
[
'major' => '1',
'extra' => 'dev',
],
],
'8.x-8.x-dev' => [
'8.x-8.x-dev',
[
'major' => '8',
'extra' => 'dev',
],
],
'1.x-dev' => [
'1.x-dev',
[
'major' => '1',
'extra' => 'dev',
],
],
'8.x-dev' => [
'8.x-dev',
[
'major' => '8',
'extra' => 'dev',
],
],
'1.0.x-dev' => [
'1.0.x-dev',
[
'major' => '1',
'extra' => 'dev',
],
],
'1.2.x-dev' => [
'1.2.x-dev',
[
'major' => '1',
'extra' => 'dev',
],
],
];
}
/**
* @covers ::createFromVersionString
*
* @dataProvider providerInvalidVersionNumber
*
* @param string $version
* The version string to test.
*/
public function testInvalidVersionNumber($version) {
$this
->expectException(\UnexpectedValueException::class);
$this
->expectExceptionMessage("Unexpected version number in: {$version}");
ModuleVersion::createFromVersionString($version);
}
/**
* Data provider for testInvalidVersionNumber().
*/
public function providerInvalidVersionNumber() {
return static::createKeyedTestCases([
'',
'8',
'x',
'xx',
'8.x-',
'8.x',
'.x',
'.0',
'.1',
'.1.0',
'1.0.',
'x.1',
'1.x.0',
'1.1.x',
'1.1.x-extra',
'x.1.1',
'1.1.1.1',
'1.1.1.0',
]);
}
/**
* @covers ::createFromVersionString
*
* @dataProvider providerInvalidVersionCorePrefix
*
* @param string $version
* The version string to test.
*/
public function testInvalidVersionCorePrefix($version) {
$this
->expectException(\UnexpectedValueException::class);
$this
->expectExceptionMessage("Unexpected version core prefix in {$version}. The only core prefix expected in \\Drupal\\update\\ModuleVersion is: 8.x-");
ModuleVersion::createFromVersionString($version);
}
/**
* Data provider for testInvalidVersionCorePrefix().
*/
public function providerInvalidVersionCorePrefix() {
return static::createKeyedTestCases([
'6.x-1.0',
'7.x-1.x',
'9.x-1.x',
'10.x-1.x',
]);
}
/**
* @covers ::createFromSupportBranch
*
* @dataProvider providerInvalidBranchCorePrefix
*
* @param string $branch
* The branch to test.
*/
public function testInvalidBranchCorePrefix($branch) {
$this
->expectException(\UnexpectedValueException::class);
$this
->expectExceptionMessage("Unexpected version core prefix in {$branch}0. The only core prefix expected in \\Drupal\\update\\ModuleVersion is: 8.x-");
ModuleVersion::createFromSupportBranch($branch);
}
/**
* Data provider for testInvalidBranchCorePrefix().
*/
public function providerInvalidBranchCorePrefix() {
return static::createKeyedTestCases([
'6.x-1.',
'7.x-1.',
'9.x-1.',
'10.x-1.',
]);
}
/**
* @covers ::createFromSupportBranch
*
* @dataProvider providerCreateFromSupportBranch
*
* @param string $branch
* The branch to test.
* @param string $expected_major
* The expected major version.
*/
public function testCreateFromSupportBranch($branch, $expected_major) {
$version = ModuleVersion::createFromSupportBranch($branch);
$this
->assertInstanceOf(ModuleVersion::class, $version);
$this
->assertSame($expected_major, $version
->getMajorVersion());
// Version extra can't be determined from a branch.
$this
->assertSame(NULL, $version
->getVersionExtra());
}
/**
* Data provider for testCreateFromSupportBranch().
*/
public function providerCreateFromSupportBranch() {
// Data provider values are:
// - The version number to test.
// - Array of expected version information with the following keys:
// -'major': The expected result from ::getMajorVersion().
// -'extra': The expected result from ::getVersionExtra().
return [
'0.' => [
'0.',
'0',
],
'1.' => [
'1.',
'1',
],
'0.1.' => [
'0.1.',
'0',
],
'1.2.' => [
'1.2.',
'1',
],
'8.x-1.' => [
'8.x-1.',
'1',
],
];
}
/**
* @covers ::createFromSupportBranch
*
* @dataProvider provideInvalidBranch
*
* @param string $branch
* The branch to test.
*/
public function testInvalidBranch($branch) {
$this
->expectException(\UnexpectedValueException::class);
$this
->expectExceptionMessage("Invalid support branch: {$branch}");
ModuleVersion::createFromSupportBranch($branch);
}
/**
* Data provider for testInvalidBranch().
*/
public function provideInvalidBranch() {
return self::createKeyedTestCases([
'8.x-1.0',
'8.x-2.x',
'2.x-1.0',
'1.1',
'1.x',
'1.1.x',
'1.1.1',
'1.1.1.1',
]);
}
/**
* Creates test case arrays for data provider methods.
*
* @param string[] $test_arguments
* The test arguments.
*
* @return array
* An array with $test_arguments as keys and each element of $test_arguments
* as a single item array
*/
protected static function createKeyedTestCases(array $test_arguments) {
return array_combine($test_arguments, array_map(function ($test_argument) {
return [
$test_argument,
];
}, $test_arguments));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ModuleVersionTest:: |
protected static | function | Creates test case arrays for data provider methods. | |
ModuleVersionTest:: |
public | function | Data provider for testInvalidBranch(). | |
ModuleVersionTest:: |
public | function | Data provider for testCreateFromSupportBranch(). | |
ModuleVersionTest:: |
public | function | Data provider for testInvalidBranchCorePrefix(). | |
ModuleVersionTest:: |
public | function | Data provider for testInvalidVersionCorePrefix(). | |
ModuleVersionTest:: |
public | function | Data provider for testInvalidVersionNumber(). | |
ModuleVersionTest:: |
public | function | Data provider for expected version information. | |
ModuleVersionTest:: |
public | function | @covers ::createFromSupportBranch | |
ModuleVersionTest:: |
public | function | @covers ::getMajorVersion | |
ModuleVersionTest:: |
public | function | @covers ::getVersionExtra | |
ModuleVersionTest:: |
public | function | @covers ::createFromSupportBranch | |
ModuleVersionTest:: |
public | function | @covers ::createFromSupportBranch | |
ModuleVersionTest:: |
public | function | @covers ::createFromVersionString | |
ModuleVersionTest:: |
public | function | @covers ::createFromVersionString | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |