Skip to content

Commit

Permalink
more tests with many error fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthaksavvy committed Oct 3, 2021
1 parent 4e90f68 commit 2d87fbe
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 27 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@ Laravel Packer was created by, and is maintained by [Sarthak](https://github.com

## Features

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

## Installation

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

### Todo
- [ ] Add resource in controller for CRUD maker
- [ ] Add form request in controller for CRUD maker

## License

This package inherits the licensing of its parent framework, Laravel, and as such is open-sourced
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Crud/AddRouteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function addLine($path)
{
$model = $this->argument('name');
$resourceType = $this->option('api') == 'api' ? 'apiResource' : 'resource';
$routes = "\nRoute::{$resourceType}('" . strToLower($model) . "','" . Str::studly($model) . "Controller');";
$routes = "\nRoute::{$resourceType}('" . strToLower($model) . "','" . Str::studly($model) . 'Controller::class);';
file_put_contents($path, $routes, FILE_APPEND);
}

Expand Down
16 changes: 8 additions & 8 deletions app/Commands/Crud/Factories/FactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ protected function getStub()
*/
protected function buildClass($name)
{
$this->addFakerData();

$factory = class_basename(Str::ucfirst(str_replace('Factory', '', $name)));

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

$model = class_basename($namespaceModel);

if (Str::startsWith($namespaceModel, $this->rootNamespace() . 'Models')) {
$namespace = Str::beforeLast('Database\\Factories\\' . Str::after($namespaceModel, $this->rootNamespace() . 'Models\\'), '\\');
} else {
$namespace = $this->rootNamespace() . 'Database\\Factories';
}
$namespace = $this->rootNamespace() . 'Database\\Factories';

$replace = [
'{{ factoryNamespace }}' => $namespace,
Expand All @@ -71,6 +69,7 @@ protected function buildClass($name)
'DummyModel' => $model,
'{{ model }}' => $model,
'{{model}}' => $model,
'//' => $this->addFakerData(),
];

return str_replace(
Expand Down Expand Up @@ -106,20 +105,21 @@ protected function getOptions()
];
}

public function addFakerData($factory)
public function addFakerData()
{
$fields = cache()->get('structure')->fields;
$newLines = '';
foreach ($fields as $field) {
foreach ($this->fakerType() as $key => $value) {
// dump($field->type);
if ($key == $field->type) {
$newLines .= "'$field->name'" . ' => $faker->' . $value . ',
$newLines .= "'$field->name'" . ' => $this->faker->' . $value . ',
';
}
}
}
return str_replace('//', $newLines, $factory);

return $newLines;
}

protected function fakerType()
Expand Down
27 changes: 25 additions & 2 deletions app/Commands/Crud/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ protected function buildClass($name)

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

$stub = $this->replaceModelNamespace($stub);

return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
}

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

$this->call('crud:factory', [
'name' => "{$factory}Factory",
'--model' => $this->qualifyClass($this->getNameInput()),
'--model' => $this->qualifyModel($this->getNameInput()),
]);
}

Expand Down Expand Up @@ -210,7 +212,28 @@ public function getPath($name)
$content = $this->getComposer();
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
$path = getcwd() . $this->devPath();
$path = $content->type === 'project' ? $path . '/app/' : $path . '/src/';
$path = $content->type === 'project' ? $path . '/app/Models/' : $path . '/src/Models/';
return $path . str_replace('\\', '/', $name) . '.php';
}

public function replaceModelNamespace($stub)
{
return str_replace('DummyNamespace', $this->rootNamespace() . 'Models', $stub);
}

protected function qualifyModel(string $model)
{
$model = ltrim($model, '\\/');

$model = str_replace('/', '\\', $model);

$rootNamespace = $this->rootNamespace();

if (Str::startsWith($model, $rootNamespace)) {
return $model;
}
return is_dir($this->projectPath() . 'Models')
? $rootNamespace . 'Models\\' . $model
: $rootNamespace . $model;
}
}
2 changes: 1 addition & 1 deletion app/Commands/Crud/TestMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected function replaceNamespace(&$stub, $name)
ucfirst($model),
$this->getNamespace($name),
$this->rootNamespace(),
$this->namespaceFromComposer() . '' . $model,
$this->namespaceFromComposer() . 'Models\\' . $model,
cache()->get('structure')->fields[0]->name,
Str::plural(Str::snake($model)),
$this->userProviderModel(),
Expand Down
3 changes: 3 additions & 0 deletions app/Commands/Crud/stubs/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace DummyNamespace;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DummyClass extends Model
{
use HasFactory;

//
}
23 changes: 22 additions & 1 deletion app/Commands/Foundation/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,28 @@ public function getPath($name)
$content = $this->getComposer();
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
$path = getcwd() . $this->devPath();
$path = $content->type === 'project' ? $path . '/app/' : $path . '/src/';
$path = $content->type === 'project' ? $path . '/app/Models/' : $path . '/src/Models/';
return $path . str_replace('\\', '/', $name) . '.php';
}

/**
* Build the class with the given name.
*
* @param string $name
* @return string
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function buildClass($name)
{
$stub = $this->files->get($this->getStub());

$stub = $this->replaceModelNamespace($stub);

return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
}

public function replaceModelNamespace($stub)
{
return str_replace('{{ namespace }}', $this->rootNamespace() . 'Models', $stub);
}
}
3 changes: 3 additions & 0 deletions app/Commands/Foundation/stubs/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace DummyNamespace;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DummyClass extends Model
{
use HasFactory;

//
}
8 changes: 6 additions & 2 deletions app/Commands/Helpers/PackageDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ public function devPath()
public function getPath($name)
{
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
return $this->projectPath() . str_replace('\\', '/', $name) . '.php';
}

public function projectPath()
{
$path = getcwd() . $this->devPath();
$path = $this->getComposer()->type !== 'project' ? $path . 'src/' : $path;
return $path . str_replace('\\', '/', $name) . '.php';
return $this->getComposer()->type !== 'project' ? $path . 'src/' : $path;
}
}
Binary file modified builds/packr
Binary file not shown.
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
|
*/

'version' => 'v4.7.4',
'version' => 'v4.7.5',

/*
|--------------------------------------------------------------------------
Expand Down
36 changes: 28 additions & 8 deletions tests/Feature/Crud/CrudMakeTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\Crud;

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

public function test_it_create_class_based_factory()
{
Artisan::call('crud:json Test');
Artisan::call('crud:make Test');
$this->isFileExists('src/database/factories/TestFactory.php');
$content = file_get_contents($this->_testPath() . 'src/database/factories/TestFactory.php');
$this->assertStringContainsString('protected $model = Test::class;', $content);
$this->assertStringContainsString('use Bitfumes\TestApp\Test;', $content);
Artisan::call('crud:json User');
Artisan::call('crud:make User');
$this->isFileExists('src/database/factories/UserFactory.php');
$content = file_get_contents($this->_testPath() . 'src/database/factories/UserFactory.php');
$this->assertStringContainsString('protected $model = User::class;', $content);
$this->assertStringContainsString('use Bitfumes\TestApp\Models\User;', $content);
$this->assertStringContainsString('namespace Bitfumes\TestApp\Database\Factories;', $content);
$this->assertStringContainsString('\'title\' => $this->faker->word', $content);
}

public function test_it_create_tests_properly()
Expand All @@ -46,6 +47,25 @@ public function test_it_create_tests_properly()
$this->isFileExists('/tests/Feature/UserTest.php');
$content = file_get_contents($this->_testPath() . '/tests/Feature/UserTest.php');
$this->assertStringContainsString('class UserTest', $content);
$this->assertStringContainsString('use Bitfumes\TestApp\Models\User;', $content);
$this->assertStringContainsString('User::factory()->count($num)->create($args);', $content);;
}

public function test_it_create_model_on_models_dir_with_correct_namespace()
{
Artisan::call('crud:json User');
Artisan::call('crud:make User');
$this->isFileExists('src/models/User.php');
$content = file_get_contents($this->_testPath() . 'src/models/User.php');
$this->assertStringContainsString('class User extends Model', $content);
$this->assertStringContainsString('namespace Bitfumes\TestApp\Models;', $content);
}

public function test_it_add_resource_route()
{
Artisan::call('crud:json User');
Artisan::call('crud:make User');
$content = file_get_contents($this->_testPath() . 'src/Http/routes.php');
$this->assertStringContainsString("Route::apiResource('user','UserController::class);", $content);
}
}
21 changes: 21 additions & 0 deletions tests/Feature/Make/ModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tests\Feature\Make;

use Tests\TestCase;
use Tests\RefreshPacker;
use Illuminate\Support\Facades\Artisan;

class ModelTest extends TestCase
{
use RefreshPacker;

public function test_it_create_model_file()
{
Artisan::call('make:model User');
$this->isFileExists('src/models/User.php');
$content = file_get_contents($this->_testPath() . 'src/models/User.php');
$this->assertStringContainsString('namespace Bitfumes\TestApp\Models;', $content);
$this->assertStringContainsString('class User extends Model', $content);
}
}
28 changes: 28 additions & 0 deletions tests/Feature/Make/TestsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tests\Feature\Make;

use Tests\TestCase;
use Tests\RefreshPacker;
use Illuminate\Support\Facades\Artisan;

class TestsTest extends TestCase
{
use RefreshPacker;

public function test_it_create_feature_test_file()
{
Artisan::call('make:test UserTest');
$this->isFileExists('tests/feature/UserTest.php');
$content = file_get_contents($this->_testPath() . 'tests/feature/UserTest.php');
$this->assertStringContainsString('class UserTest extends TestCase', $content);
}

public function test_it_create_unit_test_file()
{
Artisan::call('make:test UserTest --unit');
$this->isFileExists('tests/unit/UserTest.php');
$content = file_get_contents($this->_testPath() . 'tests/unit/UserTest.php');
$this->assertStringContainsString('class UserTest extends TestCase', $content);
}
}

0 comments on commit 2d87fbe

Please sign in to comment.