review_name = $review_name; } function setUp() { // Only do the setUp once per test case, not once per function call. static $run_once; if (!isset($run_once)) { parent::setUp(); $run_once = TRUE; } } function runTest($code, $severity = SEVERITY_MINOR) { $reviews = coder_reviews(); $ext = variable_get('coder_php_ext', array('inc', 'php', 'install', 'test')); $coder_args = array( '#severity' => $severity, '#filename' => 'snippet.php', '#test' => $code, '#php_extensions' => $ext, '#include_extensions' => _coder_get_reviews_extensions($ext, $reviews), ); _coder_read_and_parse_file($coder_args); $results = do_coder_review($coder_args, $reviews[$this->review_name]); unset($results['#stats']); return $results; } protected function assertCoderPass($code) { // Run the coder review on the code snippet. $results = $this->runTest($code); // Display the test results. $message = 'Pass:'. str_replace(array('%s', '%d'), array('%%s', '%%d'), $code); if (count($results) == 0) { $this->assertTrue(TRUE, $message); } else { $this->assertTrue(FALSE, $message .': '. $this->getWarnings($results)); } } protected function assertCoderFail($code) { // Run the coder review on the code snippet. $results = $this->runTest($code); // Display the test results. $message = 'Fail:'. str_replace(array('%s', '%d'), array('%%s', '%%d'), $code); if (count($results) <= 1) { $this->assertTrue(count($results) == 1, $message); } else { $this->assertTrue(FALSE, $message .': '. $this->getWarnings($results)); } } private function getWarnings($results) { $warnings = array(); foreach ($results as $error) { $warning = _coder_warning($error['rule']); if (is_array($warning)) { $warning = $warning['#warning']; } $warnings[] = $warning; } return implode('; ', $warnings); } }