Skip to content

Commit

Permalink
Merge pull request #19 from webNeat/bugfixes
Browse files Browse the repository at this point in the history
Preparing Version 1.2.0
  • Loading branch information
webNeat authored Jan 12, 2017
2 parents 17bc595 + a9c12e9 commit 6e7d3fb
Show file tree
Hide file tree
Showing 26 changed files with 329 additions and 253 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
tests/_output/*

lumen-test/app
lumen-test/database
lumen-test/database
lumen-test/tests/tmp
78 changes: 57 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ More then that, you can generate multiple resources with only one command ! [Cli
The `wn:model` command is used to generate a model class based on Eloquent. It has the following syntax:

```
wn:model name [--fillable=...] [--dates=...] [--has-many=...] [--has-one=...] [--belongs-to=...] [--belongs-to-many=...] [--rules=...] [--path=...]
wn:model name [--fillable=...] [--dates=...] [--has-many=...] [--has-one=...] [--belongs-to=...] [--belongs-to-many=...] [--rules=...] [--path=...] [--force=true]
```

- **name**: the name of the model.
Expand Down Expand Up @@ -357,12 +357,18 @@ gives:
];
```

- **--force**: tells the generator to override the existing file. By default, if the model file already exists, it will not be overriden and the output will be something like:

```
TestingModel model already exists; use --force option to override it !
```

### Migration Generator

The `wn:migration` command is used to generate a migration to create a table with schema. It has the following syntax:

```
wn:migration table [--schema=...] [--keys=...] [--file=...]
wn:migration table [--schema=...] [--keys=...] [--force=true] [--file=...]
```

- **table**: the name of the table to create.
Expand Down Expand Up @@ -427,7 +433,7 @@ $table->foreign('user_id')
The `wn:pivot-table` command is used to generate a migration to create a pivot table between two models. It has the following syntax:

```
wn:pivot-table model1 model2 [--file=...]
wn:pivot-table model1 model2 [--force=true] [--file=...]
```

- **model1** and **model2**: names of the two models (or the two tables if the models don't follow the naming conventions)
Expand Down Expand Up @@ -487,12 +493,14 @@ There are two commands for controllers. The first one is `wn:controller:rest-act

Note that the trait doesn't use the common used methods on Laravel (like index, store, ...) to avoid conflicts. Which enables you to use this trait with controllers you already have in your application.

The second command is `wn:controller` which actually generates the controller. The syntax of this command is `wn:controller model [--no-routes]`.
The second command is `wn:controller` which actually generates the controller. The syntax of this command is `wn:controller model [--no-routes] [--force=true]`.

- **model**: Name of the model (with namespace if not `App`).

- **--no-routes**: Since routes are generated by default for the controller, this option is used to tell the generator "do not generate routes !".

- **--force**: tells the generator to override the existing file.

`php artisan wn:controller Task --no-routes` gives:


Expand All @@ -513,12 +521,14 @@ class TasksController extends Controller {

The `wn:route` command is used to generate RESTfull routes for a controller. It has the following syntax:

`wn:route resource [--controller=...]`
`wn:route resource [--controller=...] [--force=true]`

- **resource**: the resource name to use in the URLs.

- **--controller**: the corresponding controller. If missing it's deducted from the resource name.

- **--force**: tells the generator to override the existing file.

`php artisan wn:route project-type` adds the following routes:

```php
Expand All @@ -531,7 +541,7 @@ $app->delete('project-type/{id}', 'ProjectTypesController@remove');

### Resource Generator

The `wn:resource` command makes it very easy to generate a RESTful resource. It generates a model, migration, controller and routes. The syntax is : `wn:resource name fields [--has-many=...] [--has-one=...] [--belongs-to=...] [--migration-file=...]`
The `wn:resource` command makes it very easy to generate a RESTful resource. It generates a model, migration, controller and routes. The syntax is : `wn:resource name fields [--has-many=...] [--has-one=...] [--belongs-to=...] [--migration-file=...] [--path=...] [--force=true]`

- **name**: the name of the resource used in the URLs and to determine the model, table and controller names.

Expand All @@ -555,18 +565,24 @@ The `wn:resource` command makes it very easy to generate a RESTful resource. It

- **--migration-file**: passed to the `wn:migration` as the `--file` option.

- **--path**: Defines where to store the model file as well as its namespace.

- **--force**: tells the generator to override the existing file.

### Multiple Resources From File

The `wn:resources` (note the "s" in "resources") command takes the generation process to an other level by parsing a file and generating multiple resources based on it. The syntax is

```
wn:resources filename
wn:resources filename [--path=...] [--force=true]
```

This generator is smart enough to add foreign keys automatically when finding a belongsTo relation. It also generates pivot tables for belongsToMany relations automatically.

The file given to the command should be a valid YAML file ( for the moment, support of other types like XML or JSON could be added in the future). An example is the following:

- **--path**: Defines where to store the model files as well as their namespace.

```yaml
---
Store:
Expand Down Expand Up @@ -606,35 +622,55 @@ To test the generators, I included a fresh lumen installation under the folder `

## Development Notes

- **Version 1.0.0**
- **Comming versions**

- **Seeder and Test generators**

- Model Generator
- Requested Feature: [Disabling timestamps](https://github.com/webNeat/lumen-generators/issues/15)

- Requested Feature: [Custom Templates](https://github.com/webNeat/lumen-generators/issues/13)

- Migration Generator
- **Version 1.2.0**

- Controller Generator
- Tests fixed.

- Routes Generator
- Bug fixed: [Undefined index: factory](https://github.com/webNeat/lumen-generators/issues/14)

- Feature added: [Check if file already exists before generating it](https://github.com/webNeat/lumen-generators/issues/11)

- Feature added: [Support for additional columns like nullableTimestamps() and softDeletes() in migrations](https://github.com/webNeat/lumen-generators/issues/12)

- Resource Generator
- Feature added: [Specifying namespace for `wn:resource` and `wn:resources`](https://github.com/webNeat/lumen-generators/issues/18)

- Multiple Resources From File
- **Version 1.1.1**

- Pivot table generation from the `wn:resources` command bug fixed.

- **Version 1.1.0**

- Pivot table generator added.
- Pivot table generator added.

- belongsToMany relationship added to model generator.
- belongsToMany relationship added to model generator.

- Multiple resources generator adds foreign keys for belongsTo relationships automatically.
- Multiple resources generator adds foreign keys for belongsTo relationships automatically.

- Multiple resources generator adds pivot tables for belongsToMany relationships automatically.
- Multiple resources generator adds pivot tables for belongsToMany relationships automatically.

- Generated migrations file names changed to be supported by `migrate` command.
- Generated migrations file names changed to be supported by `migrate` command.

- **Version 1.1.1**
- **Version 1.0.0**

- Model Generator

- Pivot table generation from the `wn:resources` command bug fixed.
- Migration Generator

- Controller Generator

- Routes Generator

- Resource Generator

- Multiple Resources From File

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion lumen-test/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"fzaninotto/faker": "~1.0",
"phpspec/phpspec": "2.0.0",
"codeception/codeception": "2.0.0",
"wn/lumen-generators": "@dev"
"wn/lumen-generators": "dev-bugfixes"
},
"autoload": {
"psr-4": {
Expand Down
21 changes: 0 additions & 21 deletions lumen-test/database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,3 @@
'remember_token' => str_random(10),
];
});
/**
* Factory definition for model App\Task.
*/
$factory->define(App\Task::class, function ($faker) {
return [
// Fields here
];
});

/**
* Factory definition for model App\TaskCategory.
*/
$factory->define(App\TaskCategory::class, function ($faker) {
return [
'name' => $faker->word,
'descr' => $faker->paragraph,
'due' => $faker->date,
'project_id' => $faker->key,
'user_id' => $faker->key,
];
});
22 changes: 22 additions & 0 deletions lumen-test/models.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
Post:
belongsToMany: tags
fields:
title:
schema: string
rules: required
tags: fillable
content:
schema: text nullable
tags: fillable
published_at:
schema: date
rules: date
tags: date fillable
Tag:
belongsToMany: posts
fields:
name:
schema: string unique
rules: required
tags: fillable
12 changes: 6 additions & 6 deletions lumen-test/tests/acceptance/FactoryCommandCept.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
$I->runShellCommand('php artisan wn:factory "App\Task" --fields="title:sentence(3),description:paragraph(3),due:date,hidden:boolean"');
$I->seeInShellOutput('App\Task factory generated');
$I->openFile('./database/factories/ModelFactory.php');
$I->seeInThisFile("
'title' => \$faker->sentence(3),
'description' => \$faker->paragraph(3),
'due' => \$faker->date,
'hidden' => \$faker->boolean,
");
$I->seeInThisFile(
" 'title' => \$faker->sentence(3)," . PHP_EOL .
" 'description' => \$faker->paragraph(3)," . PHP_EOL .
" 'due' => \$faker->date," . PHP_EOL .
" 'hidden' => \$faker->boolean,"
);
$I->writeToFile('./database/factories/ModelFactory.php', "<?php
/*
Expand Down
29 changes: 21 additions & 8 deletions lumen-test/tests/acceptance/MigrationCommandCept.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,24 @@ public function down()
$I->seeInShellOutput('tasks migration generated');
$I->seeFileFound('./database/migrations/create_tasks.php');
$I->openFile('./database/migrations/create_tasks.php');
$I->seeInThisFile('$table->foreign(\'category_type_id\')
->references(\'id\')
->on(\'category_types\');');
$I->seeInThisFile("\$table->foreign('user_id')
->references('identifier')
->on('members')
->onDelete('cascade');");
$I->deleteFile('./database/migrations/create_tasks.php');
$I->seeInThisFile(
"\$table->foreign('category_type_id')\n".
" ->references('id')\n".
" ->on('category_types');"
);
$I->seeInThisFile(
"\$table->foreign('user_id')\n".
" ->references('identifier')\n".
" ->on('members')". PHP_EOL .
" ->onDelete('cascade');");
$I->deleteFile('./database/migrations/create_tasks.php');

$I->wantTo('generate a migration with additional columns');
$I->runShellCommand('php artisan wn:migration tasks --file=create_tasks --add=softDeletes,nullableTimestamps');
$I->seeInShellOutput('tasks migration generated');
$I->seeFileFound('./database/migrations/create_tasks.php');
$I->openFile('./database/migrations/create_tasks.php');
$I->dontSeeInThisFile("\$table->timestamps();");
$I->seeInThisFile("\$table->softDeletes();");
$I->seeInThisFile("\$table->nullableTimestamps();");
$I->deleteFile('./database/migrations/create_tasks.php');
20 changes: 12 additions & 8 deletions lumen-test/tests/acceptance/ModelCommandCept.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$I = new AcceptanceTester($scenario);

$I->wantTo('generate a model without fillable fields or dates');
$I->runShellCommand('php artisan wn:model TestingModel --path=tests/tmp');
$I->runShellCommand('php artisan wn:model TestingModel --path=tests/tmp --force=true');
$I->seeInShellOutput('TestingModel model generated');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
Expand All @@ -25,18 +25,21 @@ class TestingModel extends Model {
}
');
$I->deleteFile('./tests/tmp/TestingModel.php');

$I->wantTo('generate a model with fillable fields');
$I->runShellCommand('php artisan wn:model TestingModel --fillable=name,title --path=tests/tmp');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeInThisFile('protected $fillable = ["name", "title"];');
$I->deleteFile('./tests/tmp/TestingModel.php');

$I->wantTo('generate a model with dates fields');
$I->runShellCommand('php artisan wn:model TestingModel --dates=started_at --path=tests/tmp');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeInThisFile('protected $dates = ["started_at"];');
$I->deleteFile('./tests/tmp/TestingModel.php');

$I->wantTo('generate a model with relations');
$I->runShellCommand('php artisan wn:model TestingModel --has-many=accounts --belongs-to="owner:App\User" --has-one=number:Phone --path=tests/tmp');
Expand All @@ -60,17 +63,18 @@ public function number()
return $this->hasOne("Tests\\Tmp\\Phone");
}
');
$I->deleteFile('./tests/tmp/TestingModel.php');

$I->wantTo('generate a model with validation rules');
$I->runShellCommand('php artisan wn:model TestingModel --rules="name=required age=integer|min:13 email=email|unique:users,email_address" --path=tests/tmp');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeInThisFile('
public static $rules = [
"name" => "required",
"age" => "integer|min:13",
"email" => "email|unique:users,email_address",
];
');
$I->seeInThisFile(
" public static \$rules = [\n" .
" \"name\" => \"required\"," . PHP_EOL .
" \"age\" => \"integer|min:13\"," . PHP_EOL .
" \"email\" => \"email|unique:users,email_address\",\n".
" ];"
);

$I->deleteFile('./tests/tmp/TestingModel.php');
Loading

0 comments on commit 6e7d3fb

Please sign in to comment.