Steady-Entertainment's avatar

test fails on member function null

Good morning friends

this time I have a easy one for you but I am not experienced enough to track the bug ...

I get the following error:

Error : Call to a member function connection() on null
<?php

namespace Tests\Unit;

use App\Models\Offer;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\TestCase;

class OfferTest extends TestCase
{
    use RefreshDatabase;

//   protected $offer;

//    public function setUp(): void
//    {
//        parent::setUp(); // TODO: Change the autogenerated stub
//        $this->offer = Offer::factory()->create();
//    }

    /** @test */
    function an_offer_has_a_creator()
    {
        $offer = Offer::factory()->create();
        $offer->assertInstanceOf(User::class, $this->offer->user);
    }

}
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateOffersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('offers', function (Blueprint $table) {
            $table->id();

            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users');
            $table->string('Name');

            $table->decimal('latitude', 8, 6);
            $table->decimal('longitude', 9, 6);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('offers');
    }
}
<?php

namespace Database\Factories;

use App\Models\Offer;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class OfferFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Offer::class;

    public function definition ()
    {
        return [
            'Name' => 'Breath Work',

            'latitude' => 51.567952 ,
            'longitude' => 8.080428 ,
        ];
    }
}

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Offer extends Model
{
    use HasFactory;

public function user()
    {
        return $this->belongsTo(User::class);
    }
}

also a side question ... ist the without exception handling not done anymore?

0 likes
6 replies
Nakov's avatar

@steady-entertainment have you setup your environment variables for testing?

in phpunit.xml add these two lines:

<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>

within the <php> tag.

Without exception is handled on the request itself, you are not getting to that point just yet.

Steady-Entertainment's avatar

yes definitively

all my other tests are passing maybe the only difference is that this test is inside the unit folder and I made it with the make:test -u and I am using livewire .... but I am not sure if that helps

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
         <server name="DB_CONNECTION" value="sqlite"/>
         <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

Nakov's avatar

If you are connecting to the database, you sure need to use use Tests\TestCase; instead of use PHPUnit\Framework\TestCase;.

Tray2's avatar

Have you installed SQLite 3?

Nakov's avatar

it is good to share your "different" test approach so someone else might benefit with the answer :) and I doubt it is very different than what I said above for your imports which TestCase class is imported.

Please or to participate in this conversation.