From a56b23beac70f32e1659a905cad0ac102b5e180f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nil=20Portugu=C3=A9s=20Calder=C3=B3?= Date: Wed, 4 Nov 2015 20:27:55 +0100 Subject: [PATCH] Added test for each case --- .gitignore | 2 +- phpunit.xml.dist | 2 + src/BackslashFixer/Command/FixerCommand.php | 2 +- src/BackslashFixer/Fixer/FileEditor.php | 8 +- tests/Fixer/FileEditorTest.php | 80 +++++++++++++++++++ .../Fixer/InMemoryFileSystem.php | 22 ++++- .../Fixer/Resources/BooleanAndNull.php | 0 .../Fixer/Resources/Constant.php | 0 .../Fixer/Resources/Function.php | 0 .../Fixer/FunctionRepositoryTest.php | 24 ------ 10 files changed, 107 insertions(+), 33 deletions(-) create mode 100644 tests/Fixer/FileEditorTest.php rename tests/{NilPortugues/Tests/BackslashFixer => }/Fixer/InMemoryFileSystem.php (59%) rename tests/{NilPortugues/Tests/BackslashFixer => }/Fixer/Resources/BooleanAndNull.php (100%) rename tests/{NilPortugues/Tests/BackslashFixer => }/Fixer/Resources/Constant.php (100%) rename tests/{NilPortugues/Tests/BackslashFixer => }/Fixer/Resources/Function.php (100%) delete mode 100644 tests/NilPortugues/Tests/BackslashFixer/Fixer/FunctionRepositoryTest.php diff --git a/.gitignore b/.gitignore index d2c5d21..82d5903 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /vendor /composer.lock -/build_test +/build .php_cs.cache diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b9e25fd..f7846fb 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -37,6 +37,8 @@ ./src ./src/bootstrap.php + ./src/BackslashFixer/Console + ./src/BackslashFixer/Command diff --git a/src/BackslashFixer/Command/FixerCommand.php b/src/BackslashFixer/Command/FixerCommand.php index b1f1c76..34d16ef 100644 --- a/src/BackslashFixer/Command/FixerCommand.php +++ b/src/BackslashFixer/Command/FixerCommand.php @@ -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); diff --git a/src/BackslashFixer/Fixer/FileEditor.php b/src/BackslashFixer/Fixer/FileEditor.php index 3125d6f..5137089 100644 --- a/src/BackslashFixer/Fixer/FileEditor.php +++ b/src/BackslashFixer/Fixer/FileEditor.php @@ -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(); @@ -76,7 +76,7 @@ public function addBackslashesToFunctions($path) * * @return array */ - public function removeFunctionsExistingInCurrentNamespaceFromBackslashing( + private function removeFunctionsExistingInCurrentNamespaceFromBackslashing( FileGenerator $generator, array $functions ) { @@ -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; diff --git a/tests/Fixer/FileEditorTest.php b/tests/Fixer/FileEditorTest.php new file mode 100644 index 0000000..721acb2 --- /dev/null +++ b/tests/Fixer/FileEditorTest.php @@ -0,0 +1,80 @@ + + * 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); + } +} \ No newline at end of file diff --git a/tests/NilPortugues/Tests/BackslashFixer/Fixer/InMemoryFileSystem.php b/tests/Fixer/InMemoryFileSystem.php similarity index 59% rename from tests/NilPortugues/Tests/BackslashFixer/Fixer/InMemoryFileSystem.php rename to tests/Fixer/InMemoryFileSystem.php index bfed826..1ea2a29 100644 --- a/tests/NilPortugues/Tests/BackslashFixer/Fixer/InMemoryFileSystem.php +++ b/tests/Fixer/InMemoryFileSystem.php @@ -4,7 +4,6 @@ use NilPortugues\BackslashFixer\Fixer\Interfaces\FileSystem; - class InMemoryFileSystem implements FileSystem { /** @@ -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; } /** diff --git a/tests/NilPortugues/Tests/BackslashFixer/Fixer/Resources/BooleanAndNull.php b/tests/Fixer/Resources/BooleanAndNull.php similarity index 100% rename from tests/NilPortugues/Tests/BackslashFixer/Fixer/Resources/BooleanAndNull.php rename to tests/Fixer/Resources/BooleanAndNull.php diff --git a/tests/NilPortugues/Tests/BackslashFixer/Fixer/Resources/Constant.php b/tests/Fixer/Resources/Constant.php similarity index 100% rename from tests/NilPortugues/Tests/BackslashFixer/Fixer/Resources/Constant.php rename to tests/Fixer/Resources/Constant.php diff --git a/tests/NilPortugues/Tests/BackslashFixer/Fixer/Resources/Function.php b/tests/Fixer/Resources/Function.php similarity index 100% rename from tests/NilPortugues/Tests/BackslashFixer/Fixer/Resources/Function.php rename to tests/Fixer/Resources/Function.php diff --git a/tests/NilPortugues/Tests/BackslashFixer/Fixer/FunctionRepositoryTest.php b/tests/NilPortugues/Tests/BackslashFixer/Fixer/FunctionRepositoryTest.php deleted file mode 100644 index b48e625..0000000 --- a/tests/NilPortugues/Tests/BackslashFixer/Fixer/FunctionRepositoryTest.php +++ /dev/null @@ -1,24 +0,0 @@ - - * Date: 10/31/15 - * Time: 11:27 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\FunctionRepository; - -class FunctionRepositoryTest extends \PHPUnit_Framework_TestCase -{ - public function testItReturnsFunctionsWithKeyValueWithSameValue() - { - $repository = new FunctionRepository(); - - $functions = $repository->getFunctions(); - - $this->assertEquals(array_keys($functions), array_values($functions)); - } -}