Skip to content

Commit

Permalink
wip: failing tests for events
Browse files Browse the repository at this point in the history
  • Loading branch information
mewejo committed Mar 4, 2024
1 parent 09d8744 commit 7c2a4f9
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions tests/Feature/Contracts/ModelSwapperServiceInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Grizzlyware\ModelSwapper\Contracts\ModelSwapperServiceInterface;
use Grizzlyware\ModelSwapper\Tests\Resources\Exceptions\CountryUpdatedException;
use Grizzlyware\ModelSwapper\Tests\Resources\Jobs\CountCountryPopulation;
use Grizzlyware\ModelSwapper\Tests\Resources\Models\Continent as OriginalContinent;
use Grizzlyware\ModelSwapper\Tests\Resources\Models\Country as OriginalCountry;
use Grizzlyware\ModelSwapper\Tests\Resources\Models\Image;
Expand All @@ -21,6 +22,7 @@
use Grizzlyware\ModelSwapper\Tests\Resources\ReplacementModels\ReplacementNonEloquentModel;
use Grizzlyware\ModelSwapper\Tests\Resources\ReplacementModels\Tag as ReplacementTag;
use Grizzlyware\ModelSwapper\Tests\TestCase;
use Illuminate\Support\Facades\Bus;

class ModelSwapperServiceInterfaceTest extends TestCase
{
Expand Down Expand Up @@ -596,4 +598,92 @@ public function testModelObserverOnReplacementModelIsUsedIfObserverRegisteredAft

$country->save();
}

public function testModelObserverOnOriginalModelFiredOnceIfObserverRegisteredBeforeSwapping(): void
{
Bus::fake();

OriginalCountry::observe(CountryObserver::class);

$this->modelSwapper->swap(
OriginalCountry::class,
ReplacementCountry::class
);

/** @var OriginalContinent $continent */
$continent = OriginalContinent::query()->create();

/** @var OriginalCountry $country */
$country = OriginalCountry::query()->create([
'continent_id' => $continent->id,
]);

Bus::assertDispatchedTimes(CountCountryPopulation::class, 1);
}

public function testModelObserverOnOriginalModelFiredOnceIfObserverRegisteredAfterSwapping(): void
{
Bus::fake();

$this->modelSwapper->swap(
OriginalCountry::class,
ReplacementCountry::class
);

OriginalCountry::observe(CountryObserver::class);

/** @var OriginalContinent $continent */
$continent = OriginalContinent::query()->create();

/** @var OriginalCountry $country */
$country = OriginalCountry::query()->create([
'continent_id' => $continent->id,
]);

Bus::assertDispatchedTimes(CountCountryPopulation::class, 1);
}

public function testModelObserverOnReplacementModelFiredOnceIfObserverRegisteredBeforeSwapping(): void
{
Bus::fake();

ReplacementCountry::observe(CountryObserver::class);

$this->modelSwapper->swap(
OriginalCountry::class,
ReplacementCountry::class
);

/** @var OriginalContinent $continent */
$continent = OriginalContinent::query()->create();

/** @var OriginalCountry $country */
$country = OriginalCountry::query()->create([
'continent_id' => $continent->id,
]);

Bus::assertDispatchedTimes(CountCountryPopulation::class, 1);
}

public function testModelObserverOnReplacementModelFiredOnceIfObserverRegisteredAfterSwapping(): void
{
Bus::fake();

$this->modelSwapper->swap(
OriginalCountry::class,
ReplacementCountry::class
);

ReplacementCountry::observe(CountryObserver::class);

/** @var OriginalContinent $continent */
$continent = OriginalContinent::query()->create();

/** @var OriginalCountry $country */
$country = OriginalCountry::query()->create([
'continent_id' => $continent->id,
]);

Bus::assertDispatchedTimes(CountCountryPopulation::class, 1);
}
}

0 comments on commit 7c2a4f9

Please sign in to comment.