Skip to content

Commit

Permalink
add functional test generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Schobel committed Jan 28, 2018
1 parent e01d252 commit 0ba3a80
Show file tree
Hide file tree
Showing 6 changed files with 247 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';
}

}

2 changes: 2 additions & 0 deletions Generator/GeneratorsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,6 +80,7 @@ public function register()
SeederGenerator::class,
ServiceProviderGenerator::class,
SubActionGenerator::class,
TestFunctionalTestGenerator::class,
TestTestCaseGenerator::class,
TestUnitTestGenerator::class,
TaskGenerator::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
}

}
26 changes: 26 additions & 0 deletions Generator/Stubs/tests/functional/cli.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

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

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

/**
* Class {{class-name}}.
*
* @group {{_container-name}}
* @group cli
*/
class {{class-name}} extends CliTestCase
{

/**
* @test
*/
public function test_()
{
// make something here

// assert basic stuff here
}

}
24 changes: 24 additions & 0 deletions Generator/Stubs/tests/functional/general.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

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

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

/**
* Class {{class-name}}.
*
* @group {{_container-name}}
* @group general
*/
class {{class-name}} extends TestCase
{

/**
* @test
*/
public function test_()
{

}

}
43 changes: 43 additions & 0 deletions Generator/Stubs/tests/functional/web.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

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

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

/**
* Class {{class-name}}.
*
* @group {{_container-name}}
* @group web
*/
class {{class-name}} extends WebTestCase
{

// 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
}

}

0 comments on commit 0ba3a80

Please sign in to comment.