PHPUnit: difference between assertEquals and assertSame

phpunit, TDD Add comments

It turned out to be easy:

  • assertEquals — check by value
  • assertSame — check by reference

So here is an example:

  $objectA = new StdClass;
  $objectB = new StdClass;
  $objectA->value = 42;
  $objectB->value = 42;
  $container = array($objectA);

  $this->assertEquals($objectA, $objectB); //true
  $this->assertSame($objectA, $objectB); //false

  $this->assertEquals($objectA, $container[0]); //true
  $this->assertSame($objectA, $container[0]); //true

Comments are closed.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in