Skip to content

Commit 2d87fbe

Browse files
committed
more tests with many error fixed
1 parent 4e90f68 commit 2d87fbe

File tree

14 files changed

+163
-27
lines changed

14 files changed

+163
-27
lines changed

README.md

+16-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,18 @@ Laravel Packer was created by, and is maintained by [Sarthak](https://github.com
2020

2121
## Features
2222

23-
- [All Artisan command while you create laravel package](#creating-new-package-scaffolding)
24-
- [Create CRUD for any model along with passing test cases ( feature and unit tests)](#crud-generator)
25-
- [Smart Clone - 4 steps in just 1 step](#smart-clone)
23+
- [Features](#features)
24+
- [Installation](#installation)
25+
- [Creating new Package Scaffolding](#creating-new-package-scaffolding)
26+
- [Same as Artisan commands](#same-as-artisan-commands)
27+
- [Smart Clone](#smart-clone)
28+
- [Specify directory to clone](#specify-directory-to-clone)
29+
- [Specify branch to clone](#specify-branch-to-clone)
30+
- [CRUD Generator](#crud-generator)
31+
- [step 1](#step-1)
32+
- [step 2](#step-2)
33+
- [Todo](#todo)
34+
- [License](#license)
2635

2736
## Installation
2837

@@ -124,6 +133,10 @@ After giving all details you can now run command to actually create full crud fo
124133
packr crud:make {relativePathOfThatJsonFile}
125134
```
126135

136+
### Todo
137+
- [ ] Add resource in controller for CRUD maker
138+
- [ ] Add form request in controller for CRUD maker
139+
127140
## License
128141

129142
This package inherits the licensing of its parent framework, Laravel, and as such is open-sourced

app/Commands/Crud/AddRouteCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function addLine($path)
5353
{
5454
$model = $this->argument('name');
5555
$resourceType = $this->option('api') == 'api' ? 'apiResource' : 'resource';
56-
$routes = "\nRoute::{$resourceType}('" . strToLower($model) . "','" . Str::studly($model) . "Controller');";
56+
$routes = "\nRoute::{$resourceType}('" . strToLower($model) . "','" . Str::studly($model) . 'Controller::class);';
5757
file_put_contents($path, $routes, FILE_APPEND);
5858
}
5959

app/Commands/Crud/Factories/FactoryMakeCommand.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ protected function getStub()
4949
*/
5050
protected function buildClass($name)
5151
{
52+
$this->addFakerData();
53+
5254
$factory = class_basename(Str::ucfirst(str_replace('Factory', '', $name)));
5355

5456
$namespaceModel = $this->option('model')
@@ -57,11 +59,7 @@ protected function buildClass($name)
5759

5860
$model = class_basename($namespaceModel);
5961

60-
if (Str::startsWith($namespaceModel, $this->rootNamespace() . 'Models')) {
61-
$namespace = Str::beforeLast('Database\\Factories\\' . Str::after($namespaceModel, $this->rootNamespace() . 'Models\\'), '\\');
62-
} else {
63-
$namespace = $this->rootNamespace() . 'Database\\Factories';
64-
}
62+
$namespace = $this->rootNamespace() . 'Database\\Factories';
6563

6664
$replace = [
6765
'{{ factoryNamespace }}' => $namespace,
@@ -71,6 +69,7 @@ protected function buildClass($name)
7169
'DummyModel' => $model,
7270
'{{ model }}' => $model,
7371
'{{model}}' => $model,
72+
'//' => $this->addFakerData(),
7473
];
7574

7675
return str_replace(
@@ -106,20 +105,21 @@ protected function getOptions()
106105
];
107106
}
108107

109-
public function addFakerData($factory)
108+
public function addFakerData()
110109
{
111110
$fields = cache()->get('structure')->fields;
112111
$newLines = '';
113112
foreach ($fields as $field) {
114113
foreach ($this->fakerType() as $key => $value) {
115114
// dump($field->type);
116115
if ($key == $field->type) {
117-
$newLines .= "'$field->name'" . ' => $faker->' . $value . ',
116+
$newLines .= "'$field->name'" . ' => $this->faker->' . $value . ',
118117
';
119118
}
120119
}
121120
}
122-
return str_replace('//', $newLines, $factory);
121+
122+
return $newLines;
123123
}
124124

125125
protected function fakerType()

app/Commands/Crud/ModelMakeCommand.php

+25-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ protected function buildClass($name)
7575

7676
$stub = $this->addMoreTo($stub);
7777

78+
$stub = $this->replaceModelNamespace($stub);
79+
7880
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
7981
}
8082

@@ -89,7 +91,7 @@ protected function createFactory()
8991

9092
$this->call('crud:factory', [
9193
'name' => "{$factory}Factory",
92-
'--model' => $this->qualifyClass($this->getNameInput()),
94+
'--model' => $this->qualifyModel($this->getNameInput()),
9395
]);
9496
}
9597

@@ -210,7 +212,28 @@ public function getPath($name)
210212
$content = $this->getComposer();
211213
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
212214
$path = getcwd() . $this->devPath();
213-
$path = $content->type === 'project' ? $path . '/app/' : $path . '/src/';
215+
$path = $content->type === 'project' ? $path . '/app/Models/' : $path . '/src/Models/';
214216
return $path . str_replace('\\', '/', $name) . '.php';
215217
}
218+
219+
public function replaceModelNamespace($stub)
220+
{
221+
return str_replace('DummyNamespace', $this->rootNamespace() . 'Models', $stub);
222+
}
223+
224+
protected function qualifyModel(string $model)
225+
{
226+
$model = ltrim($model, '\\/');
227+
228+
$model = str_replace('/', '\\', $model);
229+
230+
$rootNamespace = $this->rootNamespace();
231+
232+
if (Str::startsWith($model, $rootNamespace)) {
233+
return $model;
234+
}
235+
return is_dir($this->projectPath() . 'Models')
236+
? $rootNamespace . 'Models\\' . $model
237+
: $rootNamespace . $model;
238+
}
216239
}

app/Commands/Crud/TestMakeCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected function replaceNamespace(&$stub, $name)
111111
ucfirst($model),
112112
$this->getNamespace($name),
113113
$this->rootNamespace(),
114-
$this->namespaceFromComposer() . '' . $model,
114+
$this->namespaceFromComposer() . 'Models\\' . $model,
115115
cache()->get('structure')->fields[0]->name,
116116
Str::plural(Str::snake($model)),
117117
$this->userProviderModel(),

app/Commands/Crud/stubs/model.stub

+3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace DummyNamespace;
44

5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
56
use Illuminate\Database\Eloquent\Model;
67

78
class DummyClass extends Model
89
{
10+
use HasFactory;
11+
912
//
1013
}

app/Commands/Foundation/ModelMakeCommand.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,28 @@ public function getPath($name)
2828
$content = $this->getComposer();
2929
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
3030
$path = getcwd() . $this->devPath();
31-
$path = $content->type === 'project' ? $path . '/app/' : $path . '/src/';
31+
$path = $content->type === 'project' ? $path . '/app/Models/' : $path . '/src/Models/';
3232
return $path . str_replace('\\', '/', $name) . '.php';
3333
}
34+
35+
/**
36+
* Build the class with the given name.
37+
*
38+
* @param string $name
39+
* @return string
40+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
41+
*/
42+
protected function buildClass($name)
43+
{
44+
$stub = $this->files->get($this->getStub());
45+
46+
$stub = $this->replaceModelNamespace($stub);
47+
48+
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
49+
}
50+
51+
public function replaceModelNamespace($stub)
52+
{
53+
return str_replace('{{ namespace }}', $this->rootNamespace() . 'Models', $stub);
54+
}
3455
}

app/Commands/Foundation/stubs/model.stub

+3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace DummyNamespace;
44

5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
56
use Illuminate\Database\Eloquent\Model;
67

78
class DummyClass extends Model
89
{
10+
use HasFactory;
11+
912
//
1013
}

app/Commands/Helpers/PackageDetail.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ public function devPath()
4949
public function getPath($name)
5050
{
5151
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
52+
return $this->projectPath() . str_replace('\\', '/', $name) . '.php';
53+
}
54+
55+
public function projectPath()
56+
{
5257
$path = getcwd() . $this->devPath();
53-
$path = $this->getComposer()->type !== 'project' ? $path . 'src/' : $path;
54-
return $path . str_replace('\\', '/', $name) . '.php';
58+
return $this->getComposer()->type !== 'project' ? $path . 'src/' : $path;
5559
}
5660
}

builds/packr

239 Bytes
Binary file not shown.

config/app.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
|
2626
*/
2727

28-
'version' => 'v4.7.4',
28+
'version' => 'v4.7.5',
2929

3030
/*
3131
|--------------------------------------------------------------------------

tests/Feature/Crud/CrudMakeTest.php

+28-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Feature;
3+
namespace Tests\Feature\Crud;
44

55
use Tests\TestCase;
66
use Tests\RefreshPacker;
@@ -20,7 +20,7 @@ public function test_it_can_create_a_crud_for_a_json_file()
2020
{
2121
Artisan::call('crud:json Test');
2222
Artisan::call('crud:make Test');
23-
$this->isFileExists('src/Test.php');
23+
$this->isFileExists('src/Models/Test.php');
2424
$this->isFileExists('src/database/factories/TestFactory.php');
2525
$this->assertEquals(1, count(glob($this->_testPath() . 'src/database/migrations/*_create_tests_table.php')));
2626
$this->isFileExists('src/Http/controllers/TestController.php');
@@ -30,13 +30,14 @@ public function test_it_can_create_a_crud_for_a_json_file()
3030

3131
public function test_it_create_class_based_factory()
3232
{
33-
Artisan::call('crud:json Test');
34-
Artisan::call('crud:make Test');
35-
$this->isFileExists('src/database/factories/TestFactory.php');
36-
$content = file_get_contents($this->_testPath() . 'src/database/factories/TestFactory.php');
37-
$this->assertStringContainsString('protected $model = Test::class;', $content);
38-
$this->assertStringContainsString('use Bitfumes\TestApp\Test;', $content);
33+
Artisan::call('crud:json User');
34+
Artisan::call('crud:make User');
35+
$this->isFileExists('src/database/factories/UserFactory.php');
36+
$content = file_get_contents($this->_testPath() . 'src/database/factories/UserFactory.php');
37+
$this->assertStringContainsString('protected $model = User::class;', $content);
38+
$this->assertStringContainsString('use Bitfumes\TestApp\Models\User;', $content);
3939
$this->assertStringContainsString('namespace Bitfumes\TestApp\Database\Factories;', $content);
40+
$this->assertStringContainsString('\'title\' => $this->faker->word', $content);
4041
}
4142

4243
public function test_it_create_tests_properly()
@@ -46,6 +47,25 @@ public function test_it_create_tests_properly()
4647
$this->isFileExists('/tests/Feature/UserTest.php');
4748
$content = file_get_contents($this->_testPath() . '/tests/Feature/UserTest.php');
4849
$this->assertStringContainsString('class UserTest', $content);
50+
$this->assertStringContainsString('use Bitfumes\TestApp\Models\User;', $content);
4951
$this->assertStringContainsString('User::factory()->count($num)->create($args);', $content);;
5052
}
53+
54+
public function test_it_create_model_on_models_dir_with_correct_namespace()
55+
{
56+
Artisan::call('crud:json User');
57+
Artisan::call('crud:make User');
58+
$this->isFileExists('src/models/User.php');
59+
$content = file_get_contents($this->_testPath() . 'src/models/User.php');
60+
$this->assertStringContainsString('class User extends Model', $content);
61+
$this->assertStringContainsString('namespace Bitfumes\TestApp\Models;', $content);
62+
}
63+
64+
public function test_it_add_resource_route()
65+
{
66+
Artisan::call('crud:json User');
67+
Artisan::call('crud:make User');
68+
$content = file_get_contents($this->_testPath() . 'src/Http/routes.php');
69+
$this->assertStringContainsString("Route::apiResource('user','UserController::class);", $content);
70+
}
5171
}

tests/Feature/Make/ModelTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Tests\Feature\Make;
4+
5+
use Tests\TestCase;
6+
use Tests\RefreshPacker;
7+
use Illuminate\Support\Facades\Artisan;
8+
9+
class ModelTest extends TestCase
10+
{
11+
use RefreshPacker;
12+
13+
public function test_it_create_model_file()
14+
{
15+
Artisan::call('make:model User');
16+
$this->isFileExists('src/models/User.php');
17+
$content = file_get_contents($this->_testPath() . 'src/models/User.php');
18+
$this->assertStringContainsString('namespace Bitfumes\TestApp\Models;', $content);
19+
$this->assertStringContainsString('class User extends Model', $content);
20+
}
21+
}

tests/Feature/Make/TestsTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Tests\Feature\Make;
4+
5+
use Tests\TestCase;
6+
use Tests\RefreshPacker;
7+
use Illuminate\Support\Facades\Artisan;
8+
9+
class TestsTest extends TestCase
10+
{
11+
use RefreshPacker;
12+
13+
public function test_it_create_feature_test_file()
14+
{
15+
Artisan::call('make:test UserTest');
16+
$this->isFileExists('tests/feature/UserTest.php');
17+
$content = file_get_contents($this->_testPath() . 'tests/feature/UserTest.php');
18+
$this->assertStringContainsString('class UserTest extends TestCase', $content);
19+
}
20+
21+
public function test_it_create_unit_test_file()
22+
{
23+
Artisan::call('make:test UserTest --unit');
24+
$this->isFileExists('tests/unit/UserTest.php');
25+
$content = file_get_contents($this->_testPath() . 'tests/unit/UserTest.php');
26+
$this->assertStringContainsString('class UserTest extends TestCase', $content);
27+
}
28+
}

0 commit comments

Comments
 (0)