From 434e7f6b2c30b4b9acf8ecbef69dafbc5697d7db Mon Sep 17 00:00:00 2001 From: Josiah Dahl Date: Sat, 5 Aug 2017 13:12:46 -0700 Subject: [PATCH] Testing Resources Command Finialized testing the changes to the resources command. --- .gitignore | 1 + lumen-test/.gitignore | 3 +- lumen-test/tests/_data/ResourcesTest.yml | 26 ++++ .../tests/acceptance/ResourcesCommandCept.php | 131 ++++++++++++++++++ src/Commands/ResourcesCommand.php | 8 +- 5 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 lumen-test/tests/_data/ResourcesTest.yml create mode 100644 lumen-test/tests/acceptance/ResourcesCommandCept.php diff --git a/.gitignore b/.gitignore index 7319a28..75e576a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ tests/_output/* lumen-test/app lumen-test/database lumen-test/tests/tmp +.idea diff --git a/lumen-test/.gitignore b/lumen-test/.gitignore index edb739f..ac69ef8 100644 --- a/lumen-test/.gitignore +++ b/lumen-test/.gitignore @@ -1,7 +1,8 @@ /vendor .env +codecept.phar tests/_output/* composer.lock -tests/_output/* \ No newline at end of file +tests/_output/* diff --git a/lumen-test/tests/_data/ResourcesTest.yml b/lumen-test/tests/_data/ResourcesTest.yml new file mode 100644 index 0000000..fd2d201 --- /dev/null +++ b/lumen-test/tests/_data/ResourcesTest.yml @@ -0,0 +1,26 @@ +--- +Author: + belongsTo: book + fields: + name: + schema: string + tags: fillable +Book: + belongsTo: librarys # Yes I know it's misspelled... + hasOne: author + fields: + title: + schema: string + tags: fillable + published: + schema: date + tags: fillable +Library: + hasMany: books + fields: + name: + schema: string + tags: fillable + address: + schema: string + tags: fillable diff --git a/lumen-test/tests/acceptance/ResourcesCommandCept.php b/lumen-test/tests/acceptance/ResourcesCommandCept.php new file mode 100644 index 0000000..11e08b7 --- /dev/null +++ b/lumen-test/tests/acceptance/ResourcesCommandCept.php @@ -0,0 +1,131 @@ +wantTo('Generate RESTful resources from a file'); +$I->runShellCommand('php artisan wn:resources tests/_data/ResourcesTest.yml'); + +// Checking the model +$I->seeInShellOutput('Author model generated'); +$I->seeInShellOutput('Book model generated'); +$I->seeInShellOutput('Library model generated'); +$I->seeFileFound('./app/Author.php'); +$I->seeFileFound('./app/Book.php'); +$I->seeFileFound('./app/Library.php'); +$I->deleteFile('./app/Author.php'); +$I->deleteFile('./app/Book.php'); +$I->deleteFile('./app/Library.php'); + +// Checking the migration +$I->seeInShellOutput('authors migration generated'); +$I->seeInShellOutput('books migration generated'); +$I->seeInShellOutput('libraries migration generated'); +// Can't check for specific file names, so we'll just strip the directory +$I->cleanDir('database/migrations'); +$I->writeToFile('database/migrations/.gitkeep', ''); + +// Checking the RESTActions trait +$I->seeFileFound('./app/Http/Controllers/RESTActions.php'); +$I->deleteFile('./app/Http/Controllers/RESTActions.php'); + +// Checking the controller +$I->seeInShellOutput('AuthorsController generated'); +$I->seeInShellOutput('LibrariesController generated'); +$I->seeInShellOutput('BooksController generated'); +$I->seeFileFound('./app/Http/Controllers/AuthorsController.php'); +$I->seeFileFound('./app/Http/Controllers/LibrariesController.php'); +$I->seeFileFound('./app/Http/Controllers/BooksController.php'); + +$I->deleteFile('./app/Http/Controllers/AuthorsController.php'); +$I->deleteFile('./app/Http/Controllers/LibrariesController.php'); +$I->deleteFile('./app/Http/Controllers/BooksController.php'); + + +// Checking routes +$I->openFile('./app/Http/routes.php'); +$I->seeInThisFile(' +$app->get(\'author\', \'AuthorsController@all\'); +$app->get(\'author/{id}\', \'AuthorsController@get\'); +$app->post(\'author\', \'AuthorsController@add\'); +$app->put(\'author/{id}\', \'AuthorsController@put\'); +$app->delete(\'author/{id}\', \'AuthorsController@remove\');'); + +$I->seeInThisFile(' +$app->get(\'book\', \'BooksController@all\'); +$app->get(\'book/{id}\', \'BooksController@get\'); +$app->post(\'book\', \'BooksController@add\'); +$app->put(\'book/{id}\', \'BooksController@put\'); +$app->delete(\'book/{id}\', \'BooksController@remove\');'); + +$I->seeInThisFile(' +$app->get(\'library\', \'LibrariesController@all\'); +$app->get(\'library/{id}\', \'LibrariesController@get\'); +$app->post(\'library\', \'LibrariesController@add\'); +$app->put(\'library/{id}\', \'LibrariesController@put\'); +$app->delete(\'library/{id}\', \'LibrariesController@remove\');'); +$I->writeToFile('./app/Http/routes.php', 'get("/", function () use ($app) { + return $app->welcome(); +}); +'); + +// Checking model factory +// $I->openFile('./database/factories/ModelFactory.php'); +// $I->seeInThisFile( +// "/** +// * Factory definition for model App\TaskCategory. +// */ +// \$factory->define(App\TaskCategory::class, function (\$faker) { +// return [ +// 'name' => \$faker->word, +// 'descr' => \$faker->paragraph, +// 'due' => \$faker->date, +// ]; +// });"); +$I->writeToFile('./database/factories/ModelFactory.php', "define(App\User::class, function (\$faker) { + return [ + 'name' => \$faker->name, + 'email' => \$faker->email, + 'password' => str_random(10), + 'remember_token' => str_random(10), + ]; +}); +"); + +// Checking database seeder +// $I->openFile('./database/seeds/TaskCategoriesTableSeeder.php'); +// $I->seeInThisFile(' +// use Illuminate\Database\Seeder; + +// class TaskCategoriesTableSeeder extends Seeder +// { +// public function run() +// { +// factory(App\TaskCategory::class, 10)->create(); +// } +// }'); +// $I->deleteFile('./database/seeds/TaskCategoriesTableSeeder.php'); diff --git a/src/Commands/ResourcesCommand.php b/src/Commands/ResourcesCommand.php index 8be23b9..6055ff2 100644 --- a/src/Commands/ResourcesCommand.php +++ b/src/Commands/ResourcesCommand.php @@ -20,8 +20,12 @@ class ResourcesCommand extends BaseCommand { $content = $this->fs->get($this->argument('file')); $content = Yaml::parse($content); + $modelIndex = 0; foreach ($content as $model => $i){ $i = $this->getResourceParams($model, $i); + $migrationName = 'Create' . ucwords(str_plural($i['name'])); + $migrationFile = date('Y_m_d_His') . $modelIndex . '_' . snake_case($migrationName) . '_table'; + $this->call('wn:resource', [ 'name' => $i['name'], @@ -32,8 +36,10 @@ class ResourcesCommand extends BaseCommand { '--belongs-to' => $i['belongsTo'], '--belongs-to-many' => $i['belongsToMany'], '--path' => $this->option('path'), - '--force' => $this->option('force') + '--force' => $this->option('force'), + '--migration-file' => $migrationFile ]); + $modelIndex++; } // $this->call('migrate'); // actually needed for pivot seeders !