Skip to content

Commit cb73ca7

Browse files
committed
feat: tests completed with addon refactoring
1 parent d8ef986 commit cb73ca7

21 files changed

+246
-83
lines changed

config/laravel_generator.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@
146146

147147
'seeder' => false,
148148

149+
'swagger' => false, // generate swagger for your APIs
150+
151+
'tests' => false, // generate test cases for your APIs
152+
149153
'excluded_fields' => ['id'], // Array of columns that doesn't required while creating module
150154
],
151155

@@ -180,20 +184,6 @@
180184

181185
'tables' => 'blade',
182186

183-
/*
184-
|--------------------------------------------------------------------------
185-
| Add-Ons
186-
|--------------------------------------------------------------------------
187-
|
188-
*/
189-
190-
'add_ons' => [
191-
192-
'swagger' => false, // generate swagger for your APIs
193-
194-
'tests' => false, // generate test cases for your APIs
195-
],
196-
197187
/*
198188
|--------------------------------------------------------------------------
199189
| Timestamp Fields

src/Commands/BaseCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function generateCommonItems()
7272
$repositoryGenerator->generate();
7373
}
7474

75-
if ($this->config->options->factory || (!$this->isSkip('tests') and $this->config->addons->tests)) {
75+
if ($this->config->options->factory || (!$this->isSkip('tests') and $this->config->options->tests)) {
7676
$factoryGenerator = app(FactoryGenerator::class);
7777
$factoryGenerator->generate();
7878
}
@@ -100,7 +100,7 @@ public function generateAPIItems()
100100
$routesGenerator->generate();
101101
}
102102

103-
if (!$this->isSkip('tests') and $this->config->addons->tests) {
103+
if (!$this->isSkip('tests') and $this->config->options->tests) {
104104
if ($this->config->options->repositoryPattern) {
105105
$repositoryTestGenerator = app(RepositoryTestGenerator::class);
106106
$repositoryTestGenerator->generate();

src/Commands/Publish/GeneratorPublishCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ private function publishTestCases()
7272
$createdAtField = config('laravel_generator.timestamps.created_at', 'created_at');
7373
$updatedAtField = config('laravel_generator.timestamps.updated_at', 'updated_at');
7474

75-
$templateData = get_template('test.api_test_trait', 'laravel-generator');
76-
77-
$templateData = str_replace('$NAMESPACE_TESTS$', $testsNameSpace, $templateData);
78-
$templateData = str_replace('$TIMESTAMPS$', "['$createdAtField', '$updatedAtField']", $templateData);
75+
$templateData = view('laravel-generator::api.tet.api_test_trait', [
76+
'timestamps' => "['$createdAtField', '$updatedAtField']",
77+
'namespacesTests' => $testsNameSpace
78+
])->render();
7979

8080
$fileName = 'ApiTestTrait.php';
8181

src/Commands/RollbackGeneratorCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ public function handle()
9494
$menuGenerator->rollback();
9595
}
9696

97-
if ($this->config->addons->tests) {
97+
if ($this->config->options->tests) {
9898
$repositoryTestGenerator = app(RepositoryTestGenerator::class);
9999
$repositoryTestGenerator->rollback();
100100

101101
$apiTestGenerator = app(APITestGenerator::class);
102102
$apiTestGenerator->rollback();
103103
}
104104

105-
if ($this->config->options->factory or $this->config->addons->tests) {
105+
if ($this->config->options->factory or $this->config->options->tests) {
106106
$factoryGenerator = app(FactoryGenerator::class);
107107
$factoryGenerator->rollback();
108108
}

src/Common/GeneratorConfig.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class GeneratorConfig
1717
public GeneratorPaths $paths;
1818
public ModelNames $modelNames;
1919
public GeneratorPrefixes $prefixes;
20-
public GeneratorAddons $addons;
2120
public GeneratorOptions $options;
2221
public Command $command;
2322

@@ -31,6 +30,7 @@ class GeneratorConfig
3130

3231
public $tableName;
3332
public string $tableType;
33+
public string $apiPrefix;
3434
public $primaryName;
3535
public $connection;
3636

@@ -40,9 +40,9 @@ public function init()
4040
$this->loadPrefixes();
4141
$this->loadPaths();
4242
$this->tableType = config('laravel_generator.tables', 'blade');
43+
$this->apiPrefix = config('laravel_generator.api_prefix', 'api');
4344
$this->loadNamespaces();
4445
$this->prepareTable();
45-
$this->prepareAddons();
4646
$this->prepareOptions();
4747
}
4848

@@ -259,20 +259,13 @@ public function prepareOptions()
259259
$options->resources = config('laravel_generator.options.resources', false);
260260
$options->factory = config('laravel_generator.options.factory', false);
261261
$options->seeder = config('laravel_generator.options.seeder', false);
262+
$options->swagger = config('laravel_generator.options.swagger', false);
263+
$options->tests = config('laravel_generator.options.tests', false);
262264
$options->excludedFields = config('laravel_generator.options.excluded_fields', ['id']);
263265

264266
$this->options = $options;
265267
}
266268

267-
public function prepareAddons()
268-
{
269-
$addons = new GeneratorAddons();
270-
$addons->swagger = config('laravel_generator.add_ons.swagger', false);
271-
$addons->tests = config('laravel_generator.add_ons.tests', false);
272-
273-
$this->addons = $addons;
274-
}
275-
276269
public function overrideOptionsFromJsonFile($jsonData)
277270
{
278271
// $options = self::$availableOptions;

src/DTOs/GeneratorAddons.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/DTOs/GeneratorOptions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ class GeneratorOptions
1111
public bool $resources;
1212
public bool $factory;
1313
public bool $seeder;
14+
public bool $swagger;
15+
public bool $tests;
1416
public array $excludedFields;
1517
}

src/Generators/API/APIControllerGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private function fillDocs(): array
5353
{
5454
$methods = ['controller', 'index', 'store', 'show', 'update', 'destroy'];
5555

56-
if ($this->config->addons->swagger) {
56+
if ($this->config->options->swagger) {
5757
$templatePrefix = 'controller_docs';
5858
$templateType = 'swagger-generator';
5959
} else {

src/Generators/API/APIRoutesGenerator.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,41 @@
77

88
class APIRoutesGenerator extends BaseGenerator
99
{
10-
private string $routeContents;
11-
12-
private string $routesTemplate;
13-
1410
public function __construct()
1511
{
1612
parent::__construct();
1713

1814
$this->path = $this->config->paths->apiRoutes;
19-
20-
$this->routeContents = g_filesystem()->getFile($this->path);
21-
22-
if (!empty($this->config->prefixes->route)) {
23-
$routesTemplate = get_template('api.routes.prefix_routes', 'laravel-generator');
24-
} else {
25-
$routesTemplate = get_template('api.routes.routes', 'laravel-generator');
26-
}
27-
28-
$this->routesTemplate = fill_template($this->config->dynamicVars, $routesTemplate);
2915
}
3016

3117
public function generate()
3218
{
33-
$this->routeContents .= PHP_EOL.PHP_EOL.$this->routesTemplate;
34-
$existingRouteContents = g_filesystem()->getFile($this->path);
35-
if (Str::contains($existingRouteContents, "Route::resource('".$this->config->modelNames->dashedPlural."',")) {
19+
$routeContents = g_filesystem()->getFile($this->path);
20+
21+
$routes = view('laravel-generator::api.routes', $this->variables())->render();
22+
23+
if (Str::contains($routeContents, $routes)) {
3624
$this->config->commandInfo(PHP_EOL.'Menu '.$this->config->modelNames->dashedPlural.' already exists, Skipping Adjustment.');
3725

3826
return;
3927
}
4028

41-
g_filesystem()->createFile($this->path, $this->routeContents);
29+
$routeContents .= PHP_EOL.PHP_EOL.$routes;
30+
31+
g_filesystem()->createFile($this->path, $routeContents);
4232

4333
$this->config->commandComment(PHP_EOL.$this->config->modelNames->dashedPlural.' api routes added.');
4434
}
4535

4636
public function rollback()
4737
{
48-
if (Str::contains($this->routeContents, $this->routesTemplate)) {
49-
$this->routeContents = str_replace($this->routesTemplate, '', $this->routeContents);
50-
g_filesystem()->createFile($this->path, $this->routeContents);
38+
$routeContents = g_filesystem()->getFile($this->path);
39+
40+
$routes = view('laravel-generator::api.routes', $this->variables())->render();
41+
42+
if (Str::contains($routeContents, $routes)) {
43+
$routeContents = str_replace($routeContents, '', $routes);
44+
g_filesystem()->createFile($this->path, $routeContents);
5145
$this->config->commandComment('api routes deleted');
5246
}
5347
}

src/Generators/API/APITestGenerator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ public function __construct()
1818

1919
public function generate()
2020
{
21-
$templateData = get_template('api.test.api_test', 'laravel-generator');
22-
23-
$templateData = fill_template($this->config->dynamicVars, $templateData);
21+
$templateData = view('laravel-generator::api.test.api_test', $this->variables())->render();
2422

2523
g_filesystem()->createFile($this->path.$this->fileName, $templateData);
2624

0 commit comments

Comments
 (0)