diff --git a/Tests/Functional/Tools/Mapper/MapperTest.php b/Tests/Functional/Tools/Mapper/MapperTest.php index 4b542639..5f5cefc3 100644 --- a/Tests/Functional/Tools/Mapper/MapperTest.php +++ b/Tests/Functional/Tools/Mapper/MapperTest.php @@ -37,7 +37,7 @@ public function dataProviderForCorrectMappings() 'x' => "obj.get('x') + 1", 'y' => "obj.get('x') + 2", 'z' => "obj.get('x') + 3", - 'array' => "obj.get('a')" + 'array' => "obj.get('a')", ], ], 'mapping_name' => 'x_to_xyz', @@ -47,7 +47,7 @@ public function dataProviderForCorrectMappings() 'x' => 11, 'y' => 12, 'z' => 13, - 'array' => [] + 'array' => [], ], ], 'Test null values' => [ @@ -55,7 +55,7 @@ public function dataProviderForCorrectMappings() 'mapping_name' => [ 'x' => "obj.get('x') + 1", 'y' => "obj.get('y')", - 'z' => "obj.get('y').get('z')" + 'z' => "obj.get('y').get('z')", ], ], 'mapping_name' => 'mapping_name', @@ -123,6 +123,23 @@ public function dataProviderForCorrectMappings() 'date_3' => '2015-01-01T20:00:00.000', ], ], + 'Test keyExists' => [ + 'mappings' => [ + 'mapping_name' => [ + 'test_key_1' => "mapper.keyExists(obj.get('test_array'), 'A')", + 'test_key_2' => "mapper.keyExists(obj.get('test_array'), 'C')", + ], + ], + 'mapping_name' => 'mapping_name', + 'mapped_values' => new SerializableArray([ + 'test_array' => ['A' => 0, 'B' => 0], + ]), + 'context' => [], + 'expected_value' => [ + 'test_key_1' => true, + 'test_key_2' => false, + ], + ], 'Test mapping getting information from the context' => [ 'mappings' => [ 'x_to_xyz' => [ @@ -178,6 +195,40 @@ public function testMapForEmptyMappingName() ); } + /** + * @covers ::map + */ + public function testKeyExistsForExceptions() + { + $this->expectException(\InvalidArgumentException::class); + $this->mapper->addMappings([ + 'mapping_name' => [ + 'test_key_1' => "mapper.keyExists(obj.get('test_array'), obj.get('test_key'))", + ], + ]); + + // Testing 1st argument + $this->expectExceptionMessage('keyExists expected the first argument to be an array'); + $this->mapper->map( + new SerializableArray([ + 'test_array' => 'A', + 'test_key' => 'B', + ]), + 'mapping_name' + ); + + // Testing 2nd argument + $this->expectExceptionMessage('keyExists expected the key (second argument) to be either a string or an integer'); + $this->mapper->map( + new SerializableArray([ + 'test_array' => ['A' => 0, 'B' => 0], + 'test_key' => [10], + ]), + 'mapping_name' + ); + + } + /** * @covers ::map */ diff --git a/Tools/Mapper/Mapper.php b/Tools/Mapper/Mapper.php index 810b43c6..66e1ebaa 100644 --- a/Tools/Mapper/Mapper.php +++ b/Tools/Mapper/Mapper.php @@ -42,7 +42,7 @@ public function addMappings(array $mappings) */ public function formatDate($format, \DateTime $date = null) { - if ($date === null) { + if (null === $date) { return null; } @@ -87,7 +87,7 @@ public function resolve(&$mapping, &$obj, &$context) foreach ($mapping as $key => $value) { $resolved = $this->resolve($value, $obj, $context); - if ($resolved !== null) { + if (null !== $resolved) { $res[$key] = $resolved; } } @@ -144,9 +144,17 @@ public function mapAll($elements, $mappingName, $context = []) * * @return bool */ - public function keyExists(array $obj, $key) + public function keyExists($array, $key) { - return array_key_exists($key, $obj); + if (!is_array($array)) { + throw new \InvalidArgumentException('keyExists expected the first argument to be an array, \''.print_r($array, true).'\' was given.'); + } + + if (!is_string($key) and !is_integer($key)) { + throw new \InvalidArgumentException('keyExists expected the key (second argument) to be either a string or an integer, \''.print_r($key, true).'\' was given.'); + } + + return array_key_exists($key, $array); } /** @@ -302,11 +310,11 @@ public function serializeWithGroup($data, $format, $group) } /** - * Return the n-th section of the given string splitted by piece of the given length + * Return the n-th section of the given string splitted by piece of the given length. * * @param string $string - * @param int $length - * @param int $section + * @param int $length + * @param int $section * * @return string */ @@ -320,6 +328,7 @@ public function wordWrap($string, $length, $section) if (isset($lines[$section])) { $result = $lines[$section]; } + return $result; }