44
55use OAuth \OAuth1 \Token \StdOAuth1Token ;
66use PHPUnit \Framework \TestCase as PhpUnitTestCase ;
7+ use Samwilson \PhpFlickr \FlickrException ;
78use Samwilson \PhpFlickr \PhpFlickr ;
89
910abstract class TestCase extends PhpUnitTestCase
1011{
11- /** @var PhpFlickr */
12- private $ flickr ;
12+ /** @var PhpFlickr[] */
13+ private $ flickrs = [] ;
1314
1415 /**
1516 * Get an instance of PhpFlickr, configured by the config.php file in the tests directory.
@@ -19,8 +20,9 @@ abstract class TestCase extends PhpUnitTestCase
1920 */
2021 public function getFlickr (bool $ authenticate = false ): PhpFlickr
2122 {
22- if ($ this ->flickr instanceof PhpFlickr) {
23- return $ this ->flickr ;
23+ $ authed = $ authenticate ? 'authed ' : 'notauthed ' ;
24+ if (isset ($ this ->flickrs [$ authed ])) {
25+ return $ this ->flickrs [$ authed ];
2426 }
2527
2628 // Get config values from env vars or the tests/config.php file.
@@ -35,16 +37,34 @@ public function getFlickr(bool $authenticate = false): PhpFlickr
3537 // Skip if no key found, so PRs from forks can still be run in CI.
3638 static ::markTestSkipped ('No Flickr API key set. ' );
3739 }
38- $ this ->flickr = new PhpFlickr ($ apiKey , $ apiSecret );
40+ try {
41+ $ this ->flickrs [$ authed ] = new PhpFlickr ($ apiKey , $ apiSecret );
42+ } catch (FlickrException $ ex ) {
43+ static ::markTestSkipped ($ ex ->getMessage ());
44+ }
3945
4046 // Authenticate?
4147 if ($ authenticate && !empty ($ accessToken ) && !empty ($ accessTokenSecret )) {
4248 $ token = new StdOAuth1Token ();
4349 $ token ->setAccessToken ($ accessToken );
4450 $ token ->setAccessTokenSecret ($ accessTokenSecret );
45- $ this ->flickr ->getOauthTokenStorage ()->storeAccessToken ('Flickr ' , $ token );
51+ $ this ->flickrs [$ authed ]->getOauthTokenStorage ()->storeAccessToken ('Flickr ' , $ token );
52+ try {
53+ $ authenticated = $ this ->flickrs [$ authed ]->test ()->login ();
54+ } catch (FlickrException $ e ) {
55+ $ authenticated = false ;
56+ }
57+ if (!$ authenticated ) {
58+ static ::markTestSkipped ('Unable to authenticate with provided access token. ' );
59+ }
60+ }
61+ if ($ authenticate && empty ($ accessToken )) {
62+ static ::markTestSkipped (
63+ 'Access token required for this test. '
64+ . 'Please use examples/get_auth_token.php to get token to add to tests/config.php. '
65+ );
4666 }
4767
48- return $ this ->flickr ;
68+ return $ this ->flickrs [ $ authed ] ;
4969 }
5070}
0 commit comments