Skip to content

Commit

Permalink
Improve Unit Tests and add behavior for null values.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason-Rev committed Sep 24, 2015
1 parent 52c6a82 commit 66b894a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 7 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
<directory>test</directory>
</exclude>
</whitelist>
</filter> -->
</filter>
</phpunit>
5 changes: 5 additions & 0 deletions src/getValueFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ function getValue($doc, $fieldName, $default = null) {
return $default;
}

// Special case for null, we want to return the default in this case.
if (is_null($doc)) {
return $default;
}

throw new UnableToGetFieldException("Unable to get field: '$fieldName'");
}

Expand Down
7 changes: 6 additions & 1 deletion src/setValueFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ function setValue($doc, $fieldName, $value) {
if ($doc instanceof GetSetInterface) {
return $doc->setValue($fieldName, $value);
}
if (is_null($doc)) {
$doc = array();
}
if (is_array($doc) || $doc instanceof \ArrayAccess) {
$doc[$fieldName] = $value;
return $doc;
}

if (is_object($doc)) {
if (is_object($doc) && ! $doc instanceof \Closure) {
// Does the property exist?
$fields = get_object_vars($doc);
if (array_key_exists($fieldName, $fields)) {
Expand Down Expand Up @@ -87,6 +90,8 @@ function setValueByArrayPath($doc, $fieldPath, $value) {
if (empty($fieldPath)) {
return $value;
}
// Special case for nulls, treat them like empty arrays.
$doc = is_null($doc) ? array() : $doc;
$fieldName = array_shift($fieldPath);
$subDoc = getValue($doc, $fieldName, array());
$subDocUpdated = setValueByArrayPath($subDoc, $fieldPath, $value);
Expand Down
21 changes: 21 additions & 0 deletions test/GetFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,25 @@ public function testGetMagic() {
$this->assertNull(gs\getValue($testClass, 'a', $notFound));
}

public function testNull() {
$notFound = (object) array();
$this->assertEquals($notFound, gs\getValue(null, 'name', $notFound));
}

public function testNullFields() {
$doc = array(
'location' => null,
'info' => array(
'companyName' => null,
'address' => null,
'extra' => null,
),
);

$notFound = (object) array();
$this->assertEquals($notFound, gs\get($doc, 'location.lat', $notFound));
$this->assertNull(gs\get($doc, 'location', $notFound));
}


}
65 changes: 60 additions & 5 deletions test/SetFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,84 @@ public function testSetValueMagic() {
}

public function providerNonObjects() {
array(
return array(
array(1),
array(9.2),
array('hello'),
array(null),
array(true),
array(false),
array(function(){}),
);
}

/**
* @dataProvider providerSetters
* @expectedException Revinate\GetterSetter\UnableToSetFieldException
* @param $nonObject
* @dataProvider providerNonObjects
* @expectedException \Revinate\GetterSetter\UnableToSetFieldException
*/
public function testSetNonObject($nonObject) {
gs\setValue($nonObject, 'value', 42);
}

/**
* @param $nonObject
* @dataProvider providerSetters
* @expectedException Revinate\GetterSetter\UnableToGetFieldException
* @expectedException \Revinate\GetterSetter\UnableToGetFieldException
*/
public function testGetNonObject($nonObject) {
gs\getValue($nonObject, 'value');
}

public function testSetNullField() {
$doc = array(
'location' => null,
'info' => array(
'companyName' => null,
'address' => null,
'extra' => null,
),
);

$this->assertEquals(
array(
'location' => array('lat'=>55.6, 'lon'=>-0.6),
'info' => array(
'companyName' => null,
'address' => null,
'extra' => null,
),
),
gs\set(gs\set($doc, 'location.lat', 55.6), 'location.lon', -0.6)
);

$this->assertEquals(
array(
'location' => null,
'info' => array(
'companyName' => null,
'address' => null,
'extra' => array(
'level1' => array(
'level2' => 'level2',
)
),
),
),
gs\set($doc, 'info.extra.level1.level2', 'level2')
);
}

public function testSetNull() {
$this->assertEquals(
array(
'name'=>array('first'=>'Joe')
),
gs\set(null, 'name.first', 'Joe')
);

$this->assertEquals(
array('name.first'=>'Joe'),
gs\setValue(null, 'name.first', 'Joe')
);
}
}

0 comments on commit 66b894a

Please sign in to comment.