From ea879814e95dcbf58719637873caecf6a3a6059a Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 30 Jun 2015 21:08:21 +0200 Subject: [PATCH 1/2] Make sure is-map-detection works when Traverable contains a mix of values Before a as-map detected Traversable is not properly detected when the last key is int-keyed --- src/Incoming/Structure/StructureFactory.php | 2 +- .../Incoming/Test/Structure/StructureFactoryTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Incoming/Structure/StructureFactory.php b/src/Incoming/Structure/StructureFactory.php index 329d0b7..50cd892 100644 --- a/src/Incoming/Structure/StructureFactory.php +++ b/src/Incoming/Structure/StructureFactory.php @@ -81,7 +81,7 @@ protected static function buildFromTraversable(Traversable $data) // Traverse through the data, but only check the first item's key foreach ($data as $key => &$val) { - $is_map = !is_int($key); + $is_map = $is_map || !is_int($key); $val = self::attemptBuildTraversableLike($val); } diff --git a/tests/Incoming/Test/Structure/StructureFactoryTest.php b/tests/Incoming/Test/Structure/StructureFactoryTest.php index 13f2338..ecb24ea 100644 --- a/tests/Incoming/Test/Structure/StructureFactoryTest.php +++ b/tests/Incoming/Test/Structure/StructureFactoryTest.php @@ -122,4 +122,16 @@ public function testBuildWithInvalidStructuralType() $structure = (new StructureFactory)->build($data); } + + /** + * @see https://github.com/Rican7/incoming/pull/3 + */ + public function testBuildWithTraversableAndMixedKeys() + { + $data = ['name' => 'markus', 0 => 'a crazy mixed-in key']; + + $structure = (new StructureFactory)->build(new ArrayIterator($data)); + + $this->assertInstanceOf('Incoming\Structure\Map', $structure); + } } From 5561a761108ed2ff65d39b64f1a66b1bd414341f Mon Sep 17 00:00:00 2001 From: Trevor Suarez Date: Wed, 1 Jul 2015 10:35:20 -0400 Subject: [PATCH 2/2] A little test cleanup --- .../Test/Structure/StructureFactoryTest.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/Incoming/Test/Structure/StructureFactoryTest.php b/tests/Incoming/Test/Structure/StructureFactoryTest.php index ecb24ea..cfdad4f 100644 --- a/tests/Incoming/Test/Structure/StructureFactoryTest.php +++ b/tests/Incoming/Test/Structure/StructureFactoryTest.php @@ -122,16 +122,19 @@ public function testBuildWithInvalidStructuralType() $structure = (new StructureFactory)->build($data); } - + /** - * @see https://github.com/Rican7/incoming/pull/3 + * @link https://github.com/Rican7/incoming/pull/3 */ public function testBuildWithTraversableAndMixedKeys() { - $data = ['name' => 'markus', 0 => 'a crazy mixed-in key']; + $data = new ArrayIterator([ + 'name' => 'markus', + 0 => 'a crazy mixed-in key' + ]); - $structure = (new StructureFactory)->build(new ArrayIterator($data)); + $structure = (new StructureFactory)->build($data); - $this->assertInstanceOf('Incoming\Structure\Map', $structure); + $this->assertTrue($structure instanceof Map); } }