diff --git a/src/StubsCommand.php b/src/StubsCommand.php index e5a0daa..7d35f04 100644 --- a/src/StubsCommand.php +++ b/src/StubsCommand.php @@ -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 } '; @@ -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); diff --git a/src/StubsManager.php b/src/StubsManager.php index b891312..5fd925e 100644 --- a/src/StubsManager.php +++ b/src/StubsManager.php @@ -10,6 +10,8 @@ class StubsManager protected $name; + protected $params; + protected $type; protected $stubs; @@ -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; @@ -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); } diff --git a/src/config/template_stubs.php b/src/config/template_stubs.php index 0c41c8d..f161b05 100644 --- a/src/config/template_stubs.php +++ b/src/config/template_stubs.php @@ -58,4 +58,11 @@ 'output_path' => config_path(), ], -]; \ No newline at end of file + /** + * Creates a basic test file + */ + 'basic-test' => [ + 'stub' => 'basic-test.php', + 'output_path' => base_path('tests'), + ], +]; diff --git a/src/stubs/basic-test.php b/src/stubs/basic-test.php new file mode 100644 index 0000000..7eeb3b6 --- /dev/null +++ b/src/stubs/basic-test.php @@ -0,0 +1,70 @@ +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); + } +}