-
When testing with Test.php Post::factory()->make(); Post.php use Auditable; Run test
Result:
It should be pass the booting app. it goes the bootAuditable. public static function bootAuditable()
{
if (static::isAuditingEnabled()) {
static::observe(new AuditableObserver());
}
} then public static function isAuditingEnabled(): bool
{
if (App::runningInConsole()) {
return Config::get('audit.enabled', true) && Config::get('audit.console', false);
}
return Config::get('audit.enabled', true);
} In Probably there is not perfect way to fix this. For testing purposes manually I added return true/false; in here. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Did you try adding on TestCase class /**
* @param \Illuminate\Foundation\Application $app
* @return array
*/
protected function getPackageProviders($app)
{
return [
\OwenIt\Auditing\AuditingServiceProvider::class,
];
} |
Beta Was this translation helpful? Give feedback.
-
I fixed it with enabling abstract class TestCase extends BaseTestCase
{
// ...
protected function setUp(): void
{
Audit::$auditingGloballyDisabled = true;
parent::setUp();
}
public function withAuditing(\Closure $closure): self
{
Audit::$auditingGloballyDisabled = false;
$closure();
Audit::$auditingGloballyDisabled = true;
return $this;
}
} So i can use it the following: $user = UserFactory::new()->create();
$this->withAuditing(function() use ($user) {
$this->post('update_user', [
'username' => 'new_user'
]);
$this->assertDatabaseHas(Audit::class, [
'event' => 'updated',
'user_id' => $user->id,
'auditable_type' => User::class,
'auditable_id' => $user->id,
'old_values' => json_encode([
'username' => 'old_user',
]),
'new_values' => json_encode([
'username' => 'new_user',
]),
});
}); |
Beta Was this translation helpful? Give feedback.
Did you try adding on TestCase class
lluminate/Foundation/Application.php#runningInConsole()