PHP code example of bogdanghervan / laravel-dummy-observer

1. Go to this page and download the library: Download bogdanghervan/laravel-dummy-observer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bogdanghervan / laravel-dummy-observer example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    protected $fillable = [
        'departure',
        'destination',
        'status'
    ];

    public function landed()
    {
        $this->status = 'landed';
        $this->save();
    }
}



namespace Tests\Unit;

use Tests\TestCase;
use App\Models\Flight;
use BogdanGhervan\DummyObserver;

class FlightTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        // Prevent model from being saved
        Flight::observe(DummyObserver::class);
    }

    protected function tearDown(): void
    {
        DummyObserver::clear();
    }

    public function testLanded(): void
    {
        $flight = new Flight([
            'departure' => 'Bucharest',
            'destination' => 'New York',
        ]);
    
        $flight->landed();
    
        DummyObserver::assertSavedAttributes([
            'departure' => 'Bucharest',
            'destination' => 'New York',
            'status' => 'landed'
        ]);
    }
}

Flight::observe(DummyObserver::class);

$flight = Flight::create([
    'passenger' => 'John Smith'
]);

DummyObserver::assertSavedAttributes([
    'passenger' => 'John Smith'
]);

Flight::observe(DummyObserver::class);

$flight = Flight::create([
    'passenger' => 'John Smith',
    'departure' => 'Bucharest',
    'destination' => 'New York',
    'status' => 'boarded'
]);

DummyObserver::assertSavedAttributes([
    'passenger' => 'John Smith',
    'status' => 'boarded'
]);

Flight::observe(DummyObserver::class);

$flight = Flight::create([
    'departure' => 'Bucharest',
    'destination' => 'New York'
]);

$flight->update([
    'status' => 'boarded',
    'gate' => 'A1'
]);

DummyObserver::assertSavedAttributes([
    'destination' => 'New York'
]);
DummyObserver::assertSavedAttributes([
    'status' => 'boarded',
    'gate' => 'A1'
]);

Flight::observe(DummyObserver::class);

$flight = Flight::create([
    'departure' => 'Bucharest',
    'destination' => 'New York'
]);

$flight->update([
    'status' => 'boarded',
    'gate' => 'A1'
]);

DummyObserver::assertSavedTimes(2);

Flight::observe(DummyObserver::class);

$flight = new Flight();

DummyObserver::assertNothingSaved();

protected function tearDown(): void
{
    DummyObserver::clear();
}