Browse Source

"YiiRequirementChecker::check()" has been updated to be able to collect different requirements set in the chained call.

tags/2.0.0-beta
Klimov Paul 12 years ago
parent
commit
f82a01e4dd
  1. 56
      tests/unit/framework/requirements/YiiRequirementCheckerTest.php
  2. 28
      yii/requirements/YiiRequirementChecker.php

56
tests/unit/framework/requirements/YiiRequirementCheckerTest.php

@ -46,16 +46,20 @@ class YiiRequirementCheckerTest extends TestCase
$this->assertEquals(1, $summary['warnings'], 'Wrong summary warnings!'); $this->assertEquals(1, $summary['warnings'], 'Wrong summary warnings!');
$checkedRequirements = $checkResult['requirements']; $checkedRequirements = $checkResult['requirements'];
$requirementsKeys = array_flip(array_keys($requirements));
$this->assertEquals(false, $checkedRequirements['requirementPass']['error'], 'Passed requirement has an error!'); $this->assertEquals(false, $checkedRequirements[$requirementsKeys['requirementPass']]['error'], 'Passed requirement has an error!');
$this->assertEquals(false, $checkedRequirements['requirementPass']['warning'], 'Passed requirement has a warning!'); $this->assertEquals(false, $checkedRequirements[$requirementsKeys['requirementPass']]['warning'], 'Passed requirement has a warning!');
$this->assertEquals(true, $checkedRequirements['requirementError']['error'], 'Error requirement has no error!'); $this->assertEquals(true, $checkedRequirements[$requirementsKeys['requirementError']]['error'], 'Error requirement has no error!');
$this->assertEquals(false, $checkedRequirements['requirementWarning']['error'], 'Error requirement has an error!'); $this->assertEquals(false, $checkedRequirements[$requirementsKeys['requirementWarning']]['error'], 'Error requirement has an error!');
$this->assertEquals(true, $checkedRequirements['requirementWarning']['warning'], 'Error requirement has no warning!'); $this->assertEquals(true, $checkedRequirements[$requirementsKeys['requirementWarning']]['warning'], 'Error requirement has no warning!');
} }
/**
* @depends testCheck
*/
public function testCheckEval() { public function testCheckEval() {
$requirementsChecker = new YiiRequirementChecker(); $requirementsChecker = new YiiRequirementChecker();
@ -78,10 +82,46 @@ class YiiRequirementCheckerTest extends TestCase
$checkResult = $requirementsChecker->check($requirements)->getResult(); $checkResult = $requirementsChecker->check($requirements)->getResult();
$checkedRequirements = $checkResult['requirements']; $checkedRequirements = $checkResult['requirements'];
$requirementsKeys = array_flip(array_keys($requirements));
$this->assertEquals(false, $checkedRequirements[$requirementsKeys['requirementPass']]['error'], 'Passed requirement has an error!');
$this->assertEquals(false, $checkedRequirements[$requirementsKeys['requirementPass']]['warning'], 'Passed requirement has a warning!');
$this->assertEquals(true, $checkedRequirements[$requirementsKeys['requirementError']]['error'], 'Error requirement has no error!');
}
/**
* @depends testCheck
*/
public function testCheckChained()
{
$requirementsChecker = new YiiRequirementChecker();
$requirements1 = array(
array(
'name' => 'Requirement 1',
'mandatory' => true,
'condition' => true,
'by' => 'Requirement 1',
'memo' => 'Requirement 1',
),
);
$requirements2 = array(
array(
'name' => 'Requirement 2',
'mandatory' => true,
'condition' => true,
'by' => 'Requirement 2',
'memo' => 'Requirement 2',
),
);
$checkResult = $requirementsChecker->check($requirements1)->check($requirements2)->getResult();
$this->assertEquals(false, $checkedRequirements['requirementPass']['error'], 'Passed requirement has an error!'); $mergedRequirements = array_merge($requirements1, $requirements2);
$this->assertEquals(false, $checkedRequirements['requirementPass']['warning'], 'Passed requirement has a warning!');
$this->assertEquals(true, $checkedRequirements['requirementError']['error'], 'Error requirement has no error!'); $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!');
}
} }
} }

28
yii/requirements/YiiRequirementChecker.php

@ -15,41 +15,47 @@
*/ */
class YiiRequirementChecker class YiiRequirementChecker
{ {
/**
* Check the given requirements, collecting results into internal field.
* This method can be invoked several times checking different requirement sets.
* Use {@link getResult()} or {@link render()} to get the results.
* @param array $requirements requirements to be checked.
* @return YiiRequirementChecker self instance.
*/
function check($requirements) function check($requirements)
{ {
if (!is_array($requirements)) { if (!is_array($requirements)) {
$this->usageError("Requirements must be an array!"); $this->usageError("Requirements must be an array!");
} }
$summary = array( if (!isset($this->result)) {
$this->result = array(
'summary' => array(
'total' => 0, 'total' => 0,
'errors' => 0, 'errors' => 0,
'warnings' => 0, 'warnings' => 0,
),
'requirements' => array(),
); );
}
foreach ($requirements as $key => $rawRequirement) { foreach ($requirements as $key => $rawRequirement) {
$requirement = $this->normalizeRequirement($rawRequirement, $key); $requirement = $this->normalizeRequirement($rawRequirement, $key);
$this->result['summary']['total']++;
$summary['total']++;
if (!$requirement['condition']) { if (!$requirement['condition']) {
if ($requirement['mandatory']) { if ($requirement['mandatory']) {
$requirement['error'] = true; $requirement['error'] = true;
$requirement['warning'] = true; $requirement['warning'] = true;
$summary['errors']++; $this->result['summary']['errors']++;
} else { } else {
$requirement['error'] = false; $requirement['error'] = false;
$requirement['warning'] = true; $requirement['warning'] = true;
$summary['warnings']++; $this->result['summary']['warnings']++;
} }
} else { } else {
$requirement['error'] = false; $requirement['error'] = false;
$requirement['warning'] = false; $requirement['warning'] = false;
} }
$requirements[$key] = $requirement; $this->result['requirements'][] = $requirement;
} }
$result = array(
'summary' => $summary,
'requirements' => $requirements,
);
$this->result = $result;
return $this; return $this;
} }

Loading…
Cancel
Save