diff --git a/src/ResourceComparator.php b/src/ResourceComparator.php index 5471826..11ba8c2 100644 --- a/src/ResourceComparator.php +++ b/src/ResourceComparator.php @@ -40,7 +40,7 @@ public function accepts($expected, $actual) */ public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false) { - if ($actual != $expected) { + if ($this->asString($actual) !== $this->asString($expected)) { throw new ComparisonFailure( $expected, $actual, @@ -49,4 +49,29 @@ public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = f ); } } + + private function asString($resource) + { + if ('stream' !== \get_resource_type($resource)) { + return (string) $resource; + } + + $metaData = \stream_get_meta_data($resource); + + if (!\preg_match('(a\+|c\+|r|w\+|x\+)', $metaData['mode'])) { + return (string) $resource; + } + + $position = \ftell($resource); + \rewind($resource); + + $context = \hash_init('md5'); + \hash_update_stream($context, $resource); + + if (\is_int($position)) { + \fseek($resource, $position); + } + + return \hash_final($context); + } } diff --git a/tests/ResourceComparatorTest.php b/tests/ResourceComparatorTest.php index 158aaa9..a8c794e 100644 --- a/tests/ResourceComparatorTest.php +++ b/tests/ResourceComparatorTest.php @@ -56,22 +56,76 @@ public function acceptsFailsProvider() public function assertEqualsSucceedsProvider() { $tmpfile1 = \tmpfile(); + \fwrite($tmpfile1, 'foo'); $tmpfile2 = \tmpfile(); + \fwrite($tmpfile2, 'foo'); + $tmpfile3 = \fopen(\tempnam(\sys_get_temp_dir(), ''), 'a+'); + \fwrite($tmpfile3, 'foo'); + $tmpfile4 = \fopen(\sys_get_temp_dir().'/'.\uniqid('', true), 'x+'); + \fwrite($tmpfile4, 'foo'); + $tmpfile5 = \fopen(\tempnam(\sys_get_temp_dir(), ''), 'c+'); + \fwrite($tmpfile5, 'foo'); + + $memory1 = \fopen('php://memory', 'r+'); + \fwrite($memory1, 'foo'); + $memory2 = \fopen('php://memory', 'w+'); + \fwrite($memory2, 'foo'); + + $image1 = \imagecreate(100, 100); + $image2 = \imagecreate(100, 100); return [ [$tmpfile1, $tmpfile1], - [$tmpfile2, $tmpfile2] + [$tmpfile2, $tmpfile2], + [$tmpfile1, $tmpfile2], + [$tmpfile2, $tmpfile1], + [$tmpfile1, $tmpfile3], + [$tmpfile3, $tmpfile1], + [$tmpfile1, $tmpfile4], + [$tmpfile4, $tmpfile1], + [$tmpfile1, $tmpfile5], + [$tmpfile5, $tmpfile1], + [$tmpfile1, $memory1], + [$memory1, $tmpfile1], + [$memory1, $memory1], + [$memory2, $memory2], + [$memory1, $memory2], + [$memory2, $memory1], + [$image1, $image1], + [$image2, $image2] ]; } public function assertEqualsFailsProvider() { $tmpfile1 = \tmpfile(); + \fwrite($tmpfile1, 'foo'); $tmpfile2 = \tmpfile(); + $tmpfile3 = \fopen(\tempnam(\sys_get_temp_dir(), ''), 'a'); + $tmpfile4 = \fopen(\sys_get_temp_dir().'/'.\uniqid('', true), 'x'); + $tmpfile5 = \fopen(\tempnam(\sys_get_temp_dir(), ''), 'c'); + + $memory1 = \fopen('php://memory', 'r+'); + \fwrite($memory1, 'foo'); + $memory2 = \fopen('php://memory', 'r+'); + \fwrite($memory2, 'bar'); + + $image1 = \imagecreate(100, 100); + $image2 = \imagecreate(100, 100); return [ [$tmpfile1, $tmpfile2], - [$tmpfile2, $tmpfile1] + [$tmpfile2, $tmpfile1], + [$tmpfile2, $tmpfile3], + [$tmpfile3, $tmpfile2], + [$tmpfile2, $tmpfile4], + [$tmpfile4, $tmpfile2], + [$tmpfile2, $tmpfile5], + [$tmpfile5, $tmpfile2], + [$memory1, $memory2], + [$memory2, $memory1], + [$image1, $image2], + [$image2, $image1] ]; }