Why use equalTo in phpunit mock checks for the passed arguments?

  Kiến thức lập trình

When I mock a function in phpunit, I can assert that it got called with the right arguments using with, which accepts constraints like identicalTo, greaterThan, and stringContains. These make sense to me, but why do I need equalTo?

In this example, the test passes no matter if I use the expected value as is, or with an equalTo wrapper.

<?php
class Mcve {
    function foo(string $param): string {
        return "foo " . $param;
    }
}

and

<?php

use PHPUnitFrameworkMockObjectMockObject;
use PHPUnitFrameworkTestCase;

class McveTest extends TestCase {
    public function testMcve(): void {
        $fooMock = $this->getMockBuilder(Mcve::class)->getMock();
        $fooMock
            ->method('foo')
            // ->with('1')
            ->with(self::equalTo('1'))
            ->willReturn('bar');
        $got = $fooMock->foo('1');
        self::assertEquals('bar', $got);
    }
}

What is the difference between just passing the expected value vs wrapping it in equalTo?

LEAVE A COMMENT