在 Matomo 中尝试 运行 资产等于数组时单元测试失败
Unit test fail when try run asset equals of array in Matomo
可能需要启用 PHP 扩展才能完成这项工作。当在数组中使用 usort
时,所有带有 assertEquals
的测试都会下降。
下面看看跌落测试的结果:
13) Piwik\Tests\Unit\DataAccess\JoinGeneratorTest::test_sortTablesForJoin_shouldSortTablesWithCustomJoinRequiringEachOther2
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
Array (
0 => 'log_link_visit_action'
1 => Array (
'table' => 'log_action'
- 'tableAlias' => 'log_action_idaction_event_action'
- 'joinOn' => 'log_link_visit_action.idaction_event_action = log_action_idaction_event_action.idaction'
+ 'tableAlias' => 'log_action_visit_entry_idaction_name'
+ 'joinOn' => 'log_visit.visit_entry_idaction_name = log_action_visit_entry_idaction_name.idaction'
)
2 => Array (
'table' => 'log_action'
- 'tableAlias' => 'log_action_visit_entry_idaction_name'
- 'joinOn' => 'log_visit.visit_entry_idaction_name = log_action_visit_entry_idaction_name.idaction'
+ 'tableAlias' => 'log_action_idaction_event_action'
+ 'joinOn' => 'log_link_visit_action.idaction_event_action = log_action_idaction_event_action.idaction'
)
)
/matomo-3.5.1/tests/PHPUnit/Unit/DataAccess/LogQueryBuilder/JoinGeneratorTest.php:428
FAILURES!
Tests: 6521, Assertions: 10544, Failures: 13.
也许你应该尝试 assertSame
更好的数组
数组的排序顺序必须相同,assertEquals
和 assertSame
才能通过。想象一下,如果你 json_encode 两者最终是否会得到相同的结果。根据 phpunit 输出,看起来您的数组的排序顺序不同。
<?php
class ArrayTest extends PHPUnit\Framework\TestCase {
// Fails
public function testArraysEqualsDifferentOrder() {
$a = Array(['3', '2'], '1');
$b = Array(['2', '3'], '1');
$this->assertEquals($a, $b);
}
// Fails
public function testArraysSameDifferentOrder() {
$a = Array(['3', '2'], '1');
$b = Array(['2', '3'], '1');
$this->assertSame($a, $b);
}
// Passes
public function testArraysEqualSameOrder() {
$a = Array(['2', '3'], '1');
$b = Array(['2', '3'], '1');
$this->assertEquals($a, $b);
}
// Passes
public function testArraysSameSameOrder() {
$a = Array(['2', '3'], '1');
$b = Array(['2', '3'], '1');
$this->assertSame($a, $b);
}
}
可能需要启用 PHP 扩展才能完成这项工作。当在数组中使用 usort
时,所有带有 assertEquals
的测试都会下降。
下面看看跌落测试的结果:
13) Piwik\Tests\Unit\DataAccess\JoinGeneratorTest::test_sortTablesForJoin_shouldSortTablesWithCustomJoinRequiringEachOther2
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
Array (
0 => 'log_link_visit_action'
1 => Array (
'table' => 'log_action'
- 'tableAlias' => 'log_action_idaction_event_action'
- 'joinOn' => 'log_link_visit_action.idaction_event_action = log_action_idaction_event_action.idaction'
+ 'tableAlias' => 'log_action_visit_entry_idaction_name'
+ 'joinOn' => 'log_visit.visit_entry_idaction_name = log_action_visit_entry_idaction_name.idaction'
)
2 => Array (
'table' => 'log_action'
- 'tableAlias' => 'log_action_visit_entry_idaction_name'
- 'joinOn' => 'log_visit.visit_entry_idaction_name = log_action_visit_entry_idaction_name.idaction'
+ 'tableAlias' => 'log_action_idaction_event_action'
+ 'joinOn' => 'log_link_visit_action.idaction_event_action = log_action_idaction_event_action.idaction'
)
)
/matomo-3.5.1/tests/PHPUnit/Unit/DataAccess/LogQueryBuilder/JoinGeneratorTest.php:428
FAILURES!
Tests: 6521, Assertions: 10544, Failures: 13.
也许你应该尝试 assertSame
更好的数组
数组的排序顺序必须相同,assertEquals
和 assertSame
才能通过。想象一下,如果你 json_encode 两者最终是否会得到相同的结果。根据 phpunit 输出,看起来您的数组的排序顺序不同。
<?php
class ArrayTest extends PHPUnit\Framework\TestCase {
// Fails
public function testArraysEqualsDifferentOrder() {
$a = Array(['3', '2'], '1');
$b = Array(['2', '3'], '1');
$this->assertEquals($a, $b);
}
// Fails
public function testArraysSameDifferentOrder() {
$a = Array(['3', '2'], '1');
$b = Array(['2', '3'], '1');
$this->assertSame($a, $b);
}
// Passes
public function testArraysEqualSameOrder() {
$a = Array(['2', '3'], '1');
$b = Array(['2', '3'], '1');
$this->assertEquals($a, $b);
}
// Passes
public function testArraysSameSameOrder() {
$a = Array(['2', '3'], '1');
$b = Array(['2', '3'], '1');
$this->assertSame($a, $b);
}
}