Skip to content
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"require-dev": {
"phpdocumentor/phpdocumentor": "2.*",
"phpunit/phpunit": "@stable"
"phpunit/phpunit": "^4"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 3 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
<testsuites>
<testsuite name="TinCanPHP Test Suite">
<directory suffix="Test.php">tests</directory>
<exclude>
<directory>tests/config</directory>
<directory>tests/files</directory>
<directory>tests/keys</directory>
</exclude>
<exclude>tests/config</exclude>
<exclude>tests/files</exclude>
<exclude>tests/keys</exclude>
</testsuite>
</testsuites>

Expand Down
20 changes: 18 additions & 2 deletions src/RemoteLRS.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ protected function sendRequest($method, $resource) {
//
set_error_handler(
function ($errno, $errstr, $errfile, $errline, array $errcontext) {
// "!== false" is intentional. strpos() can return 0, which is falsey, but returning
// 0 matches our "true" condition. Using strict equality to avoid that confusion.
if ($errno == E_NOTICE && strpos($errstr, 'Array to string conversion') !== false) {
// The way HHVM handles array comparison results in a Notice being raised in fopen(),
// but that's expected here and won't affect functionality. We don't want to throw
// those Notices as Errors. Checking if this is a Notice before looking at the
// contents of the string to hopefully minimize any performance impact here.
// See https://github.com/facebook/hhvm/issues/1561 for the "won't fix" from HHVM.

return true;
}

throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
}
);
Expand Down Expand Up @@ -479,7 +491,9 @@ public function retrieveStatement($id, $options = array()) {
}

foreach ($response->content->getAttachments() as $attachment) {
$attachment->setContent($attachmentsByHash[$attachment->getSha2()]['body']);
if (array_key_exists($attachment->getSha2(), $attachmentsByHash)) {
$attachment->setContent($attachmentsByHash[$attachment->getSha2()]['body']);
}
}
}
else {
Expand Down Expand Up @@ -555,7 +569,9 @@ private function _queryStatementsResult(&$response) {

foreach ($response->content->getStatements() as $st) {
foreach ($st->getAttachments() as $attachment) {
$attachment->setContent($attachmentsByHash[$attachment->getSha2()]['body']);
if (array_key_exists($attachment->getSha2(), $attachmentsByHash)) {
$attachment->setContent($attachmentsByHash[$attachment->getSha2()]['body']);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ final class Version
*
* @var string
*/
const V103 = "1.0.3";
const V102 = "1.0.2";
const V101 = "1.0.1";
const V100 = "1.0.0";
const V095 = "0.95";
/**#@- */

/** @var array string => bool */
private static $supported = [
self::V103 => true,
self::V102 => true,
self::V101 => true,
self::V100 => true,
self::V095 => false
Expand Down
38 changes: 38 additions & 0 deletions tests/RemoteLRSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,44 @@ public function testMoreStatementsWithAttachments() {
$this->assertInstanceOf('TinCan\StatementsResult', $response->content, 'content');
}

public function testRetrieveStatementWithFileUrlAttachments() {
$lrs = new RemoteLRS(self::$endpoint, self::$version, self::$username, self::$password);
$attachments = new Attachment();
$attachmentUrl = 'https://github.com/RusticiSoftware/TinCanPHP/raw/master/tests/files/image.jpg';
// Store Attachments in and retrieve them from the LRS
$attachments
->setUsageType('http://id.tincanapi.com/attachment/supporting_media')
->setDisplay(['en-US' => 'Test image attachment'])
->setContentType('image/jpg')
->setLength(filesize('tests/files/image.jpg'))
->setSha2(hash_file('sha256', 'tests/files/image.jpg')) // hash of the attachment data
->setFileUrl($attachmentUrl)
->setDescription(['en-US' => 'A test document used in an Attachments object example.']);

// Compose statement for sending to the LRS
$statement = new Statement(
[
'actor' => [
'mbox' => COMMON_MBOX
],
'verb' => [
'id' => COMMON_VERB_ID
],
'object' => new Activity([
'id' => COMMON_ACTIVITY_ID
])
]
);
$statement->setAttachments([$attachments]);
$saveResponse = $lrs->saveStatement($statement);
$statementResponse = $lrs->retrieveStatement($saveResponse->content->getId(), ['attachments' => true]);

$this->assertInstanceOf('TinCan\LRSResponse', $statementResponse);
$this->assertTrue($statementResponse->success);
$this->assertInstanceOf('TinCan\Statement', $statementResponse->content);
$this->assertEquals($attachmentUrl, $statementResponse->content->getAttachments()[0]->getFileUrl());
}

public function testRetrieveStateIds() {
$lrs = new RemoteLRS(self::$endpoint, self::$version, self::$username, self::$password);
$response = $lrs->retrieveStateIds(
Expand Down
30 changes: 22 additions & 8 deletions tests/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,20 +601,34 @@ public function testHasAttachmentWithContent() {
public function testSignNoArgs() {
$obj = new Statement();

$this->setExpectedException(
'PHPUnit_Framework_Error_Warning',
(getenv('TRAVIS_PHP_VERSION') == "hhvm" ? 'sign() expects at least 2 parameters, 0 given' : 'Missing argument 1')
);
if (PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) {
$this->setExpectedException(
'ArgumentCountError'
);
}
else {
$this->setExpectedException(
'PHPUnit_Framework_Error_Warning',
(getenv('TRAVIS_PHP_VERSION') == "hhvm" ? 'sign() expects at least 2 parameters, 0 given' : 'Missing argument 1')
);
}
$obj->sign();
}

public function testSignOneArg() {
$obj = new Statement();

$this->setExpectedException(
'PHPUnit_Framework_Error_Warning',
(getenv('TRAVIS_PHP_VERSION') == "hhvm" ? 'sign() expects at least 2 parameters, 1 given' : 'Missing argument 2')
);
if (PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) {
$this->setExpectedException(
'ArgumentCountError'
);
}
else {
$this->setExpectedException(
'PHPUnit_Framework_Error_Warning',
(getenv('TRAVIS_PHP_VERSION') == "hhvm" ? 'sign() expects at least 2 parameters, 1 given' : 'Missing argument 2')
);
}
$obj->sign('test');
}

Expand Down
4 changes: 2 additions & 2 deletions tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testIsSupportedReturnsBool() {
}

public function testIsLatestReturnsBool() {
$this->assertTrue(Version::v101()->isLatest(), "1.0.1 should be the latest version");
$this->assertTrue(Version::v103()->isLatest(), "1.0.3 should be the latest version");
$this->assertFalse(Version::v095()->isLatest(), "0.95 should not be the latest version");
}

Expand All @@ -52,7 +52,7 @@ public function testSupported() {
}

public function testLatest() {
$this->assertSame(Version::V101, Version::latest(), "match latest");
$this->assertSame(Version::V103, Version::latest(), "match latest");
}

public function testVersionFromString() {
Expand Down