From 434e7f6b2c30b4b9acf8ecbef69dafbc5697d7db Mon Sep 17 00:00:00 2001 From: Josiah Dahl Date: Sat, 5 Aug 2017 13:12:46 -0700 Subject: [PATCH 1/5] 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 ! From 982dee15fcf034d550677d4a644e1a1119e97a83 Mon Sep 17 00:00:00 2001 From: Josiah Dahl Date: Sun, 6 Aug 2017 09:37:25 -0700 Subject: [PATCH 2/5] Possible codeception updates --- .../_generated/AcceptanceTesterActions.php | 134 ++++++++++++++---- .../_generated/FunctionalTesterActions.php | 16 ++- .../_support/_generated/UnitTesterActions.php | 87 +++++++++++- 3 files changed, 206 insertions(+), 31 deletions(-) diff --git a/lumen-test/tests/_support/_generated/AcceptanceTesterActions.php b/lumen-test/tests/_support/_generated/AcceptanceTesterActions.php index ce0221f..83b4f6e 100644 --- a/lumen-test/tests/_support/_generated/AcceptanceTesterActions.php +++ b/lumen-test/tests/_support/_generated/AcceptanceTesterActions.php @@ -1,4 +1,4 @@ -getScenario()->runStep(new \Codeception\Step\Action('assertArraySubset', func_get_args())); + } + + /** * [!] Method is generated. Documentation taken from corresponding module. * @@ -95,7 +111,7 @@ trait AcceptanceTesterActions /** * [!] Method is generated. Documentation taken from corresponding module. * - * + * @param $regex * Conditional Assertion: Test won't be stopped on fail * @see \Codeception\Module\Cli::seeShellOutputMatches() */ @@ -105,7 +121,7 @@ trait AcceptanceTesterActions /** * [!] Method is generated. Documentation taken from corresponding module. * - * + * @param $regex * @see \Codeception\Module\Cli::seeShellOutputMatches() */ public function seeShellOutputMatches($regex) { @@ -113,13 +129,83 @@ trait AcceptanceTesterActions } + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Checks result code + * + * ```php + * seeResultCodeIs(0); + * ``` + * + * @param $code + * Conditional Assertion: Test won't be stopped on fail + * @see \Codeception\Module\Cli::seeResultCodeIs() + */ + public function canSeeResultCodeIs($code) { + return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeResultCodeIs', func_get_args())); + } + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Checks result code + * + * ```php + * seeResultCodeIs(0); + * ``` + * + * @param $code + * @see \Codeception\Module\Cli::seeResultCodeIs() + */ + public function seeResultCodeIs($code) { + return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeResultCodeIs', func_get_args())); + } + + + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Checks result code + * + * ```php + * seeResultCodeIsNot(0); + * ``` + * + * @param $code + * Conditional Assertion: Test won't be stopped on fail + * @see \Codeception\Module\Cli::seeResultCodeIsNot() + */ + public function canSeeResultCodeIsNot($code) { + return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeResultCodeIsNot', func_get_args())); + } + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Checks result code + * + * ```php + * seeResultCodeIsNot(0); + * ``` + * + * @param $code + * @see \Codeception\Module\Cli::seeResultCodeIsNot() + */ + public function seeResultCodeIsNot($code) { + return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeResultCodeIsNot', func_get_args())); + } + + /** * [!] Method is generated. Documentation taken from corresponding module. * * Enters a directory In local filesystem. * Project root directory is used by default * - * @param $path + * @param string $path * @see \Codeception\Module\Filesystem::amInPath() */ public function amInPath($path) { @@ -141,7 +227,7 @@ trait AcceptanceTesterActions * ?> * ``` * - * @param $filename + * @param string $filename * @see \Codeception\Module\Filesystem::openFile() */ public function openFile($filename) { @@ -160,7 +246,7 @@ trait AcceptanceTesterActions * ?> * ``` * - * @param $filename + * @param string $filename * @see \Codeception\Module\Filesystem::deleteFile() */ public function deleteFile($filename) { @@ -179,7 +265,7 @@ trait AcceptanceTesterActions * ?> * ``` * - * @param $dirname + * @param string $dirname * @see \Codeception\Module\Filesystem::deleteDir() */ public function deleteDir($dirname) { @@ -198,8 +284,8 @@ trait AcceptanceTesterActions * ?> * ``` * - * @param $src - * @param $dst + * @param string $src + * @param string $dst * @see \Codeception\Module\Filesystem::copyDir() */ public function copyDir($src, $dst) { @@ -221,7 +307,7 @@ trait AcceptanceTesterActions * ?> * ``` * - * @param $text + * @param string $text * Conditional Assertion: Test won't be stopped on fail * @see \Codeception\Module\Filesystem::seeInThisFile() */ @@ -242,7 +328,7 @@ trait AcceptanceTesterActions * ?> * ``` * - * @param $text + * @param string $text * @see \Codeception\Module\Filesystem::seeInThisFile() */ public function seeInThisFile($text) { @@ -298,7 +384,7 @@ trait AcceptanceTesterActions * * Checks that contents of currently opened file matches $regex * - * @param $regex + * @param string $regex * Conditional Assertion: Test won't be stopped on fail * @see \Codeception\Module\Filesystem::seeThisFileMatches() */ @@ -310,7 +396,7 @@ trait AcceptanceTesterActions * * Checks that contents of currently opened file matches $regex * - * @param $regex + * @param string $regex * @see \Codeception\Module\Filesystem::seeThisFileMatches() */ public function seeThisFileMatches($regex) { @@ -333,7 +419,7 @@ trait AcceptanceTesterActions * ?> * ``` * - * @param $text + * @param string $text * Conditional Assertion: Test won't be stopped on fail * @see \Codeception\Module\Filesystem::seeFileContentsEqual() */ @@ -355,7 +441,7 @@ trait AcceptanceTesterActions * ?> * ``` * - * @param $text + * @param string $text * @see \Codeception\Module\Filesystem::seeFileContentsEqual() */ public function seeFileContentsEqual($text) { @@ -375,7 +461,7 @@ trait AcceptanceTesterActions * ?> * ``` * - * @param $text + * @param string $text * Conditional Assertion: Test won't be stopped on fail * @see \Codeception\Module\Filesystem::dontSeeInThisFile() */ @@ -394,7 +480,7 @@ trait AcceptanceTesterActions * ?> * ``` * - * @param $text + * @param string $text * @see \Codeception\Module\Filesystem::dontSeeInThisFile() */ public function dontSeeInThisFile($text) { @@ -425,7 +511,7 @@ trait AcceptanceTesterActions * ?> * ``` * - * @param $filename + * @param string $filename * @param string $path * Conditional Assertion: Test won't be stopped on fail * @see \Codeception\Module\Filesystem::seeFileFound() @@ -445,7 +531,7 @@ trait AcceptanceTesterActions * ?> * ``` * - * @param $filename + * @param string $filename * @param string $path * @see \Codeception\Module\Filesystem::seeFileFound() */ @@ -459,7 +545,7 @@ trait AcceptanceTesterActions * * Checks if file does not exist in path * - * @param $filename + * @param string $filename * @param string $path * Conditional Assertion: Test won't be stopped on fail * @see \Codeception\Module\Filesystem::dontSeeFileFound() @@ -472,7 +558,7 @@ trait AcceptanceTesterActions * * Checks if file does not exist in path * - * @param $filename + * @param string $filename * @param string $path * @see \Codeception\Module\Filesystem::dontSeeFileFound() */ @@ -492,7 +578,7 @@ trait AcceptanceTesterActions * ?> * ``` * - * @param $dirname + * @param string $dirname * @see \Codeception\Module\Filesystem::cleanDir() */ public function cleanDir($dirname) { @@ -505,8 +591,8 @@ trait AcceptanceTesterActions * * Saves contents to file * - * @param $filename - * @param $contents + * @param string $filename + * @param string $contents * @see \Codeception\Module\Filesystem::writeToFile() */ public function writeToFile($filename, $contents) { diff --git a/lumen-test/tests/_support/_generated/FunctionalTesterActions.php b/lumen-test/tests/_support/_generated/FunctionalTesterActions.php index 01e7df9..074ea91 100644 --- a/lumen-test/tests/_support/_generated/FunctionalTesterActions.php +++ b/lumen-test/tests/_support/_generated/FunctionalTesterActions.php @@ -1,4 +1,4 @@ -getScenario()->runStep(new \Codeception\Step\Action('assertArraySubset', func_get_args())); + } } diff --git a/lumen-test/tests/_support/_generated/UnitTesterActions.php b/lumen-test/tests/_support/_generated/UnitTesterActions.php index 35bd8ec..9b2691f 100644 --- a/lumen-test/tests/_support/_generated/UnitTesterActions.php +++ b/lumen-test/tests/_support/_generated/UnitTesterActions.php @@ -1,4 +1,4 @@ -assertEquals($element->getChildrenCount(), 5); + * ``` + * + * Floating-point example: + * ```php + * assertEquals($calculator->add(0.1, 0.2), 0.3, 'Calculator should add the two numbers correctly.', 0.01); + * ``` * * @param $expected * @param $actual * @param string $message + * @param float $delta * @see \Codeception\Module\Asserts::assertEquals() */ - public function assertEquals($expected, $actual, $message = null) { + public function assertEquals($expected, $actual, $message = null, $delta = null) { return $this->getScenario()->runStep(new \Codeception\Step\Action('assertEquals', func_get_args())); } @@ -34,14 +49,29 @@ trait UnitTesterActions /** * [!] Method is generated. Documentation taken from corresponding module. * - * Checks that two variables are not equal + * Checks that two variables are not equal. If you're comparing floating-point values, + * you can specify the optional "delta" parameter which dictates how great of a precision + * error are you willing to tolerate in order to consider the two values not equal. + * + * Regular example: + * ```php + * assertNotEquals($element->getChildrenCount(), 0); + * ``` + * + * Floating-point example: + * ```php + * assertNotEquals($calculator->add(0.1, 0.2), 0.4, 'Calculator should add the two numbers correctly.', 0.01); + * ``` * * @param $expected * @param $actual * @param string $message + * @param float $delta * @see \Codeception\Module\Asserts::assertNotEquals() */ - public function assertNotEquals($expected, $actual, $message = null) { + public function assertNotEquals($expected, $actual, $message = null, $delta = null) { return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotEquals', func_get_args())); } @@ -54,7 +84,6 @@ trait UnitTesterActions * @param $expected * @param $actual * @param string $message - * @return mixed|void * @see \Codeception\Module\Asserts::assertSame() */ public function assertSame($expected, $actual, $message = null) { @@ -197,6 +226,36 @@ trait UnitTesterActions } + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Checks that a string starts with the given prefix. + * + * @param string $prefix + * @param string $string + * @param string $message + * @see \Codeception\Module\Asserts::assertStringStartsWith() + */ + public function assertStringStartsWith($prefix, $string, $message = null) { + return $this->getScenario()->runStep(new \Codeception\Step\Action('assertStringStartsWith', func_get_args())); + } + + + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Checks that a string doesn't start with the given prefix. + * + * @param string $prefix + * @param string $string + * @param string $message + * @see \Codeception\Module\Asserts::assertStringStartsNotWith() + */ + public function assertStringStartsNotWith($prefix, $string, $message = null) { + return $this->getScenario()->runStep(new \Codeception\Step\Action('assertStringStartsNotWith', func_get_args())); + } + + /** * [!] Method is generated. Documentation taken from corresponding module. * @@ -373,6 +432,22 @@ trait UnitTesterActions } + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Checks that array contains subset. + * + * @param array $subset + * @param array $array + * @param bool $strict + * @param string $message + * @see \Codeception\Module::assertArraySubset() + */ + public function assertArraySubset($subset, $array, $strict = null, $message = null) { + return $this->getScenario()->runStep(new \Codeception\Step\Action('assertArraySubset', func_get_args())); + } + + /** * [!] Method is generated. Documentation taken from corresponding module. * From afa5bf1f0c71391b882f6964592a57d27d0c9688 Mon Sep 17 00:00:00 2001 From: Josiah Dahl Date: Sun, 6 Aug 2017 09:44:50 -0700 Subject: [PATCH 3/5] Pad the model index to 3 characters --- src/Commands/ResourcesCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/ResourcesCommand.php b/src/Commands/ResourcesCommand.php index 6055ff2..0b1456c 100644 --- a/src/Commands/ResourcesCommand.php +++ b/src/Commands/ResourcesCommand.php @@ -24,7 +24,7 @@ class ResourcesCommand extends BaseCommand { 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'; + $migrationFile = date('Y_m_d_His') . '-' . str_pad($modelIndex , 3, 0) . '_' . snake_case($migrationName) . '_table'; $this->call('wn:resource', [ From 6e00a91eea5470a1f97613ba3d103cc8d49babbe Mon Sep 17 00:00:00 2001 From: Josiah Dahl Date: Sun, 6 Aug 2017 15:56:01 -0700 Subject: [PATCH 4/5] Fixed string padding --- src/Commands/ResourcesCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/ResourcesCommand.php b/src/Commands/ResourcesCommand.php index 0b1456c..8d31b93 100644 --- a/src/Commands/ResourcesCommand.php +++ b/src/Commands/ResourcesCommand.php @@ -24,7 +24,7 @@ class ResourcesCommand extends BaseCommand { foreach ($content as $model => $i){ $i = $this->getResourceParams($model, $i); $migrationName = 'Create' . ucwords(str_plural($i['name'])); - $migrationFile = date('Y_m_d_His') . '-' . str_pad($modelIndex , 3, 0) . '_' . snake_case($migrationName) . '_table'; + $migrationFile = date('Y_m_d_His') . '-' . str_pad($modelIndex , 3, 0, STR_PAD_LEFT) . '_' . snake_case($migrationName) . '_table'; $this->call('wn:resource', [ From 8b6e2db315bf9ff7720c16f37f54fc41b189cc8c Mon Sep 17 00:00:00 2001 From: Josiah Dahl Date: Mon, 7 Aug 2017 12:28:22 -0700 Subject: [PATCH 5/5] Added database for testing --- lumen-test/tests/acceptance/ResourcesCommandCept.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lumen-test/tests/acceptance/ResourcesCommandCept.php b/lumen-test/tests/acceptance/ResourcesCommandCept.php index 11e08b7..467ee7b 100644 --- a/lumen-test/tests/acceptance/ResourcesCommandCept.php +++ b/lumen-test/tests/acceptance/ResourcesCommandCept.php @@ -2,6 +2,8 @@ $I = new AcceptanceTester($scenario); $I->wantTo('Generate RESTful resources from a file'); +$I->writeToFile('database/database.sqlite', ''); + $I->runShellCommand('php artisan wn:resources tests/_data/ResourcesTest.yml'); // Checking the model @@ -116,6 +118,9 @@ $I->writeToFile('./database/factories/ModelFactory.php', "deleteFile('database/database.sqlite'); + + // Checking database seeder // $I->openFile('./database/seeds/TaskCategoriesTableSeeder.php'); // $I->seeInThisFile('