php artisan command to make tests file inside a particular folder

Viewed 5998

Currently I have started learning about Unit Testing in Laravel 5.6. By default my laravel project has a 'tests' directory inside which I have 2 more directories namely, 'Features' and 'Unit'. Each of these directories contain a 'ExampleTest.php'

./tests/Features/ExampleTest.php
./tests/Unit/ExampleTest.php

Whenever I create new test file using command

php artisan make:test BasicTest

It always creates the test file inside the 'Features' directory by default, where as I want the file to be created under the 'tests' directory.

Is there a command using which I can specify the path fro creation of the test file. Something like this

php artisan make:test BasicTest --path="tests"

I have already tried the above path command but it is not a valid command.

Do I need to change some code in my phpunit.xml file?

2 Answers
php artisan make:test  Web/StatementPolicies/StatementPolicyListTest

It will by default create a file namely StatementPolicyListTest under StatementPolicies(if not exist it will create a new folder of this name) folder under tests/Feature/Web

Use this command

php artisan make:test BasicTest --unit

Also you can use

php artisan make:test --help

to see available options

You must be create your custom artiasn command

<?php

namespace App\Console;

class TestMakeCommand extends \Illuminate\Foundation\Console\TestMakeCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $signature = 'make:test-custom {name : The name of the class} {--unit : Create a unit test} {--path= : Create a test in path}';

    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        $path = $this->option('path');
        if (!is_null($path)) {
            if ($path) {
                return $rootNamespace. '\\' . $path;
            }         

            return $rootNamespace;
        }

        if ($this->option('unit')) {
            return $rootNamespace.'\Unit';
        }

        return $rootNamespace.'\Feature';
    }
}

Register it in kernel

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        TestMakeCommand::class
    ];
    ......  
}

Then you can use

php artisan make:test-custom BasicTest --path=

or

php artisan make:test-custom BasicTest --path=Example
Related