From ee58fbeb7512059cb6e4ae62191360c915ed14b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Mon, 18 Sep 2017 17:26:14 +0200 Subject: [PATCH] Fix integration tests on PHP 5.6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As stated in the documentation for "uasort", "if two members compare as equal, their relative order in the sorted array is undefined". "uasort" is used in "RoomController" to sort the participants of a room by their last ping, so when two participants have the same last ping they could be returned in any order. Although undefined, the order is probably consistent (so, whatever the order is, it is the same between different executions for the same data). When using PHP 7 the participants are sorted in the same order that currently appears in the integration test definitions. However, PHP 5.6 uses a different order, so the tests fail in that case. This commit adds sorting of the participants in a room returned by the server, so they can be checked against the expected participant list no matter the order used by the server; the list is sorted by default, but that behaviour can be prevented adding ` [exact order]` to the participant list in a test definition, so the participant list can be checked in the exact order returned by the server too (for example, if needed to check the participant list after setting the last ping for them). Signed-off-by: Daniel Calviño Sánchez --- .../features/bootstrap/FeatureContext.php | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tests/integration/features/bootstrap/FeatureContext.php b/tests/integration/features/bootstrap/FeatureContext.php index 59faf689a..d4bd4bd0a 100644 --- a/tests/integration/features/bootstrap/FeatureContext.php +++ b/tests/integration/features/bootstrap/FeatureContext.php @@ -80,16 +80,33 @@ class FeatureContext implements Context, SnippetAcceptingContext { PHPUnit_Framework_Assert::assertCount(count($formData->getHash()), $rooms, 'Room count does not match'); - PHPUnit_Framework_Assert::assertEquals($formData->getHash(), array_map(function($room) { + PHPUnit_Framework_Assert::assertEquals($formData->getHash(), array_map(function($room, $expectedRoom) { + $participantNames = array_map(function($participant) { + return $participant['name']; + }, $room['participants']); + + // When participants have the same last ping the order in which they + // are returned from the server is undefined. That is the most + // common case during the tests, so by default the list of + // participants returned by the server is sorted alphabetically. In + // order to check the exact order of participants returned by the + // server " [exact order]" can be appended in the test definition to + // the list of expected participants of the room. + if (strpos($expectedRoom['participants'], ' [exact order]') === false) { + sort($participantNames); + } else { + // Append " [exact order]" to the last participant so the + // imploded string is the same as the expected one. + $participantNames[end(array_keys($participantNames))] .= ' [exact order]'; + } + return [ 'id' => self::$tokenToIdentifier[$room['token']], 'type' => (string) $room['type'], 'participantType' => (string) $room['participantType'], - 'participants' => implode(', ', array_map(function($participant) { - return $participant['name']; - }, $room['participants'])), + 'participants' => implode(', ', $participantNames), ]; - }, $rooms)); + }, $rooms, $formData->getHash())); } /**