Skip to content

Commit

Permalink
Added test for each case
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Nov 4, 2015
1 parent d6cdc2c commit a56b23b
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/vendor
/composer.lock
/build_test
/build
.php_cs.cache
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
<directory>./src</directory>
<exclude>
<directory>./src/bootstrap.php</directory>
<directory>./src/BackslashFixer/Console</directory>
<directory>./src/BackslashFixer/Command</directory>
</exclude>
</whitelist>
</filter>
Expand Down
2 changes: 1 addition & 1 deletion src/BackslashFixer/Command/FixerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$fileEditor = new FileEditor(new FileGenerator(), new FunctionRepository(), $fileSystem);

foreach ($fileSystem->getFilesFromPath($path) as $file) {
$fileEditor->addBackslashesToFunctions($file);
$fileEditor->addBackslashes($file);
}

$output->write('Success!', \true);
Expand Down
8 changes: 4 additions & 4 deletions src/BackslashFixer/Fixer/FileEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public function __construct(

/**
* @param $path
* @return float
* @return void
*/
public function addBackslashesToFunctions($path)
public function addBackslashes($path)
{
$generator = $this->fileGenerator->fromReflectedFileName($path);
$source = $generator->getSourceContent();
Expand Down Expand Up @@ -76,7 +76,7 @@ public function addBackslashesToFunctions($path)
*
* @return array
*/
public function removeFunctionsExistingInCurrentNamespaceFromBackslashing(
private function removeFunctionsExistingInCurrentNamespaceFromBackslashing(
FileGenerator $generator,
array $functions
) {
Expand All @@ -102,7 +102,7 @@ public function removeFunctionsExistingInCurrentNamespaceFromBackslashing(
*
* @return array
*/
public function removeUseFunctionsFromBackslashing(FileGenerator $generator, array $functions)
private function removeUseFunctionsFromBackslashing(FileGenerator $generator, array $functions)
{
foreach ($generator->getUses() as $namespacedFunction) {
list($functionOrClass, $alias) = $namespacedFunction;
Expand Down
80 changes: 80 additions & 0 deletions tests/Fixer/FileEditorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 11/4/15
* Time: 8:03 PM
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Tests\BackslashFixer\Fixer;

use NilPortugues\BackslashFixer\Fixer\FileEditor;
use NilPortugues\BackslashFixer\Fixer\FunctionRepository;
use Zend\Code\Generator\FileGenerator;


/**
* Class FileEditorTest
* @package NilPortugues\Tests\BackslashFixer\Fixer
*/
class FileEditorTest extends \PHPUnit_Framework_TestCase
{
/**
* @var FileEditor
*/
protected $fileEditor;

/**
* @var InMemoryFileSystem
*/
protected $fileSystem;

/**
* @var string
*/
protected $base;

protected function setUp()
{
$reflector = new \ReflectionClass(get_class($this));
$this->base = dirname($reflector->getFileName());

$this->fileSystem = new InMemoryFileSystem();

$this->fileEditor = new FileEditor(
new FileGenerator(),
new FunctionRepository(),
$this->fileSystem
);
}

public function testItCanBackSlashBooleansAndAndNull()
{
$this->fileEditor->addBackslashes($this->base.'/Resources/BooleanAndNull.php');
$output = $this->fileSystem->getFile($this->base.'/Resources/BooleanAndNull.php');

$this->assertContains('\true', $output);
$this->assertContains('\false', $output);
$this->assertContains('\null', $output);

}

public function testItCanBackSlashConstants()
{
$this->fileEditor->addBackslashes($this->base.'/Resources/Constant.php');
$output = $this->fileSystem->getFile($this->base.'/Resources/Constant.php');

$this->assertContains('$this->a = self::DIRECTORY_SEPARATOR;', $output);
$this->assertContains('$this->b = \DIRECTORY_SEPARATOR;', $output);
}

public function testItCanBackSlashFunctions()
{
$this->fileEditor->addBackslashes($this->base.'/Resources/Function.php');
$output = $this->fileSystem->getFile($this->base.'/Resources/Function.php');

$this->assertContains('return \strlen($string)', $output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use NilPortugues\BackslashFixer\Fixer\Interfaces\FileSystem;


class InMemoryFileSystem implements FileSystem
{
/**
Expand All @@ -21,22 +20,39 @@ class InMemoryFileSystem implements FileSystem
"Resources/BooleanAndNull.php",
];

private $base;

/**
* InMemoryFileSystem constructor.
*/
public function __construct()
{
$reflector = new \ReflectionClass(get_class($this));
$this->base = dirname($reflector->getFileName());

foreach(self::$filePath as $file) {
self::$fileSystem[$file] = \file_get_contents($file);
$path = $this->base.DIRECTORY_SEPARATOR.$file;
self::$fileSystem[$path] = \file_get_contents($path);
}
}

public function getFile($file)
{
return self::$fileSystem[$file];
}

/**
* @inheritDoc
*/
public function getFilesFromPath($path)
{
return array_keys(self::$fileSystem);
$files = [];
foreach(array_keys(self::$fileSystem) as $file) {
$path = $this->base.DIRECTORY_SEPARATOR.$file;
$files[$path] = $path;
}

return $files;
}

/**
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

This file was deleted.

0 comments on commit a56b23b

Please sign in to comment.