From b3230b173573f48ab6208d804ce48c63970771a3 Mon Sep 17 00:00:00 2001 From: Davis Peixoto Date: Sun, 5 Oct 2014 03:56:15 -0300 Subject: [PATCH] PSR-1/PSR-2 output class --- .gitignore | 5 - CONTRIBUTING.md | 3 - README.md | 4 +- composer.json | 11 +- .../Commands/TestsGeneratorCommand.php | 98 ++++++------ src/Davispeixoto/TestGenerator/Generator.php | 86 ++++++----- .../TestGenerator/Templates/class.txt | 1 + .../Templates/methodWithDataProvider.txt | 18 +-- .../Templates/methodWithoutDataProvider.txt | 14 +- .../TestGenerator/Templates/provider.txt | 14 +- .../TestGeneratorServiceProvider.php | 41 +++--- tests/BaseClass.php | 17 ++- tests/GeneratorTest.php | 41 +++--- tests/TestGeneratorSampleClass.php | 139 +++++++++--------- ...TestGeneratorSampleClassExpectedOutput.php | 105 ++++++------- 15 files changed, 304 insertions(+), 293 deletions(-) delete mode 100644 CONTRIBUTING.md diff --git a/.gitignore b/.gitignore index cde7140..335df2d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,3 @@ /vendor -composer.phar composer.lock -.DS_Store -.buildpath -.settings -.project .idea diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 015febc..0000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,3 +0,0 @@ -# Contribution Guidelines - -Please submit all issues and pull requests to the [laravel/framework](http://github.com/laravel/framework) repository! \ No newline at end of file diff --git a/README.md b/README.md index 15d259c..9c6af9e 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Installation Begin by installing this package through Composer. Edit your project's `composer.json` file to require `davispeixoto/testingtool`. "require": { - "laravel/framework": "4.1.*", + "laravel/framework": "4.*", "davispeixoto/testingtool": "1.0.*" }, "minimum-stability" : "stable" @@ -229,4 +229,4 @@ This Test Generator is open-sourced software licensed under the [MIT license](ht ### Versioning -This projetct follows the [Semantic Versioning](http://semver.org/) +This project follows the [Semantic Versioning](http://semver.org/) diff --git a/composer.json b/composer.json index f5c51d4..d86fb4c 100644 --- a/composer.json +++ b/composer.json @@ -10,14 +10,13 @@ } ], "require": { - "php": ">=5.3.0", - "illuminate/support": "4.1.*", - "illuminate/console": "4.1.*", - "illuminate/filesystem": "4.1.*" + "illuminate/support": "4.*", + "illuminate/console": "4.*", + "illuminate/filesystem": "4.*", + "symfony/yaml": "2.5.*@dev" }, "require-dev": { - "phpunit/phpunit": "4.0.*", - "phpmd/phpmd": "1.4.*" + "phpunit/phpunit": "4.3.*" }, "autoload": { "classmap": [ diff --git a/src/Davispeixoto/TestGenerator/Commands/TestsGeneratorCommand.php b/src/Davispeixoto/TestGenerator/Commands/TestsGeneratorCommand.php index 35e8bf3..5a6550e 100644 --- a/src/Davispeixoto/TestGenerator/Commands/TestsGeneratorCommand.php +++ b/src/Davispeixoto/TestGenerator/Commands/TestsGeneratorCommand.php @@ -1,25 +1,26 @@ generator = new Generator(); } - - /** - * Execute the console command. - * - * @return void - */ - public function fire() - { + + /** + * Execute the console command. + * + * @return void + */ + public function fire() + { try { - if($this->generator->generate($this->argument('name') , $this->option('path'))) { + if ($this->generator->generate($this->argument('name'), $this->option('path'))) { $this->info('Test class for ' . $this->argument('name') . ' successfully generated!'); } else { $this->error('Could not generate test class for ' . $this->argument('name') . '. Check writing permissions'); @@ -46,30 +47,31 @@ public function fire() $this->error($e->getMessage()); $this->error($e->getTraceAsString()); } - } - - /** - * Get the console command arguments. - * - * @return array - */ - protected function getArguments() - { - return array( - array('name', InputArgument::REQUIRED, 'Name of the class you want the test skeleton.'), - ); - } + } - /** - * Get the console command options. - * - * @return array - */ - protected function getOptions() - { - return array( - array('path', null, InputOption::VALUE_OPTIONAL, 'Path to output tests directory.', app_path() . '/tests'), - ); - } + /** + * Get the console command arguments. + * + * @return array + */ + protected function getArguments() + { + return array( + array('name', InputArgument::REQUIRED, 'Name of the class you want the test skeleton.'), + ); + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return array( + array('path', null, InputOption::VALUE_OPTIONAL, 'Path to output tests directory.', app_path() . '/tests'), + ); + } } + ?> \ No newline at end of file diff --git a/src/Davispeixoto/TestGenerator/Generator.php b/src/Davispeixoto/TestGenerator/Generator.php index 02dc110..487a8de 100644 --- a/src/Davispeixoto/TestGenerator/Generator.php +++ b/src/Davispeixoto/TestGenerator/Generator.php @@ -9,13 +9,14 @@ use Illuminate\Filesystem\Filesystem as File; -class Generator { +class Generator +{ public function __construct() { return $this; } - public function generate($className , $savePath) + public function generate($className, $savePath) { //instantiate the class reflector $reflector = null; @@ -26,71 +27,82 @@ public function generate($className , $savePath) throw $e; } - //get methods - $methods = $reflector->getMethods(\ReflectionMethod::IS_PUBLIC); - - //start filesystem stuff - $file = new File(); - $strClassStructure = $file->get(__DIR__ . DIRECTORY_SEPARATOR . 'Templates' . DIRECTORY_SEPARATOR . 'class.txt'); - $strMethods = ''; - $strProviders = ''; - $strMethodWithData = $file->get(__DIR__ . DIRECTORY_SEPARATOR . 'Templates' . DIRECTORY_SEPARATOR . 'methodWithDataProvider.txt'); - $strMethodNoData = $file->get(__DIR__ . DIRECTORY_SEPARATOR . 'Templates' . DIRECTORY_SEPARATOR . 'methodWithoutDataProvider.txt'); - $strMethodProviders = $file->get(__DIR__ . DIRECTORY_SEPARATOR . 'Templates' . DIRECTORY_SEPARATOR . 'provider.txt'); - if (!$file->exists($savePath . DIRECTORY_SEPARATOR . 'data') || !$file->isDirectory($savePath . DIRECTORY_SEPARATOR . 'data')) { - $file->makeDirectory($savePath . DIRECTORY_SEPARATOR . 'data' , 0775); + //get methods + $methods = $reflector->getMethods(\ReflectionMethod::IS_PUBLIC); + + //start filesystem stuff + $file = new File(); + $strClassStructure = $file->get(__DIR__ . DIRECTORY_SEPARATOR . 'Templates' . DIRECTORY_SEPARATOR . 'class.txt'); + $strMethods = ''; + $strProviders = ''; + $strMethodWithData = $file->get(__DIR__ . DIRECTORY_SEPARATOR . 'Templates' . DIRECTORY_SEPARATOR . 'methodWithDataProvider.txt'); + $strMethodNoData = $file->get(__DIR__ . DIRECTORY_SEPARATOR . 'Templates' . DIRECTORY_SEPARATOR . 'methodWithoutDataProvider.txt'); + $strMethodProviders = $file->get(__DIR__ . DIRECTORY_SEPARATOR . 'Templates' . DIRECTORY_SEPARATOR . 'provider.txt'); + if (!$file->exists($savePath . DIRECTORY_SEPARATOR . 'data') || !$file->isDirectory($savePath . DIRECTORY_SEPARATOR . 'data')) { + $file->makeDirectory($savePath . DIRECTORY_SEPARATOR . 'data', 0775); } - //compose replacing placeholders with data + //compose replacing placeholders with data foreach ($methods as $method) { - if($this->isValidMethod($method , $className)) { + if ($this->isValidMethod($method, $className)) { $params = $method->getParameters(); $paramsArray = array(); if (!empty($params)) { - foreach($params as $param) { + foreach ($params as $param) { $paramsArray[] = '$' . $param->name; } $csvFileName = $className . '.' . ucfirst($method->name); $fullCsvFileName = $csvFileName . '.csv'; - $methodArgs = join(' , ', $paramsArray); - - $placeholders = array('{{className}}' , '{{methodName}}' , '{{methodNameLowerCase}}' , '{{methodArgs}}'); - $replacements = array($className , ucfirst($method->name) , $method->name , $methodArgs); + $methodArgs = join(', ', $paramsArray); + + $placeholders = array( + '{{className}}', + '{{methodName}}', + '{{methodNameLowerCase}}', + '{{methodArgs}}' + ); + $replacements = array($className, ucfirst($method->name), $method->name, $methodArgs); $strMethods .= str_replace($placeholders, $replacements, $strMethodWithData); - $placeholders = array('{{className}}' , '{{methodName}}' , '{{methodNameLowerCase}}' , '{{csvFileName}}'); - $replacements = array($className , ucfirst($method->name) , $method->name , $csvFileName); + $placeholders = array( + '{{className}}', + '{{methodName}}', + '{{methodNameLowerCase}}', + '{{csvFileName}}' + ); + $replacements = array($className, ucfirst($method->name), $method->name, $csvFileName); $strProviders .= str_replace($placeholders, $replacements, $strMethodProviders); $file->put($savePath . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . $fullCsvFileName, ''); } else { - $placeholders = array('{{className}}' , '{{methodNameLowerCase}}' , '{{methodName}}'); - $replacements = array($className , $method->name , ucfirst($method->name)); + $placeholders = array('{{className}}', '{{methodNameLowerCase}}', '{{methodName}}'); + $replacements = array($className, $method->name, ucfirst($method->name)); $strMethods .= str_replace($placeholders, $replacements, $strMethodNoData); } } } - $placeholders = array('{{className}}' , '{{methods}}' , '{{providers}}'); - $replacements = array($className , $strMethods , $strProviders); - $strClassStructure = str_replace($placeholders, $replacements, $strClassStructure); + $placeholders = array('{{className}}', '{{methods}}', '{{providers}}'); + $replacements = array($className, $strMethods, $strProviders); + $strClassStructure = str_replace($placeholders, $replacements, $strClassStructure); - $outputFile = $savePath . DIRECTORY_SEPARATOR . $className . 'Test.php'; + $outputFile = $savePath . DIRECTORY_SEPARATOR . $className . 'Test.php'; - $file->put($outputFile, $strClassStructure); - $status = $file->exists($outputFile) && ($file->size($outputFile) > 0); + $file->put($outputFile, $strClassStructure); + $status = $file->exists($outputFile) && ($file->size($outputFile) > 0); - return $status; + return $status; } - private function isValidMethod(\ReflectionMethod $method , $className) { - if( + private function isValidMethod(\ReflectionMethod $method, $className) + { + if ( $method->class == $className - && + && !$method->isConstructor() - && + && !$method->isDestructor() ) { return true; diff --git a/src/Davispeixoto/TestGenerator/Templates/class.txt b/src/Davispeixoto/TestGenerator/Templates/class.txt index 42eafd4..0af6524 100644 --- a/src/Davispeixoto/TestGenerator/Templates/class.txt +++ b/src/Davispeixoto/TestGenerator/Templates/class.txt @@ -1,4 +1,5 @@ dataProvider('{{csvFileName}}.csv'); - } + /** + * Data provider function for {{className}}::{{methodNameLowerCase}} + */ + public function provider{{methodName}}() + { + return $this->dataProvider('{{csvFileName}}.csv'); + } diff --git a/src/Davispeixoto/TestGenerator/TestGeneratorServiceProvider.php b/src/Davispeixoto/TestGenerator/TestGeneratorServiceProvider.php index 85ed2f6..a21be09 100644 --- a/src/Davispeixoto/TestGenerator/TestGeneratorServiceProvider.php +++ b/src/Davispeixoto/TestGenerator/TestGeneratorServiceProvider.php @@ -3,26 +3,27 @@ use Davispeixoto\TestGenerator\Commands; use Illuminate\Support\ServiceProvider; -class TestGeneratorServiceProvider extends ServiceProvider { +class TestGeneratorServiceProvider extends ServiceProvider +{ - /** - * Indicates if loading of the provider is deferred. - * - * @var bool - */ - protected $defer = TRUE; + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = true; - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $this->app['tests.generate'] = $this->app->share(function($app) { - return new Commands\TestsGeneratorCommand($app['files']); - }); - - $this->commands('tests.generate'); - } + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app['tests.generate'] = $this->app->share(function ($app) { + return new Commands\TestsGeneratorCommand($app['files']); + }); + + $this->commands('tests.generate'); + } } \ No newline at end of file diff --git a/tests/BaseClass.php b/tests/BaseClass.php index e593408..7d45fc2 100644 --- a/tests/BaseClass.php +++ b/tests/BaseClass.php @@ -1,12 +1,13 @@ generator = new Generator(); - self::$output_dir = __DIR__ . DIRECTORY_SEPARATOR; - self::$output_data_dir = self::$output_dir . 'data' . DIRECTORY_SEPARATOR; + self::$outputDir = __DIR__ . DIRECTORY_SEPARATOR; + self::$outputDataDir = self::$outputDir . 'data' . DIRECTORY_SEPARATOR; } - public function testGenerate() - { - $this->generator->generate('TestGeneratorSampleClass' , self::$output_dir); - $this->assertFileExists(self::$output_dir . 'TestGeneratorSampleClassTest.php'); //Output class - $this->assertFileEquals(__DIR__ . '/TestGeneratorSampleClassExpectedOutput.php' , self::$output_dir . 'TestGeneratorSampleClassTest.php'); - $this->assertFileExists(self::$output_data_dir . 'TestGeneratorSampleClass.WithParamsMethod.csv'); //CSV file for TestGeneratorSampleClass::withParamsStaticMethod - $this->assertFileExists(self::$output_data_dir . 'TestGeneratorSampleClass.WithParamsStaticMethod.csv'); //CSV file for TestGeneratorSampleClass::withParamsStaticMethod - } + public function testGenerate() + { + $this->generator->generate('TestGeneratorSampleClass', self::$outputDir); + $this->assertFileExists(self::$outputDir . 'TestGeneratorSampleClassTest.php'); //Output class + $this->assertFileEquals(__DIR__ . '/TestGeneratorSampleClassExpectedOutput.php', + self::$outputDir . 'TestGeneratorSampleClassTest.php'); + $this->assertFileExists(self::$outputDataDir . 'TestGeneratorSampleClass.WithParamsMethod.csv'); //CSV file for TestGeneratorSampleClass::withParamsStaticMethod + $this->assertFileExists(self::$outputDataDir . 'TestGeneratorSampleClass.WithParamsStaticMethod.csv'); //CSV file for TestGeneratorSampleClass::withParamsStaticMethod + } /** * @expectedException \Exception */ public function testException() { - $this->generator->generate('DoNotExistForTesting' , self::$output_dir); + $this->generator->generate('DoNotExistForTesting', self::$outputDir); } - public static function tearDownAfterClass() - { - unlink(self::$output_dir . 'TestGeneratorSampleClassTest.php'); - unlink(self::$output_data_dir . 'TestGeneratorSampleClass.WithParamsMethod.csv'); - unlink(self::$output_data_dir . 'TestGeneratorSampleClass.WithParamsStaticMethod.csv'); - rmdir(self::$output_data_dir); - } + public static function tearDownAfterClass() + { + unlink(self::$outputDir . 'TestGeneratorSampleClassTest.php'); + unlink(self::$outputDataDir . 'TestGeneratorSampleClass.WithParamsMethod.csv'); + unlink(self::$outputDataDir . 'TestGeneratorSampleClass.WithParamsStaticMethod.csv'); + rmdir(self::$outputDataDir); + } } \ No newline at end of file diff --git a/tests/TestGeneratorSampleClass.php b/tests/TestGeneratorSampleClass.php index b48b63d..1aaff01 100644 --- a/tests/TestGeneratorSampleClass.php +++ b/tests/TestGeneratorSampleClass.php @@ -1,73 +1,74 @@ dataProvider('TestGeneratorSampleClass.WithParamsMethod.csv'); - } - - /** - * Data provider function for TestGeneratorSampleClass::withParamsStaticMethod - */ - public function providerWithParamsStaticMethod() - { - return $this->dataProvider('TestGeneratorSampleClass.WithParamsStaticMethod.csv'); - } + /** + * Tests TestGeneratorSampleClass::noParamsMethod + */ + public function testNoParamsMethod() + { + //@TODO implement testNoParamsMethod body + } + + /** + * Tests TestGeneratorSampleClass::withParamsMethod + * + * @dataProvider providerWithParamsMethod + */ + public function testWithParamsMethod($param1, $param2) + { + //@TODO implement testWithParamsMethod body + } + + /** + * Tests TestGeneratorSampleClass::noParamsStaticMethod + */ + public function testNoParamsStaticMethod() + { + //@TODO implement testNoParamsStaticMethod body + } + + /** + * Tests TestGeneratorSampleClass::withParamsStaticMethod + * + * @dataProvider providerWithParamsStaticMethod + */ + public function testWithParamsStaticMethod($param1, $param2) + { + //@TODO implement testWithParamsStaticMethod body + } + + + /** + * Data provider function for TestGeneratorSampleClass::withParamsMethod + */ + public function providerWithParamsMethod() + { + return $this->dataProvider('TestGeneratorSampleClass.WithParamsMethod.csv'); + } + + /** + * Data provider function for TestGeneratorSampleClass::withParamsStaticMethod + */ + public function providerWithParamsStaticMethod() + { + return $this->dataProvider('TestGeneratorSampleClass.WithParamsStaticMethod.csv'); + } } \ No newline at end of file