Skip to content

Commit 4e90f68

Browse files
committed
more tests
1 parent 5b5c556 commit 4e90f68

File tree

15 files changed

+255
-60
lines changed

15 files changed

+255
-60
lines changed

app/Commands/Crud/Factories/FactoryMakeCommand.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,34 @@ protected function getStub()
4949
*/
5050
protected function buildClass($name)
5151
{
52-
$model = $this->option('model')
53-
? $this->qualifyClass($this->option('model'))
54-
: 'Model';
52+
$factory = class_basename(Str::ucfirst(str_replace('Factory', '', $name)));
5553

56-
$factory = parent::buildClass($name);
54+
$namespaceModel = $this->option('model')
55+
? $this->qualifyModel($this->option('model'))
56+
: $this->qualifyModel($this->guessModelName($name));
5757

58-
$factory = $this->addFakerData($factory);
58+
$model = class_basename($namespaceModel);
59+
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+
}
65+
66+
$replace = [
67+
'{{ factoryNamespace }}' => $namespace,
68+
'NamespacedDummyModel' => $namespaceModel,
69+
'{{ namespacedModel }}' => $namespaceModel,
70+
'{{namespacedModel}}' => $namespaceModel,
71+
'DummyModel' => $model,
72+
'{{ model }}' => $model,
73+
'{{model}}' => $model,
74+
];
5975

6076
return str_replace(
61-
'DummyModel',
62-
$model,
63-
$factory
77+
array_keys($replace),
78+
array_values($replace),
79+
parent::buildClass($name)
6480
);
6581
}
6682

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

3-
use Faker\Generator as Faker;
3+
namespace {{ factoryNamespace }};
44

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

app/Commands/Crud/stubs/tests/test-api.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class DummyClass extends TestCase
1212

1313
public function create_DummyModelName($args = [], $num = null)
1414
{
15-
return DummyModelClass::factory(), $num)->create($args);
15+
return DummyModelClass::factory()->count($num)->create($args);
1616
}
1717

1818
/** @test */

app/Commands/Foundation/ChannelMakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace App\Commands\Foundation;
44

5-
use Illuminate\Foundation\Console\ChannelMakeCommand as ChannelMake;
65
use App\Commands\Helpers\PackageDetail;
6+
use Illuminate\Foundation\Console\ChannelMakeCommand as ChannelMake;
77

88
class ChannelMakeCommand extends ChannelMake
99
{

app/Commands/Foundation/ConsoleMakeCommand.php

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
namespace App\Commands\Foundation;
44

5-
use Illuminate\Foundation\Console\ConsoleMakeCommand as ConsoleMake;
65
use App\Commands\Helpers\PackageDetail;
6+
use Illuminate\Console\GeneratorCommand;
7+
use Symfony\Component\Console\Input\InputOption;
8+
use Symfony\Component\Console\Input\InputArgument;
79

8-
class ConsoleMakeCommand extends ConsoleMake
10+
class ConsoleMakeCommand extends GeneratorCommand
911
{
1012
use PackageDetail;
13+
1114
/**
12-
* The console command name.
13-
*
14-
* @var string
15-
*/
15+
* The console command name.
16+
*
17+
* @var string
18+
*/
1619
protected $name = 'make:command';
1720

1821
/**
@@ -22,6 +25,62 @@ class ConsoleMakeCommand extends ConsoleMake
2225
*/
2326
protected $description = 'Create a new Artisan command';
2427

28+
/**
29+
* The type of class being generated.
30+
*
31+
* @var string
32+
*/
33+
protected $type = 'Console command';
34+
35+
/**
36+
* Replace the class name for the given stub.
37+
*
38+
* @param string $stub
39+
* @param string $name
40+
* @return string
41+
*/
42+
protected function replaceClass($stub, $name)
43+
{
44+
$stub = parent::replaceClass($stub, $name);
45+
46+
return str_replace(['dummy:command', '{{ command }}'], $this->option('command'), $stub);
47+
}
48+
49+
/**
50+
* Get the default namespace for the class.
51+
*
52+
* @param string $rootNamespace
53+
* @return string
54+
*/
55+
protected function getDefaultNamespace($rootNamespace)
56+
{
57+
return $rootNamespace . '\Console\Commands';
58+
}
59+
60+
/**
61+
* Get the console command arguments.
62+
*
63+
* @return array
64+
*/
65+
protected function getArguments()
66+
{
67+
return [
68+
['name', InputArgument::REQUIRED, 'The name of the command'],
69+
];
70+
}
71+
72+
/**
73+
* Get the console command options.
74+
*
75+
* @return array
76+
*/
77+
protected function getOptions()
78+
{
79+
return [
80+
['command', null, InputOption::VALUE_OPTIONAL, 'The terminal command that should be assigned', 'command:name'],
81+
];
82+
}
83+
2584
/**
2685
* Get the stub file for the generator.
2786
*

app/Commands/Foundation/Factories/FactoryMakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected function buildClass($name)
6969
if (Str::startsWith($namespaceModel, 'App\\Models')) {
7070
$namespace = Str::beforeLast('Database\\Factories\\' . Str::after($namespaceModel, 'App\\Models\\'), '\\');
7171
} else {
72-
$namespace = 'Database\\Factories';
72+
$namespace = $this->rootNamespace() . 'Database\\Factories';
7373
}
7474

7575
$replace = [

builds/packr

588 Bytes
Binary file not shown.

config/app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
|
2626
*/
2727

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

3030
/*
3131
|--------------------------------------------------------------------------
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use Tests\TestCase;
6+
use Tests\RefreshPacker;
7+
use Illuminate\Support\Facades\Artisan;
8+
9+
class CrudMakeTest extends TestCase
10+
{
11+
use RefreshPacker;
12+
13+
public function test_it_can_create_a_json_file_to_write_crud_structure()
14+
{
15+
Artisan::call('crud:json Test');
16+
$this->isFileExists('crud/Test.json');
17+
}
18+
19+
public function test_it_can_create_a_crud_for_a_json_file()
20+
{
21+
Artisan::call('crud:json Test');
22+
Artisan::call('crud:make Test');
23+
$this->isFileExists('src/Test.php');
24+
$this->isFileExists('src/database/factories/TestFactory.php');
25+
$this->assertEquals(1, count(glob($this->_testPath() . 'src/database/migrations/*_create_tests_table.php')));
26+
$this->isFileExists('src/Http/controllers/TestController.php');
27+
$this->isFileExists('tests/Feature/TestTest.php');
28+
$this->isFileExists('tests/Unit/TestTest.php');
29+
}
30+
31+
public function test_it_create_class_based_factory()
32+
{
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);
39+
$this->assertStringContainsString('namespace Bitfumes\TestApp\Database\Factories;', $content);
40+
}
41+
42+
public function test_it_create_tests_properly()
43+
{
44+
Artisan::call('crud:json User');
45+
Artisan::call('crud:make User');
46+
$this->isFileExists('/tests/Feature/UserTest.php');
47+
$content = file_get_contents($this->_testPath() . '/tests/Feature/UserTest.php');
48+
$this->assertStringContainsString('class UserTest', $content);
49+
$this->assertStringContainsString('User::factory()->count($num)->create($args);', $content);;
50+
}
51+
}

tests/Feature/CrudMakeTest.php

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

0 commit comments

Comments
 (0)