Skip to content

Commit 5af4575

Browse files
andreacivitabastianjoelrestyled-io[bot]
authored
Feat upgrade lv8 (#27)
* Change model folder name * Add request comments * Generate resource * Generate resource controller * CHange model namespace * Fix generated message * Create test in correct folder and namespace * Generate factory * Fix variable naming * Test refactoring * Fix factory path * Fix test template * Overwrite test instead append * Revert "Merge pull request #7 from bastianjoel/master" This reverts commit ecfaa98, reversing changes made to 275df6f. * Revert "Merge pull request #7 from bastianjoel/master" This reverts commit ecfaa98, reversing changes made to 275df6f. * Update README.md * Complete refactor (#12) * Create CONTRIBUTING.MD * Use laravel filesystem * Fix cs * Update README.md * Fix bastianjoel pr errors Fixing truble into pull request * switched to STR str_plural * Removed unused dependencies * Fix timestamp not working * Refactoring PHPDoc & Added Str dependency Refactored PHPDoc and added dependency injection for \Illuminate\Support\String * Some style fix * Refactoring ApiCrudGenerator class Refactoring ApiCrudGenerator class for keeping number of methods under 10. This should help in fixing. Added Stub Class (Helper for parse & get stub) Added Generator Class (Helper for generating files) * Missing str dependency * Updated readme * Fix readme authors * Moved core class into Core * Fixed dependencies * Added unit tests * Added stub test and refactor laravel helpers * Deleted laravel_helpers * Fix generator * Another fix * Added test for generator * Added travis support * Fix * Fix travis * Passport integration (#16) * Fix test & added test secure * Added passport support * Fix styling * Secure routes * Fix testSecureRoot * Codecov + travis * Delete CODE_OF_CONDUCT.md * Fixed deprecated touch() call * Added Dependency Injection Refactored file with dependency injection for DB & Schema Facades * Fix documentation * Testing fix * Fixed --all bug * Delete LICENSE * Merge fixing-all branch to dev (#24) * Fixed all method * Removed $db support instance * Update to laravel 8 * Update .travis.yml * Update * Upgrade dependencies * Laravel v8 supports Supports also for php8 && phpunit 8 Co-authored-by: Bastian Rihm <[email protected]> Co-authored-by: restyled-io[bot] <32688539+restyled-io[bot]@users.noreply.github.com>
1 parent d301cd8 commit 5af4575

File tree

14 files changed

+91
-66
lines changed

14 files changed

+91
-66
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
22
*.iml
33
vendor/
4-
composer.lock
4+
composer.lock
5+
.php_cs.cache

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
"api generator"
1111
],
1212
"require": {
13-
"php": "^7.1.3",
14-
"laravel/framework": "^5.7",
13+
"php": "^7.3|^8.0",
14+
"laravel/framework": "^8.0",
1515
"doctrine/dbal": "^2.9"
1616
},
1717
"require-dev": {
18-
"phpunit/phpunit": "^7"
18+
"phpunit/phpunit": "^7|^8",
19+
"friendsofphp/php-cs-fixer": "^2.16"
1920
},
2021
"license": "MIT",
2122
"authors": [

phpunit.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
<whitelist processUncoveredFilesFromWhitelist="true">
1010
<directory suffix=".php">./src</directory>
1111
<exclude>
12-
src/ApiCrudGeneratorServiceProvider.php
12+
<file>src/ApiCrudGeneratorServiceProvider.php</file>
1313
</exclude>
1414
<exclude>
15-
src/Commands/ApiCrudGenerator.php
15+
<file>src/Commands/ApiCrudGenerator.php</file>
1616
</exclude>
1717
</whitelist>
1818
</filter>
19-
</phpunit>
19+
</phpunit>

src/Commands/ApiCrudGenerator.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ public function handle()
9898
}
9999

100100
// Checking Passport mode
101-
if ($this->option('passport') == "")
101+
if ($this->option('passport') == "") {
102102
$this->passport = true;
103+
}
103104

104105
// If here, no interactive || all selected
105106
$name = ucwords($this->argument('name'));
@@ -125,7 +126,6 @@ protected function all()
125126
in_array('created_at', $columns) ? $timestamps = true : $timestamps = false;
126127
$this->generate($name, $table, $timestamps);
127128
}
128-
}
129129
catch (QueryException $exception) {
130130
$this->error("Error: " . $exception->getMessage());
131131
}
@@ -144,8 +144,9 @@ protected function interactive()
144144
$name = $this->ask('What is name of your Model?');
145145
$name = ucwords($name);
146146
$table = $this->ask("Table name [" . strtolower($this->str->plural($name)) . "]:");
147-
if ($table == "")
147+
if ($table == "") {
148148
$table = $this->str->plural($name);
149+
}
149150
$table = strtolower($table);
150151
$choice = $this->choice('Do your table has timestamps column?', ['No', 'Yes'], 0);
151152
$choice === "Yes" ? $timestamps = true : $timestamps = false;
@@ -180,15 +181,15 @@ protected function generate($name, $table, $timestamps)
180181
$this->info("Generated Request!");
181182
$this->generator->resource($name);
182183
$this->info("Generated Resource!");
183-
if ($this->passport)
184+
if ($this->passport) {
184185
$this->generator->secureRoutes($name);
185-
else
186+
} else {
186187
$this->generator->routes($name);
188+
}
187189
$this->info("Generated routes!");
188190
$this->generator->factory($name);
189191
$this->info("Generated Factory!");
190192
$this->generator->test($name);
191193
$this->info("Generated Test!");
192194
}
193-
194195
}

src/Core/Generator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Generator
2424

2525
/**
2626
* The Stub support instance
27-
*
27+
*
2828
* @var \AndreaCivita\ApiCrudGenerator\Core\Stub;
2929
*/
3030
protected $stub;
@@ -170,4 +170,4 @@ public function test($name)
170170
}
171171
return $this->files->append("tests/Feature/{$name}Test.php", $content);
172172
}
173-
}
173+
}

src/Core/Stub.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Stub
3535
* @param Filesystem $filesystem
3636
* @param Str $str
3737
*/
38-
public function __construct(Filesystem $filesystem,Str $str)
38+
public function __construct(Filesystem $filesystem, Str $str)
3939
{
4040
$this->files = $filesystem;
4141
$this->str = $str;
@@ -89,5 +89,4 @@ public function parseStub($stub, $name, $args = [])
8989
return "Stub not found";
9090
}
9191
}
92-
93-
}
92+
}

src/stubs/Factory.stub

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
<?php
22

3-
use Faker\Generator as Faker;
3+
use Illuminate\Database\Eloquent\Factories\Factory;
4+
use Illuminate\Support\Str;
45

5-
$factory->define(App\Models\{{modelName}}::class, function (Faker $faker) {
6-
return [
7-
//
8-
];
9-
});
6+
class {{modelName}}Factory extends Factory
7+
{
8+
/**
9+
* The name of the factory's corresponding model.
10+
*
11+
* @var string
12+
*/
13+
protected $model = \App\Models\{{modelName}}::class;
14+
15+
/**
16+
* Define the model's default state.
17+
*
18+
* @return array
19+
*/
20+
public function definition()
21+
{
22+
return [
23+
24+
];
25+
}
26+
}

src/stubs/Model.stub

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@
33
namespace App\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Factories\HasFactory;
67

78
class {{modelName}} extends Model
89
{
10+
use HasFactory;
11+
912
protected $guarded = ['id'];
1013

1114
protected $table = "{{tableDeclaration}}";
12-
{{timestamps}}
1315

14-
}
16+
/**
17+
* The attributes that are mass assignable.
18+
*
19+
* @var array
20+
*/
21+
protected $fillable = [
22+
23+
];
24+
25+
26+
}

src/stubs/Passport-Routes.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Route::::middleware('auth:api')->resource('{{modelNameSingularLowerCase}}', '{{modelName}}Controller', ['only' => [
1+
Route::::middleware('auth:api')->resource('{{modelNameSingularLowerCase}}', \App\Http\Controllers\{{modelName}}Controller::class, ['only' => [
22
'index', 'store', 'show', 'update', 'destroy'
33
]]);

src/stubs/Routes.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Route::resource('{{modelNameSingularLowerCase}}', '{{modelName}}Controller', ['only' => [
1+
Route::resource('{{modelNameSingularLowerCase}}', \App\Http\Controllers\{{modelName}}Controller::class, ['only' => [
22
'index', 'store', 'show', 'update', 'destroy'
33
]]);

src/stubs/Test.stub

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use App\Model\{{modelName}};
77

88
class {{modelName}}Test extends TestCase
99
{
10+
1011
/** @var \App\Models\{{modelName}} */
1112
protected ${{modelNameSingularLowerCase}};
1213

@@ -19,9 +20,7 @@ class {{modelName}}Test extends TestCase
1920
{
2021
parent::setUp();
2122

22-
$this->{{modelNameSingularLowerCase}} = factory(\App\Models\{{modelName}}::class)->create();
23-
24-
factory(\App\Models\{{modelName}}::class, 5)->create();
23+
$this->{{modelNameSingularLowerCase}} = \App\Models\{{modelName}}::factory()->make();
2524
}
2625

2726
/**
@@ -34,18 +33,18 @@ class {{modelName}}Test extends TestCase
3433
$this->getJson('/api/{{modelNameSingularLowerCase}}')
3534
->assertSuccessful();
3635
}
37-
36+
3837
/**
3938
* Show {{modelNameSingularLowerCase}}
4039
*
4140
* @return void
4241
*/
4342
public function testShow()
4443
{
45-
$this->getJson('/api/{{modelNameSingularLowerCase}}/'.$this->{{modelNameSingularLowerCase}}->id)
44+
$this->getJson('/api/{{modelNameSingularLowerCase}}/1')
4645
->assertSuccessful();
4746

48-
$this->getJson('/api/{{modelNameSingularLowerCase}}/9'.$this->{{modelNameSingularLowerCase}}->id)
47+
$this->getJson('/api/{{modelNameSingularLowerCase}}/0')
4948
->assertNotFound();
5049
}
5150

@@ -67,7 +66,7 @@ class {{modelName}}Test extends TestCase
6766
*/
6867
public function testUpdate()
6968
{
70-
$this->patchJson('/api/{{modelNameSingularLowerCase}}/'.$this->{{modelNameSingularLowerCase}}->id, $this->{{modelNameSingularLowerCase}}->toArray())
69+
$this->patchJson('/api/{{modelNameSingularLowerCase}}/1', $this->{{modelNameSingularLowerCase}}->toArray())
7170
->assertSuccessful();
7271
}
7372

@@ -78,9 +77,9 @@ class {{modelName}}Test extends TestCase
7877
*/
7978
public function testDestroy()
8079
{
81-
${{modelNameSingularLowerCase}} = factory(\App\Models\{{modelName}}::class)->create();
80+
$id = \App\Models\{{modelName}}::all()->last()->id;
8281

83-
$this->deleteJson('/api/{{modelNameSingularLowerCase}}/'.${{modelNameSingularLowerCase}}->id)
82+
$this->deleteJson('/api/{{modelNameSingularLowerCase}}/'.$id)
8483
->assertSuccessful();
8584
}
8685
}

tests/GeneratorTest.php

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
class GeneratorTest extends TestCase
77
{
8-
98
protected $name;
109
protected $generator;
1110
protected $files;
@@ -22,7 +21,6 @@ public function __construct()
2221

2322
public function setUp(): void
2423
{
25-
2624
parent::setUp();
2725
$this->name = "Car";
2826
$this->generator = new Generator(
@@ -33,63 +31,61 @@ public function setUp(): void
3331
new \Illuminate\Support\Str()
3432
)
3533
);
36-
3734
}
3835

39-
public function testModel()
36+
public function testModel() : void
4037
{
41-
$this->files->makeDirectory('app/Models',0777, true);
42-
$this->assertIsInt($this->generator->model($this->name,"cars",true));
43-
$this->assertIsInt($this->generator->model($this->name,"cars",false));
44-
38+
$this->files->makeDirectory('app/Models', 0777, true);
39+
$this->assertIsInt($this->generator->model($this->name, "cars", true));
40+
$this->assertIsInt($this->generator->model($this->name, "cars", false));
4541
}
4642

47-
public function testController()
43+
public function testController() : void
4844
{
49-
$this->files->makeDirectory('app/Http/Controllers',0777, true);
45+
$this->files->makeDirectory('app/Http/Controllers', 0777, true);
5046
$this->assertIsInt($this->generator->controller($this->name));
5147
}
5248

53-
public function testRequest()
49+
public function testRequest() : void
5450
{
55-
$this->files->makeDirectory('app/Http/Requests',0777, true);
51+
$this->files->makeDirectory('app/Http/Requests', 0777, true);
5652
$this->assertIsInt($this->generator->request($this->name));
5753
}
5854

59-
public function testResource()
55+
public function testResource() : void
6056
{
61-
$this->files->makeDirectory('app/Http/Resources',0777, true);
57+
$this->files->makeDirectory('app/Http/Resources', 0777, true);
6258
$this->assertIsInt($this->generator->resource($this->name));
6359
}
6460

65-
public function testFactory()
61+
public function testFactory() : void
6662
{
67-
$this->files->makeDirectory('database/factories',0777, true);
63+
$this->files->makeDirectory('database/factories', 0777, true);
6864
$this->assertIsInt($this->generator->factory($this->name));
6965
}
7066

71-
public function testRoutes()
67+
public function testRoutes() : void
7268
{
73-
$this->files->makeDirectory('routes',0777, true);
74-
$this->files->put('routes/api.php',"");
69+
$this->files->makeDirectory('routes', 0777, true);
70+
$this->files->put('routes/api.php', "");
7571
$this->assertIsInt($this->generator->routes($this->name));
7672
}
7773

78-
public function testSecureRoutes()
74+
public function testSecureRoutes() : void
7975
{
80-
$this->files->makeDirectory('routes',0777, true);
81-
$this->files->put('routes/api.php',"");
76+
$this->files->makeDirectory('routes', 0777, true);
77+
$this->files->put('routes/api.php', "");
8278
$this->assertIsInt($this->generator->secureRoutes($this->name));
8379
}
8480

85-
public function testMakeTests()
81+
public function testMakeTests(): void
8682
{
87-
$this->files->makeDirectory('tests/Feature',0777, true);
83+
$this->files->makeDirectory('tests/Feature', 0777, true);
8884
$this->assertIsInt($this->generator->test($this->name));
8985
}
9086

9187

92-
public function tearDown()
88+
public function tearDown(): void
9389
{
9490
$files = new Illuminate\Filesystem\Filesystem();
9591
$files->cleanDirectory('app');

tests/StubTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ public function testFailParseStub()
2828
try {
2929
$stub->parseStub("StubThatDoesNotExist", "Car");
3030
$this->fail("Stub not found.");
31-
}
32-
catch (Exception $ex) {
31+
} catch (Exception $ex) {
3332
$this->assertEquals($ex->getMessage(), "Stub not found.");
3433
}
3534
}
36-
}
35+
}

tests/Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ public function testOne()
88
{
99
$this->assertTrue(!false);
1010
}
11-
}
11+
}

0 commit comments

Comments
 (0)