Skip to content

Commit

Permalink
Merge pull request #73 from brianjmiller/master
Browse files Browse the repository at this point in the history
Test Coverage improvement and various fixes
  • Loading branch information
brianjmiller authored Aug 26, 2016
2 parents 9cba2f3 + c845d8f commit 522afdb
Show file tree
Hide file tree
Showing 21 changed files with 602 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/AsVersionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function asVersion($version) {
if ($value instanceof VersionableInterface) {
$value = $value->asVersion($version);
}
else if (is_array($value) && !empty($value)) {
elseif (is_array($value) && !empty($value)) {
$tmp_value = array();
foreach ($value as $element) {
if ($element instanceof VersionableInterface) {
Expand Down
2 changes: 1 addition & 1 deletion src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __call($func, $args) {
return $this->_unset($args[0]);
break;
default:
throw new BadMethodCallException(__CLASS__ . "::$func() does not exist");
throw new \BadMethodCallException(get_class($this) . "::$func() does not exist");
}
}
}
6 changes: 3 additions & 3 deletions src/RemoteLRS.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private function _parseMetadata($metadata) {
if ($pair[0] === 'charset') {
$result['headers']['contentTypeCharset'] = $pair[1];
}
else if ($pair[0] === 'boundary') {
elseif ($pair[0] === 'boundary') {
$result['headers']['contentTypeBoundary'] = $pair[1];
}
}
Expand All @@ -246,7 +246,7 @@ private function _parseMultipart($boundary, $content) {
if ($part === '') {
continue;
}
else if ($part === '--') {
elseif ($part === '--') {
break;
}
list($header, $body) = explode("\r\n\r\n", $part, 2);
Expand Down Expand Up @@ -956,7 +956,7 @@ public function deleteActivityProfile($activity, $id) {
public function retrieveActivity($activityid) {
$headers = array('Accept-language: *');
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$headers = 'Accept-language: ' . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . ', *';
$headers = array('Accept-language: ' . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . ', *');
}

$response = $this->sendRequest(
Expand Down
2 changes: 1 addition & 1 deletion src/SignatureComparisonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private static function doMatch($a, $b, $description) {
}

if (is_object($a) && ! ($b instanceof $a)) {
$result['reason'] = "Comparison of $description failed: not a " . get_class($this) . " value";
$result['reason'] = "Comparison of $description failed: not a " . get_class($a) . " value";
return $result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getAgent() { return $this->agent; }

public function setRegistration($value) {
if (isset($value) && ! preg_match(Util::UUID_REGEX, $value)) {
throw new InvalidArgumentException('arg1 must be a UUID');
throw new \InvalidArgumentException('arg1 must be a UUID');
}

$this->registration = $value;
Expand Down
2 changes: 1 addition & 1 deletion src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function verify($options = array()) {
if (isset($options['publicKey'])) {
$publicKeyFile = $options['publicKey'];
}
else if (isset($header['x5c'])) {
elseif (isset($header['x5c'])) {
$cert = "-----BEGIN CERTIFICATE-----\r\n" . chunk_split($header['x5c'][0], 64, "\r\n") . "-----END CERTIFICATE-----\r\n";
$cert = openssl_x509_read($cert);
if (! $cert) {
Expand Down
30 changes: 30 additions & 0 deletions tests/ActivityProfileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/*
Copyright 2016 Rustici Software
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

namespace TinCanTest;

use TinCan\ActivityProfile;
use TinCan\Activity;

class ActivityProfileTest extends \PHPUnit_Framework_TestCase {
public function testSetActivity() {
$profile = new ActivityProfile();
$profile->setActivity(['id' => COMMON_ACTIVITY_ID]);

$this->assertInstanceOf('TinCan\Activity', $profile->getActivity());
}
}
35 changes: 35 additions & 0 deletions tests/AgentProfileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/*
Copyright 2016 Rustici Software
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

namespace TinCanTest;

use TinCan\AgentProfile;
use TinCan\Agent;
use TinCan\Group;

class AgentProfileTest extends \PHPUnit_Framework_TestCase {
public function testSetAgent() {
$profile = new AgentProfile();
$profile->setAgent(['mbox' => COMMON_MBOX]);

$this->assertInstanceOf('TinCan\Agent', $profile->getAgent());

$profile->setAgent(['objectType' => 'Group']);

$this->assertInstanceOf('TinCan\Group', $profile->getAgent());
}
}
22 changes: 22 additions & 0 deletions tests/ContextActivitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,26 @@ public function testCompareWithSignature() {
}
$this->runSignatureCases("TinCan\ContextActivities", $cases);
}

/**
* @dataProvider invalidListSetterDataProvider
*/
public function testListSetterThrowsInvalidArgumentException($publicMethodName, $invalidValue) {
$this->setExpectedException(
'InvalidArgumentException',
'type of arg1 must be Activity, array of Activity properties, or array of Activity/array of Activity properties'
);
$obj = new ContextActivities();
$obj->$publicMethodName($invalidValue);
}

public function invalidListSetterDataProvider() {
$invalidValue = 1;
return [
["setCategory", $invalidValue],
["setParent", $invalidValue],
["setGrouping", $invalidValue],
["setOther", $invalidValue]
];
}
}
19 changes: 19 additions & 0 deletions tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,23 @@ public function testCompareWithSignature() {
];
$this->runSignatureCases("TinCan\Context", $cases);
}

public function testSetInstructorConvertToGroup() {
$obj = new Context();
$obj->setInstructor(
[
'objectType' => 'Group'
]
);
$this->assertInstanceOf('TinCan\Group', $obj->getInstructor());
}

public function testSetRegistrationInvalidArgumentException() {
$this->setExpectedException(
'InvalidArgumentException',
'arg1 must be a UUID'
);
$obj = new Context();
$obj->setRegistration('232....3.3..3./2/2/1m3m3m3');
}
}
34 changes: 34 additions & 0 deletions tests/DocumentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/*
Copyright 2016 Rustici Software
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

namespace TinCanTest;

use TinCan\Document;

class StubDocument extends Document {}

class DocumentTest extends \PHPUnit_Framework_TestCase {
public function testExceptionOnInvalidDateTime() {
$this->setExpectedException(
"InvalidArgumentException",
'type of arg1 must be string or DateTime'
);

$obj = new StubDocument();
$obj->setTimestamp(1);
}
}
3 changes: 3 additions & 0 deletions tests/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ public function testAddMember() {

$obj->addMember($common_agent);
$this->assertEquals([$common_agent], $obj->getMember(), 'member list existing Agent');

$versioned = $obj->asVersion('1.0.0');
$this->assertSame($versioned['member'][0], $common_agent->asVersion('1.0.0'));
}

public function testCompareWithSignature() {
Expand Down
29 changes: 29 additions & 0 deletions tests/LRSResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/*
Copyright 2016 Rustici Software
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

namespace TinCanTest;

use TinCan\LRSResponse;

class LRSResponseTest extends \PHPUnit_Framework_TestCase {
public function testInstantiation() {
$obj = new LRSResponse(true, '', false);
$this->assertTrue($obj->success);
$this->assertEquals('', $obj->content);
$this->assertFalse($obj->httpResponse);
}
}
18 changes: 18 additions & 0 deletions tests/LanguageMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,23 @@ public function testGetNegotiatedLanguageString() {

$this->assertEquals($usValue, $langs['en-US'], 'US name equal');
$this->assertEquals($ukValue, $langs['en-GB'], 'UK name equal');

$nullValue = $obj->getNegotiatedLanguageString();
$this->assertEquals($nullValue, $langs['en-GB'], 'from null: UK name equal');

if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$restore = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
}
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US';

$nullAcceptValue = $obj->getNegotiatedLanguageString();
$this->assertEquals($nullAcceptValue, $langs['en-US'], 'from server: US name equal');

if (isset($restore)) {
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = $restore;
}
else {
unset($_SERVER['HTTP_ACCEPT_LANGUAGE']);
}
}
}
60 changes: 60 additions & 0 deletions tests/MapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/*
Copyright 2016 Rustici Software
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

namespace TinCanTest;

use TinCan\Map;

class StubMap extends Map {}

class MapTest extends \PHPUnit_Framework_TestCase {
public function testInstantiation() {
$obj = new StubMap();
}

public function testInstantiationWithArg() {
$obj = new StubMap([]);
$this->assertTrue($obj->isEmpty());
}

public function testSetUnset() {
$obj = new StubMap();

$code = 'code';
$value = 'value';

$obj->set($code, $value);

$this->assertEquals($value, $obj->asVersion()[$code]);

$obj->unset($code);

$this->assertFalse(isset($obj->asVersion()[$code]));
}

public function testExceptionOnBadMethodCall() {
$badName ="dsadasdasdasdasdasdas";

$this->setExpectedException(
'\BadMethodCallException',
get_class(new StubMap) . "::$badName() does not exist"
);

$obj = new StubMap();
$obj->$badName();
}
}
28 changes: 28 additions & 0 deletions tests/RemoteLRSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ public function testSaveStatement() {
$this->assertSame($response->content, $statement, 'content');
}

public function testSaveStatementStamped() {
$lrs = new RemoteLRS(self::$endpoint, self::$version, self::$username, self::$password);
$statement = new Statement(
[
'actor' => [
'mbox' => COMMON_MBOX
],
'verb' => [
'id' => COMMON_VERB_ID
],
'object' => new Activity([
'id' => COMMON_ACTIVITY_ID
])
]
);
$statement->stamp();

$response = $lrs->saveStatement($statement);
$this->assertInstanceOf('TinCan\LRSResponse', $response);
$this->assertTrue($response->success, 'success');
$this->assertSame($response->content, $statement, 'content');
}

public function testSaveStatements() {
$lrs = new RemoteLRS(self::$endpoint, self::$version, self::$username, self::$password);
$statements = [
Expand Down Expand Up @@ -504,6 +527,11 @@ public function testRetrieveActivity() {
$this->assertEquals($testActivity, $response->content, 'retrieved activity');
}

public function testRetrieveActivityWithHttpAcceptLanguageHeader() {
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US';
$this->testRetrieveActivity();
}

public function testRetrieveAgentProfileIds() {
$lrs = new RemoteLRS(self::$endpoint, self::$version, self::$username, self::$password);
$response = $lrs->retrieveAgentProfileIds(
Expand Down
Loading

0 comments on commit 522afdb

Please sign in to comment.