Create test for app/Observers/BonusScoreObserver
This commit is contained in:
parent
c7ffe6be02
commit
f45aaec506
|
|
@ -2,11 +2,14 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
class BonusScore extends Model
|
class BonusScore extends Model
|
||||||
{
|
{
|
||||||
|
use hasFactory;
|
||||||
|
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
|
||||||
public function entry(): BelongsTo
|
public function entry(): BelongsTo
|
||||||
|
|
|
||||||
|
|
@ -33,20 +33,4 @@ class BonusScoreObserver
|
||||||
$calculator = app(TotalEntryScores::class);
|
$calculator = app(TotalEntryScores::class);
|
||||||
$calculator($bonusScore->entry, true);
|
$calculator($bonusScore->entry, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle the ScoreSheet "restored" event.
|
|
||||||
*/
|
|
||||||
public function restored(BonusScore $bonusScore): void
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle the ScoreSheet "force deleted" event.
|
|
||||||
*/
|
|
||||||
public function forceDeleted(BonusScore $bonusScore): void
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Models\BonusScore;
|
||||||
|
use App\Models\Entry;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
class BonusScoreFactory extends Factory
|
||||||
|
{
|
||||||
|
protected $model = BonusScore::class;
|
||||||
|
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'score' => $this->faker->randomNumber(),
|
||||||
|
'created_at' => Carbon::now(),
|
||||||
|
'updated_at' => Carbon::now(),
|
||||||
|
|
||||||
|
'entry_id' => Entry::factory(),
|
||||||
|
'user_id' => User::factory(),
|
||||||
|
'originally_scored_entry' => Entry::factory(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function forEntry(Entry $entry): self
|
||||||
|
{
|
||||||
|
return $this->state(function (array $attributes) use ($entry) {
|
||||||
|
return [
|
||||||
|
'entry_id' => $entry->id,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,60 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Actions\Tabulation\TotalEntryScores;
|
||||||
|
use App\Models\BonusScore;
|
||||||
|
use App\Models\Entry;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
|
||||||
|
uses(RefreshDatabase::class);
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->mock = Mockery::mock(TotalEntryScores::class);
|
||||||
|
app()->instance(TotalEntryScores::class, $this->mock);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('recalculates total scores when bonus score is created', function () {
|
||||||
|
$entry = Entry::factory()->create();
|
||||||
|
|
||||||
|
$this->mock->shouldReceive('__invoke')
|
||||||
|
->once()
|
||||||
|
->withAnyArgs()
|
||||||
|
->andReturn(null);
|
||||||
|
|
||||||
|
BonusScore::factory()
|
||||||
|
->forEntry($entry)
|
||||||
|
->create();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it('recalculates total scores when bonus score is updated', function () {
|
||||||
|
$this->mock->shouldReceive('__invoke')
|
||||||
|
->once()
|
||||||
|
->withAnyArgs()
|
||||||
|
->andReturn(null);
|
||||||
|
$bonusScore = BonusScore::factory()->create();
|
||||||
|
|
||||||
|
$this->mock->shouldReceive('__invoke')
|
||||||
|
->once()
|
||||||
|
->withAnyArgs()
|
||||||
|
->andReturn(null);
|
||||||
|
|
||||||
|
$bonusScore->update(['score' => 95]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('recalculates total scores when bonus score is deleted', function () {
|
||||||
|
DB::table('bonus_scores')->insert([
|
||||||
|
'entry_id' => Entry::factory()->create()->id,
|
||||||
|
'user_id' => User::factory()->create()->id,
|
||||||
|
'originally_scored_entry' => Entry::factory()->create()->id,
|
||||||
|
'score' => 28,
|
||||||
|
]);
|
||||||
|
$bonusScore = BonusScore::first();
|
||||||
|
|
||||||
|
$this->mock->shouldReceive('__invoke')
|
||||||
|
->once()
|
||||||
|
->withAnyArgs()
|
||||||
|
->andReturn(null);
|
||||||
|
|
||||||
|
$bonusScore->delete();
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue