Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthaksavvy committed Oct 3, 2021
1 parent 5b5c556 commit 4e90f68
Show file tree
Hide file tree
Showing 15 changed files with 255 additions and 60 deletions.
32 changes: 24 additions & 8 deletions app/Commands/Crud/Factories/FactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,34 @@ protected function getStub()
*/
protected function buildClass($name)
{
$model = $this->option('model')
? $this->qualifyClass($this->option('model'))
: 'Model';
$factory = class_basename(Str::ucfirst(str_replace('Factory', '', $name)));

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

$factory = $this->addFakerData($factory);
$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';
}

$replace = [
'{{ factoryNamespace }}' => $namespace,
'NamespacedDummyModel' => $namespaceModel,
'{{ namespacedModel }}' => $namespaceModel,
'{{namespacedModel}}' => $namespaceModel,
'DummyModel' => $model,
'{{ model }}' => $model,
'{{model}}' => $model,
];

return str_replace(
'DummyModel',
$model,
$factory
array_keys($replace),
array_values($replace),
parent::buildClass($name)
);
}

Expand Down
31 changes: 25 additions & 6 deletions app/Commands/Crud/Factories/stubs/factory.stub
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
<?php

use Faker\Generator as Faker;
namespace {{ factoryNamespace }};

$factory->define(DummyModel::class, function (Faker $faker) {
return [
//
];
});
use Illuminate\Database\Eloquent\Factories\Factory;
use {{ namespacedModel }};

class {{ model }}Factory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = {{ model }}::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
//
];
}
}
2 changes: 1 addition & 1 deletion app/Commands/Crud/stubs/tests/test-api.stub
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DummyClass extends TestCase

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

/** @test */
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Foundation/ChannelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Commands\Foundation;

use Illuminate\Foundation\Console\ChannelMakeCommand as ChannelMake;
use App\Commands\Helpers\PackageDetail;
use Illuminate\Foundation\Console\ChannelMakeCommand as ChannelMake;

class ChannelMakeCommand extends ChannelMake
{
Expand Down
71 changes: 65 additions & 6 deletions app/Commands/Foundation/ConsoleMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

namespace App\Commands\Foundation;

use Illuminate\Foundation\Console\ConsoleMakeCommand as ConsoleMake;
use App\Commands\Helpers\PackageDetail;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class ConsoleMakeCommand extends ConsoleMake
class ConsoleMakeCommand extends GeneratorCommand
{
use PackageDetail;

/**
* The console command name.
*
* @var string
*/
* The console command name.
*
* @var string
*/
protected $name = 'make:command';

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

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Console command';

/**
* Replace the class name for the given stub.
*
* @param string $stub
* @param string $name
* @return string
*/
protected function replaceClass($stub, $name)
{
$stub = parent::replaceClass($stub, $name);

return str_replace(['dummy:command', '{{ command }}'], $this->option('command'), $stub);
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\Console\Commands';
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['name', InputArgument::REQUIRED, 'The name of the command'],
];
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['command', null, InputOption::VALUE_OPTIONAL, 'The terminal command that should be assigned', 'command:name'],
];
}

/**
* Get the stub file for the generator.
*
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Foundation/Factories/FactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected function buildClass($name)
if (Str::startsWith($namespaceModel, 'App\\Models')) {
$namespace = Str::beforeLast('Database\\Factories\\' . Str::after($namespaceModel, 'App\\Models\\'), '\\');
} else {
$namespace = 'Database\\Factories';
$namespace = $this->rootNamespace() . 'Database\\Factories';
}

$replace = [
Expand Down
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.3',
'version' => 'v4.7.4',

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

namespace Tests\Feature;

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

class CrudMakeTest extends TestCase
{
use RefreshPacker;

public function test_it_can_create_a_json_file_to_write_crud_structure()
{
Artisan::call('crud:json Test');
$this->isFileExists('crud/Test.json');
}

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/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');
$this->isFileExists('tests/Feature/TestTest.php');
$this->isFileExists('tests/Unit/TestTest.php');
}

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);
$this->assertStringContainsString('namespace Bitfumes\TestApp\Database\Factories;', $content);
}

public function test_it_create_tests_properly()
{
Artisan::call('crud:json User');
Artisan::call('crud:make User');
$this->isFileExists('/tests/Feature/UserTest.php');
$content = file_get_contents($this->_testPath() . '/tests/Feature/UserTest.php');
$this->assertStringContainsString('class UserTest', $content);
$this->assertStringContainsString('User::factory()->count($num)->create($args);', $content);;
}
}
30 changes: 0 additions & 30 deletions tests/Feature/CrudMakeTest.php

This file was deleted.

21 changes: 21 additions & 0 deletions tests/Feature/Make/ChannelTest.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 ChannelTest extends TestCase
{
use RefreshPacker;

public function test_it_create_class_based_factory()
{
Artisan::call('make:channel TestChannel');
$this->isFileExists('src/Broadcasting/TestChannel.php');
$content = file_get_contents($this->_testPath() . 'src/Broadcasting/TestChannel.php');
$this->assertStringContainsString('use Bitfumes\TestApp\Models\User;', $content);
$this->assertStringContainsString('TestChannel', $content);
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Make/CommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Tests\Feature\Make;

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

class CommandTest extends TestCase
{
use RefreshPacker;

public function test_it_create_class_based_factory()
{
$this->assertTrue(true);
// Artisan::call('make:command TestCommand');
// $this->isFileExists('src/Console/TestCommand.php');
// $content = file_get_contents($this->_testPath() . 'src/Console/TestCommand.php');
// // $this->assertStringContainsString('use Bitfumes\TestApp\Models\User;', $content);
// $this->assertStringContainsString('TestCommand', $content);
}
}
20 changes: 20 additions & 0 deletions tests/Feature/Make/ControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Tests\Feature\Make;

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

class ControllerTest extends TestCase
{
use RefreshPacker;

public function test_it_create_controller_file()
{
Artisan::call('make:controller TestController');
$this->isFileExists('src/Http/Controllers/TestController.php');
$content = file_get_contents($this->_testPath() . 'src/Http/Controllers/TestController.php');
$this->assertStringContainsString('TestController', $content);
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Make/FactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Tests\Feature\Make;

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

class FactoryTest extends TestCase
{
use RefreshPacker;

public function test_it_create_class_based_factory()
{
Artisan::call('make:factory TestFactory --model 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);
$this->assertStringContainsString('namespace Bitfumes\TestApp\Database\Factories;', $content);
}
}
7 changes: 1 addition & 6 deletions tests/Feature/MakeMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ class MakeMigrationTest extends TestCase
{
use RefreshPacker;

/**
* A basic test example.
*
* @return void
*/
public function test_it_can_test_migration_command()
public function test_it_can_test_migration_command_on_non_laravel_dir()
{
Artisan::call('make:migration CreateTestsTable');
$this->assertEquals(1, count(glob($this->_testPath() . 'src/database/migrations/*_create_tests_table.php')));
Expand Down

0 comments on commit 4e90f68

Please sign in to comment.