Tracy McCormick
Tracy McCormick

Reputation: 461

Laravel / PHP Unit Test failing after update

After Updating Laravel from 5.4 to 5.8 and PHPUnit 8 several tests began to fail. It appears to be related to Authenticatable but I'm unsure how to fix it. After reading some other posts it appears that I need to change the user model. I am seeing the following error when running one of my tests

1) DataViewControllerTest::testImportWithRecords
TypeError: Argument 1 passed to Laravel\BrowserKitTesting\TestCase::actingAs() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in /var/www/tests/Feature/Controllers/DataViewControllerTest.php on line 41

User Model

<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Auth;

class User extends Authenticatable
{
    use Notifiable;
    public static $snakeAttributes = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

}

Since the entire DataViewControllerTest.php is very long I have attached the relevant parts.

    public function setUp() {
           parent::setUp();
           Artisan::call('migrate');
           Artisan::call('db:seed');
           Session::setDefaultDriver('array');
           $this->manager = app('session');

           // find admin and test user accounts
           $this->admin = User::where('name', '=', 'admin')->first();
           $this->user = User::where('name', '=', 'test')->first();
    }

The Failure appears to start here I am calling this routine that creates a new table dynamically. Admin is defined in the setUp above.

    public function createTestTable($tblname, $path, $file) {
            // create a test collection
            $this->collection = (new TestHelper)->createCollection('collection1');

            $this->actingAs($this->admin)
                 ->visit('table/create')
                 ->type($tblname, 'imprtTblNme')
                 ->type('1', 'colID')
                 ->attach($path . $file, 'fltFile')
                 ->press('Import')
                 ->assertResponseStatus(200)
                 ->see('Edit Schema')
                 ->submitForm('Submit', [ 'col-0-data' => 'string', 'col-0-size' => 'default',
                                         'col-1-data' => 'string', 'col-1-size' => 'default',
                                         'col-2-data' => 'string', 'col-2-size' => 'default',
                                         'col-3-data' => 'string', 'col-3-size' => 'default',
                                         'col-4-data' => 'string', 'col-4-size' => 'default',
                                         'col-5-data' => 'string', 'col-5-size' => 'default',
                                         'col-6-data' => 'string', 'col-6-size' => 'default',
                                         'col-7-data' => 'string', 'col-7-size' => 'default',
                                         'col-8-data' => 'string', 'col-8-size' => 'default',
                                         'col-9-data' => 'string', 'col-9-size' => 'default',
                                         'col-10-data' => 'string', 'col-10-size' => 'default',
                                         'col-11-data' => 'string', 'col-11-size' => 'default',
                                         'col-12-data' => 'string', 'col-12-size' => 'default',
                                         'col-13-data' => 'string', 'col-13-size' => 'default',
                                         'col-14-data' => 'string', 'col-14-size' => 'default',
                                         'col-15-data' => 'string', 'col-15-size' => 'default',
                                         'col-16-data' => 'string', 'col-16-size' => 'default',
                                         'col-17-data' => 'string', 'col-17-size' => 'default',
                                         'col-18-data' => 'string', 'col-18-size' => 'default',
                                         'col-19-data' => 'string', 'col-19-size' => 'default',
                                         'col-20-data' => 'string', 'col-20-size' => 'big',
                                         'col-21-data' => 'text', 'col-21-size' => 'default',
                                         'col-22-data' => 'text', 'col-22-size' => 'default',
                                         'col-23-data' => 'string', 'col-23-size' => 'default',
                                         'col-24-data' => 'string', 'col-24-size' => 'default',
                                         'col-25-data' => 'string', 'col-25-size' => 'default',
                                         'col-26-data' => 'string', 'col-26-size' => 'default',
                                         'col-27-data' => 'string', 'col-27-size' => 'default',
                                         'col-28-data' => 'string', 'col-28-size' => 'big',
                                         'col-29-data' => 'text', 'col-29-size' => 'default',
                                         'col-30-data' => 'text', 'col-30-size' => 'default',
                                         'col-31-data' => 'string', 'col-31-size' => 'default' ])
                 ->assertResponseStatus(200)
                 ->see('Load Data')
                 ->press('Load Data')
                 ->see('Table(s)')
                 ->assertResponseStatus(200);
    }

Related question that I posted the other day. Related QuestionFailing tests after upgrade to laravel 5.8 / PHPUnit 8

Any suggestions would be appreciated.

Upvotes: 1

Views: 1556

Answers (1)

Mister Verleg
Mister Verleg

Reputation: 4303

The following code is probably returning nothing:

// find admin and test user accounts
$this->admin = User::where('name', '=', 'admin')->first();

This might have to do with your seeder or migrations.

I suggest the following steps: 1. Check your seeder to see if the admin user gets added.

Consider adding this code:

$this->admin = User::where('name', '=', 'admin')->first();
if($this->admin == null){
  $this->fail('No admin user present');
}

Upvotes: 2

Related Questions