diff --git a/src/Agent.php b/src/Agent.php index 62f3394..8bed2bd 100644 --- a/src/Agent.php +++ b/src/Agent.php @@ -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; @@ -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); } diff --git a/src/AsVersionTrait.php b/src/AsVersionTrait.php index acc217f..4cfef15 100644 --- a/src/AsVersionTrait.php +++ b/src/AsVersionTrait.php @@ -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; } } diff --git a/src/Context.php b/src/Context.php index 52f4496..5ba6965 100644 --- a/src/Context.php +++ b/src/Context.php @@ -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); - } - } - } } diff --git a/src/ContextActivities.php b/src/ContextActivities.php index 2c95ce2..93edc01 100644 --- a/src/ContextActivities.php +++ b/src/ContextActivities.php @@ -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'])) { diff --git a/src/Result.php b/src/Result.php index 9de0bf8..0ae4dd2 100644 --- a/src/Result.php +++ b/src/Result.php @@ -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); diff --git a/src/StatementBase.php b/src/StatementBase.php index 1800619..d63e55a 100644 --- a/src/StatementBase.php +++ b/src/StatementBase.php @@ -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']); diff --git a/tests/ActivityDefinitionTest.php b/tests/ActivityDefinitionTest.php index 3133657..2427b7e 100644 --- a/tests/ActivityDefinitionTest.php +++ b/tests/ActivityDefinitionTest.php @@ -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"); } } diff --git a/tests/ActivityTest.php b/tests/ActivityTest.php index 5101ab5..ecc503c 100644 --- a/tests/ActivityTest.php +++ b/tests/ActivityTest.php @@ -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() { diff --git a/tests/AgentAccountTest.php b/tests/AgentAccountTest.php index 7499cc1..f863a73 100644 --- a/tests/AgentAccountTest.php +++ b/tests/AgentAccountTest.php @@ -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() { diff --git a/tests/AgentTest.php b/tests/AgentTest.php index 10c98d3..406a988 100644 --- a/tests/AgentTest.php +++ b/tests/AgentTest.php @@ -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() { diff --git a/tests/AttachmentTest.php b/tests/AttachmentTest.php index f1272b1..5ce76d8 100644 --- a/tests/AttachmentTest.php +++ b/tests/AttachmentTest.php @@ -120,6 +120,26 @@ public function testAsVersion() { ); } + public function testAsVersionEmpty() { + $args = []; + + $obj = Attachment::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 = ['display' => []]; + + $obj = Attachment::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + unset($args['display']); + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + public function testCompareWithSignature() { $full = [ 'usageType' => self::USAGE_TYPE, diff --git a/tests/ContextActivitiesTest.php b/tests/ContextActivitiesTest.php index 9072784..c8edd2f 100644 --- a/tests/ContextActivitiesTest.php +++ b/tests/ContextActivitiesTest.php @@ -75,18 +75,45 @@ public function testFromJSONInstantiations() { } // TODO: need to loop versions - public function testAsVersion() { - $obj = new ContextActivities(); - $obj->setCategory(self::$common_activity_cfg); + public function testAsVersionWithSingleList() { + $keys = ['category', 'parent', 'grouping', 'other']; + foreach ($keys as $k) { + $args = []; + $args[$k] = [ self::$common_activity_cfg ]; + + $obj = ContextActivities::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $args[$k][0]['objectType'] = 'Activity'; + + $this->assertEquals($versioned, $args, "serialized version matches original"); + + unset($args[$k][0]['objectType']); + } + } + + public function testAsVersionEmpty() { + $args = []; + + $obj = ContextActivities::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); $versioned = $obj->asVersion('1.0.0'); - $this->assertEquals( - ['category' => [ - ['objectType' => 'Activity', 'id' => COMMON_ACTIVITY_ID] - ]], - $versioned, - "category only: 1.0.0" - ); + $this->assertEquals($versioned, $args, "serialized version matches original"); + } + + public function testAsVersionWithEmptyList() { + $keys = ['category', 'parent', 'grouping', 'other']; + foreach ($keys as $k) { + $args = []; + $args[$k] = []; + + $obj = ContextActivities::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + unset($args[$k]); + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } } public function testListSetters() { diff --git a/tests/ContextTest.php b/tests/ContextTest.php index a12fed2..cd514ec 100644 --- a/tests/ContextTest.php +++ b/tests/ContextTest.php @@ -77,18 +77,15 @@ public function testAsVersion() { $args = [ 'registration' => Util::getUUID(), 'instructor' => [ - 'objectType' => 'Agent', - 'name' => 'test agent' + 'name' => 'test agent' ], 'team' => [ - 'objectType' => 'Group', - 'name' => 'test group' + 'name' => 'test group' ], 'contextActivities' => [ 'category' => [ [ - 'objectType' => 'Activity', - 'id' => 'test category' + 'id' => 'test category' ] ] ], @@ -96,16 +93,47 @@ public function testAsVersion() { 'platform' => 'test platform', 'language' => 'test language', 'statement' => [ - 'objectType' => 'StatementRef', - 'id' => Util::getUUID() + 'id' => Util::getUUID() ], - 'extensions' => ['test extension'], + 'extensions' => [], ]; + $args['extensions'][COMMON_EXTENSION_ID_1] = "test"; - $obj = new Context($args); + $obj = Context::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); $versioned = $obj->asVersion('1.0.0'); - $this->assertEquals($versioned, $args, "platform only: 1.0.0"); + $args['instructor']['objectType'] = 'Agent'; + $args['team']['objectType'] = 'Group'; + $args['contextActivities']['category'][0]['objectType'] = 'Activity'; + $args['statement']['objectType'] = 'StatementRef'; + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionEmpty() { + $args = []; + + $obj = Context::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $this->assertEquals($versioned, $args, "serialized version matches original"); + } + + public function testAsVersionEmptyLists() { + $args = [ + 'contextActivities' => [ + 'category' => [] + ], + 'extensions' => [], + ]; + + $obj = Context::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + unset($args['contextActivities']); + unset($args['extensions']); + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); } public function testSetInstructor() { diff --git a/tests/ExtensionsTest.php b/tests/ExtensionsTest.php index fc4a85e..6bfd2c3 100644 --- a/tests/ExtensionsTest.php +++ b/tests/ExtensionsTest.php @@ -38,6 +38,15 @@ public function testAsVersion() { $this->assertEquals($versioned, $args, "version: 1.0.0"); } + public function testAsVersionEmpty() { + $args = []; + + $obj = Extensions::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $this->assertEquals($versioned, null, "serialized version matches original"); + } + public function testCompareWithSignature() { $success = ['success' => true, 'reason' => null]; diff --git a/tests/GroupTest.php b/tests/GroupTest.php index 305029b..5720519 100644 --- a/tests/GroupTest.php +++ b/tests/GroupTest.php @@ -40,15 +40,101 @@ public function testFromJSONInstantiations() { } // TODO: need to loop versions - public function testAsVersion() { - $obj = new Group(); + public function testAsVersionMbox() { + $args = [ + 'mbox' => COMMON_GROUP_MBOX + ]; + + $obj = Group::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); $versioned = $obj->asVersion('1.0.0'); - $this->assertEquals( - [ 'objectType' => 'Group' ], - $versioned, - "empty: 1.0.0" - ); + $args['objectType'] = 'Group'; + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionMboxSha1() { + $args = [ + 'mbox_sha1sum' => COMMON_GROUP_MBOX_SHA1 + ]; + + $obj = Group::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $args['objectType'] = 'Group'; + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionAccount() { + $args = [ + 'account' => [ + 'name' => COMMON_ACCT_NAME, + 'homePage' => COMMON_ACCT_HOMEPAGE + ] + ]; + + $obj = Group::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $args['objectType'] = 'Group'; + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionAccountEmptyStrings() { + $args = [ + 'account' => [ + 'name' => '', + 'homePage' => '' + ] + ]; + + $obj = Group::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $args['objectType'] = 'Group'; + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionEmptyAccount() { + $args = [ + 'account' => [] + ]; + + $obj = Group::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $args['objectType'] = 'Group'; + unset($args['account']); + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionEmptyMember() { + $args = [ + 'member' => [] + ]; + + $obj = Group::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $args['objectType'] = 'Group'; + unset($args['member']); + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionEmpty() { + $args = []; + + $obj = Group::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $args['objectType'] = 'Group'; + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); } public function testAddMember() { diff --git a/tests/LanguageMapTest.php b/tests/LanguageMapTest.php index c7e5418..66ad17a 100644 --- a/tests/LanguageMapTest.php +++ b/tests/LanguageMapTest.php @@ -29,10 +29,29 @@ public function testInstantiation() { public function testAsVersion() { $args = ['en' => [self::NAME]]; - $obj = new LanguageMap($args); + + $obj = LanguageMap::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion(); + + $this->assertEquals($versioned, $args, "serialized version matches original"); + } + + public function testAsVersionEmpty() { + $args = []; + + $obj = LanguageMap::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $this->assertEquals($versioned, null, "serialization returns null"); + } + + public function testAsVersionValueEmptyString() { + $args = ['en' => ['']]; + + $obj = LanguageMap::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); $versioned = $obj->asVersion(); - $this->assertEquals($versioned, $args, "en only: test"); + $this->assertEquals($versioned, $args, "serialized version matches original"); } public function testGetNegotiatedLanguageString() { diff --git a/tests/ResultTest.php b/tests/ResultTest.php index 4d4004e..a2f50bd 100644 --- a/tests/ResultTest.php +++ b/tests/ResultTest.php @@ -76,10 +76,31 @@ public function testAsVersion() { $obj = new Result($args); $versioned = $obj->asVersion('1.0.0'); - $this->assertEquals($versioned, $args, "success only: 1.0.0"); + $this->assertEquals($versioned, $args, "serialized version matches original"); + } + + public function testAsVersionEmpty() { + $args = []; + + $obj = Result::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $this->assertEquals($versioned, $args, "serialized version matches original"); + } + + public function testAsVersionScoreEmpty() { + $args = [ + 'score' => [] + ]; + + $obj = Result::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + unset($args['score']); + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); } - // reported https://github.com/RusticiSoftware/TinCanPHP/issues/34 public function testAsVersionScoreZeroRaw() { $args = [ 'score' => [ @@ -93,6 +114,30 @@ public function testAsVersionScoreZeroRaw() { $this->assertEquals($versioned, $args, "serialized version matches original"); } + public function testAsVersionResponseEmptyString() { + $args = [ + 'response' => '' + ]; + + $obj = Result::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $this->assertEquals($versioned, $args, "serialized version matches original"); + } + + public function testAsVersionDurationEmptyString() { + $args = [ + 'duration' => '' + ]; + + $obj = Result::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + unset($args['duration']); + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + public function testCompareWithSignature() { $score1 = new Score( [ diff --git a/tests/ScoreTest.php b/tests/ScoreTest.php index 47b1f99..4dd98de 100644 --- a/tests/ScoreTest.php +++ b/tests/ScoreTest.php @@ -135,6 +135,44 @@ public function testAsVersion() { $this->assertEquals($versioned, $args, "version: 1.0.0"); } + public function testAsVersionEmpty() { + $obj = new Score([]); + $versioned = $obj->asVersion('1.0.0'); + + $this->assertEquals($versioned, [], "version: 1.0.0"); + } + + public function testAsVersionSingleZero() { + $args = [ + 'raw' => 0 + ]; + $obj = new Score($args); + $versioned = $obj->asVersion('1.0.0'); + + $this->assertEquals($versioned, $args, "version: 1.0.0"); + + $args = [ + 'scaled' => 0 + ]; + $obj = new Score($args); + $versioned = $obj->asVersion('1.0.0'); + + $this->assertEquals($versioned, $args, "version: 1.0.0"); + } + + public function testAsVersionWithZeroScores() { + $args = [ + 'raw' => '0', + 'min' => '-1.0', + 'max' => 2.0, + 'scaled' => 0 + ]; + $obj = new Score($args); + $versioned = $obj->asVersion('1.0.0'); + + $this->assertEquals($versioned, $args, "version: 1.0.0"); + } + public function testCompareWithSignature() { $full = [ 'raw' => 97, diff --git a/tests/StatementRefTest.php b/tests/StatementRefTest.php index 8105c1e..3567d31 100644 --- a/tests/StatementRefTest.php +++ b/tests/StatementRefTest.php @@ -51,13 +51,26 @@ public function testSetIdThrowsException() { // TODO: need to loop versions public function testAsVersion() { $args = [ - 'objectType' => 'StatementRef', 'id' => Util::getUUID(), ]; - $obj = new StatementRef($args); + + $obj = StatementRef::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $args['objectType'] = 'StatementRef'; + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionEmpty() { + $args = []; + + $obj = StatementRef::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); $versioned = $obj->asVersion('1.0.0'); - $this->assertEquals($versioned, $args, "version 1.0.0"); + $args['objectType'] = 'StatementRef'; + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); } public function testCompareWithSignature() { diff --git a/tests/StatementTest.php b/tests/StatementTest.php index b0b3ba5..feff6aa 100644 --- a/tests/StatementTest.php +++ b/tests/StatementTest.php @@ -88,21 +88,11 @@ public function testSetId() { $obj->setId('some invalid id'); } - /* - // TODO: need to loop possible configs - public function testFromJSONInstantiations() { - $obj = Statement::fromJSON('{"id":"' . COMMON_MBOX . '"}'); - $this->assertInstanceOf('TinCan\Statement', $obj); - $this->assertSame(COMMON_MBOX, $obj->getMbox(), 'mbox value'); - } - */ - // TODO: need to loop versions public function testAsVersion() { $args = [ 'actor' => [ 'mbox' => COMMON_MBOX, - 'objectType' => 'Agent', ], 'verb' => [ 'id' => COMMON_VERB_ID, @@ -111,16 +101,15 @@ public function testAsVersion() { ] ], 'object' => [ - 'objectType' => 'Activity', 'id' => COMMON_ACTIVITY_ID, 'definition' => [ 'type' => 'Invalid type', 'name' => [ 'en-US' => 'Test', ], - //'description' => [ - //'en-US' => 'Test description', - //], + 'description' => [ + 'en-US' => 'Test description', + ], 'extensions' => [ 'http://someuri' => 'some value' ], @@ -130,7 +119,6 @@ public function testAsVersion() { 'contextActivities' => [ 'parent' => [ [ - 'objectType' => 'Activity', 'id' => COMMON_ACTIVITY_ID . '/1', 'definition' => [ 'name' => [ @@ -163,21 +151,116 @@ public function testAsVersion() { ] ] ]; - $obj = new Statement($args); + $obj = Statement::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); $obj->stamp(); - $args['id'] = $obj->getId(); + + $versioned = $obj->asVersion('1.0.0'); + + $args['id'] = $obj->getId(); $args['timestamp'] = $obj->getTimestamp(); + $args['actor']['objectType'] = 'Agent'; + $args['object']['objectType'] = 'Activity'; + $args['context']['contextActivities']['parent'][0]['objectType'] = 'Activity'; + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionEmpty() { + $args = []; + + $obj = Statement::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $this->assertEquals($versioned, $args, "serialized version matches original"); + } + + public function testAsVersionEmptySubObjects() { + $args = [ + 'actor' => [ + 'mbox' => COMMON_MBOX, + ], + 'verb' => [ + 'id' => COMMON_VERB_ID, + 'display' => [] + ], + 'object' => [ + 'id' => COMMON_ACTIVITY_ID, + 'definition' => [ + 'type' => 'Invalid type', + 'name' => [], + 'description' => [], + 'extensions' => [], + ] + ], + 'context' => [ + 'contextActivities' => [ + 'parent' => [], + ], + 'registration' => Util::getUUID(), + ], + 'result' => [ + 'completion' => true, + 'success' => false, + 'score' => [] + ], + 'version' => '1.0.0', + 'attachments' => [] + ]; - $obj->getTarget()->getDefinition()->getDescription()->set('en-ES', 'Testo descriptiono'); - $args['object']['definition']['description'] = ['en-ES' => 'Testo descriptiono']; + $obj = Statement::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); - $obj->getTarget()->getDefinition()->getName()->unset('en-US'); + $args['actor']['objectType'] = 'Agent'; + $args['object']['objectType'] = 'Activity'; + unset($args['verb']['display']); unset($args['object']['definition']['name']); + unset($args['object']['definition']['description']); + unset($args['object']['definition']['extensions']); + unset($args['context']['contextActivities']); + unset($args['result']['score']); + unset($args['attachments']); + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionSubObjectWithEmptyValue() { + $args = [ + 'actor' => [ + 'mbox' => COMMON_MBOX, + ], + 'verb' => [ + 'id' => COMMON_VERB_ID, + ], + 'object' => [ + 'id' => COMMON_ACTIVITY_ID, + 'definition' => [ + 'type' => 'Invalid type', + 'name' => [ + 'en-US' => '' + ], + ] + ], + 'context' => [ + 'contextActivities' => [], + ], + 'result' => [ + 'completion' => true, + 'success' => false, + 'score' => [ + 'raw' => 0 + ] + ] + ]; + $obj = Statement::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); $versioned = $obj->asVersion('1.0.0'); - $this->assertEquals($args, $versioned, 'version 1.0.0'); + $args['actor']['objectType'] = 'Agent'; + $args['object']['objectType'] = 'Activity'; + unset($args['context']); + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); } public function testCompareWithSignature() { diff --git a/tests/SubStatementTest.php b/tests/SubStatementTest.php index bc31a43..9d3ab4f 100644 --- a/tests/SubStatementTest.php +++ b/tests/SubStatementTest.php @@ -41,10 +41,9 @@ public function testGetObjectType() { // TODO: need to loop versions public function testAsVersion() { $args = [ - 'objectType' => 'SubStatement', + 'timestamp' => '2015-01-28T14:23:37.159Z', 'actor' => [ 'mbox' => COMMON_MBOX, - 'objectType' => 'Agent', ], 'verb' => [ 'id' => COMMON_VERB_ID, @@ -53,16 +52,15 @@ public function testAsVersion() { ] ], 'object' => [ - 'objectType' => 'Activity', 'id' => COMMON_ACTIVITY_ID, 'definition' => [ 'type' => 'Invalid type', 'name' => [ 'en-US' => 'Test', ], - //'description' => [ - //'en-US' => 'Test description', - //], + 'description' => [ + 'en-US' => 'Test description', + ], 'extensions' => [ 'http://someuri' => 'some value' ], @@ -72,7 +70,6 @@ public function testAsVersion() { 'contextActivities' => [ 'parent' => [ [ - 'objectType' => 'Activity', 'id' => COMMON_ACTIVITY_ID . '/1', 'definition' => [ 'name' => [ @@ -95,19 +92,119 @@ public function testAsVersion() { ] ], ]; - $obj = new SubStatement($args); - $obj->getTarget()->getDefinition()->getDescription()->set('en-ES', 'Testo descriptiono'); - $args['object']['definition']['description'] = ['en-ES' => 'Testo descriptiono']; + $obj = SubStatement::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + + $versioned = $obj->asVersion('1.0.0'); + + $args['objectType'] = 'SubStatement'; + $args['timestamp'] = $obj->getTimestamp(); + $args['actor']['objectType'] = 'Agent'; + $args['object']['objectType'] = 'Activity'; + $args['context']['contextActivities']['parent'][0]['objectType'] = 'Activity'; + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionEmpty() { + $args = []; + + $obj = SubStatement::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); - $obj->getTarget()->getDefinition()->getName()->unset('en-US'); + $args['objectType'] = 'SubStatement'; + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionEmptySubObjects() { + $args = [ + 'actor' => [ + 'mbox' => COMMON_MBOX, + ], + 'verb' => [ + 'id' => COMMON_VERB_ID, + 'display' => [] + ], + 'object' => [ + 'id' => COMMON_ACTIVITY_ID, + 'definition' => [ + 'type' => 'Invalid type', + 'name' => [], + 'description' => [], + 'extensions' => [], + ] + ], + 'context' => [ + 'contextActivities' => [ + 'parent' => [], + ], + 'registration' => Util::getUUID(), + ], + 'result' => [ + 'completion' => true, + 'success' => false, + 'score' => [] + ], + ]; + + $obj = SubStatement::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + $args['objectType'] = 'SubStatement'; + $args['actor']['objectType'] = 'Agent'; + $args['object']['objectType'] = 'Activity'; + unset($args['verb']['display']); unset($args['object']['definition']['name']); + unset($args['object']['definition']['description']); + unset($args['object']['definition']['extensions']); + unset($args['context']['contextActivities']); + unset($args['result']['score']); + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionSubObjectWithEmptyValue() { + $args = [ + 'actor' => [ + 'mbox' => COMMON_MBOX, + ], + 'verb' => [ + 'id' => COMMON_VERB_ID, + ], + 'object' => [ + 'id' => COMMON_ACTIVITY_ID, + 'definition' => [ + 'type' => 'Invalid type', + 'name' => [ + 'en-US' => '' + ], + ] + ], + 'context' => [ + 'contextActivities' => [], + ], + 'result' => [ + 'completion' => true, + 'success' => false, + 'score' => [ + 'raw' => 0 + ] + ] + ]; + + $obj = SubStatement::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); $versioned = $obj->asVersion('1.0.0'); - $this->assertEquals($args, $versioned, 'version 1.0.0'); + $args['objectType'] = 'SubStatement'; + $args['actor']['objectType'] = 'Agent'; + $args['object']['objectType'] = 'Activity'; + unset($args['context']); + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); } + public function testCompareWithSignature() { $actor1 = new Agent( [ 'mbox' => COMMON_MBOX ] diff --git a/tests/VerbTest.php b/tests/VerbTest.php index b6a443e..fa55559 100644 --- a/tests/VerbTest.php +++ b/tests/VerbTest.php @@ -78,10 +78,40 @@ public function testAsVersion() { 'id' => COMMON_VERB_ID, 'display' => self::$DISPLAY ]; - $obj = new Verb($args); + + $obj = Verb::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 = Verb::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 = ['display' => []]; + + $obj = Verb::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); + $versioned = $obj->asVersion('1.0.0'); + + unset($args['display']); + + $this->assertEquals($versioned, $args, "serialized version matches corrected"); + } + + public function testAsVersionEmptyStringInLanguageMap() { + $args = ['display' => ['en' => '']]; + + $obj = Verb::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES)); $versioned = $obj->asVersion('1.0.0'); - $this->assertEquals($versioned, $args, "version 1.0.0"); + $this->assertEquals($versioned, $args, "serialized version matches original"); } public function testCompareWithSignature() {