Skip to content

Commit 0abbc06

Browse files
authored
Merge pull request #100 from reedmclean/FileUrlAttachments
Added support for file URL attachments
2 parents abb63a0 + ba08459 commit 0abbc06

7 files changed

Lines changed: 88 additions & 18 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
},
3232
"require-dev": {
3333
"phpdocumentor/phpdocumentor": "2.*",
34-
"phpunit/phpunit": "@stable"
34+
"phpunit/phpunit": "^4"
3535
},
3636
"autoload": {
3737
"psr-4": {

phpunit.xml.dist

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414
<testsuites>
1515
<testsuite name="TinCanPHP Test Suite">
1616
<directory suffix="Test.php">tests</directory>
17-
<exclude>
18-
<directory>tests/config</directory>
19-
<directory>tests/files</directory>
20-
<directory>tests/keys</directory>
21-
</exclude>
17+
<exclude>tests/config</exclude>
18+
<exclude>tests/files</exclude>
19+
<exclude>tests/keys</exclude>
2220
</testsuite>
2321
</testsuites>
2422

src/RemoteLRS.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ protected function sendRequest($method, $resource) {
138138
//
139139
set_error_handler(
140140
function ($errno, $errstr, $errfile, $errline, array $errcontext) {
141+
// "!== false" is intentional. strpos() can return 0, which is falsey, but returning
142+
// 0 matches our "true" condition. Using strict equality to avoid that confusion.
143+
if ($errno == E_NOTICE && strpos($errstr, 'Array to string conversion') !== false) {
144+
// The way HHVM handles array comparison results in a Notice being raised in fopen(),
145+
// but that's expected here and won't affect functionality. We don't want to throw
146+
// those Notices as Errors. Checking if this is a Notice before looking at the
147+
// contents of the string to hopefully minimize any performance impact here.
148+
// See https://github.com/facebook/hhvm/issues/1561 for the "won't fix" from HHVM.
149+
150+
return true;
151+
}
152+
141153
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
142154
}
143155
);
@@ -479,7 +491,9 @@ public function retrieveStatement($id, $options = array()) {
479491
}
480492

481493
foreach ($response->content->getAttachments() as $attachment) {
482-
$attachment->setContent($attachmentsByHash[$attachment->getSha2()]['body']);
494+
if (array_key_exists($attachment->getSha2(), $attachmentsByHash)) {
495+
$attachment->setContent($attachmentsByHash[$attachment->getSha2()]['body']);
496+
}
483497
}
484498
}
485499
else {
@@ -555,7 +569,9 @@ private function _queryStatementsResult(&$response) {
555569

556570
foreach ($response->content->getStatements() as $st) {
557571
foreach ($st->getAttachments() as $attachment) {
558-
$attachment->setContent($attachmentsByHash[$attachment->getSha2()]['body']);
572+
if (array_key_exists($attachment->getSha2(), $attachmentsByHash)) {
573+
$attachment->setContent($attachmentsByHash[$attachment->getSha2()]['body']);
574+
}
559575
}
560576
}
561577

src/Version.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ final class Version
3232
*
3333
* @var string
3434
*/
35+
const V103 = "1.0.3";
36+
const V102 = "1.0.2";
3537
const V101 = "1.0.1";
3638
const V100 = "1.0.0";
3739
const V095 = "0.95";
3840
/**#@- */
3941

4042
/** @var array string => bool */
4143
private static $supported = [
44+
self::V103 => true,
45+
self::V102 => true,
4246
self::V101 => true,
4347
self::V100 => true,
4448
self::V095 => false

tests/RemoteLRSTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,44 @@ public function testMoreStatementsWithAttachments() {
374374
$this->assertInstanceOf('TinCan\StatementsResult', $response->content, 'content');
375375
}
376376

377+
public function testRetrieveStatementWithFileUrlAttachments() {
378+
$lrs = new RemoteLRS(self::$endpoint, self::$version, self::$username, self::$password);
379+
$attachments = new Attachment();
380+
$attachmentUrl = 'https://github.com/RusticiSoftware/TinCanPHP/raw/master/tests/files/image.jpg';
381+
// Store Attachments in and retrieve them from the LRS
382+
$attachments
383+
->setUsageType('http://id.tincanapi.com/attachment/supporting_media')
384+
->setDisplay(['en-US' => 'Test image attachment'])
385+
->setContentType('image/jpg')
386+
->setLength(filesize('tests/files/image.jpg'))
387+
->setSha2(hash_file('sha256', 'tests/files/image.jpg')) // hash of the attachment data
388+
->setFileUrl($attachmentUrl)
389+
->setDescription(['en-US' => 'A test document used in an Attachments object example.']);
390+
391+
// Compose statement for sending to the LRS
392+
$statement = new Statement(
393+
[
394+
'actor' => [
395+
'mbox' => COMMON_MBOX
396+
],
397+
'verb' => [
398+
'id' => COMMON_VERB_ID
399+
],
400+
'object' => new Activity([
401+
'id' => COMMON_ACTIVITY_ID
402+
])
403+
]
404+
);
405+
$statement->setAttachments([$attachments]);
406+
$saveResponse = $lrs->saveStatement($statement);
407+
$statementResponse = $lrs->retrieveStatement($saveResponse->content->getId(), ['attachments' => true]);
408+
409+
$this->assertInstanceOf('TinCan\LRSResponse', $statementResponse);
410+
$this->assertTrue($statementResponse->success);
411+
$this->assertInstanceOf('TinCan\Statement', $statementResponse->content);
412+
$this->assertEquals($attachmentUrl, $statementResponse->content->getAttachments()[0]->getFileUrl());
413+
}
414+
377415
public function testRetrieveStateIds() {
378416
$lrs = new RemoteLRS(self::$endpoint, self::$version, self::$username, self::$password);
379417
$response = $lrs->retrieveStateIds(

tests/StatementTest.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -601,20 +601,34 @@ public function testHasAttachmentWithContent() {
601601
public function testSignNoArgs() {
602602
$obj = new Statement();
603603

604-
$this->setExpectedException(
605-
'PHPUnit_Framework_Error_Warning',
606-
(getenv('TRAVIS_PHP_VERSION') == "hhvm" ? 'sign() expects at least 2 parameters, 0 given' : 'Missing argument 1')
607-
);
604+
if (PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) {
605+
$this->setExpectedException(
606+
'ArgumentCountError'
607+
);
608+
}
609+
else {
610+
$this->setExpectedException(
611+
'PHPUnit_Framework_Error_Warning',
612+
(getenv('TRAVIS_PHP_VERSION') == "hhvm" ? 'sign() expects at least 2 parameters, 0 given' : 'Missing argument 1')
613+
);
614+
}
608615
$obj->sign();
609616
}
610617

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

614-
$this->setExpectedException(
615-
'PHPUnit_Framework_Error_Warning',
616-
(getenv('TRAVIS_PHP_VERSION') == "hhvm" ? 'sign() expects at least 2 parameters, 1 given' : 'Missing argument 2')
617-
);
621+
if (PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) {
622+
$this->setExpectedException(
623+
'ArgumentCountError'
624+
);
625+
}
626+
else {
627+
$this->setExpectedException(
628+
'PHPUnit_Framework_Error_Warning',
629+
(getenv('TRAVIS_PHP_VERSION') == "hhvm" ? 'sign() expects at least 2 parameters, 1 given' : 'Missing argument 2')
630+
);
631+
}
618632
$obj->sign('test');
619633
}
620634

tests/VersionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testIsSupportedReturnsBool() {
4242
}
4343

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

@@ -52,7 +52,7 @@ public function testSupported() {
5252
}
5353

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

5858
public function testVersionFromString() {

0 commit comments

Comments
 (0)