Refactor base test into Trait, clean test output for easier comparison

This commit is contained in:
Brady Wetherington
2025-07-18 13:16:35 +01:00
parent 58759acfe4
commit 8a5f6d2a5d
2 changed files with 19 additions and 15 deletions

View File

@@ -0,0 +1,17 @@
<?php
namespace Tests\Support;
use App\Models\Actionlog;
use Illuminate\Database\Eloquent\Model;
use PHPUnit\Framework\Assert;
use function PHPUnit\Framework\assertEquals;
trait AssertHasActionLogs
{
public function assertHasTheseActionLogs(Model $item, array $statuses)
{
Assert::assertEquals($statuses, $item->assetlog()->orderBy('id')->pluck('action_type')->toArray(), "Failed asserting that action logs match");
}
}

View File

@@ -3,13 +3,11 @@
namespace Tests;
use App\Http\Middleware\SecurityHeaders;
use App\Models\Actionlog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use PHPUnit\Framework\Assert;
use RuntimeException;
use Tests\Support\AssertsAgainstSlackNotifications;
use Tests\Support\AssertHasActionLogs;
use Tests\Support\CanSkipTests;
use Tests\Support\CustomTestMacros;
use Tests\Support\InteractsWithAuthentication;
@@ -24,6 +22,7 @@ abstract class TestCase extends BaseTestCase
use InteractsWithAuthentication;
use InitializesSettings;
use LazilyRefreshDatabase;
use AssertHasActionLogs;
private array $globallyDisabledMiddleware = [
SecurityHeaders::class,
@@ -51,16 +50,4 @@ abstract class TestCase extends BaseTestCase
}
}
public function assertHasTheseActionLogs(Model $item, array $statuses)
{
\Log::error("Okay, we're running the test macro now?");
$logs = Actionlog::where(['item_id' => $item->id, 'item_type' => get_class($item)])->orderBy('id')->get();
Assert::assertEquals(count($statuses), count($logs), "Wrong count of logs expected - expecting " . count($statuses) . ", got " . count($logs));
$i = 0;
foreach ($statuses as $status) {
Assert::assertEquals($status, $logs[$i]->action_type, "Unexpected action type - " . $logs[$i]->action_type . " - expecting $status");
$i++;
}
}
}