YiiRequirementCheckerTest.php 5.97 KB
Newer Older
1 2
<?php

Qiang Xue committed
3
require_once(__DIR__.'/../../../../framework/yii/requirements/YiiRequirementChecker.php');
4 5 6 7

use yiiunit\TestCase;

/**
8
 * Test case for [[YiiRequirementChecker]].
9
 * @see YiiRequirementChecker
10
 * @group requirements
11 12 13
 */
class YiiRequirementCheckerTest extends TestCase
{
14 15 16 17
	public function testCheck()
	{
		$requirementsChecker = new YiiRequirementChecker();

Alexander Makarov committed
18 19
		$requirements = [
			'requirementPass' => [
20 21 22 23 24
				'name' => 'Requirement 1',
				'mandatory' => true,
				'condition' => true,
				'by' => 'Requirement 1',
				'memo' => 'Requirement 1',
Alexander Makarov committed
25 26
			],
			'requirementError' => [
27 28 29 30 31
				'name' => 'Requirement 2',
				'mandatory' => true,
				'condition' => false,
				'by' => 'Requirement 2',
				'memo' => 'Requirement 2',
Alexander Makarov committed
32 33
			],
			'requirementWarning' => [
34 35 36 37 38
				'name' => 'Requirement 3',
				'mandatory' => false,
				'condition' => false,
				'by' => 'Requirement 3',
				'memo' => 'Requirement 3',
Alexander Makarov committed
39 40
			],
		];
41

42
		$checkResult = $requirementsChecker->check($requirements)->getResult();
43 44 45 46 47 48 49
		$summary = $checkResult['summary'];

		$this->assertEquals(count($requirements), $summary['total'], 'Wrong summary total!');
		$this->assertEquals(1, $summary['errors'], 'Wrong summary errors!');
		$this->assertEquals(1, $summary['warnings'], 'Wrong summary warnings!');

		$checkedRequirements = $checkResult['requirements'];
50
		$requirementsKeys = array_flip(array_keys($requirements));
51

52 53
		$this->assertEquals(false, $checkedRequirements[$requirementsKeys['requirementPass']]['error'], 'Passed requirement has an error!');
		$this->assertEquals(false, $checkedRequirements[$requirementsKeys['requirementPass']]['warning'], 'Passed requirement has a warning!');
54

55
		$this->assertEquals(true, $checkedRequirements[$requirementsKeys['requirementError']]['error'], 'Error requirement has no error!');
56

57 58
		$this->assertEquals(false, $checkedRequirements[$requirementsKeys['requirementWarning']]['error'], 'Error requirement has an error!');
		$this->assertEquals(true, $checkedRequirements[$requirementsKeys['requirementWarning']]['warning'], 'Error requirement has no warning!');
59
	}
60

61 62 63
	/**
	 * @depends testCheck
	 */
Alexander Makarov committed
64 65
	public function testCheckEval()
	{
66 67
		$requirementsChecker = new YiiRequirementChecker();

Alexander Makarov committed
68 69
		$requirements = [
			'requirementPass' => [
70 71 72 73 74
				'name' => 'Requirement 1',
				'mandatory' => true,
				'condition' => 'eval:2>1',
				'by' => 'Requirement 1',
				'memo' => 'Requirement 1',
Alexander Makarov committed
75 76
			],
			'requirementError' => [
77 78 79 80 81
				'name' => 'Requirement 2',
				'mandatory' => true,
				'condition' => 'eval:2<1',
				'by' => 'Requirement 2',
				'memo' => 'Requirement 2',
Alexander Makarov committed
82 83
			],
		];
84 85 86

		$checkResult = $requirementsChecker->check($requirements)->getResult();
		$checkedRequirements = $checkResult['requirements'];
87
		$requirementsKeys = array_flip(array_keys($requirements));
88

89 90
		$this->assertEquals(false, $checkedRequirements[$requirementsKeys['requirementPass']]['error'], 'Passed requirement has an error!');
		$this->assertEquals(false, $checkedRequirements[$requirementsKeys['requirementPass']]['warning'], 'Passed requirement has a warning!');
91

92 93 94 95 96 97 98 99 100 101
		$this->assertEquals(true, $checkedRequirements[$requirementsKeys['requirementError']]['error'], 'Error requirement has no error!');
	}

	/**
	 * @depends testCheck
	 */
	public function testCheckChained()
	{
		$requirementsChecker = new YiiRequirementChecker();

Alexander Makarov committed
102 103
		$requirements1 = [
			[
104 105 106 107 108
				'name' => 'Requirement 1',
				'mandatory' => true,
				'condition' => true,
				'by' => 'Requirement 1',
				'memo' => 'Requirement 1',
Alexander Makarov committed
109 110 111 112
			],
		];
		$requirements2 = [
			[
113 114 115 116 117
				'name' => 'Requirement 2',
				'mandatory' => true,
				'condition' => true,
				'by' => 'Requirement 2',
				'memo' => 'Requirement 2',
Alexander Makarov committed
118 119
			],
		];
120 121 122 123 124 125 126 127
		$checkResult = $requirementsChecker->check($requirements1)->check($requirements2)->getResult();

		$mergedRequirements = array_merge($requirements1, $requirements2);

		$this->assertEquals(count($mergedRequirements), $checkResult['summary']['total'], 'Wrong total checks count!');
		foreach ($mergedRequirements as $key => $mergedRequirement) {
			$this->assertEquals($mergedRequirement['name'], $checkResult['requirements'][$key]['name'], 'Wrong requirements list!');
		}
128
	}
129 130 131 132 133 134 135 136 137 138

	public function testCheckPhpExtensionVersion()
	{
		$requirementsChecker = new YiiRequirementChecker();

		$this->assertFalse($requirementsChecker->checkPhpExtensionVersion('some_unexisting_php_extension', '0.1'), 'No fail while checking unexisting extension!');
		$this->assertTrue($requirementsChecker->checkPhpExtensionVersion('pdo', '1.0'), 'Unable to check PDO version!');
	}

	/**
139
	 * Data provider for [[testGetByteSize()]].
140 141 142 143
	 * @return array
	 */
	public function dataProviderGetByteSize()
	{
Alexander Makarov committed
144 145 146 147 148 149 150 151 152
		return [
			['456', 456],
			['5K', 5*1024],
			['16KB', 16*1024],
			['4M', 4*1024*1024],
			['14MB', 14*1024*1024],
			['7G', 7*1024*1024*1024],
			['12GB', 12*1024*1024*1024],
		];
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	}

	/**
	 * @dataProvider dataProviderGetByteSize
	 *
	 * @param string $verboseValue verbose value.
	 * @param integer $expectedByteSize expected byte size.
	 */
	public function testGetByteSize($verboseValue, $expectedByteSize)
	{
		$requirementsChecker = new YiiRequirementChecker();

		$this->assertEquals($expectedByteSize, $requirementsChecker->getByteSize($verboseValue), "Wrong byte size for '{$verboseValue}'!");
	}

	/**
169
	 * Data provider for [[testCompareByteSize()]]
170 171 172 173
	 * @return array
	 */
	public function dataProviderCompareByteSize()
	{
Alexander Makarov committed
174 175 176 177 178 179 180
		return [
			['2M', '2K', '>', true],
			['2M', '2K', '>=', true],
			['1K', '1024', '==', true],
			['10M', '11M', '<', true],
			['10M', '11M', '<=', true],
		];
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	}

	/**
	 * @depends testGetByteSize
	 * @dataProvider dataProviderCompareByteSize
	 *
	 * @param string $a first value.
	 * @param string $b second value.
	 * @param string $compare comparison.
	 * @param boolean $expectedComparisonResult expected comparison result.
	 */
	public function testCompareByteSize($a, $b, $compare, $expectedComparisonResult)
	{
		$requirementsChecker = new YiiRequirementChecker();
		$this->assertEquals($expectedComparisonResult, $requirementsChecker->compareByteSize($a, $b, $compare), "Wrong compare '{$a}{$compare}{$b}'");
	}
197
}