Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for creating an Auditable model even if application is not spun up #962

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,22 @@ trait Auditable
*/
public static function bootAuditable()
{
if (static::isAuditingEnabled()) {
try {
$isAuditingEnabled = static::isAuditingEnabled();
} catch (\RuntimeException $e) {
if ($e->getMessage() !== 'A facade root has not been set.') {
throw $e;
}

/**
* Facade root has not been set. The user may be attempting to use
* their Auditable outside of the application context. We will
* just skip booting for now.
*/
return;
}

if ($isAuditingEnabled) {
static::observe(new AuditableObserver());
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/OutsideOfAppContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace OwenIt\Auditing\Tests\Unit;

use OwenIt\Auditing\Tests\Models\Article;
use PHPUnit\Framework\TestCase;

class OutsideOfAppContextTest extends TestCase
{
public function test_can_create_new_model(): void
{
$article = new Article();
$this->assertInstanceOf(Article::class, $article);
}
}