为什么对于明显相同的字符串,PHPUnit 会给我一个assertEquals 失败?

发布于 2024-12-21 18:13:24 字数 875 浏览 3 评论 0原文

以下脚本既演示并记录了问题(在标题注释中),即我无法检测“预期”和“实际”字符串之间的任何差异:

<?php

/*
$ phpunit MyTest.php
PHPUnit 3.4.0 by Sebastian Bergmann.

F

Time: 0 seconds

There was 1 failure:

1) MyTest::test_print_r
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-O:7:"MyClass":2:{s:13:"MyClassvar3";N;s:4:"var1";N;}
+O:7:"MyClass":2:{s:13:"MyClassvar3";N;s:4:"var1";N;}

.../MyTest.php:41
.../bin/phpunit:54

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
*/

class MyClass {
    static protected $var2;
    private $var3;
    public $var1;

    public function foo($item) {
        echo $item . "\n";
    }
}

class MyTest extends PHPUnit_Framework_TestCase {
    function test_print_r() {
        $m = new MyClass();
        $this->assertEquals(trim('O:7:"MyClass":2:{s:13:"MyClassvar3";N;s:4:"var1";N;}'), trim(serialize($m)));
    }
}

The following script both demonstrates and documents (in the header comment) the issue, namely that I am unable to detect any difference between the "expected" and "actual" strings:

<?php

/*
$ phpunit MyTest.php
PHPUnit 3.4.0 by Sebastian Bergmann.

F

Time: 0 seconds

There was 1 failure:

1) MyTest::test_print_r
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-O:7:"MyClass":2:{s:13:"MyClassvar3";N;s:4:"var1";N;}
+O:7:"MyClass":2:{s:13:"MyClassvar3";N;s:4:"var1";N;}

.../MyTest.php:41
.../bin/phpunit:54

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
*/

class MyClass {
    static protected $var2;
    private $var3;
    public $var1;

    public function foo($item) {
        echo $item . "\n";
    }
}

class MyTest extends PHPUnit_Framework_TestCase {
    function test_print_r() {
        $m = new MyClass();
        $this->assertEquals(trim('O:7:"MyClass":2:{s:13:"MyClassvar3";N;s:4:"var1";N;}'), trim(serialize($m)));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2024-12-28 18:13:24

您没有看到问题,因为您使用的是相当旧的 PHPUnit 版本。当前版本是 3.6.5,如果可以的话,您应该升级。

PHPUnit>>当字符串包含不可打印字符时,3.6 将向您显示不同的差异。就像这里的情况一样。

这是使用更新版本的输出。失败原因的解释如下:

phpunit
PHPUnit 3.6.5 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 3.25Mb

There was 1 failure:

1) MyTest::test_print_r
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'O:7:"MyClass":2:{s:13:"MyClassvar3";N;s:4:"var1";N;}'
+Binary String: 0x4f3a373a224d79436c617373223a323a7b733a31333a22004d79436c6173730076617233223b4e3b733a343a2276617231223b4e3b7d

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

解释

序列化的 php 字符串包含 NULL BYTES 来表示私有和受保护的类变量。

字符串 "MyClassvar3" 实际上是 "\0MyClass\0var3"

修复断言:

$this->assertEquals(
    "O:7:\"MyClass\":2:{s:13:\"\x00MyClass\x00var3\";N;s:4:\"var1\";N;}",
    serialize($m)
);

使用此断言将导致测试正常工作。


You don't see the Problem because you are using a quite old PHPUnit Version. The current version is 3.6.5 and if you can you should upgrade.

PHPUnit > 3.6 will show a differnt diff to you when the string contains unprintable characters. As is the case here.

Here is the output using a more current Version. The explanation on why it fails is below:

phpunit
PHPUnit 3.6.5 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 3.25Mb

There was 1 failure:

1) MyTest::test_print_r
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'O:7:"MyClass":2:{s:13:"MyClassvar3";N;s:4:"var1";N;}'
+Binary String: 0x4f3a373a224d79436c617373223a323a7b733a31333a22004d79436c6173730076617233223b4e3b733a343a2276617231223b4e3b7d

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

Explanation

A serialized php string contains NULL BYTES to denote private and protected class variables.

The string "MyClassvar3" is really "\0MyClass\0var3".

To fix the assertion:

$this->assertEquals(
    "O:7:\"MyClass\":2:{s:13:\"\x00MyClass\x00var3\";N;s:4:\"var1\";N;}",
    serialize($m)
);

Using this will result in the test working.


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文