From f82a01e4dd212e88af10a2ef7baa7bca07b569d9 Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Sat, 11 May 2013 17:01:54 +0300 Subject: [PATCH] "YiiRequirementChecker::check()" has been updated to be able to collect different requirements set in the chained call. --- .../requirements/YiiRequirementCheckerTest.php | 56 ++++++++++++++++++---- yii/requirements/YiiRequirementChecker.php | 36 ++++++++------ 2 files changed, 69 insertions(+), 23 deletions(-) diff --git a/tests/unit/framework/requirements/YiiRequirementCheckerTest.php b/tests/unit/framework/requirements/YiiRequirementCheckerTest.php index 66a81b7..3dc8265 100644 --- a/tests/unit/framework/requirements/YiiRequirementCheckerTest.php +++ b/tests/unit/framework/requirements/YiiRequirementCheckerTest.php @@ -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['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!'); + } + + /** + * @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(); + + $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!'); + } } } diff --git a/yii/requirements/YiiRequirementChecker.php b/yii/requirements/YiiRequirementChecker.php index 46fe106..26cb041 100644 --- a/yii/requirements/YiiRequirementChecker.php +++ b/yii/requirements/YiiRequirementChecker.php @@ -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( - 'total' => 0, - 'errors' => 0, - 'warnings' => 0, - ); + 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; }