phpunit: Move assertArraySubmapSame() to MediaWikiTestCaseTrait

This isn't specific to integration tests.

Rename it to assertArrayContains() as the terms "submap" is uncommon.
The term "same" does correctly connotate strict equality, but seems
redundant/implied when operating on mutiple values, such as with
the built-in assertContains(), assertJsonStringEqualsJsonString(),
etc.

Change-Id: I9d8e7dca128a73d03b173a8ec3566f55edddde6a
This commit is contained in:
Timo Tijhof 2023-03-06 01:30:40 +00:00 committed by Krinkle
parent f25b439b8d
commit 12347418ae
2 changed files with 56 additions and 36 deletions

View file

@ -19,11 +19,9 @@ use MediaWiki\SiteStats\SiteStatsInit;
use MediaWiki\Storage\PageUpdateStatus;
use MediaWiki\Title\Title;
use MediaWiki\User\UserIdentityValue;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestResult;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SebastianBergmann\Comparator\ComparisonFailure;
use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\IMaintainableDatabase;
@ -2316,46 +2314,22 @@ abstract class MediaWikiIntegrationTestCase extends PHPUnit\Framework\TestCase {
}
/**
* Assert that the key-based intersection of the two arrays matches the expected subset
* Assert that an associative array contains the subset of an expected array.
*
* Order does not matter. Strict type and object identity will be checked.
* The internal key order does not matter.
* Values are compared with strict equality.
*
* @param array $expectedSubset
* @param array $actualSuperset
* @param string $description
* @since 1.35
* @param array $expected
* @param array $actual
* @param string $message
*/
protected function assertArraySubmapSame(
array $expectedSubset,
array $actualSuperset,
$description = ''
array $expected,
array $actual,
$message = ''
) {
$patched = array_replace_recursive( $actualSuperset, $expectedSubset );
ksort( $patched );
ksort( $actualSuperset );
$result = ( $actualSuperset === $patched );
if ( !$result ) {
$comparisonFailure = new ComparisonFailure(
$patched,
$actualSuperset,
var_export( $patched, true ),
var_export( $actualSuperset, true )
);
$failureDescription = 'Failed asserting that array contains the expected submap.';
if ( $description != '' ) {
$failureDescription = $description . "\n" . $failureDescription;
}
throw new ExpectationFailedException(
$failureDescription,
$comparisonFailure
);
} else {
$this->assertTrue( true, $description );
}
$this->assertArrayContains( $expected, $actual, $message );
}
/**

View file

@ -2,8 +2,10 @@
use MediaWiki\HookContainer\HookContainer;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Container\ContainerInterface;
use SebastianBergmann\Comparator\ComparisonFailure;
use Wikimedia\ObjectFactory\ObjectFactory;
use Wikimedia\Services\NoSuchServiceException;
use Wikimedia\Timestamp\ConvertibleTimestamp;
@ -194,6 +196,50 @@ trait MediaWikiTestCaseTrait {
$this->assertEquals( file_get_contents( $fileName ), $actualData, $msg );
}
/**
* Assert that an associative array contains the subset of an expected array.
*
* The internal key order does not matter.
* Values are compared with strict equality.
*
* @since 1.41
* @param array $expected
* @param array $actual
* @param string $message
*/
protected function assertArrayContains(
array $expected,
array $actual,
$message = ''
) {
$patched = array_replace_recursive( $actual, $expected );
ksort( $patched );
ksort( $actual );
$result = ( $actual === $patched );
if ( !$result ) {
$comparisonFailure = new ComparisonFailure(
$patched,
$actual,
var_export( $patched, true ),
var_export( $actual, true )
);
$failureDescription = 'Failed asserting that array contains the expected submap.';
if ( $message != '' ) {
$failureDescription = $message . "\n" . $failureDescription;
}
throw new ExpectationFailedException(
$failureDescription,
$comparisonFailure
);
} else {
$this->assertTrue( true, $message );
}
}
/**
* Assert that two arrays are equal. By default this means that both arrays need to hold
* the same set of values. Using additional arguments, order and associated key can also