Difference between assertEquals and assertSame in PHPUnit?

phpunit

Solution

I use both sporadically, but according to the docs:

`assertSame`

Reports an error identified by `$message` if the two variables `$expected` and `$actual` do not have the same type and value."

And as you can see in the example below the above excerpt, they are passing `'2204'` and `2204`, which will fail using `assertSame` because one is a `string` and one is an `int,` basically:

'2204' !== 2204
assertSame('2204', 2204) // this test fails

`assertEquals`

"Reports an error identified by $message if the two variables $expected and $actual are not equal."

`assertEquals` does not appear to take datatype into consideration so using the above example of `2204`:

'2204' == 2204
assertEquals('2204', 2204) // this test passes

I just ran some unit tests against the above examples, and indeed they resulted in documented behavior.

Problem

PHPUnit contains an `assertEquals()` method, but it also has an `assertSame()` one. At first glance it looks like they do the same thing. What is the difference between the two? Why are they both specified?

Original source