- /**
- * Convenience method to assert that actual items array fetched from API is equal to the expected
- * array, Unlike assertEquals this only checks if values of specified keys are equal in both
- * arrays. This could be used e.g. not to compare IDs that could change between test run
- * but only stable keys.
- * Optionally this also checks that specified keys are present in the actual item without
- * performing any checks on the related values.
- *
- * @param array $actualItems array of actual items (associative arrays)
- * @param array $expectedItems array of expected items (associative arrays),
- * those items have less keys than actual items
- * @param array $keysUsedInValueComparison list of keys of the actual item that will be used
- * in the comparison of values
- * @param array $requiredKeys optional, list of keys that must be present in the
- * actual items. Values of those keys are not checked.
- */
- private function assertArraySubsetsEqual(
- array $actualItems,
- array $expectedItems,
- array $keysUsedInValueComparison,
- array $requiredKeys = []
- ) {
- $this->assertCount( count( $expectedItems ), $actualItems );
-
- // not checking values of all keys of the actual item, so removing unwanted keys from comparison
- $actualItemsOnlyComparedValues = array_map(
- function ( array $item ) use ( $keysUsedInValueComparison ) {
- return array_intersect_key( $item, array_flip( $keysUsedInValueComparison ) );
- },
- $actualItems
- );
-
- $this->assertEquals(
- $expectedItems,
- $actualItemsOnlyComparedValues
- );
-
- // Check that each item in $actualItems contains all of keys specified in $requiredKeys
- $actualItemsKeysOnly = array_map( 'array_keys', $actualItems );
- foreach ( $actualItemsKeysOnly as $keysOfTheItem ) {
- $this->assertEmpty( array_diff( $requiredKeys, $keysOfTheItem ) );
- }
- }
-