Laravel-auditing: Testing with PHPUnit, Verify that a model got an audit?

Created on 3 Feb 2018  路  3Comments  路  Source: owen-it/laravel-auditing

| Q | A
| ----------------- | ---
| Bug? | no
| New Feature? | no
| Framework | Laravel
| Framework version | 5.4
| Package version | 3.1
| PHP version | 7.1

Actual Behaviour

I need to update the package but wanted to make sure i don't break things. So i'm adding a few functional tests to verify that audits are working so that when i make the migration it should be easy to tell what breaks.

Right now i have a very simple test

private function createEmployee($params, $user = null)
    {
        $permission = new Permission();
        $permission->name = 'create-employee';
        $permission->save();
        if ($user == null) {
            $user = factory(User::class)->create();
        }
        $user->attachPermission($permission);

        $response = $this->actingAs($user)->json('POST', '/employees', $params);

        return $response;
    }

    /** @test */
    public function creating_employee_record_adds_audits()
    {
        $user = factory(User::class)->create();
        $input = [
            "f_name" => "First Name",
            "l_name" => "Last Name"
        ];
        $response = $this->createEmployee($input);
        $response->assertStatus(200);

        $employee = Employee::find($response->json()['id']);

        dd($employee->audits);
    }

The problem i'm having is that the audits return empty collection, if i just do a simple $this-> assertDatabaseHas('audits', ['user_id'=>$user->id]); that will return an audits table is empty response. So it looks like when i submit the data to the endpoint it isn't triggering the audit.

It's also worth mentioning that this package works just fine outside of PHPUnit

Expected Behaviour

able to get back audit data and see that the table has audit data

Steps to Reproduce

I suspect it's my lack of testing experience, so to avoid spamming this with a bunch of useless code i'll just show my controller code, if you need other bits just let me know, but as i mentioned before it works just fine outside of PHPUnit

    public function store(CreateEmployeeRequest $request)
    {
        $employee = Employee::create($request->all());


        $employee->save();

        Flash::success('Employee saved successfully.');

        return $employee;
    }

Anywhos, hopefully you can see the errors of my ways and point me in the right direction

Thanks,
Jeff

Most helpful comment

I swear you always figure out your issue after you click the submit button...

Anyways for anyone else who stumbles on this, my solution was to add

audit_console' => true,

to the config/audit.php file

All 3 comments

I swear you always figure out your issue after you click the submit button...

Anyways for anyone else who stumbles on this, my solution was to add

audit_console' => true,

to the config/audit.php file

Is there a way to change config in the test?

config(['audit.console' => true]); is not working

Is there a way to change config in the test?

config(['audit.console' => true]); is not working

try
config()->set('audit.console', true);

Was this page helpful?
0 / 5 - 0 ratings