From df66d1550b6d6ec75115235986f371af4655242b Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Tue, 30 Oct 2018 09:33:56 +0000 Subject: [PATCH 1/2] Compare readable stream content --- src/ResourceComparator.php | 27 +++++++++++++++- tests/ResourceComparatorTest.php | 53 ++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 3 deletions(-) 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..aae1cb2 100644 --- a/tests/ResourceComparatorTest.php +++ b/tests/ResourceComparatorTest.php @@ -56,22 +56,71 @@ public function acceptsFailsProvider() public function assertEqualsSucceedsProvider() { $tmpfile1 = \tmpfile(); + \fwrite($tmpfile1, 'foo'); $tmpfile2 = \tmpfile(); + \fwrite($tmpfile2, 'foo'); + + $memory1 = \fopen('php://memory', 'r+'); + \fwrite($memory1, 'foo'); + $memory2 = \fopen('php://memory', 'w+'); + \fwrite($memory2, 'foo'); + $memory3 = \fopen('php://memory', 'a+'); + \fwrite($memory3, 'foo'); + $memory4 = \fopen('php://memory', 'x+'); + \fwrite($memory4, 'foo'); + $memory5 = \fopen('php://memory', 'c+'); + \fwrite($memory5, 'foo'); + + $image1 = \imagecreate(100, 100); + $image2 = \imagecreate(100, 100); return [ [$tmpfile1, $tmpfile1], - [$tmpfile2, $tmpfile2] + [$tmpfile2, $tmpfile2], + [$tmpfile1, $tmpfile2], + [$tmpfile2, $tmpfile1], + [$tmpfile1, $memory1], + [$memory1, $tmpfile1], + [$memory1, $memory1], + [$memory2, $memory2], + [$memory3, $memory3], + [$memory1, $memory2], + [$memory2, $memory1], + [$memory1, $memory3], + [$memory1, $memory4], + [$memory1, $memory5], + [$image1, $image1], + [$image2, $image2] ]; } public function assertEqualsFailsProvider() { $tmpfile1 = \tmpfile(); + \fwrite($tmpfile1, 'foo'); $tmpfile2 = \tmpfile(); + $memory1 = \fopen('php://memory', 'r+'); + \fwrite($memory1, 'foo'); + $memory2 = \fopen('php://memory', 'r+'); + \fwrite($memory2, 'bar'); + $memory3 = \fopen('php://memory', 'x'); + \fwrite($memory3, 'foo'); + $memory4 = \fopen('php://memory', 'c'); + \fwrite($memory4, 'foo'); + + $image1 = \imagecreate(100, 100); + $image2 = \imagecreate(100, 100); + return [ [$tmpfile1, $tmpfile2], - [$tmpfile2, $tmpfile1] + [$tmpfile2, $tmpfile1], + [$memory1, $memory2], + [$memory2, $memory1], + [$memory1, $memory3], + [$memory1, $memory4], + [$image1, $image2], + [$image2, $image1] ]; } From fe3cbff8c6e6039b0660f1f78290ab7396c3f8fd Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Tue, 30 Oct 2018 09:55:18 +0000 Subject: [PATCH 2/2] Those modes don't seem to work with php://memory --- tests/ResourceComparatorTest.php | 37 ++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/tests/ResourceComparatorTest.php b/tests/ResourceComparatorTest.php index aae1cb2..a8c794e 100644 --- a/tests/ResourceComparatorTest.php +++ b/tests/ResourceComparatorTest.php @@ -59,17 +59,17 @@ public function assertEqualsSucceedsProvider() \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'); - $memory3 = \fopen('php://memory', 'a+'); - \fwrite($memory3, 'foo'); - $memory4 = \fopen('php://memory', 'x+'); - \fwrite($memory4, 'foo'); - $memory5 = \fopen('php://memory', 'c+'); - \fwrite($memory5, 'foo'); $image1 = \imagecreate(100, 100); $image2 = \imagecreate(100, 100); @@ -79,16 +79,18 @@ public function assertEqualsSucceedsProvider() [$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], - [$memory3, $memory3], [$memory1, $memory2], [$memory2, $memory1], - [$memory1, $memory3], - [$memory1, $memory4], - [$memory1, $memory5], [$image1, $image1], [$image2, $image2] ]; @@ -99,15 +101,14 @@ 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'); - $memory3 = \fopen('php://memory', 'x'); - \fwrite($memory3, 'foo'); - $memory4 = \fopen('php://memory', 'c'); - \fwrite($memory4, 'foo'); $image1 = \imagecreate(100, 100); $image2 = \imagecreate(100, 100); @@ -115,10 +116,14 @@ public function assertEqualsFailsProvider() return [ [$tmpfile1, $tmpfile2], [$tmpfile2, $tmpfile1], + [$tmpfile2, $tmpfile3], + [$tmpfile3, $tmpfile2], + [$tmpfile2, $tmpfile4], + [$tmpfile4, $tmpfile2], + [$tmpfile2, $tmpfile5], + [$tmpfile5, $tmpfile2], [$memory1, $memory2], [$memory2, $memory1], - [$memory1, $memory3], - [$memory1, $memory4], [$image1, $image2], [$image2, $image1] ];