Skip to content

Commit

Permalink
Merge pull request #59 from brianjmiller/issue48
Browse files Browse the repository at this point in the history
Adjust `asVersion` trait to handle arrays and empty values
  • Loading branch information
brianjmiller committed Apr 5, 2016
2 parents 3aa6057 + 608ec2f commit d329340
Show file tree
Hide file tree
Showing 22 changed files with 801 additions and 136 deletions.
10 changes: 7 additions & 3 deletions src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ public function asVersion($version) {
}

//
// only one of these
// only one of these, note that if 'account' has been set
// but is returned empty then no IFI will be included
//
if (isset($this->account)) {
$result['account'] = $this->account->asVersion($version);
$versioned_acct = $this->account->asVersion($version);
if (! empty($versioned_acct)) {
$result['account'] = $versioned_acct;
}
}
elseif (isset($this->mbox_sha1sum)) {
$result['mbox_sha1sum'] = $this->mbox_sha1sum;
Expand Down Expand Up @@ -90,7 +94,7 @@ public function compareWithSignature($fromSig) {

return array('success' => false, 'reason' => 'Comparison of this.mbox to signature.mbox_sha1sum failed: no match');
}
else if (isset($fromSig->mbox) && isset($this->mbox_sha1sum)) {
elseif (isset($fromSig->mbox) && isset($this->mbox_sha1sum)) {
if ($fromSig->getMbox_sha1sum() === $this->mbox_sha1sum) {
return array('success' => true, 'reason' => null);
}
Expand Down
15 changes: 14 additions & 1 deletion src/AsVersionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,20 @@ public function asVersion($version) {
if ($value instanceof VersionableInterface) {
$value = $value->asVersion($version);
}
if (isset($value)) {
else if (is_array($value) && !empty($value)) {
$tmp_value = array();
foreach ($value as $element) {
if ($element instanceof VersionableInterface) {
array_push($tmp_value, $element->asVersion($version));
}
else {
array_push($tmp_value, $element);
}
}
$value = $tmp_value;
}

if (isset($value) && (!is_array($value) || !empty($value))) {
$result[$property] = $value;
}
}
Expand Down
11 changes: 0 additions & 11 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,4 @@ public function setExtensions($value) {
return $this;
}
public function getExtensions() { return $this->extensions; }

private function _asVersion(array &$result, $version) {
foreach ($result as $property => $value) {
if (! isset($value)) {
unset($result[$property]);
}
elseif ($value instanceof VersionableInterface) {
$result[$property] = $value->asVersion($version);
}
}
}
}
15 changes: 0 additions & 15 deletions src/ContextActivities.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,6 @@ public function __construct() {
}
}

private function _asVersion(array &$result, $version) {
foreach ($result as $property => $value) {
if (empty($value)) {
unset($result[$property]);
}
elseif (is_array($value)) {
$this->_asVersion($value, $version);
$result[$property] = $value;
}
elseif ($value instanceof VersionableInterface) {
$result[$property] = $value->asVersion($version);
}
}
}

private function _listSetter($prop, $value) {
if (is_array($value)) {
if (isset($value['id'])) {
Expand Down
9 changes: 9 additions & 0 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public function __construct() {
}
}

private function _asVersion(&$result, $version) {
//
// empty string is an invalid duration
//
if (isset($result['duration']) && $result['duration'] == '') {
unset($result['duration']);
}
}

public function setScore($value) {
if (! $value instanceof Score && is_array($value)) {
$value = new Score($value);
Expand Down
12 changes: 0 additions & 12 deletions src/StatementBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,6 @@ public function __construct() {
}

private function _asVersion(&$result, $version) {
foreach ($result as $property => $value) {
if ($value !== false && empty($value)) {
unset($result[$property]);
}
elseif (is_array($value)) {
$this->_asVersion($value, $version);
$result[$property] = $value;
}
elseif ($value instanceof VersionableInterface) {
$result[$property] = $value->asVersion($version);
}
}
if (isset($result['target'])) {
$result['object'] = $result['target'];
unset($result['target']);
Expand Down
53 changes: 49 additions & 4 deletions tests/ActivityDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,55 @@ public function testUsesAsVersionTrait() {

// TODO: need more robust test (happy-path)
public function testAsVersion() {
$args = ['name' => [self::NAME]];
$obj = new ActivityDefinition($args);
$versioned = $obj->asVersion('test');
$args = [
'name' => ['en' => self::NAME],
'extensions' => []
];
$args['extensions'][COMMON_EXTENSION_ID_1] = 'test';

$this->assertEquals($versioned, $args, "name only: test");
$obj = ActivityDefinition::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$this->assertEquals($versioned, $args, "serialized version matches original");
}

public function testAsVersionEmpty() {
$args = [];

$obj = ActivityDefinition::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$this->assertEquals($versioned, $args, "serialized version matches original");
}

public function testAsVersionEmptyLanguageMap() {
$args = ['name' => []];

$obj = ActivityDefinition::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

unset($args['name']);

$this->assertEquals($versioned, $args, "serialized version matches corrected");
}

public function testAsVersionEmptyExtensions() {
$args = ['extensions' => []];

$obj = ActivityDefinition::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

unset($args['extensions']);

$this->assertEquals($versioned, $args, "serialized version matches corrected");
}

public function testAsVersionEmptyStringInLanguageMap() {
$args = ['name' => ['en' => '']];

$obj = ActivityDefinition::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$this->assertEquals($versioned, $args, "serialized version matches original");
}
}
58 changes: 50 additions & 8 deletions tests/ActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,58 @@ public function testFromJSONIDOnly() {

// TODO: need to loop versions
public function testAsVersion() {
$obj = new Activity(
array('id' => COMMON_ACTIVITY_ID)
);
$args = [
'id' => COMMON_ACTIVITY_ID,
'definition' => [
'name' => [
'en' => 'test'
]
]
];

$obj = Activity::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$this->assertEquals(
$versioned,
[ 'objectType' => 'Activity', 'id' => COMMON_ACTIVITY_ID ],
"id only: 1.0.0"
);
$args['objectType'] = 'Activity';

$this->assertEquals($versioned, $args, "serialized version matches corrected");
}

public function testAsVersionEmpty() {
$args = [];

$obj = Activity::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$args['objectType'] = 'Activity';

$this->assertEquals($versioned, $args, "serialized version matches corrected");
}

public function testAsVersionIdOnly() {
$args = [ 'id' => COMMON_ACTIVITY_ID ];

$obj = Activity::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$args['objectType'] = 'Activity';

$this->assertEquals($versioned, $args, "serialized version matches corrected");
}

public function testAsVersionEmptyDefinition() {
$args = [
'id' => COMMON_ACTIVITY_ID,
'definition' => []
];

$obj = Activity::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$args['objectType'] = 'Activity';
unset($args['definition']);

$this->assertEquals($versioned, $args, "serialized version matches corrected");
}

public function testCompareWithSignature() {
Expand Down
31 changes: 28 additions & 3 deletions tests/AgentAccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,36 @@ public function testHomePage() {
}

public function testAsVersion() {
$args = ['name' => COMMON_ACCT_NAME, 'homePage' => COMMON_ACCT_HOMEPAGE];
$obj = new AgentAccount($args);
$args = [
'name' => COMMON_ACCT_NAME,
'homePage' => COMMON_ACCT_HOMEPAGE
];

$obj = AgentAccount::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$this->assertEquals($versioned, $args, "serialized version matches original");
}

public function testAsVersionEmpty() {
$args = [];

$obj = AgentAccount::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$this->assertEquals($versioned, $args, "serialized version matches original");
}

public function testAsVersionEmptyStrings() {
$args = [
'name' => '',
'homePage' => ''
];

$obj = AgentAccount::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$this->assertEquals($versioned, $args, "all properties: test");
$this->assertEquals($versioned, $args, "serialized version matches original");
}

public function testCompareWithSignature() {
Expand Down
88 changes: 79 additions & 9 deletions tests/AgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,87 @@ public function testFromJSONInstantiations() {
}

// TODO: need to loop versions
public function testAsVersion() {
$obj = new Agent(
[ 'mbox' => COMMON_MBOX ]
);
public function testAsVersionMbox() {
$args = [
'mbox' => COMMON_MBOX
];

$obj = Agent::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$this->assertEquals(
[ 'objectType' => 'Agent', 'mbox' => COMMON_MBOX ],
$versioned,
"mbox only: 1.0.0"
);
$args['objectType'] = 'Agent';

$this->assertEquals($versioned, $args, "serialized version matches corrected");
}

public function testAsVersionMboxSha1() {
$args = [
'mbox_sha1sum' => COMMON_MBOX_SHA1
];

$obj = Agent::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$args['objectType'] = 'Agent';

$this->assertEquals($versioned, $args, "serialized version matches corrected");
}

public function testAsVersionAccount() {
$args = [
'account' => [
'name' => COMMON_ACCT_NAME,
'homePage' => COMMON_ACCT_HOMEPAGE
]
];

$obj = Agent::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$args['objectType'] = 'Agent';

$this->assertEquals($versioned, $args, "serialized version matches corrected");
}

public function testAsVersionAccountEmptyStrings() {
$args = [
'account' => [
'name' => '',
'homePage' => ''
]
];

$obj = Agent::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$args['objectType'] = 'Agent';

$this->assertEquals($versioned, $args, "serialized version matches corrected");
}

public function testAsVersionEmptyAccount() {
$args = [
'account' => []
];

$obj = Agent::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$args['objectType'] = 'Agent';
unset($args['account']);

$this->assertEquals($versioned, $args, "serialized version matches corrected");
}

public function testAsVersionEmpty() {
$args = [];

$obj = Agent::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');

$args['objectType'] = 'Agent';

$this->assertEquals($versioned, $args, "serialized version matches corrected");
}

public function testIsIdentified() {
Expand Down
Loading

0 comments on commit d329340

Please sign in to comment.