From 321e5b6b993a3c09291f732cb5f9c899000b82b9 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 11 Apr 2022 12:01:15 -0700 Subject: [PATCH] feat: add TypedItem to allow for psr/cache:3 (#364) --- .github/apply-phpunit-patches.sh | 18 -- .github/workflows/tests.yml | 24 +-- composer.json | 6 +- src/Cache/MemoryCacheItemPool.php | 24 +-- src/Cache/SysVCacheItemPool.php | 21 +- src/Cache/TypedItem.php | 187 ++++++++++++++++++ tests/AccessTokenTest.php | 12 +- tests/ApplicationDefaultCredentialsTest.php | 16 +- tests/BaseTest.php | 9 + tests/Cache/ItemTest.php | 5 + tests/Cache/MemoryCacheItemPoolTest.php | 10 +- tests/Cache/SysVCacheItemPoolTest.php | 10 +- tests/Cache/sysv_cache_creator.php | 7 +- tests/CacheTraitTest.php | 6 +- .../ServiceAccountCredentialsTest.php | 24 +-- tests/CredentialsLoaderTest.php | 4 +- tests/FetchAuthTokenCacheTest.php | 17 +- tests/GCECacheTest.php | 12 +- tests/Middleware/AuthTokenMiddlewareTest.php | 11 +- .../ScopedAccessTokenMiddlewareTest.php | 10 +- 20 files changed, 317 insertions(+), 116 deletions(-) delete mode 100644 .github/apply-phpunit-patches.sh create mode 100644 src/Cache/TypedItem.php diff --git a/.github/apply-phpunit-patches.sh b/.github/apply-phpunit-patches.sh deleted file mode 100644 index 4761b3e8d..000000000 --- a/.github/apply-phpunit-patches.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh - -# Script used from php-webdriver/php-webdriver - -# All commands below must not fail -set -e - -# Be in the root dir -cd "$(dirname "$0")/../" - -find tests/ -type f -print0 | xargs -0 sed -i 's/function setUp(): void/function setUp()/g'; -find tests/ -type f -print0 | xargs -0 sed -i 's/function tearDown(): void/function tearDown()/g'; - -# Drop the listener from the config file -sed -i '//,+2d' phpunit.xml.dist; - -# Return back to original dir -cd - > /dev/null diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 35e4cdb84..efa68af5c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,7 +9,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: [ "5.6", "7.0", "7.1", "7.2", "7.3", "7.4", "8.0", "8.1" ] + php: [ "7.1", "7.2", "7.3", "7.4", "8.0", "8.1" ] name: PHP ${{matrix.php }} Unit Test steps: - uses: actions/checkout@v3 @@ -23,46 +23,34 @@ jobs: timeout_minutes: 10 max_attempts: 3 command: composer install - - if: ${{ contains(fromJson('["5.6", "7.0", "7.1"]'), matrix.php)}} - name: Run PHPUnit Patches - run: sh .github/apply-phpunit-patches.sh - name: Run Script run: vendor/bin/phpunit test_lowest: runs-on: ubuntu-latest - strategy: - matrix: - php: [ "5.6", "7.2" ] - name: PHP ${{matrix.php }} Unit Test Prefer Lowest + name: Test Prefer Lowest steps: - uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: ${{ matrix.php }} + php-version: "7.1" - name: Install Dependencies uses: nick-invision/retry@v2 with: timeout_minutes: 10 max_attempts: 3 command: composer update --prefer-lowest - - if: ${{ matrix.php == '5.6' }} - name: Run PHPUnit Patches - run: sh .github/apply-phpunit-patches.sh - name: Run Script run: vendor/bin/phpunit guzzle6: runs-on: ubuntu-latest - strategy: - matrix: - php: [ "7.2" ] - name: PHP ${{ matrix.php }} Unit Test Guzzle 6 + name: Test Guzzle 6 steps: - uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: ${{ matrix.php }} + php-version: "7.2" - name: Install Dependencies uses: nick-invision/retry@v2 with: @@ -79,7 +67,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: "7.4" + php-version: "8.0" - name: Install Dependencies uses: nick-invision/retry@v2 with: diff --git a/composer.json b/composer.json index 541c6716f..974c1f274 100644 --- a/composer.json +++ b/composer.json @@ -9,17 +9,17 @@ "docs": "https://googleapis.github.io/google-auth-library-php/main/" }, "require": { - "php": ">=5.6", + "php": "^7.1||^8.0", "firebase/php-jwt": "~5.0", "guzzlehttp/guzzle": "^6.2.1|^7.0", "guzzlehttp/psr7": "^1.7|^2.0", "psr/http-message": "^1.0", - "psr/cache": "^1.0|^2.0" + "psr/cache": "^1.0|^2.0|^3.0" }, "require-dev": { "guzzlehttp/promises": "0.1.1|^1.3", "squizlabs/php_codesniffer": "^3.5", - "phpunit/phpunit": "^5.7||^8.5.13", + "phpunit/phpunit": "^7.5||^8.5", "phpspec/prophecy-phpunit": "^1.1", "sebastian/comparator": ">=1.2.3", "phpseclib/phpseclib": "^2.0.31", diff --git a/src/Cache/MemoryCacheItemPool.php b/src/Cache/MemoryCacheItemPool.php index 1189e37c9..a02e39030 100644 --- a/src/Cache/MemoryCacheItemPool.php +++ b/src/Cache/MemoryCacheItemPool.php @@ -41,7 +41,7 @@ final class MemoryCacheItemPool implements CacheItemPoolInterface * @return CacheItemInterface * The corresponding Cache Item. */ - public function getItem($key) + public function getItem($key): CacheItemInterface { return current($this->getItems([$key])); } @@ -49,18 +49,18 @@ public function getItem($key) /** * {@inheritdoc} * - * @return array + * @return iterable * A traversable collection of Cache Items keyed by the cache keys of * each item. A Cache item will be returned for each key, even if that * key is not found. However, if no keys are specified then an empty * traversable MUST be returned instead. */ - public function getItems(array $keys = []) + public function getItems(array $keys = []): iterable { $items = []; - + $itemClass = \PHP_VERSION_ID >= 80000 ? TypedItem::class : Item::class; foreach ($keys as $key) { - $items[$key] = $this->hasItem($key) ? clone $this->items[$key] : new Item($key); + $items[$key] = $this->hasItem($key) ? clone $this->items[$key] : new $itemClass($key); } return $items; @@ -72,7 +72,7 @@ public function getItems(array $keys = []) * @return bool * True if item exists in the cache, false otherwise. */ - public function hasItem($key) + public function hasItem($key): bool { $this->isValidKey($key); @@ -85,7 +85,7 @@ public function hasItem($key) * @return bool * True if the pool was successfully cleared. False if there was an error. */ - public function clear() + public function clear(): bool { $this->items = []; $this->deferredItems = []; @@ -99,7 +99,7 @@ public function clear() * @return bool * True if the item was successfully removed. False if there was an error. */ - public function deleteItem($key) + public function deleteItem($key): bool { return $this->deleteItems([$key]); } @@ -110,7 +110,7 @@ public function deleteItem($key) * @return bool * True if the items were successfully removed. False if there was an error. */ - public function deleteItems(array $keys) + public function deleteItems(array $keys): bool { array_walk($keys, [$this, 'isValidKey']); @@ -127,7 +127,7 @@ public function deleteItems(array $keys) * @return bool * True if the item was successfully persisted. False if there was an error. */ - public function save(CacheItemInterface $item) + public function save(CacheItemInterface $item): bool { $this->items[$item->getKey()] = $item; @@ -140,7 +140,7 @@ public function save(CacheItemInterface $item) * @return bool * False if the item could not be queued or if a commit was attempted and failed. True otherwise. */ - public function saveDeferred(CacheItemInterface $item) + public function saveDeferred(CacheItemInterface $item): bool { $this->deferredItems[$item->getKey()] = $item; @@ -153,7 +153,7 @@ public function saveDeferred(CacheItemInterface $item) * @return bool * True if all not-yet-saved items were successfully saved or there were none. False otherwise. */ - public function commit() + public function commit(): bool { foreach ($this->deferredItems as $item) { $this->save($item); diff --git a/src/Cache/SysVCacheItemPool.php b/src/Cache/SysVCacheItemPool.php index 1834cf1b3..c552d8d2e 100644 --- a/src/Cache/SysVCacheItemPool.php +++ b/src/Cache/SysVCacheItemPool.php @@ -90,7 +90,7 @@ public function __construct($options = []) $this->sysvKey = ftok(__FILE__, $this->options['proj']); } - public function getItem($key) + public function getItem($key): CacheItemInterface { $this->loadItems(); return current($this->getItems([$key])); @@ -99,14 +99,15 @@ public function getItem($key) /** * {@inheritdoc} */ - public function getItems(array $keys = []) + public function getItems(array $keys = []): iterable { $this->loadItems(); $items = []; + $itemClass = \PHP_VERSION_ID >= 80000 ? TypedItem::class : Item::class; foreach ($keys as $key) { $items[$key] = $this->hasItem($key) ? clone $this->items[$key] : - new Item($key); + new $itemClass($key); } return $items; } @@ -114,7 +115,7 @@ public function getItems(array $keys = []) /** * {@inheritdoc} */ - public function hasItem($key) + public function hasItem($key): bool { $this->loadItems(); return isset($this->items[$key]) && $this->items[$key]->isHit(); @@ -123,7 +124,7 @@ public function hasItem($key) /** * {@inheritdoc} */ - public function clear() + public function clear(): bool { $this->items = []; $this->deferredItems = []; @@ -133,7 +134,7 @@ public function clear() /** * {@inheritdoc} */ - public function deleteItem($key) + public function deleteItem($key): bool { return $this->deleteItems([$key]); } @@ -141,7 +142,7 @@ public function deleteItem($key) /** * {@inheritdoc} */ - public function deleteItems(array $keys) + public function deleteItems(array $keys): bool { if (!$this->hasLoadedItems) { $this->loadItems(); @@ -156,7 +157,7 @@ public function deleteItems(array $keys) /** * {@inheritdoc} */ - public function save(CacheItemInterface $item) + public function save(CacheItemInterface $item): bool { if (!$this->hasLoadedItems) { $this->loadItems(); @@ -169,7 +170,7 @@ public function save(CacheItemInterface $item) /** * {@inheritdoc} */ - public function saveDeferred(CacheItemInterface $item) + public function saveDeferred(CacheItemInterface $item): bool { $this->deferredItems[$item->getKey()] = $item; return true; @@ -178,7 +179,7 @@ public function saveDeferred(CacheItemInterface $item) /** * {@inheritdoc} */ - public function commit() + public function commit(): bool { foreach ($this->deferredItems as $item) { if ($this->save($item) === false) { diff --git a/src/Cache/TypedItem.php b/src/Cache/TypedItem.php new file mode 100644 index 000000000..7ad071068 --- /dev/null +++ b/src/Cache/TypedItem.php @@ -0,0 +1,187 @@ +key = $key; + $this->expiration = null; + } + + /** + * {@inheritdoc} + */ + public function getKey(): string + { + return $this->key; + } + + /** + * {@inheritdoc} + */ + public function get(): mixed + { + return $this->isHit() ? $this->value : null; + } + + /** + * {@inheritdoc} + */ + public function isHit(): bool + { + if (!$this->isHit) { + return false; + } + + if ($this->expiration === null) { + return true; + } + + return $this->currentTime()->getTimestamp() < $this->expiration->getTimestamp(); + } + + /** + * {@inheritdoc} + */ + public function set(mixed $value): static + { + $this->isHit = true; + $this->value = $value; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function expiresAt($expiration): static + { + if ($this->isValidExpiration($expiration)) { + $this->expiration = $expiration; + + return $this; + } + + $implementationMessage = interface_exists('DateTimeInterface') + ? 'implement interface DateTimeInterface' + : 'be an instance of DateTime'; + + $error = sprintf( + 'Argument 1 passed to %s::expiresAt() must %s, %s given', + get_class($this), + $implementationMessage, + gettype($expiration) + ); + + $this->handleError($error); + } + + /** + * {@inheritdoc} + */ + public function expiresAfter($time): static + { + if (is_int($time)) { + $this->expiration = $this->currentTime()->add(new \DateInterval("PT{$time}S")); + } elseif ($time instanceof \DateInterval) { + $this->expiration = $this->currentTime()->add($time); + } elseif ($time === null) { + $this->expiration = $time; + } else { + $message = 'Argument 1 passed to %s::expiresAfter() must be an ' . + 'instance of DateInterval or of the type integer, %s given'; + $error = sprintf($message, get_class($this), gettype($time)); + + $this->handleError($error); + } + + return $this; + } + + /** + * Handles an error. + * + * @param string $error + * @throws \TypeError + */ + private function handleError($error) + { + if (class_exists('TypeError')) { + throw new \TypeError($error); + } + + trigger_error($error, \E_USER_ERROR); + } + + /** + * Determines if an expiration is valid based on the rules defined by PSR6. + * + * @param mixed $expiration + * @return bool + */ + private function isValidExpiration($expiration) + { + if ($expiration === null) { + return true; + } + + // We test for two types here due to the fact the DateTimeInterface + // was not introduced until PHP 5.5. Checking for the DateTime type as + // well allows us to support 5.4. + if ($expiration instanceof \DateTimeInterface) { + return true; + } + + if ($expiration instanceof \DateTime) { + return true; + } + + return false; + } + + protected function currentTime() + { + return new \DateTime('now', new \DateTimeZone('UTC')); + } +} diff --git a/tests/AccessTokenTest.php b/tests/AccessTokenTest.php index 6299896c5..a6e66b1b8 100644 --- a/tests/AccessTokenTest.php +++ b/tests/AccessTokenTest.php @@ -289,9 +289,11 @@ public function testRetrieveCertsFromLocationLocalFile() ->shouldBeCalledTimes(1) ->willReturn(null); $item->set($certsData) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($item->reveal()); $item->expiresAt(Argument::type('\DateTime')) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($item->reveal()); $this->cache->getItem('google_auth_certs_cache|' . sha1($certsLocation)) ->shouldBeCalledTimes(1) @@ -411,9 +413,11 @@ public function testRetrieveCertsFromLocationRemote() ->shouldBeCalledTimes(1) ->willReturn(null); $item->set($certsData) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($item->reveal()); $item->expiresAt(Argument::type('\DateTime')) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($item->reveal()); $this->cache->getItem('google_auth_certs_cache|federated_signon_certs_v3') ->shouldBeCalledTimes(1) diff --git a/tests/ApplicationDefaultCredentialsTest.php b/tests/ApplicationDefaultCredentialsTest.php index a1655f5c1..75b50697b 100644 --- a/tests/ApplicationDefaultCredentialsTest.php +++ b/tests/ApplicationDefaultCredentialsTest.php @@ -140,7 +140,7 @@ public function testGceCredentials() // used default scope $tokenUri = $uriProperty->getValue($creds); - $this->assertContains('a+default+scope', $tokenUri); + $this->assertStringContainsString('a+default+scope', $tokenUri); $creds = ApplicationDefaultCredentials::getCredentials( 'a+user+scope', // $scope @@ -156,7 +156,7 @@ public function testGceCredentials() // did not use default scope $tokenUri = $uriProperty->getValue($creds); - $this->assertContains('a+user+scope', $tokenUri); + $this->assertStringContainsString('a+user+scope', $tokenUri); } /** @runInSeparateProcess */ @@ -396,9 +396,11 @@ public function testOnGceCacheWithoutHit() $mockCacheItem->isHit() ->willReturn(false); $mockCacheItem->set(true) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($mockCacheItem->reveal()); $mockCacheItem->expiresAfter(1500) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($mockCacheItem->reveal()); $mockCache = $this->prophesize('Psr\Cache\CacheItemPoolInterface'); $mockCache->getItem(GCECache::GCE_CACHE_KEY) @@ -433,9 +435,11 @@ public function testOnGceCacheWithOptions() $mockCacheItem->isHit() ->willReturn(false); $mockCacheItem->set(true) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($mockCacheItem->reveal()); $mockCacheItem->expiresAfter($lifetime) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($mockCacheItem->reveal()); $mockCache = $this->prophesize('Psr\Cache\CacheItemPoolInterface'); $mockCache->getItem($prefix . GCECache::GCE_CACHE_KEY) diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 26b127dc1..e38e3edf1 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -41,4 +41,13 @@ public function getValidKeyName($key) { return preg_replace('|[^a-zA-Z0-9_\.! ]|', '', $key); } + + protected function getCacheItemClass() + { + if (\PHP_VERSION_ID >= 80000) { + return 'Google\Auth\Cache\TypedItem'; + } + + return 'Google\Auth\Cache\Item'; + } } diff --git a/tests/Cache/ItemTest.php b/tests/Cache/ItemTest.php index 9312d82ad..8917f8b32 100644 --- a/tests/Cache/ItemTest.php +++ b/tests/Cache/ItemTest.php @@ -18,12 +18,17 @@ namespace Google\Auth\Tests\Cache; use Google\Auth\Cache\Item; +use Google\Auth\Cache\TypedItem; use PHPUnit\Framework\TestCase; class ItemTest extends TestCase { public function getItem($key) { + if (\PHP_VERSION_ID >= 80000) { + return new TypedItem($key); + } + return new Item($key); } diff --git a/tests/Cache/MemoryCacheItemPoolTest.php b/tests/Cache/MemoryCacheItemPoolTest.php index 056369089..6a8aff038 100644 --- a/tests/Cache/MemoryCacheItemPoolTest.php +++ b/tests/Cache/MemoryCacheItemPoolTest.php @@ -18,10 +18,10 @@ namespace Google\Auth\Tests\Cache; use Google\Auth\Cache\MemoryCacheItemPool; -use PHPUnit\Framework\TestCase; +use Google\Auth\Tests\BaseTest; use Psr\Cache\InvalidArgumentException; -class MemoryCacheItemPoolTest extends TestCase +class MemoryCacheItemPoolTest extends BaseTest { private $pool; @@ -43,7 +43,7 @@ public function testGetsFreshItem() { $item = $this->pool->getItem('item'); - $this->assertInstanceOf('Google\Auth\Cache\Item', $item); + $this->assertInstanceOf($this->getCacheItemClass(), $item); $this->assertNull($item->get()); $this->assertFalse($item->isHit()); } @@ -55,7 +55,7 @@ public function testGetsExistingItem() $this->saveItem($key, $value); $item = $this->pool->getItem($key); - $this->assertInstanceOf('Google\Auth\Cache\Item', $item); + $this->assertInstanceOf($this->getCacheItemClass(), $item); $this->assertEquals($value, $item->get()); $this->assertTrue($item->isHit()); } @@ -66,7 +66,7 @@ public function testGetsMultipleItems() $items = $this->pool->getItems($keys); $this->assertEquals($keys, array_keys($items)); - $this->assertContainsOnlyInstancesOf('Google\Auth\Cache\Item', $items); + $this->assertContainsOnlyInstancesOf($this->getCacheItemClass(), $items); } public function testHasItem() diff --git a/tests/Cache/SysVCacheItemPoolTest.php b/tests/Cache/SysVCacheItemPoolTest.php index 902b9c2b4..46f7812c1 100644 --- a/tests/Cache/SysVCacheItemPoolTest.php +++ b/tests/Cache/SysVCacheItemPoolTest.php @@ -18,9 +18,9 @@ namespace Google\Auth\Tests\Cache; use Google\Auth\Cache\SysVCacheItemPool; -use PHPUnit\Framework\TestCase; +use Google\Auth\Tests\BaseTest; -class SysVCacheItemPoolTest extends TestCase +class SysVCacheItemPoolTest extends BaseTest { private $pool; @@ -48,7 +48,7 @@ public function testGetsFreshItem() { $item = $this->pool->getItem('item'); - $this->assertInstanceOf('Google\Auth\Cache\Item', $item); + $this->assertInstanceOf($this->getCacheItemClass(), $item); $this->assertNull($item->get()); $this->assertFalse($item->isHit()); } @@ -70,7 +70,7 @@ public function testGetsExistingItem() $this->saveItem($key, $value); $item = $this->pool->getItem($key); - $this->assertInstanceOf('Google\Auth\Cache\Item', $item); + $this->assertInstanceOf($this->getCacheItemClass(), $item); $this->assertEquals($value, $item->get()); $this->assertTrue($item->isHit()); } @@ -81,7 +81,7 @@ public function testGetsMultipleItems() $items = $this->pool->getItems($keys); $this->assertEquals($keys, array_keys($items)); - $this->assertContainsOnlyInstancesOf('Google\Auth\Cache\Item', $items); + $this->assertContainsOnlyInstancesOf($this->getCacheItemClass(), $items); } public function testHasItem() diff --git a/tests/Cache/sysv_cache_creator.php b/tests/Cache/sysv_cache_creator.php index 618097d23..3231c4301 100644 --- a/tests/Cache/sysv_cache_creator.php +++ b/tests/Cache/sysv_cache_creator.php @@ -21,10 +21,15 @@ use Google\Auth\Cache\Item; use Google\Auth\Cache\SysVCacheItemPool; +use Google\Auth\Cache\TypedItem; $value = $argv[1]; // Use the same variableKey in the test. $pool = new SysVCacheItemPool(['variableKey' => 99]); -$item = new Item('separate-process-item'); +if (\PHP_VERSION_ID >= 80000) { + $item = new TypedItem('separate-process-item'); +} else { + $item = new Item('separate-process-item'); +} $item->set($value); $pool->save($item); diff --git a/tests/CacheTraitTest.php b/tests/CacheTraitTest.php index a83019076..ac93eb5de 100644 --- a/tests/CacheTraitTest.php +++ b/tests/CacheTraitTest.php @@ -128,9 +128,11 @@ public function testSuccessfullySetsToCache() { $value = '1234'; $this->mockCacheItem->set($value) - ->shouldBeCalled(); + ->shouldBeCalled() + ->willReturn($this->mockCacheItem->reveal()); $this->mockCacheItem->expiresAfter(Argument::any()) - ->shouldBeCalled(); + ->shouldBeCalled() + ->willReturn($this->mockCacheItem->reveal()); $this->mockCache->getItem('key') ->willReturn($this->mockCacheItem->reveal()); $this->mockCache->save(Argument::type('Psr\Cache\CacheItemInterface')) diff --git a/tests/Credentials/ServiceAccountCredentialsTest.php b/tests/Credentials/ServiceAccountCredentialsTest.php index af8896388..9546ee5d7 100644 --- a/tests/Credentials/ServiceAccountCredentialsTest.php +++ b/tests/Credentials/ServiceAccountCredentialsTest.php @@ -660,10 +660,10 @@ public function testUpdateMetadataWithScopeAndUseJwtAccessWithScopeParameter() ); $authorization = $actual_metadata[CredentialsLoader::AUTH_METADATA_KEY]; - $this->assertInternalType('array', $authorization); + $this->assertTrue(is_array($authorization)); $bearer_token = current($authorization); - $this->assertInternalType('string', $bearer_token); + $this->assertTrue(is_string($bearer_token)); $this->assertEquals(0, strpos($bearer_token, 'Bearer ')); // Ensure scopes are signed inside @@ -671,7 +671,7 @@ public function testUpdateMetadataWithScopeAndUseJwtAccessWithScopeParameter() $this->assertEquals(2, substr_count($token, '.')); list($header, $payload, $sig) = explode('.', $bearer_token); $json = json_decode(base64_decode($payload), true); - $this->assertInternalType('array', $json); + $this->assertTrue(is_array($json)); $this->assertArrayHasKey('scope', $json); $this->assertEquals($json['scope'], $scope); } @@ -699,10 +699,10 @@ public function testUpdateMetadataWithScopeAndUseJwtAccessWithScopeParameterAndA ); $authorization = $actual_metadata[CredentialsLoader::AUTH_METADATA_KEY]; - $this->assertInternalType('array', $authorization); + $this->assertTrue(is_array($authorization)); $bearer_token = current($authorization); - $this->assertInternalType('string', $bearer_token); + $this->assertTrue(is_string($bearer_token)); $this->assertEquals(0, strpos($bearer_token, 'Bearer ')); // Ensure scopes are signed inside @@ -710,13 +710,13 @@ public function testUpdateMetadataWithScopeAndUseJwtAccessWithScopeParameterAndA $this->assertEquals(2, substr_count($token, '.')); list($header, $payload, $sig) = explode('.', $bearer_token); $json = json_decode(base64_decode($payload), true); - $this->assertInternalType('array', $json); + $this->assertTrue(is_array($json)); $this->assertArrayHasKey('scope', $json); $this->assertEquals($json['scope'], implode(' ', $scope)); // Test last received token $cachedToken = $sa->getLastReceivedToken(); - $this->assertInternalType('array', $cachedToken); + $this->assertTrue(is_array($cachedToken)); $this->assertArrayHasKey('access_token', $cachedToken); $this->assertEquals($token, $cachedToken['access_token']); } @@ -734,7 +734,7 @@ public function testFetchAuthTokenWithScopeAndUseJwtAccessWithScopeParameter() $sa->useJwtAccessWithScope(); $access_token = $sa->fetchAuthToken(); - $this->assertInternalType('array', $access_token); + $this->assertTrue(is_array($access_token)); $this->assertArrayHasKey('access_token', $access_token); $token = $access_token['access_token']; @@ -742,7 +742,7 @@ public function testFetchAuthTokenWithScopeAndUseJwtAccessWithScopeParameter() $this->assertEquals(2, substr_count($token, '.')); list($header, $payload, $sig) = explode('.', $token); $json = json_decode(base64_decode($payload), true); - $this->assertInternalType('array', $json); + $this->assertTrue(is_array($json)); $this->assertArrayHasKey('scope', $json); $this->assertEquals($json['scope'], $scope); } @@ -760,7 +760,7 @@ public function testFetchAuthTokenWithScopeAndUseJwtAccessWithScopeParameterAndA $sa->useJwtAccessWithScope(); $access_token = $sa->fetchAuthToken(); - $this->assertInternalType('array', $access_token); + $this->assertTrue(is_array($access_token)); $this->assertArrayHasKey('access_token', $access_token); $token = $access_token['access_token']; @@ -768,13 +768,13 @@ public function testFetchAuthTokenWithScopeAndUseJwtAccessWithScopeParameterAndA $this->assertEquals(2, substr_count($token, '.')); list($header, $payload, $sig) = explode('.', $token); $json = json_decode(base64_decode($payload), true); - $this->assertInternalType('array', $json); + $this->assertTrue(is_array($json)); $this->assertArrayHasKey('scope', $json); $this->assertEquals($json['scope'], implode(' ', $scope)); // Test last received token $cachedToken = $sa->getLastReceivedToken(); - $this->assertInternalType('array', $cachedToken); + $this->assertTrue(is_array($cachedToken)); $this->assertArrayHasKey('access_token', $cachedToken); $this->assertEquals($token, $cachedToken['access_token']); } diff --git a/tests/CredentialsLoaderTest.php b/tests/CredentialsLoaderTest.php index 64222d7d1..770d9195a 100644 --- a/tests/CredentialsLoaderTest.php +++ b/tests/CredentialsLoaderTest.php @@ -103,8 +103,8 @@ public function testActualDefaultClientCertSource() } $creds = $clientCertSource(); $this->assertTrue(is_string($creds)); - $this->assertContains('-----BEGIN CERTIFICATE-----', $creds); - $this->assertContains('-----BEGIN PRIVATE KEY-----', $creds); + $this->assertStringContainsString('-----BEGIN CERTIFICATE-----', $creds); + $this->assertStringContainsString('-----BEGIN PRIVATE KEY-----', $creds); } /** diff --git a/tests/FetchAuthTokenCacheTest.php b/tests/FetchAuthTokenCacheTest.php index 566721e1a..088540d34 100644 --- a/tests/FetchAuthTokenCacheTest.php +++ b/tests/FetchAuthTokenCacheTest.php @@ -157,9 +157,11 @@ public function testUpdateMetadataWithoutCache() ->shouldBeCalled() ->willReturn($value); $this->mockCacheItem->set($value) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); $this->mockCacheItem->expiresAfter(1500) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); $this->mockCache->save($this->mockCacheItem) ->shouldBeCalledTimes(1); $this->mockFetcher->updateMetadata(Argument::type('array'), null, null) @@ -299,9 +301,11 @@ public function testShouldNotReturnValueWhenExpired() ->shouldBeCalledTimes(1) ->willReturn($cachedValue); $this->mockCacheItem->set($newToken) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); $this->mockCacheItem->expiresAfter(1500) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); $this->mockCache->getItem($cacheKey) ->shouldBeCalledTimes(2) ->willReturn($this->mockCacheItem->reveal()); @@ -368,9 +372,10 @@ public function testShouldSaveValueInCacheWithCacheOptions() ->willReturn(false); $this->mockCacheItem->set($cachedValue) ->shouldBeCalledTimes(1) - ->willReturn(false); + ->willReturn($this->mockCacheItem->reveal()); $this->mockCacheItem->expiresAfter($lifetime) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); $this->mockCache->getItem($prefix . $cacheKey) ->shouldBeCalledTimes(2) ->willReturn($this->mockCacheItem->reveal()); diff --git a/tests/GCECacheTest.php b/tests/GCECacheTest.php index d735932e3..d91804013 100644 --- a/tests/GCECacheTest.php +++ b/tests/GCECacheTest.php @@ -86,9 +86,11 @@ public function testUncached() ->shouldBeCalledTimes(1) ->willReturn(false); $this->mockCacheItem->set(true) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); $this->mockCacheItem->expiresAfter(1500) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); $this->mockCache->getItem(GCECache::GCE_CACHE_KEY) ->shouldBeCalledTimes(2) ->willReturn($this->mockCacheItem->reveal()); @@ -139,9 +141,11 @@ public function testShouldSaveValueInCacheWithCacheOptions() $this->mockCacheItem->isHit() ->willReturn(false); $this->mockCacheItem->set(true) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); $this->mockCacheItem->expiresAfter($lifetime) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); $this->mockCache->getItem($prefix . GCECache::GCE_CACHE_KEY) ->shouldBeCalledTimes(2) ->willReturn($this->mockCacheItem->reveal()); diff --git a/tests/Middleware/AuthTokenMiddlewareTest.php b/tests/Middleware/AuthTokenMiddlewareTest.php index 8b09156cf..a9e7aa6ec 100644 --- a/tests/Middleware/AuthTokenMiddlewareTest.php +++ b/tests/Middleware/AuthTokenMiddlewareTest.php @@ -220,9 +220,10 @@ public function testShouldSaveValueInCacheWithSpecifiedPrefix() ->willReturn(false); $this->mockCacheItem->set($cachedValue) ->shouldBeCalledTimes(1) - ->willReturn(false); + ->willReturn($this->mockCacheItem->reveal()); $this->mockCacheItem->expiresAfter($lifetime) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); $this->mockCache->getItem($prefix . $cacheKey) ->shouldBeCalled() ->willReturn($this->mockCacheItem->reveal()); @@ -264,9 +265,11 @@ public function testShouldNotifyTokenCallback(callable $tokenCallback) $this->mockCacheItem->isHit() ->willReturn(false); $this->mockCacheItem->set($cachedValue) - ->shouldBeCalled(); + ->shouldBeCalled() + ->willReturn($this->mockCacheItem->reveal()); $this->mockCacheItem->expiresAfter(Argument::any()) - ->shouldBeCalled(); + ->shouldBeCalled() + ->willReturn($this->mockCacheItem->reveal()); $this->mockCache->getItem($prefix . $cacheKey) ->willReturn($this->mockCacheItem->reveal()); $this->mockCache->save(Argument::type('Psr\Cache\CacheItemInterface')) diff --git a/tests/Middleware/ScopedAccessTokenMiddlewareTest.php b/tests/Middleware/ScopedAccessTokenMiddlewareTest.php index 34dcce488..a7b31650a 100644 --- a/tests/Middleware/ScopedAccessTokenMiddlewareTest.php +++ b/tests/Middleware/ScopedAccessTokenMiddlewareTest.php @@ -140,9 +140,10 @@ public function testShouldSaveValueInCache() ->willReturn(false); $this->mockCacheItem->set($token) ->shouldBeCalledTimes(1) - ->willReturn(false); + ->willReturn($this->mockCacheItem->reveal()); $this->mockCacheItem->expiresAfter(Argument::any()) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); $this->mockCache->getItem($this->getValidKeyName(self::TEST_SCOPE)) ->shouldBeCalledTimes(2) ->willReturn($this->mockCacheItem->reveal()); @@ -178,9 +179,10 @@ public function testShouldSaveValueInCacheWithCacheOptions() ->willReturn(false); $this->mockCacheItem->set($token) ->shouldBeCalledTimes(1) - ->willReturn(false); + ->willReturn($this->mockCacheItem->reveal()); $this->mockCacheItem->expiresAfter($lifetime) - ->shouldBeCalledTimes(1); + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); $this->mockCache->getItem($prefix . $this->getValidKeyName(self::TEST_SCOPE)) ->shouldBeCalledTimes(2) ->willReturn($this->mockCacheItem->reveal());