如何以编程方式从php artisan命令调用黄昏测试

问题描述 投票:2回答:1

我需要从工匠命令中运行我的一个Laravel Dusk测试,以便它每天处理。我已经在我的命令中尝试了$this->call('dusk');,但它运行了所有的黄昏测试,并且不允许我添加组或过滤器。我只需要运行一次测试。如何添加过滤器?

$this->call('dusk', [ '--group' => 'communication_tests' ]);

要么

$this->call('dusk', [ '--filter' => 'tests\Browser\myTestFile::myTestMethod' ]);

不起作用,它忽略了传入的选项。有关如何实现这一点的任何想法?

php laravel-5 artisan laravel-dusk laravel-dusk2
1个回答
0
投票

1创建你的工作Laravel黄昏测试。用php artisan黄昏测试它并确保它正常工作。

第二个在名为DuskCommand的app \ Commands文件夹中创建自己的命令,以覆盖laravels本机DuskCommand并让它的签名为“黄昏”。让它扩展Laravel \ Dusk \ Console \ DuskCommand并将下面的代码写入其handle方法(有关此代码的其他版本,请参阅https://github.com/laravel/dusk/issues/371)。我编辑了我的删除$this->option('without-tty') ? 3 : 2三元语句所以它只是读取我的2,因为该选项不存在或我的版本的laravel不需要。

3将您的新类添加到内核中,这样当您调用php artisan dusk时,该类将被识别。

4创建新命令,使用@taytus添加的最后一行以编程方式运行黄昏测试。

5也将新类添加到内核中。

这是我的文件在下面运行...

我的laravel黄昏测试(测试\ Browser \ CommunicationsTest.php)(步骤1)

<?php

namespace Tests\Browser;

use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use App\Models\User;

class CommunicationsTest extends DuskTestCase
{
    /**
     * A Dusk test example.
     * @group all_communication_tests
     * @return void
     */
    public function test_that_all_coms_work()
    {
        // Test Text
        $this->browse(function (Browser $browser) {

            $browser->resize(1920, 1080);
            $browser->loginAs(7)
                ->visit('/events/types/appointments')
                ->assertSee('Automated Messages')
                ->click('.text_message0')
                ->pause(1000)
                ->click('.send-eng-text-btn')
                ->pause(1000)
                ->type('phone', env('TESTING_DEVELOPER_PHONE'))
                ->click('.send-text')
                ->pause(5000)
                ->assertSee('Your test text has been sent.');

            // Test Email
            $browser->visit('/events/types/appointments')
                ->assertSee('Automated Messages')
                ->click('.email0')
                ->assertSee('Automated Messages')
                ->driver->executeScript('window.scrollTo(595, 1063);');
            $browser->click('.send-eng-email-btn')
                ->pause(2000)
                ->click('.send-email')
                ->pause(10000)
                ->assertSee('Your test email has been sent.');

            // Test Call
            $browser->visit('/audio/testcall')
                ->assertSee('Test Call')
                ->type('phone', env('TESTING_DEVELOPER_PHONE'))
                ->press('Call')
                ->pause(3000)
                ->assertSee('Test Call Queued');
        });
    }
}

我的覆盖黄昏命令(app \ Console \ Commands \ Dusk Command.php)(步骤2)

<?php

namespace App\Console\Commands;

use Laravel\Dusk\Console\DuskCommand as VendorDuskCommand;
use Symfony\Component\Process\ProcessBuilder;

class DuskCommand extends VendorDuskCommand
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'dusk';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run Tests on our system... by extending the Laravel Vendor DuskCommand.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->purgeScreenshots();

        $this->purgeConsoleLogs();

        $options=array(); 
        // This line checks if it is a direct call or if has been called from Artisan (we assume is Artisan, more checks can be added) 
        if($_SERVER['argv'][1]!='dusk'){ 
            $filter=$this->input->getParameterOption('--filter'); 
            // $filter returns 0 if has not been set up 
            if($filter){
                $options[]='--filter'; 
                $options[]=$filter; 
                // note: --path is a custom key, check how I use it in Commands\CommunicationsTest.php 
                $options[]=$this->input->getParameterOption('--path'); 
            } 
        }else{ 
            $options = array_slice($_SERVER['argv'], 2); 
        }

        return $this->withDuskEnvironment(function () use ($options) {
            $process = (new ProcessBuilder())
                ->setTimeout(null)
                ->setPrefix($this->binary())
                ->setArguments($this->phpunitArguments($options))
                ->getProcess();

            try {
                $process->setTty(true);
            } catch (RuntimeException $e) {
                $this->output->writeln('Warning: '.$e->getMessage());
            }

            return $process->run(function ($type, $line) {
                $this->output->write($line);
            });
        });
    }

}

现在将新命令添加到内核中,以便在调用它时注册并覆盖本机/供应商DuskCommand的功能。 (第3步)

/**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
         // .... preceding commands....
         Commands\DuskCommand::class,
   ];

现在创建一个以编程方式调用黄昏的命令 - 这是我的(第4步)

<?php 

namespace App\Console\Commands;

use DB;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Test Email, Text, and Phone Calls to make sure they\'re operational.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

       $response = $this->call('dusk',['--filter'=>'test_that_all_coms_work','--path'=>'tests/Browser/CommunicationsTest.php']);

    }
}

并将其注册到内核中(步骤5)......

/**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
         .... preceding commands....
         Commands\DuskCommand::class,
         Commands\TestCommunications::class,
   ];

现在运行php artisan dusk,它应该击中延伸DuskCommand并正常工作。然后调用你的新的php artisan命令替换我的TestCommunications.php文件,它应该完美地运行黄昏...假设你的测试工作当然。如果您有任何疑问或者我遗漏了什么,请告诉我。

记住黄昏只适用于本地环境......这不是你想要/本来可以在生产环境中实现的东西

© www.soinside.com 2019 - 2024. All rights reserved.