coder_review_style.test in Coder 7.2
File
coder_review/tests/coder_review_style.test
View source
<?php
require_once dirname(__FILE__) . '/coder_review_test_case.tinc';
class CoderReviewStyleTest extends CoderReviewTestCase {
function __construct($id = NULL) {
parent::__construct('style', $id);
}
public static function getInfo() {
return array(
'name' => 'Coder Review Style Tests',
'description' => 'Tests for the coder style review.',
'group' => 'CoderReview',
);
}
function testStyleArrayIndex() {
$this
->assertCoderReviewPass('$some_array[FOO_BAR] = $baz;');
$this
->assertCoderReviewFail('$some_array[foo_bar] = $baz;');
$this
->assertCoderReviewFail(' $a[hello] = "hello";');
$this
->assertCoderReviewPass(' $a[\'hello\'] = "hello";');
}
function testStyleTabs() {
$this
->assertCoderReviewPass('// Tab in comment');
$this
->assertCoderReviewPass('$var = "tab in double quote"');
$this
->assertCoderReviewPass('$var = \'tab in single quote\'');
$this
->assertCoderReviewFail(' $var = "tab in line";');
}
function testStyleCamelCase() {
$this
->assertCoderReviewPass(' $var = new stdClass();');
$this
->assertCoderReviewPass(' $var = $obj->camelCase();');
$this
->assertCoderReviewFail(' $camelCase = "1";');
$this
->assertCoderReviewFail(' function camelCase() {');
$this
->assertCoderReviewFail(" class myClass {\nfunction camelCase() {");
$this
->assertCoderReviewFail(" interface myClass {\nfunction camelCase() {");
$this
->assertCoderReviewPass(" class MyClass {\nfunction camelCase() {");
$this
->assertCoderReviewPass(" interface MyClass {\nfunction camelCase() {");
$this
->assertCoderReviewPass(' ?>This is embedded php and should Not trigger a camelCase error.<?php');
$this
->assertCoderReviewPass(" ?>This second embedded php and should Not trigger\na camelCase error.<?php");
$this
->assertCoderReviewPass(' $var = 0xFF;');
}
function testStyleBr() {
$this
->assertCoderReviewFail(' print "<br>";');
$this
->assertCoderReviewFail(' ?><br><?php');
}
function testStyleConstants() {
$this
->assertCoderReviewPass(' $a = TRUE;');
$this
->assertCoderReviewPass(' $atrue = "true";');
$this
->assertCoderReviewFail(' $a = true;');
$this
->assertCoderReviewFail(' $a =true;');
$this
->assertCoderReviewFail(' if ($a == true) {');
$this
->assertCoderReviewFail(' return false;');
}
function testStyleClosingPhp() {
$this
->assertCoderReviewFail(" print 'hello';\n?>");
}
function testStyleTrailingSpaces() {
$this
->assertCoderReviewFail(' $left = "trailing spaces follow"; ');
$this
->assertCoderReviewPass(' $left = "no trailing spaces";');
}
function testStyleControlStructures() {
$this
->assertCoderReviewFail(' if ($a == 1) { }');
$this
->assertCoderReviewPass(' if ($a == 1) {');
$this
->assertCoderReviewFail(' }else {');
$this
->assertCoderReviewFail(' if ($a == 1) { $b = 2;');
$this
->assertCoderReviewFail(' if ($a == 1) {$b = 2;');
$this
->assertCoderReviewFail(' else if ($a == 1) {');
$this
->assertCoderReviewPass(' elseif ($a == 1) {');
$this
->assertCoderReviewFail(' if($a == 1) {');
}
function testMultibyte() {
$this
->assertCoderReviewFail(" \$a = 'óóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóóó';\n if(\$a == 1) {\n");
}
function testStyleStringConcatentation() {
$this
->assertCoderReviewPass(' if ($file = file_check_upload($fieldname . "_upload")) {');
$this
->assertCoderReviewFail(' if ($file = file_check_upload($fieldname ."_upload")) {');
$this
->assertCoderReviewPass(' $v .= \'string\';');
$this
->assertCoderReviewPass(' $a = $v . \'string\';');
$this
->assertCoderReviewPass(' $a = $v . "string";');
$this
->assertCoderReviewPass(' $a = "string" . $v;');
$this
->assertCoderReviewPass(' $a = \'string\' . $v;');
$this
->assertCoderReviewFail(' $a = "string". $v;');
$this
->assertCoderReviewFail(' $a = \'string\'. $v;');
$this
->assertCoderReviewFail(' $a = $v. \'string\';');
$this
->assertCoderReviewFail(' $a = $v. "string";');
$this
->assertCoderReviewFail(' $a = $v .\'string\';');
$this
->assertCoderReviewFail(' $a = $v ."string";');
$this
->assertCoderReviewFail(' $a = $v.\'string\';');
$this
->assertCoderReviewFail(' $a = $v . \'string\';');
$this
->assertCoderReviewFail(' $a = $v . "string";');
$this
->assertCoderReviewFail(' $a = $v . \'string\';');
$this
->assertCoderReviewFail(' $a = $some_func(). \'string\';');
$this
->assertCoderReviewPass(' $a = 1.0 * .1 * 1. * (0.1) * (1.) * (.1) * (1.0);');
}
function testStyleHerdocStrings() {
$this
->assertCoderReviewFail(" \$var = <<< __EOD__\n<br>\n__EOD__;");
$this
->assertCoderReviewFail(" \$var = <<< __EOD__\n<B>\n__EOD__;");
$this
->assertCoderReviewPass(" \$var = <<< __EOD__\n<br/>\n__EOD__;");
}
function testFunctionName() {
$this
->assertCoderReviewFail('function camelCase() {');
$this
->assertCoderReviewFail('function any_Upper_case() {');
$this
->assertCoderReviewPass("class CamelCase {\nfunction camelCase() {\n");
}
function testShellComment() {
$this
->assertCoderReviewPass("# Shell Comment.\n");
$this
->assertCoderReviewPass("\$a = '# Not a Shell Comment.'\n");
}
function testIgnores() {
$bad = ' $a = $v. "string";';
$good = ' $a = $v . "string";';
$ignore = '// @ignore style_string_spacing';
$this
->assertCoderReviewFail($bad);
$this
->assertCoderReviewPass("{$ignore}:file\n{$bad}");
$this
->assertCoderReviewFail("function bad() {\n{$bad}\n}\n");
$this
->assertCoderReviewPass("{$ignore}:function\nfunction bad() {\n{$bad}\n}\n");
$this
->assertCoderReviewFail("{$ignore}:function\nfunction good() {\n{$good}\n}\nfunction bad() {\n{$bad};\n}\n");
$this
->assertCoderReviewPass("{$ignore}:1\n{$bad}");
$this
->assertCoderReviewPass("{$ignore}\n{$bad}");
$this
->assertCoderReviewFail("{$ignore}\n\n{$bad}");
$this
->assertCoderReviewPass("{$ignore}:2\n\n{$bad}");
$this
->assertCoderReviewFail("class C {\n\nfunction bad() {\n{$bad}\n}\n};\n");
$this
->assertCoderReviewPass("{$ignore}:class\nclass C {\n\n protected function bad() {\n {$bad}\n }\n};\n");
$this
->assertCoderReviewPass("class C {\n{$ignore}:class\n protected function bad() {\n{$bad}\n}\n};\n");
$this
->assertCoderReviewFail("class C {\n protected function bad() {\n {$bad}\n }\n};{$ignore}:file\n");
$this
->assertCoderReviewPass("class C {\n protected function bad() {\n{$ignore}:function\n {$bad}\n }\n};\n");
$this
->assertCoderReviewPass("class C {\n{$ignore}:class\n protected function bad() {\n {$bad}\n }\n};\n");
}
}