Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Paupoo's avatar
Level 1

Call to a member function connection() on null in Unit test

Hello,

I'm starting to right my fist unit tests ever (I'm a big newbie learning Laravel, PHP & programming in general). I've read the doc and the web and I understand that when I run my test, "call to a member function connection() on null means that Laravel was not booted and could not access some database. But for the unit testing, I understood that I don't care, because I just want to pass data in my methods, and make sure it returns what I expect.

Here is my test class :

class TrainingBuilderTest extends TestCase

{

public function test_set_time_to_date(): void

{
    // Date references
    $date = Carbon::parse('2024-08-15');
    $start_time = '20:00';
    $end_time = '22:00';

    // Expectated dateTimes
    $expected_start = '2024-08-15 20:00';
    $expected_end = '2024-08-15 22:00';

    $trainingBuilder = new TrainingBuilder();

    // Appliquez la méthode à tester
    $trainingBuilder->setTimeToDate($date, $start_time, $end_time);

    // Assertions pour vérifier les valeurs
    $this->assertEquals($expected_start, $trainingBuilder->training->start);
    $this->assertEquals($expected_end, $trainingBuilder->training->end);

}

}

Basically I just send some date (one via a Carbon object, and 2 "times" formatted as strings. I expect the method to merge the date and set the start/end time in a datetime format.

This unit test is about a service class I've built to simplify a controller a split my own logic into its own class.

Here is the extract of my service class, if it helps you to help me :)

class TrainingBuilder

{

private Training $training;

public function __construct()

{

    $this->training = new Training();

}

public function setTimeToDate(Carbon $date, string $start_time, string $end_time): self

{

    $this->training->start = $date->setTimeFrom($start_time)->format('Y-m-d H:i');
    $this->training->end = $date->setTimeFrom($end_time)->format('Y-m-d H:i');

    return $this;

}

...

I just construct an instance of a model (could this be the reason ? For me instantiating a new model should only happen in the memory and should not interact with the database... right ?)

then my method just take the date, adds the time to 'start' and to 'end' attributes of the Training model and return the object to allow me to chain with other methods.

This works fine, but I'm stuck in my unit testing. I've read stufs about stubs, mocks, and so on but I'm a newbie and I'm lost. I would like to understand why is laravel trying to set up a connection while I'm only loading an instance of a model that is empty, and setting some attributes in the memory only.

Thanks for your help in advance !

0 likes
1 reply
tykus's avatar

What else is happening in the Service class (outside of the extract you shared)? Also, what is happening in the Model class - are there mutators/accessors or Observer(s) registered which are reading/writing to the database?

Also, if $training is private visibility; $trainingBuilder->training->end will not work...

Please or to participate in this conversation.