Skip to content

Commit

Permalink
PSR-1/PSR-2 output class
Browse files Browse the repository at this point in the history
  • Loading branch information
davispeixoto committed Oct 5, 2014
1 parent 3015ecc commit b3230b1
Show file tree
Hide file tree
Showing 15 changed files with 304 additions and 293 deletions.
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/vendor
composer.phar
composer.lock
.DS_Store
.buildpath
.settings
.project
.idea
3 changes: 0 additions & 3 deletions CONTRIBUTING.md

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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/)
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
98 changes: 50 additions & 48 deletions src/Davispeixoto/TestGenerator/Commands/TestsGeneratorCommand.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<?php namespace Davispeixoto\TestGenerator\Commands;

use Davispeixoto\TestGenerator\Generator;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Davispeixoto\TestGenerator\Generator;
use Symfony\Component\Console\Input\InputOption;

class TestsGeneratorCommand extends Command {
class TestsGeneratorCommand extends Command
{

/**
* The console command name.
*
* @var string
*/
protected $name = 'tests:generate';
/**
* The console command name.
*
* @var string
*/
protected $name = 'tests:generate';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate a PHPUnit/Laravel Test class skeleton.';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate a PHPUnit/Laravel Test class skeleton.';

protected $generator;

Expand All @@ -28,16 +29,16 @@ public function __construct()
parent::__construct();
$this->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');
Expand All @@ -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'),
);
}
}

?>
86 changes: 49 additions & 37 deletions src/Davispeixoto/TestGenerator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Davispeixoto/TestGenerator/Templates/class.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

class {{className}}Test extends TestCase
{
{{methods}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Tests {{className}}::{{methodNameLowerCase}}
*
* @dataProvider provider{{methodName}}
*/
public function test{{methodName}}({{methodArgs}})
{
//@TODO implement test{{methodName}} body
}
/**
* Tests {{className}}::{{methodNameLowerCase}}
*
* @dataProvider provider{{methodName}}
*/
public function test{{methodName}}({{methodArgs}})
{
//@TODO implement test{{methodName}} body
}

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Tests {{className}}::{{methodNameLowerCase}}
*/
public function test{{methodName}}()
{
//@TODO implement test{{methodName}} body
}
/**
* Tests {{className}}::{{methodNameLowerCase}}
*/
public function test{{methodName}}()
{
//@TODO implement test{{methodName}} body
}

14 changes: 7 additions & 7 deletions src/Davispeixoto/TestGenerator/Templates/provider.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Data provider function for {{className}}::{{methodNameLowerCase}}
*/
public function provider{{methodName}}()
{
return $this->dataProvider('{{csvFileName}}.csv');
}
/**
* Data provider function for {{className}}::{{methodNameLowerCase}}
*/
public function provider{{methodName}}()
{
return $this->dataProvider('{{csvFileName}}.csv');
}

Loading

0 comments on commit b3230b1

Please sign in to comment.