Skip to content

Commit

Permalink
Testgenerators (#74)
Browse files Browse the repository at this point in the history
Adding some new `apiato:generate:test:*` commands
  • Loading branch information
johannesschobel authored Jan 28, 2018
2 parents b59741c + 0ba3a80 commit 27ea220
Show file tree
Hide file tree
Showing 13 changed files with 567 additions and 0 deletions.
109 changes: 109 additions & 0 deletions Generator/Commands/TestFunctionalTestGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Apiato\Core\Generator\Commands;

use Apiato\Core\Generator\GeneratorCommand;
use Apiato\Core\Generator\Interfaces\ComponentsGenerator;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;

/**
* Class TestFunctionalTestGenerator
*
* @author Johannes Schobel <[email protected]>
*/
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';
}

}

116 changes: 116 additions & 0 deletions Generator/Commands/TestTestCaseGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Apiato\Core\Generator\Commands;

use Apiato\Core\Generator\GeneratorCommand;
use Apiato\Core\Generator\Interfaces\ComponentsGenerator;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;

/**
* Class TestTestCaseGenerator
*
* @author Johannes Schobel <[email protected]>
*/
class TestTestCaseGenerator extends GeneratorCommand implements ComponentsGenerator
{

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

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create the TestCase file.';

/**
* The type of class being generated.
*
* @var string
*/
protected $fileType = 'TestCase';

/**
* The structure of the file path.
*
* @var string
*/
protected $pathStructure = '{container-name}/Tests/*';

/**
* The structure of the file name.
*
* @var string
*/
protected $nameStructure = '{file-name}';

/**
* The name of the stub file.
*
* @var string
*/
protected $stubName = 'tests/testcase/generic.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 TestCase for.'],
];

/**
* @return array
*/
public function getUserInputs()
{
// we manually set the filename to TestCase as this is the preferred name within apiato
$this->fileName = 'TestCase';

$ui = Str::lower($this->checkParameterOrChoice('ui', 'Select the UI for the controller', ['Generic', 'API', 'WEB', 'CLI'], 0));

// we need to generate the generic testcase first!
if ($ui != 'generic') {
$this->call('apiato:generate:test:testcase', [
'--container' => $this->containerName,
'--file' => 'TestCase',
'--ui' => 'generic',
]);

// however, as this generator here is NOT the one for the generic TestCase, we need to prepend the UI before
// this results in something like ApiTestCase
$this->fileName = Str::ucfirst($ui) . $this->fileName;
}

$this->stubName = 'tests/testcase/' . $ui . '.stub';

return [
'path-parameters' => [
'container-name' => $this->containerName,
],
'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 'TestCase';
}

}

101 changes: 101 additions & 0 deletions Generator/Commands/TestUnitTestGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Apiato\Core\Generator\Commands;

use Apiato\Core\Generator\GeneratorCommand;
use Apiato\Core\Generator\Interfaces\ComponentsGenerator;
use Illuminate\Support\Str;

/**
* Class TestUnitTestGenerator
*
* @author Johannes Schobel <[email protected]>
*/
class TestUnitTestGenerator extends GeneratorCommand implements ComponentsGenerator
{

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

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a Unit Test file.';

/**
* The type of class being generated.
*
* @var string
*/
protected $fileType = 'Unit Test';

/**
* The structure of the file path.
*
* @var string
*/
protected $pathStructure = '{container-name}/Tests/Unit/*';

/**
* The structure of the file name.
*
* @var string
*/
protected $nameStructure = '{file-name}';

/**
* The name of the stub file.
*
* @var string
*/
protected $stubName = 'tests/unit/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 = [
];

/**
* @return array
*/
public function getUserInputs()
{
// we need to generate the TestCase class before
$this->call('apiato:generate:test:testcase', [
'--container' => $this->containerName,
'--file' => 'TestCase',
'--ui' => 'generic',
]);

return [
'path-parameters' => [
'container-name' => $this->containerName,
],
'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 'DefaultUnitTest';
}

}

6 changes: 6 additions & 0 deletions Generator/GeneratorsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
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;
use Apiato\Core\Generator\Commands\TransporterGenerator;
use Apiato\Core\Generator\Commands\ValueGenerator;
Expand Down Expand Up @@ -77,6 +80,9 @@ public function register()
SeederGenerator::class,
ServiceProviderGenerator::class,
SubActionGenerator::class,
TestFunctionalTestGenerator::class,
TestTestCaseGenerator::class,
TestUnitTestGenerator::class,
TaskGenerator::class,
TransformerGenerator::class,
TransporterGenerator::class,
Expand Down
43 changes: 43 additions & 0 deletions Generator/Stubs/tests/functional/api.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Containers\{{container-name}}\UI\API\Tests\Functional;

use App\Containers\{{container-name}}\Tests\ApiTestCase;

/**
* Class {{class-name}}.
*
* @group {{_container-name}}
* @group api
*/
class {{class-name}} extends ApiTestCase
{

// the endpoint to be called within this test (e.g., get@v1/users)
protected $endpoint = 'method@endpoint';

// fake some access rights
protected $access = [
'permissions' => '',
'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
}

}
Loading

0 comments on commit 27ea220

Please sign in to comment.