Created
March 23, 2012 06:39
-
-
Save shama/2167716 to your computer and use it in GitHub Desktop.
Correct parameter order of assertions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('Folder', 'Utility'); | |
App::uses('File', 'Utility'); | |
/** | |
* Fix Assert Shell | |
* Will correct the parameter ordering of assertEquals() | |
* Not perfect but gets most and only causes a few errors :) | |
* | |
* @author Kyle Robinson Young <kyle at dontkry.com> | |
*/ | |
class FixAssertShell extends AppShell { | |
/** | |
* true = actually run or false = just output | |
* | |
* @var boolean | |
*/ | |
public $dry = false; | |
/** | |
* How many we fixed | |
* | |
* @var integer | |
*/ | |
public $count = 0; | |
/** | |
* main | |
*/ | |
public function main() { | |
$methods = array('assertEquals', 'assertNotEquals'); | |
$tests = CAKE . 'Test' . DS . 'Case' . DS; | |
$Folder = new Folder($tests); | |
$files = $Folder->findRecursive(); | |
foreach ($files as $file) { | |
$file = new File($file); | |
$code = $file->read(); | |
$write = false; | |
foreach ($methods as $method) { | |
$mod = $this->_fixAsserts($code, $method); | |
if ($mod) { | |
$write = true; | |
} | |
} | |
if (!$this->dry && $write) { | |
$file->write($code); | |
} | |
$file->close(); | |
} | |
$this->out('Fixed ' . $this->count . ' occurrences'); | |
} | |
/** | |
* test | |
*/ | |
public function test() { | |
$code = array( | |
'$this->assertEquals($var, \'expected\');', | |
'$this->assertEquals($var[\'one\'], \'expected\', \'You got some splainin to do\');', | |
'$this->assertEquals($test, array(1, 2, 3), "Ill mess you up");', | |
'$this->assertEquals($result[\'shama\'][0], $var[\'expect\']);', | |
'$this->assertEquals($Apple->Behaviors->dispatchMethod($Apple, \'testMethod\'), $expected);', | |
'$this->assertEquals($Controller->params[\'paging\'][\'PaginatorControllerPost\'][\'options\'][\'limit\'], 1000);', | |
); | |
$code = implode("\n", $code); | |
$code = $this->_fixAsserts($code); | |
} | |
/** | |
* Fix Asserts | |
* | |
* @param string $code | |
* @return boolean If modified code | |
*/ | |
protected function _fixAsserts(&$code = null, $method = 'assertEquals') { | |
$mod = false; | |
preg_match_all('/\$this-\>' . $method . '\(.+\)/', $code, $matches); | |
if (empty($matches[0])) { | |
return false; | |
} | |
foreach ($matches[0] as $key => $match) { | |
$tokens = token_get_all("<?php\n" . $match); | |
$new = array(); | |
foreach ($tokens as $token) { | |
$val = is_array($token) ? $token[1] : $token; | |
if (!empty($new)) { | |
$new[] = $val; | |
} | |
if ($val == '(' && empty($new)) { | |
$new[] = $val; | |
} | |
} | |
$tokens = array_slice($new, 1, -1); | |
$inArray = false; | |
$params = array(); | |
$param = ''; | |
foreach ($tokens as $token) { | |
// Doesn't handle deep nested arrays well | |
if ($token == '(') { | |
$inArray = true; | |
} | |
if ($inArray && $token == ')') { | |
$inArray = false; | |
} | |
if ($token == ',' && !$inArray) { | |
$params[] = $param; | |
$param = ''; | |
continue; | |
} | |
$param .= $token; | |
} | |
$params[] = $param; | |
$params = array_map('trim', $params); | |
$left = $params[0]; | |
$right = $params[1]; | |
if (isset($params[2])) { | |
$msg = $params[2]; | |
} | |
$switch = false; // IF SHOULD SWITCH | |
preg_match('/\$[^0-9]{1}/', $left, $match); | |
$leftHasVar = empty($match) ? false : true; | |
preg_match('/\$[^0-9]{1}/', $right, $match); | |
$rightHasVar = empty($match) ? false : true; | |
// If left has a variable and right does not, switch them | |
if ($leftHasVar && !$rightHasVar) { | |
$switch = true; | |
} | |
// If left uses the var $result, then switch | |
if (strpos($left, '$result') !== false) { | |
$switch = true; | |
} | |
// If right is named expected | |
if (strpos($right, '$expected') !== false) { | |
$switch = true; | |
} | |
// switch? | |
if ($switch) { | |
$search = $left . ', ' . $right; | |
$replace = $right . ', ' . $left; | |
$code = str_replace($search, $replace, $code); | |
$this->count++; | |
$this->out($search . ' -> ' . $replace); | |
$mod = true; | |
} | |
} | |
return $mod; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment