tryRefreshDatabase trait instead of DatabaseMigrations(not sure about Lumen)
Lumen 5.5 POST tests using actingAs() returning 500 Error
I am having some strange behavior during testing in my Lumen 5.5 application. Any time that I try to call actingAs() and do a POST request I get a 500 error with Trying to get property of non-object in the logs as the error message. The route works when using postman so I’m not sure why it is unable to find the property on the Auth facade. What’s strange is that all GET calls that use se `Acting work fine.
The second strange thing is that the database is not being reset after each test when I am using DatabaseMigrations trait in my tests. With that being said, there are tests where the use DatabaseMigrations trait is not being called (only in tests that do not require hitting the DB).
Here is my controller method that I am testing:
public function logout()
{
$customer = Customer::findOrFail(Auth::user()->customer_id);
$customer->auth_token = '';
$customer->save();
return response()->json([], 200);
}
Here is my test for the above controller method:
<?php
namespace Tests\Unit\Controllers;
use Laravel\Lumen\Testing\DatabaseMigrations;
use Laravel\Lumen\Testing\DatabaseTransactions;
use TestCase;
class AuthControllerTest extends TestCase
{
use DatabaseMigrations;
public function testLogoutMethodSucceeds()
{
// this works
$customer = factory(\App\Models\Customer::class)->create();
// this returns a 500 response but works if i call from postman
$response = $this->actingAs($customer)->call('POST', '/api/logout', []);
// why is this giving me a 500 error?
$this->assertEquals(200, $response->status());
$this->seeInDatabase('tbl_customer', ['customer_id' => $customer->customer_id, 'auth_token' => '']);
}
}
Also, just for reference, here is my TestCase.php file:
abstract class TestCase extends Laravel\Lumen\Testing\TestCase
{
public function createApplication()
{
return require __DIR__.'/../bootstrap/app.php';
}
public function setUp()
{
parent::setUp();
$this->createApplication();
}
public function tearDown()
{
// how do i get the database to reset?
parent::tearDown();
}
}
Lastly, here is my phpunit.xml file where I declare the sqlite database.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/app.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite" />
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
Please or to participate in this conversation.