Commit f82a01e4 by Klimov Paul

"YiiRequirementChecker::check()" has been updated to be able to collect…

"YiiRequirementChecker::check()" has been updated to be able to collect different requirements set in the chained call.
parent 3b7ac70c
......@@ -46,16 +46,20 @@ class YiiRequirementCheckerTest extends TestCase
$this->assertEquals(1, $summary['warnings'], 'Wrong summary warnings!');
$checkedRequirements = $checkResult['requirements'];
$requirementsKeys = array_flip(array_keys($requirements));
$this->assertEquals(false, $checkedRequirements['requirementPass']['error'], 'Passed requirement has an error!');
$this->assertEquals(false, $checkedRequirements['requirementPass']['warning'], 'Passed requirement has a warning!');
$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['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(true, $checkedRequirements['requirementWarning']['warning'], 'Error requirement has no warning!');
$this->assertEquals(false, $checkedRequirements[$requirementsKeys['requirementWarning']]['error'], 'Error requirement has an error!');
$this->assertEquals(true, $checkedRequirements[$requirementsKeys['requirementWarning']]['warning'], 'Error requirement has no warning!');
}
/**
* @depends testCheck
*/
public function testCheckEval() {
$requirementsChecker = new YiiRequirementChecker();
......@@ -78,10 +82,46 @@ class YiiRequirementCheckerTest extends TestCase
$checkResult = $requirementsChecker->check($requirements)->getResult();
$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!');
$this->assertEquals(false, $checkedRequirements['requirementPass']['warning'], 'Passed requirement has a warning!');
$mergedRequirements = array_merge($requirements1, $requirements2);
$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!');
}
}
}
......@@ -15,41 +15,47 @@
*/
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)
{
if (!is_array($requirements)) {
$this->usageError("Requirements must be an array!");
}
$summary = array(
if (!isset($this->result)) {
$this->result = array(
'summary' => array(
'total' => 0,
'errors' => 0,
'warnings' => 0,
),
'requirements' => array(),
);
}
foreach ($requirements as $key => $rawRequirement) {
$requirement = $this->normalizeRequirement($rawRequirement, $key);
$summary['total']++;
$this->result['summary']['total']++;
if (!$requirement['condition']) {
if ($requirement['mandatory']) {
$requirement['error'] = true;
$requirement['warning'] = true;
$summary['errors']++;
$this->result['summary']['errors']++;
} else {
$requirement['error'] = false;
$requirement['warning'] = true;
$summary['warnings']++;
$this->result['summary']['warnings']++;
}
} else {
$requirement['error'] = false;
$requirement['warning'] = false;
}
$requirements[$key] = $requirement;
$this->result['requirements'][] = $requirement;
}
$result = array(
'summary' => $summary,
'requirements' => $requirements,
);
$this->result = $result;
return $this;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment