1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CakePHP2のUnitTestで発生する(Declaration of PHPUnit_Framework_Comparator_DOMDocument::assertEquals)エラーの対応方法

Last updated at Posted at 2022-08-13

現象

PHP7.3、PHPUnit3.7をインストールした環境で、CakePHP2のユニットテストを実行すると下記のようなエラーが発生しました。

image.png

Error: Declaration of PHPUnit_Framework_Comparator_DOMDocument::assertEquals($expected, $actual, $delta = 0, $canonicalize = false, $ignoreCase = false) must be compatible with PHPUnit_Framework_Comparator_Object::assertEquals($expected, $actual, $delta = 0, $canonicalize = false, $ignoreCase = false, array &$processed = Array)
File: /var/www/html/blogTutorial/vendors/phpunit/phpunit/PHPUnit/Framework/Comparator/DOMDocument.php
Line: 259

原因

PHPUnitのバグ
assertEqualsをオーバーライドする際、引数の数が間違っているため。

  • PHPUnit_Framework_Comparator_Object(親)のassertEquals定義
    /approot/vendors/phpunit/phpunit/PHPUnit/Framework/Comparator/Object.php
class PHPUnit_Framework_Comparator_Object extends PHPUnit_Framework_Comparator_Array
{
    public function assertEquals($expected, $actual, $delta = 0, $canonicalize = FALSE, $ignoreCase = FALSE, array &$processed = array())
    {
  • PHPUnit_Framework_Comparator_DOMDocument(子)のassertEquals
    /approot/vendors/phpunit/phpunit/PHPUnit/Framework/Comparator/DOMDocument.php
class PHPUnit_Framework_Comparator_DOMDocument extends PHPUnit_Framework_Comparator_Object
{
    // ~~ 途中略 ~~
    public function assertEquals($expected, $actual, $delta = 0, $canonicalize = FALSE, $ignoreCase = FALSE)

対応方法

  • ①安直な方法(あまり褒められたやり方ではないですが・・・・)

引数を親に合わせるため、直接書き換える

/approot/vendors/phpunit/phpunit/PHPUnit/Framework/Comparator/DOMDocument.php

class PHPUnit_Framework_Comparator_DOMDocument extends PHPUnit_Framework_Comparator_Object
{
    // ~~ 途中略 ~~
    public function assertEquals($expected, $actual, $delta = 0, $canonicalize = FALSE, $ignoreCase = FALSE, array &$processed = array())
  • ②PHPUnitのバージョンを新しくする方法もあるようです。参考ページをご確認ください。

参考ページ

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?