Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions Tests/Functional/Tools/Mapper/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -47,15 +47,15 @@ public function dataProviderForCorrectMappings()
'x' => 11,
'y' => 12,
'z' => 13,
'array' => []
'array' => [],
],
],
'Test null values' => [
'mappings' => [
'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',
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -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
*/
Expand Down
23 changes: 16 additions & 7 deletions Tools/Mapper/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function addMappings(array $mappings)
*/
public function formatDate($format, \DateTime $date = null)
{
if ($date === null) {
if (null === $date) {
return null;
}

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
*/
Expand All @@ -320,6 +328,7 @@ public function wordWrap($string, $length, $section)
if (isset($lines[$section])) {
$result = $lines[$section];
}

return $result;
}

Expand Down