[GH-ISSUE #168] Bug in testing due to PHPUnit's interpretation #311

Closed
opened 2026-03-01 17:49:06 +03:00 by kerem · 1 comment
Owner

Originally created by @alexw23 on GitHub (Jun 14, 2021).
Original GitHub issue: https://github.com/antonioribeiro/google2fa/issues/168

As was building some of my own tests I noticed that where you assert verifyKeyNewer should be returning a timestamp, it actually returns a boolean.

Take the following as an example of this (Source):

$this->assertEquals(
    26213400,
    $this->google2fa->verifyKeyNewer(
        Constants::SECRET,
        '758576',
        null,
        2,
        26213400
    )
); // 26213400

It turns out that if the "timestamp" is empty verifyKeyNewer returns true not a timestamp.

And PHPunit for some reason considers these both to be equal, for some reason I have no idea.

The following example also works:

$this->assertEquals(
    26213400,
    true
); // 26213400

Just thought I would report this in an effort to have any expectation of how it should be working clarified.

Edit: looks like you need to be using assertSame.

Originally created by @alexw23 on GitHub (Jun 14, 2021). Original GitHub issue: https://github.com/antonioribeiro/google2fa/issues/168 As was building some of my own tests I noticed that where you assert `verifyKeyNewer` should be returning a timestamp, it actually returns a boolean. Take the following as an example of this ([Source](https://github.com/antonioribeiro/google2fa/blob/8.x/tests/Google2FATest.php#L425-L434)): ```php $this->assertEquals( 26213400, $this->google2fa->verifyKeyNewer( Constants::SECRET, '758576', null, 2, 26213400 ) ); // 26213400 ``` It turns out that if the "timestamp" is empty `verifyKeyNewer` returns `true` not a timestamp. And PHPunit for some reason considers these both to be equal, for some reason I have no idea. The following example also works: ```php $this->assertEquals( 26213400, true ); // 26213400 ``` Just thought I would report this in an effort to have any expectation of how it should be working clarified. Edit: looks like you need to be using `assertSame`.
kerem closed this issue 2026-03-01 17:49:07 +03:00
Author
Owner

@thinkverse commented on GitHub (Oct 20, 2021):

And PHPunit for some reason considers these both to be equal, for some reason I have no idea.

I know it's been a long time since this issue was open. But just to clarify that PHPUnit's interpretation is not wrong here.

It's interpreting the assertion correctly, 26213400 is a truthy value in PHP. Therefore assetEquals will return true since a truthy value is equal to true in PHP. Using assertSame will fail however since the expectation and the actual value aren't of the same type, they are both truthy but one is an int and the other is a boolean.

Boiled down, PHPUnit's assertEquals is a non-strict equality check in PHP (==), assertSame would be a strict equality check (===).

This can be seen if you try and write it out the checks yourself in plain PHP, as demonstrated here.

var_dump(26213400 == true);  // true
var_dump(26213400 === true); // false
<!-- gh-comment-id:947373414 --> @thinkverse commented on GitHub (Oct 20, 2021): > And PHPunit for some reason considers these both to be equal, for some reason I have no idea. I know it's been a long time since this issue was open. But just to clarify that PHPUnit's interpretation is not wrong here. It's interpreting the assertion correctly, `26213400` is a __truthy__ value in PHP. Therefore `assetEquals` will return `true` since a truthy value is equal to true in PHP. Using `assertSame` will fail however since the expectation and the actual value aren't of the same type, they are both truthy but one is an `int` and the other is a `boolean`. Boiled down, PHPUnit's `assertEquals` is a non-strict equality check in PHP (`==`), `assertSame` would be a strict equality check (`===`). This can be seen if you try and write it out the checks yourself in plain PHP, as demonstrated [here](https://3v4l.org/5BD4O). ```php var_dump(26213400 == true); // true var_dump(26213400 === true); // false ```
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/google2fa#311
No description provided.