diff --git a/composer.json b/composer.json index 7786424..be50a03 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ }, "require-dev": { "phpdocumentor/phpdocumentor": "2.*", - "phpunit/phpunit": "@stable" + "phpunit/phpunit": "^4" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 446b5df..f5854e3 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -14,11 +14,9 @@ tests - - tests/config - tests/files - tests/keys - + tests/config + tests/files + tests/keys diff --git a/src/RemoteLRS.php b/src/RemoteLRS.php index 9f47257..de04356 100644 --- a/src/RemoteLRS.php +++ b/src/RemoteLRS.php @@ -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); } ); @@ -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 { @@ -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']); + } } } diff --git a/src/Version.php b/src/Version.php index e22592b..4277ac0 100644 --- a/src/Version.php +++ b/src/Version.php @@ -32,6 +32,8 @@ 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"; @@ -39,6 +41,8 @@ final class Version /** @var array string => bool */ private static $supported = [ + self::V103 => true, + self::V102 => true, self::V101 => true, self::V100 => true, self::V095 => false diff --git a/tests/RemoteLRSTest.php b/tests/RemoteLRSTest.php index c41f4cc..c880448 100644 --- a/tests/RemoteLRSTest.php +++ b/tests/RemoteLRSTest.php @@ -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( diff --git a/tests/StatementTest.php b/tests/StatementTest.php index ba04c09..d8307a2 100644 --- a/tests/StatementTest.php +++ b/tests/StatementTest.php @@ -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'); } diff --git a/tests/VersionTest.php b/tests/VersionTest.php index aacba71..fa71ab9 100644 --- a/tests/VersionTest.php +++ b/tests/VersionTest.php @@ -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"); } @@ -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() {