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

dstewart101's avatar

PHP Unit - Can't get an expected result with a date

Hi folks - I have what I thought was a straightforward test to write for a date:

function testABetHasACreatedAtDate() {
        $this->assertEquals('2015-12-30 00:59:06', $this->bet->created_at());
    }

and there is the model I'm testing:

public function created_at() {
        return $this->created_at;
    }

I get this as the error:

LogicException: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

Am I encroaching on a Laravel built in function or anything. I have 20-something other tests on integers and decimal and other date fields but the created_at date function is the only one giving me problems.

Thanks. DS

0 likes
4 replies
skliche's avatar

@dstewart101 Why are using a method to access that value? Try $this->bet->created_at.

Furthermore created_at should automatically get casted to a Carbon instance. The following should work:

$this->assertEquals("2015-12-30 00:59:06", $this->bet->created_at->toDateTimeString());
dstewart101's avatar

Hi - thanks for the reply. I put it on a method so I have a "hook" if I want to do anything else in the future. But to revert to your suggestion, I didn't have any luck with it, I'm afraid. I wondered was created_at some kind of reserved name or something:

Here is some set up and tests:

$this->bet = new Bet([
            'customer_id' => 3, 
            'date' => '2015-02-02', 
            'event' => 'ds v karen', 
            'betting_on' => 'karen', 
            'bookmaker_id' => '1', 
            'back_or_lay' => 'Back', 
            'bet_type_id' => '4', 
            'odds' => '2.5', 
            'stake' => '25', 
            'commission' => 0, 
            'outcome' => 'Pending', 
            'profit' => null, 
            'profit_to' => '1',
            'created_at' => '2015-12-30 00:59:06' 
            // 'updated_at' => '2015-12-30 00:59:06'
            ]);

Here is my test...

function testABetHasACreatedAtDate() {
        $this->assertEquals('2015-12-30 00:59:06', $this->bet->created_at);
    }

Here is my error...

Fatal error: Call to a member function connection() on a non-object in /Applications/MAMP/htdocs/mb-app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 3286

I actually think it is to do my created_at attribute in my Bet object, but I don't know what to do about it. I just can't seem to test it, trivial and pointless as it may be!

Thanks. DS

ifpingram's avatar

I presume this is something to do with the Eloquent timestamps on the model? Out of interest, if Eloquent is providing this functionality for you out of the box, why do you need to test it? I don't see there is a need to test the functionality of something you don't "own" and that is a given to work as expected out of the box.

dstewart101's avatar

It was an academic exercise to wrap a "get attribute" call within a function so I could use the function as a hook of some sort later in my work. I don't need to test the attribute reliability, I wanted to test my function. I can't manage it though, and I think it's because I don't understand how the created_at column works. Thanks for looking everyone.

Please or to participate in this conversation.