summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/composer/ComposerVersionNormalizerTest.php
blob: c5c0dc7d6b8f0cd4d1c7f0df52fcb2ab07dbb554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php

/**
 * @covers ComposerVersionNormalizer
 *
 * @group ComposerHooks
 *
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class ComposerVersionNormalizerTest extends PHPUnit\Framework\TestCase {

	use MediaWikiCoversValidator;
	use PHPUnit4And6Compat;

	/**
	 * @dataProvider nonStringProvider
	 */
	public function testGivenNonString_normalizeThrowsInvalidArgumentException( $nonString ) {
		$normalizer = new ComposerVersionNormalizer();

		$this->setExpectedException( InvalidArgumentException::class );
		$normalizer->normalizeSuffix( $nonString );
	}

	public function nonStringProvider() {
		return [
			[ null ],
			[ 42 ],
			[ [] ],
			[ new stdClass() ],
			[ true ],
		];
	}

	/**
	 * @dataProvider simpleVersionProvider
	 */
	public function testGivenSimpleVersion_normalizeSuffixReturnsAsIs( $simpleVersion ) {
		$this->assertRemainsUnchanged( $simpleVersion );
	}

	protected function assertRemainsUnchanged( $version ) {
		$normalizer = new ComposerVersionNormalizer();

		$this->assertEquals(
			$version,
			$normalizer->normalizeSuffix( $version )
		);
	}

	public function simpleVersionProvider() {
		return [
			[ '1.22.0' ],
			[ '1.19.2' ],
			[ '1.19.2.0' ],
			[ '1.9' ],
			[ '123.321.456.654' ],
		];
	}

	/**
	 * @dataProvider complexVersionProvider
	 */
	public function testGivenComplexVersionWithoutDash_normalizeSuffixAddsDash(
		$withoutDash, $withDash
	) {
		$normalizer = new ComposerVersionNormalizer();

		$this->assertEquals(
			$withDash,
			$normalizer->normalizeSuffix( $withoutDash )
		);
	}

	public function complexVersionProvider() {
		return [
			[ '1.22.0alpha', '1.22.0-alpha' ],
			[ '1.22.0RC', '1.22.0-RC' ],
			[ '1.19beta', '1.19-beta' ],
			[ '1.9RC4', '1.9-RC4' ],
			[ '1.9.1.2RC4', '1.9.1.2-RC4' ],
			[ '1.9.1.2RC', '1.9.1.2-RC' ],
			[ '123.321.456.654RC9001', '123.321.456.654-RC9001' ],
		];
	}

	/**
	 * @dataProvider complexVersionProvider
	 */
	public function testGivenComplexVersionWithDash_normalizeSuffixReturnsAsIs(
		$withoutDash, $withDash
	) {
		$this->assertRemainsUnchanged( $withDash );
	}

	/**
	 * @dataProvider fourLevelVersionsProvider
	 */
	public function testGivenFourLevels_levelCountNormalizationDoesNothing( $version ) {
		$normalizer = new ComposerVersionNormalizer();

		$this->assertEquals(
			$version,
			$normalizer->normalizeLevelCount( $version )
		);
	}

	public function fourLevelVersionsProvider() {
		return [
			[ '1.22.0.0' ],
			[ '1.19.2.4' ],
			[ '1.19.2.0' ],
			[ '1.9.0.1' ],
			[ '123.321.456.654' ],
			[ '123.321.456.654RC4' ],
			[ '123.321.456.654-RC4' ],
		];
	}

	/**
	 * @dataProvider levelNormalizationProvider
	 */
	public function testGivenFewerLevels_levelCountNormalizationEnsuresFourLevels(
		$expected, $version
	) {
		$normalizer = new ComposerVersionNormalizer();

		$this->assertEquals(
			$expected,
			$normalizer->normalizeLevelCount( $version )
		);
	}

	public function levelNormalizationProvider() {
		return [
			[ '1.22.0.0', '1.22' ],
			[ '1.22.0.0', '1.22.0' ],
			[ '1.19.2.0', '1.19.2' ],
			[ '12345.0.0.0', '12345' ],
			[ '12345.0.0.0-RC4', '12345-RC4' ],
			[ '12345.0.0.0-alpha', '12345-alpha' ],
		];
	}

	/**
	 * @dataProvider invalidVersionProvider
	 */
	public function testGivenInvalidVersion_normalizeSuffixReturnsAsIs( $invalidVersion ) {
		$this->assertRemainsUnchanged( $invalidVersion );
	}

	public function invalidVersionProvider() {
		return [
			[ '1.221-a' ],
			[ '1.221-' ],
			[ '1.22rc4a' ],
			[ 'a1.22rc' ],
			[ '.1.22rc' ],
			[ 'a' ],
			[ 'alpha42' ],
		];
	}
}