اذهب إلى المحتوى
  • 0

كيفية إجراء unit test على task في لارافيل

Amir Alsaeed

السؤال

لقد قمت بجدولة عمل في لارافيل ليتم تنفيذه كل مدّة معينة:

<?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 = [
        \App\Console\Commands\MyCommand::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('MyCommand')->hourly();
        $schedule->command('echo "Happy Coding!" ')->everyMinute();       
}
}

 ولكن سؤالي هو كيف يمكنني إجراء Unit Test على هذا الإجراء والتأكد من أن بعض الخصائص فيه تعمل بدون وجود مشاكل؟

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

إن الاختبار يتم بتفيذ الأمر مباشرةً ورؤية النتائج:

php artisan schedule:run

ولكن في حال كان الهدف هو طباعة رسائل الخطأ المخصصة في حالات معيّنة، فإن scheduler لا يقوم بالطباعة بطبيعته، ولكن يمكنك توجيه الطباعة للكود الذي يتم تنفيذه لأي ملف آخر عن طريق استخدام:

writeOutputTo()

أو:

appendOutputTo()

وهو مايعرف في TaskOutput في التوثيق الرسمي للارافيل، مثال:

$schedule->command('emails:send')
         ->daily()
         ->sendOutputTo($filePath);

حيث نستبدل $filepath بمسار الملف الذي نريد الطباعة عليه.

أما لإجراء Unit Test للعمل بشكل مخصص أكثر، فيمكن إضافة التالي ليتم اختبار schedule محدد مثل الكود السابق لديك:

public function testIsAvailableInTheScheduler()
{
    /** @var \Illuminate\Console\Scheduling\Schedule $schedule */
    $schedule = app()->make(\Illuminate\Console\Scheduling\Schedule::class);

    $events = collect($schedule->events())->filter(function (\Illuminate\Console\Scheduling\Event $event) {
        return stripos($event->command, 'NewCommand');
    });

    if ($events->count() == 0) {
        $this->fail('No events found');
    }

    $events->each(function (\Illuminate\Console\Scheduling\Event $event) {
        // This example is for hourly commands.
        $this->assertEquals('0 * * * * *', $event->expression);
    });
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...