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('coder_review'); $run_once = TRUE; } } function runTest($code, $severity = SEVERITY_MINOR) { $reviews = coder_review_reviews(); $ext = variable_get('coder_review_php_ext', array('inc', 'php', 'install', 'test')); // Review code snippets in the test files. $coder_args = array( '#severity' => $severity, '#filename' => 'snippet.php', '#test' => $code, '#php_extensions' => $ext, '#include_extensions' => _coder_review_get_reviews_extensions($ext, $reviews), '#ignore_lines' => array(), ); _coder_review_read_and_parse_file($coder_args); $reviews[$this->review_name]['#review_name'] = $this->review_name; $results = do_coder_review($coder_args, $reviews[$this->review_name]); unset($results['#stats']); return $results; } function runTestModuleTest($severity = SEVERITY_MINOR) { $reviews = coder_review_reviews(); $ext = variable_get('coder_review_php_ext', array('inc', 'php', 'install', 'test')); // Review the coder_review_test module. $coder_args = array( '#severity' => $severity, '#filename' => drupal_get_path('module', 'coder_review_test') . '/coder_review_test.module', '#php_extensions' => $ext, '#include_extensions' => _coder_review_get_reviews_extensions($ext, $reviews), '#ignore_lines' => array(), ); _coder_review_read_and_parse_file($coder_args); $reviews[$this->review_name]['#review_name'] = $this->review_name; $results = do_coder_review($coder_args, $reviews[$this->review_name]); unset($results['#stats']); return $results; } protected function assertCoderReviewPass($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, check_plain($message)); } else { $this->assertTrue(FALSE, check_plain($message) .': '. $this->getWarnings($results)); } } protected function assertCoderReviewFail($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, check_plain($message)); } else { $this->assertTrue(FALSE, check_plain($message) .': '. $this->getWarnings($results)); } } private function getWarnings($results) { $warnings = array(); foreach ($results as $error) { $warning = _coder_review_warning($error['rule']); if (is_array($warning)) { $warning = $warning['#warning']; } $warnings[] = $warning; } return implode('; ', $warnings); } }