【问题标题】:How to programmatically call a dusk test from a php artisan command如何以编程方式从 php artisan 命令调用黄昏测试
【发布时间】:2019-06-29 22:26:22
【问题描述】:

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

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

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

不起作用,它会忽略传入的选项。关于如何完成此操作的任何想法?

【问题讨论】:

    标签: php laravel-5 laravel-artisan laravel-dusk laravel-dusk2


    【解决方案1】:

    1st 创建你的工作 Laravel Dusk 测试。使用 php artisan Huang 进行测试并确保它正常工作。

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

    3rd 将新类添加到内核中,以便在调用 php artisan黄昏时识别该类。

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

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

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

    我的 laravel 黄昏测试(tests\Browser\CommunicationsTest.php)(STEP 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\DuskCommand.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 并且可以正常工作。然后调用替换我的 TestCommunications.php 文件的新 php artisan 命令,它应该可以完美地运行黄昏......当然假设你的测试有效。如果您有任何问题或我遗漏了什么,请告诉我。

    记住 Dusk 只能在本地环境中工作......这不是你想要/不应该/本机可以在生产环境中实现的东西

    【讨论】:

    • BadMethodCallException 方法 App\Console\Commands\DuskCommand::withDuskEnvironment 不存在。
    猜你喜欢
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多