Permalink
Browse files

Unit test for RenameLocalVariable

  • Loading branch information...
1 parent 7d07f9d commit 36bd3f962e55f0e713c7e8cbfa7f2691428aeaa6 @beberlei beberlei committed Mar 23, 2013
View
@@ -15,4 +15,10 @@
<directory>src/test</directory>
</testsuite>
</testsuites>
+
+ <filter>
+ <whitelist>
+ <directory>src/main</directory>
+ </whitelist>
+ </filter>
</phpunit>
@@ -42,31 +42,37 @@ public function refactor(File $file, $line, $oldName, $newName)
$newName = ltrim($newName, '$');
$methodRange = $this->findMethodRange($file, $line);
- $declaredVariables = $this->variableScanner->scanForVariables($file, $methodRange);
+ $definedVariables = $this->variableScanner->scanForVariables($file, $methodRange);
- if ( ! isset($declaredVariables->localVariables[$oldName]) &&
- ! isset($declaredVariables->assignments[$oldName])) {
+ if ( ! isset($definedVariables->localVariables[$oldName]) &&
+ ! isset($definedVariables->assignments[$oldName])) {
return;
}
$buffer = $this->editor->openBuffer($file);
- if (isset($declaredVariables->localVariables[$oldName])) {
- foreach ($declaredVariables->localVariables[$oldName] as $line) {
- $buffer->replaceString($line, '$' . $oldName, '$' . $newName);
- }
- }
+ $this->replaceString($buffer, $definedVariables->localVariables, $oldName, $newName);
+ $this->replaceString($buffer, $definedVariables->assignments, $oldName, $newName);
+
+ $this->editor->save();
+ }
- if (isset($declaredVariables->assignments[$oldName])) {
- foreach ($declaredVariables->assignments[$oldName] as $line) {
+ private function replaceString($buffer, array $variables, $oldName, $newName)
+ {
+ if (isset($variables[$oldName])) {
+ foreach ($variables[$oldName] as $line) {
$buffer->replaceString($line, '$' . $oldName, '$' . $newName);
}
}
-
- $this->editor->save();
}
+ /**
+ * @param File $file
+ * @param integer $line
+ *
+ * @return LineRange
+ */
private function findMethodRange(File $file, $line)
{
$range = LineRange::fromSingleLine($line);
@@ -26,7 +26,7 @@ class DefinedVariables extends ValueObject
*/
protected $assignments;
- public function __construct(array $localVariables, array $assignments)
+ public function __construct(array $localVariables = array(), array $assignments = array())
{
$this->localVariables = $localVariables;
$this->assignments = $assignments;
@@ -3,6 +3,7 @@
namespace QafooLabs\Refactoring\Application;
use QafooLabs\Refactoring\Domain\Model\File;
+use QafooLabs\Refactoring\Domain\Model\DefinedVariables;
use QafooLabs\Refactoring\Adapters\PHPParser\ParserVariableScanner;
use QafooLabs\Refactoring\Adapters\TokenReflection\StaticCodeAnalysis;
use QafooLabs\Refactoring\Adapters\Patches\PatchEditor;
@@ -14,6 +15,12 @@ public function testRenameLocalVariable()
$scanner = \Phake::mock('QafooLabs\Refactoring\Domain\Services\VariableScanner');
$codeAnalysis = \Phake::mock('QafooLabs\Refactoring\Domain\Services\CodeAnalysis');
$editor = \Phake::mock('QafooLabs\Refactoring\Domain\Services\Editor');
+ $buffer = \Phake::mock('QafooLabs\Refactoring\Domain\Model\EditorBuffer');
+
+ \Phake::when($scanner)->scanForVariables(\Phake::anyParameters())->thenReturn(
+ new DefinedVariables(array('helloWorld' => array(6)))
+ );
+ \Phake::when($editor)->openBuffer(\Phake::anyParameters())->thenReturn($buffer);
$refactoring = new RenameLocalVariable($scanner, $codeAnalysis, $editor);
@@ -28,5 +35,7 @@ public function main()
}
PHP
), 6, '$helloWorld', '$var');
+
+ \Phake::verify($buffer)->replaceString(6, '$helloWorld', '$var');
}
}

0 comments on commit 36bd3f9

Please sign in to comment.