Skip to content

Commit

Permalink
Added parameters to stub
Browse files Browse the repository at this point in the history
Here's an example of creating a stub basic-test file using parameters in artisan command line.

Example:

php artisan make:a basic-test Feature\Modules\Pages\PageCategoryTest "{TEST}:Feature|{MODULE}:Pages|{MODEL}:PageCategory|{URL}:page_category"
  • Loading branch information
Hardpunk committed Dec 12, 2018
1 parent 06a3d6c commit d52007a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/StubsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class StubsCommand extends Command
protected $signature = 'make:a
{ type : what type of file do you want? }
{ name : component name? }
{ params? : params to override in stub file }
{ --f|force : Overwrite existing files without confirmation }
';

Expand All @@ -38,6 +39,7 @@ public function handle()
$path = $manager
->setType($this->argument('type'))
->setName($this->argument('name'))
->setParams($this->argument('params') ?? '')
->convertStubs($this->option('force'));

return $this->info('File created at ' . $path);
Expand Down
30 changes: 30 additions & 0 deletions src/StubsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class StubsManager

protected $name;

protected $params;

protected $type;

protected $stubs;
Expand All @@ -31,6 +33,28 @@ public function setName($name)
return $this;
}

public function setParams($params = '')
{
$array_params = [];

if (!empty($params)) {
$explode = explode('|', $params);
foreach ($explode as $row) {
$expl = explode(':', $row);
$array_params[$expl[0]] = $expl[1];
}
}

$this->params = $array_params;

return $this;
}

public function getParams()
{
return $this->params;
}

public function getType()
{
return $this->type;
Expand Down Expand Up @@ -89,6 +113,12 @@ public function convertStubs($forceOverwrite = false)

$newContent = str_replace($this->stub['placeholder'], $this->getName(), $stubContent);

if (!empty($this->getParams())) {
foreach ($this->getParams() as $key => $stub) {
$newContent = str_replace($key, $stub, $newContent);
}
}

if (!file_exists($this->stub['output_path'])) {
mkdir($this->stub['output_path'], 0755, true);
}
Expand Down
9 changes: 8 additions & 1 deletion src/config/template_stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@
'output_path' => config_path(),
],

];
/**
* Creates a basic test file
*/
'basic-test' => [
'stub' => 'basic-test.php',
'output_path' => base_path('tests'),
],
];
70 changes: 70 additions & 0 deletions src/stubs/basic-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Tests\{TEST}\Modules\{MODULE};

use App\Api\V1\Models\{MODEL};
use Tests\TestCase;

class {MODEL}Test extends TestCase
{

/** @test */
public function testIndex()
{
factory({MODEL}::class, 10)->create();
$response = $this->json('GET', 'api/auth/{URL}');
$expect = $response->decodeResponseJson();
$response
->assertOk()
->assertExactJson($expect);
}

/** @test */
public function testShow()
{
factory({MODEL}::class, 10)->create();
$random_id = random_int(1, 10);
$response = $this->json('GET', 'api/auth/{URL}/' . $random_id);
$expect = $response->decodeResponseJson();
$response
->assertOk()
->assertExactJson($expect);
}

/** @test */
public function testStore()
{
factory({MODEL}::class, 10)->create();
$data = ['name' => 'Category 1'];
$response = $this->json('POST', 'api/auth/{URL}', $data);
$expect = $response->decodeResponseJson();
$response
->assertOk()
->assertExactJson($expect);
}

/** @test */
public function testUpdate()
{
factory({MODEL}::class, 10)->create();
$random_id = random_int(1, 10);
$data = ['nome' => 'Category updated'];
$response = $this->json('PUT', 'api/auth/{URL}/' . $random_id, $data);
$expect = $response->decodeResponseJson();
$response
->assertOk()
->assertExactJson($expect);
}

/** @test */
public function testDelete()
{
factory({MODEL}::class, 10)->create();
$random_id = random_int(1, 10);
$response = $this->json('DELETE', 'api/auth/{URL}/' . $random_id);
$expect = $response->decodeResponseJson();
$response
->assertOk()
->assertExactJson($expect);
}
}

0 comments on commit d52007a

Please sign in to comment.