johannesschobel's avatar

Call Symfony/Command from Laravel (Artisan)

Dear all,

I have the following question - and was not able to get it managed. So i thought, i may ask you guys here ;)

So here is my problem: I have a 3rd party package (codeception), that provides a lot of pre-defined commands. These commands, in turn, provide basic Generators in order to create testing suite files within my laravel project.

They may look like this:

class BuildCommand extends Command { ... }
class InitCommand extends Command { ... }
...

They all, in turn, extend the Symfony\Component\Console\Command\Command..

I would like to call these Symfony/Commands from within my basic Artisan Commands that are provided by Laravel. For example, i would like to create a new command artisan tests:setup that creates my defined testing suits, by calling the respective Symfony/Commands provided by Codeception.

The commands are available (i.e., i am able to call them via call vendor/bin/codecept g:suite ..) however, this may not work from within Artisan.

How can i achieve this? I am happy about any help you can provide! Thanks in advance and cheers

0 likes
4 replies
Sergiu17's avatar

Not sure if I understood you quite right, so instead of call vendor/bin/codecept g:suite you want to run artisan tests:setup if so, just create a new artisan command with php artisan make:command SetupTests witch will looks like

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class SetupTests extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tests:setup';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Setup tests description';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->exec('call vendor/bin/codecept g:suite');
    }
}

exec or execute or call I can't remember. Hope it helps :)

johannesschobel's avatar

Hey @Sergiu17 ,

thank you for your fast and detailled answer on this issue here... Basically, you are completely right on this - and that is my current "go to" strategy here... However, as codeception offers a huge amount of commands, this is a lot of work to do :D

However, best case, i do not want to kind of wrap all Codeception Commands in my own Artisan Commands, because this is kind of re-inventing the wheel.

Is there a "better" approach on this? What do you think?

Sergiu17's avatar

@johannesschobel to write php artisan all the time, it becomes too overwhelming, I suggest you to create aliases, much more flexible, you can create simple aliases or even functions

simple example

alias my-test="vendor/bin/codecept g:suite"

or you could say

alias my-test="vendor/bin/codecept"

// and next

my-test g:suite

https://www.digitalocean.com/community/tutorials/an-introduction-to-useful-bash-aliases-and-functions

https://unix.stackexchange.com/questions/329994/alias-and-functions

here are some good links

johannesschobel's avatar

Hey @Sergiu17 ,

again, thank you very much.. writing php artisan generate:test:suite is not the problem for me.. However, "wrapping" already existing commands into Artisan commands is..

More specifically, i would like to call respective command vendor/bin/codecept g:suite XXX within my own application's code.. i.e., i want to call it from a controller...

However, in order to do this, i think, i need to wrap it in Artisan commands :(

Please or to participate in this conversation.