From 0ba3a8018be08f5d8b8e1e26cb508e92a677fe69 Mon Sep 17 00:00:00 2001 From: Johannes Schobel Date: Sun, 28 Jan 2018 10:34:25 +0100 Subject: [PATCH] add functional test generator --- .../Commands/TestFunctionalTestGenerator.php | 109 ++++++++++++++++++ Generator/GeneratorsServiceProvider.php | 2 + Generator/Stubs/tests/functional/api.stub | 43 +++++++ Generator/Stubs/tests/functional/cli.stub | 26 +++++ Generator/Stubs/tests/functional/general.stub | 24 ++++ Generator/Stubs/tests/functional/web.stub | 43 +++++++ 6 files changed, 247 insertions(+) create mode 100644 Generator/Commands/TestFunctionalTestGenerator.php create mode 100644 Generator/Stubs/tests/functional/api.stub create mode 100644 Generator/Stubs/tests/functional/cli.stub create mode 100644 Generator/Stubs/tests/functional/general.stub create mode 100644 Generator/Stubs/tests/functional/web.stub diff --git a/Generator/Commands/TestFunctionalTestGenerator.php b/Generator/Commands/TestFunctionalTestGenerator.php new file mode 100644 index 000000000..a946aed9a --- /dev/null +++ b/Generator/Commands/TestFunctionalTestGenerator.php @@ -0,0 +1,109 @@ + + */ +class TestFunctionalTestGenerator extends GeneratorCommand implements ComponentsGenerator +{ + + /** + * The console command name. + * + * @var string + */ + protected $name = 'apiato:generate:test:functional'; + + /** + * The console command description. + * + * @var string + */ + protected $description = 'Create a Functional Test file.'; + + /** + * The type of class being generated. + * + * @var string + */ + protected $fileType = 'Functional Test'; + + /** + * The structure of the file path. + * + * @var string + */ + protected $pathStructure = '{container-name}/UI/{user-interface}/Tests/Functional/*'; + + /** + * The structure of the file name. + * + * @var string + */ + protected $nameStructure = '{file-name}'; + + /** + * The name of the stub file. + * + * @var string + */ + protected $stubName = 'tests/functional/general.stub'; + + /** + * User required/optional inputs expected to be passed while calling the command. + * This is a replacement of the `getArguments` function "which reads whenever it's called". + * + * @var array + */ + public $inputs = [ + ['ui', null, InputOption::VALUE_OPTIONAL, 'The user-interface to generate the Test for.'], + ]; + + /** + * @return array + */ + public function getUserInputs() + { + $ui = Str::lower($this->checkParameterOrChoice('ui', 'Select the UI for the Test', ['API', 'WEB', 'CLI'], 0)); + + // set the stub file accordingly + $this->stubName = 'tests/functional/' . $ui . '.stub'; + + // we need to generate the TestCase class before + $this->call('apiato:generate:test:testcase', [ + '--container' => $this->containerName, + '--file' => 'TestCase', + '--ui' => $ui, + ]); + + return [ + 'path-parameters' => [ + 'container-name' => $this->containerName, + 'user-interface' => Str::upper($ui), + ], + 'stub-parameters' => [ + '_container-name' => Str::lower($this->containerName), + 'container-name' => $this->containerName, + 'class-name' => $this->fileName, + ], + 'file-parameters' => [ + 'file-name' => $this->fileName, + ], + ]; + } + + public function getDefaultFileName() + { + return 'DefaultFunctionalTest'; + } + +} + diff --git a/Generator/GeneratorsServiceProvider.php b/Generator/GeneratorsServiceProvider.php index 64c63d3ea..b9aa3e979 100755 --- a/Generator/GeneratorsServiceProvider.php +++ b/Generator/GeneratorsServiceProvider.php @@ -24,6 +24,7 @@ use Apiato\Core\Generator\Commands\ServiceProviderGenerator; use Apiato\Core\Generator\Commands\SubActionGenerator; use Apiato\Core\Generator\Commands\TaskGenerator; +use Apiato\Core\Generator\Commands\TestFunctionalTestGenerator; use Apiato\Core\Generator\Commands\TestTestCaseGenerator; use Apiato\Core\Generator\Commands\TestUnitTestGenerator; use Apiato\Core\Generator\Commands\TransformerGenerator; @@ -79,6 +80,7 @@ public function register() SeederGenerator::class, ServiceProviderGenerator::class, SubActionGenerator::class, + TestFunctionalTestGenerator::class, TestTestCaseGenerator::class, TestUnitTestGenerator::class, TaskGenerator::class, diff --git a/Generator/Stubs/tests/functional/api.stub b/Generator/Stubs/tests/functional/api.stub new file mode 100644 index 000000000..7e8aeb5a8 --- /dev/null +++ b/Generator/Stubs/tests/functional/api.stub @@ -0,0 +1,43 @@ + '', + 'roles' => '', + ]; + + /** + * @test + */ + public function test_() + { + $data = [ + // 'key' => 'value', + ]; + + // send the HTTP request + $response = $this->makeCall($data); + + // assert the response status + $response->assertStatus(200); + + // make other asserts here + } + +} diff --git a/Generator/Stubs/tests/functional/cli.stub b/Generator/Stubs/tests/functional/cli.stub new file mode 100644 index 000000000..546481353 --- /dev/null +++ b/Generator/Stubs/tests/functional/cli.stub @@ -0,0 +1,26 @@ + '', + 'roles' => '', + ]; + + /** + * @test + */ + public function test_() + { + $data = [ + // 'key' => 'value', + ]; + + // send the HTTP request + $response = $this->makeCall($data); + + // assert the response status + $response->assertStatus(200); + + // make other asserts here + } + +}