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

tenzan's avatar

How to refactor $this->assertEquals() ?

Hi,

With latest Laravel, it seems the $this part of the

$this->assertEquals('December 1, 2016', $date); is not correct. How it should be with latest Laravel?

<?php

namespace Tests\Unit;

use App\Models\Concert;
use Carbon\Carbon;
use PHPUnit\Framework\TestCase;

class ConcertTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function test_that_true_is_true()
    {
        /** @test */
    function can_get_formatted_date()
    {
        // Create concert with a known date
        $concert = Concert::create([
            'date' => Carbon::parse('2016-12-01 8:00pm'),
        ]);

        // Retrieve the formatted date
        $date = $concert->formatted_date;

        // Verify the date formatted as expected
        $this->assertEquals('December 1, 2016', $date);
    }
    }
}
0 likes
5 replies
Sinnbeck's avatar

That is still correct in the newest phpunit. Error?

mvd's avatar
mvd
Best Answer
Level 48

Hi @tenzan

I think your code structure is wrong, your test can_get_formatted_date is inside test_that_true_is_true Needs to be?

<?php
namespace Tests\Unit;

use App\Models\Concert;
use Carbon\Carbon;
use PHPUnit\Framework\TestCase;

class ConcertTest extends TestCase{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function test_that_true_is_true() {

    }
    
    /** @test */
    function can_get_formatted_date() {
        // Create concert with a known date
        $concert = Concert::create([
            'date' => Carbon::parse('2016-12-01 8:00pm'),
        ]);

        // Retrieve the formatted date
        $date = $concert->formatted_date;

        // Verify the date formatted as expected
        $this->assertEquals('December 1, 2016', $date);        
    }
}
1 like
tenzan's avatar

@mvd Oops, it was really a mess in the code. Thanks!

1 like

Please or to participate in this conversation.