Menu

← Back to phpunit.*

Error Identifier: phpunit.assertEquals

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
	public function testFoo(): void
	{
		$expected = 5;
		$actual = 5;
		$this->assertEquals($expected, $actual);
	}
}

Why is it reported? #

This rule is part of phpstan-phpunit.

assertEquals() performs a loose comparison (==), while assertSame() performs a strict comparison (===). When both the expected and actual values are scalars of the same type, assertSame() should be used instead because it avoids unexpected type coercion and produces clearer failure messages.

How to fix it #

Replace assertEquals() with assertSame():

 <?php declare(strict_types = 1);
 
 use PHPUnit\Framework\TestCase;

 class MyTest extends TestCase
 {
 	public function testFoo(): void
 	{
 		$expected = 5;
 		$actual = 5;
-		$this->assertEquals($expected, $actual);
+		$this->assertSame($expected, $actual);
 	}
 }

How to ignore this error #

You can use the identifier phpunit.assertEquals to ignore this error using a comment:

// @phpstan-ignore phpunit.assertEquals
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: phpunit.assertEquals

Rules that report this error #

  • PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule [1] phpstan/phpstan-phpunit

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.