Skip to content

Commit

Permalink
Switch to carlos-mg89/oauth fork of lusitanian/oauth, add PHP 8.1 to …
Browse files Browse the repository at this point in the history
…CI, and fix failures

* Switch to carlos-mg89/oauth fork of lusitanian/oauth and add PHP 8.1 to CI
* Skip tests if unable to authenticate
* Skip echo test on exception
* Fix up search test
  • Loading branch information
samwilson authored Jul 5, 2022
1 parent 6a7570c commit d6d2415
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
# All supported PHP versions https://www.php.net/supported-versions.php
php: [ '7.3', '7.4', '8.0' ]
php: [ '7.3', '7.4', '8.0', '8.1' ]

runs-on: ${{matrix.os}}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"ext-json": "*",
"ext-libxml": "*",
"ext-simplexml": "*",
"lusitanian/oauth": "dev-master#ee5a83310c6014b6cc07ac0610ec9d67ba452664 as 0.8.12",
"carlos-mg89/oauth": "^0.8",
"psr/cache": "^1.0"
},
"require-dev": {
Expand Down
28 changes: 22 additions & 6 deletions tests/ApiMethodGroup/PhotosApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,29 @@ public function testSetTags()
public function testSearch()
{
$flickr = $this->getFlickr(true);
$testFilename = dirname(__DIR__) . '/../examples/Agateware_Example.JPG';
$photo = $flickr->uploader()->upload($testFilename);
$search = $flickr->photos()->search([
$testTitle = uniqid('PhpFlickr search test ');
$searchParams = [
'user_id' => 'me',
'text' => 'Agateware_Example',
]);
static::assertGreaterThan(1, count($search['photo']));
'text' => $testTitle,
];

// Make sure there are no search results to start with.
$search = $flickr->photos()->search($searchParams);
static::assertCount(0, $search['photo']);

// Upload a test photo.
$testFilename = dirname(__DIR__) . '/../examples/Agateware_Example.JPG';
$photo = $flickr->uploader()->upload($testFilename, $testTitle, null, null, true, null, null, null, 1);

// Look for search results, looping because it's a new file and can take time to be indexed.
for ($i = 0; $i < 15; $i++) {
sleep(1);
$search = $flickr->photos()->search($searchParams);
if (count($search['photo']) > 0) {
break;
}
}
static::assertGreaterThanOrEqual(1, count($search['photo']));

// Clean up.
$flickr->photos()->delete($photo['photoid']);
Expand Down
6 changes: 5 additions & 1 deletion tests/ApiMethodGroup/TestApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public function testEcho()
{
$flickr = $this->getFlickr();

$echo = $flickr->test()->testEcho(['foo' => 'bar']);
try {
$echo = $flickr->test()->testEcho(['foo' => 'bar']);
} catch (FlickrException $e) {
static::markTestSkipped($e->getMessage());
}

$this->assertArrayHasKey('foo', $echo);
}
Expand Down
34 changes: 27 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use OAuth\OAuth1\Token\StdOAuth1Token;
use PHPUnit\Framework\TestCase as PhpUnitTestCase;
use Samwilson\PhpFlickr\FlickrException;
use Samwilson\PhpFlickr\PhpFlickr;

abstract class TestCase extends PhpUnitTestCase
{
/** @var PhpFlickr */
private $flickr;
/** @var PhpFlickr[] */
private $flickrs = [];

/**
* Get an instance of PhpFlickr, configured by the config.php file in the tests directory.
Expand All @@ -19,8 +20,9 @@ abstract class TestCase extends PhpUnitTestCase
*/
public function getFlickr(bool $authenticate = false): PhpFlickr
{
if ($this->flickr instanceof PhpFlickr) {
return $this->flickr;
$authed = $authenticate ? 'authed' : 'notauthed';
if (isset($this->flickrs[$authed])) {
return $this->flickrs[$authed];
}

// Get config values from env vars or the tests/config.php file.
Expand All @@ -35,16 +37,34 @@ public function getFlickr(bool $authenticate = false): PhpFlickr
// Skip if no key found, so PRs from forks can still be run in CI.
static::markTestSkipped('No Flickr API key set.');
}
$this->flickr = new PhpFlickr($apiKey, $apiSecret);
try {
$this->flickrs[$authed] = new PhpFlickr($apiKey, $apiSecret);
} catch (FlickrException $ex) {
static::markTestSkipped($ex->getMessage());
}

// Authenticate?
if ($authenticate && !empty($accessToken) && !empty($accessTokenSecret)) {
$token = new StdOAuth1Token();
$token->setAccessToken($accessToken);
$token->setAccessTokenSecret($accessTokenSecret);
$this->flickr->getOauthTokenStorage()->storeAccessToken('Flickr', $token);
$this->flickrs[$authed]->getOauthTokenStorage()->storeAccessToken('Flickr', $token);
try {
$authenticated = $this->flickrs[$authed]->test()->login();
} catch (FlickrException $e) {
$authenticated = false;
}
if (!$authenticated) {
static::markTestSkipped('Unable to authenticate with provided access token.');
}
}
if ($authenticate && empty($accessToken)) {
static::markTestSkipped(
'Access token required for this test. '
. 'Please use examples/get_auth_token.php to get token to add to tests/config.php.'
);
}

return $this->flickr;
return $this->flickrs[$authed];
}
}

0 comments on commit d6d2415

Please sign in to comment.