Skip to content

Commit

Permalink
support for php 7.4 and symfony 5.1 and phpunit 9
Browse files Browse the repository at this point in the history
  • Loading branch information
batenburg committed Oct 18, 2020
1 parent d3141b4 commit 3a288fe
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 46 deletions.
21 changes: 13 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,38 @@ script:
jobs:
include:
# Test against latest Symfony 4.3 stable
- php: 7.3
- php: 7.4
install:
- composer require symfony/cache:4.3.* symfony/dependency-injection:4.3.* symfony/framework-bundle:4.3.*

# Test against latest Symfony 4.4 dev
- php: 7.3
# Test against latest Symfony 4.4 stable
- php: 7.4
install:
- composer require symfony/cache:4.4.* symfony/dependency-injection:4.4.* symfony/framework-bundle:4.4.*

# Test against latest Symfony 5.0 dev
- php: 7.3
# Test against latest Symfony 5.0 stable
- php: 7.4
install:
- composer require symfony/cache:5.0.* symfony/dependency-injection:5.0.* symfony/framework-bundle:5.0.*

# Test against latest Symfony 5.1 stable
- php: 7.4
install:
- composer require symfony/cache:5.1.* symfony/dependency-injection:5.1.* symfony/framework-bundle:5.1.*

# Test dev versions
- php: 7.3
- php: 7.4
if: type = cron
install:
- composer update

- stage: Code Quality
php: 7.3
php: 7.4
script:
- ./vendor/bin/phpcs

- stage: Coverage
php: 7.3
php: 7.4
install:
- composer update
before_script:
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
],
"require": {
"php": ">= 7.2.0",
"symfony/cache": "^4.3 || ^4.4 || ^5.0",
"symfony/dependency-injection": "^4.3 || ^4.4 || ^5.0",
"symfony/framework-bundle": "^4.3 || ^4.4 || ^5.0"
"symfony/cache": "^4.3 || ^4.4 || ^5.0 || ^5.1 || ^5.2" ,
"symfony/dependency-injection": "^4.3 || ^4.4 || ^5.0 || ^5.1 || ^5.2",
"symfony/framework-bundle": "^4.3 || ^4.4 || ^5.0 || ^5.1 || ^5.2"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
"phpunit/phpunit": "^8.5 || ^9.0",
"satooshi/php-coveralls": "^2.2",
"squizlabs/php_codesniffer": "^3.5"
},
Expand Down
77 changes: 43 additions & 34 deletions tests/Unit/Repository/CacheRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function testHasItem(
bool $isCacheHit
): void {
// Setup
$this->mockCacheItemIsHit($key, $isCacheHit);
$this->mockGetItem(1, [$key]);
$this->mockCacheItemIsHit($isCacheHit);
// Execute
$result = $this->cacheRepository->hasItem($key);
// Validate
Expand All @@ -79,7 +80,8 @@ public function testMissingItem(
bool $isCacheHit
): void {
// Setup
$this->mockCacheItemIsHit($key, $isCacheHit);
$this->mockGetItem(1, [$key]);
$this->mockCacheItemIsHit($isCacheHit);
// Execute
$result = $this->cacheRepository->missingItem($key);
// Validate
Expand All @@ -94,7 +96,8 @@ public function testGetItemResolvesTheItem(): void
{
// Setup
$key = 'test.key';
$this->mockCacheItemIsHit($key, true);
$this->mockGetItem(1, [$key]);
$this->mockCacheItemIsHit(true);
// Expectation
$this->cachedItem->expects($this->never())
->method('set');
Expand All @@ -115,7 +118,8 @@ public function testGetItemResolvesTheDefaultIntoTheItem(): void
// Setup
$key = 'test.key';
$cachingText = 'test cache text';
$this->mockCacheItemIsHit($key, false);
$this->mockGetItem(1, [$key]);
$this->mockCacheItemIsHit(false);
// Expectation
$this->cachedItem->expects($this->once())
->method('set')
Expand All @@ -137,7 +141,8 @@ public function testGetItemReturnsTheItemWithoutResolvingDefaultWhenDefaultNull(
{
// Setup
$key = 'test.key';
$this->mockCacheItemIsHit($key, false);
$this->mockGetItem(1, [$key]);
$this->mockCacheItemIsHit(false);
// Expectation
$this->cachedItem->expects($this->never())
->method('set');
Expand All @@ -160,7 +165,8 @@ public function testWhenTheItemIsRetrievedFromTheCacheItWillNotSaveToTheCache():
$closure = function () {
return 'item';
};
$this->mockCacheItemIsHit($key, true);
$this->mockGetItem(1, [$key]);
$this->mockCacheItemIsHit(true);
// Expectations
$this->cachedItem->expects($this->never())
->method('set');
Expand All @@ -184,9 +190,10 @@ public function testWhenAnItemIsNotInCacheTheRememberItemWillSaveAndReturnIt():
$closure = function () use ($expected) {
return $expected;
};
$this->mockCacheItemIsHit($key, false);
$this->mockGetItem(2, [$key], [$key]);
$this->mockCacheItemIsHit(false);
// Expectations
$this->expectedItemSave($key, 1, $expected, 3600);
$this->expectedItemSave($expected, 3600);
// Execute
$result = $this->cacheRepository->rememberItem($key, $closure);
// Validate
Expand All @@ -203,9 +210,10 @@ public function testAnItemIsRememberedWithArguments(): void
$id = 999;
$key = 'test.key';
$closure = [$this, 'fakeFindById'];
$this->mockCacheItemIsHit($key, false);
$this->mockGetItem(2, [$key], [$key]);
$this->mockCacheItemIsHit(false);
// Expectations
$this->expectedItemSave($key, 1, $id, 3600);
$this->expectedItemSave($id, 3600);
// Execute
$result = $this->cacheRepository->rememberItem($key, $closure, null, false, $id);
// Validate
Expand All @@ -223,9 +231,10 @@ public function testAnItemIsRememberedWithMultipleArguments(): void
$argument = 'text';
$key = 'test.key';
$closure = [$this, 'fakeFindByIdWithArgument'];
$this->mockCacheItemIsHit($key, false);
$this->mockGetItem(2, [$key], [$key]);
$this->mockCacheItemIsHit(false);
// Expectations
$this->expectedItemSave($key, 1, $id . $argument, 3600);
$this->expectedItemSave($id . $argument, 3600);
// Execute
$result = $this->cacheRepository->rememberItem(
$key,
Expand All @@ -252,9 +261,10 @@ public function testARememberItemCanBeStoredWithACustomExpiresAfter(): void
return $expected;
};
$expiresAfterInSeconds = 600;
$this->mockCacheItemIsHit($key, false);
$this->mockGetItem(2, [$key], [$key]);
$this->mockCacheItemIsHit(false);
// Expectations
$this->expectedItemSave($key, 1, $expected, $expiresAfterInSeconds);
$this->expectedItemSave($expected, $expiresAfterInSeconds);
// Execute
$result = $this->cacheRepository->rememberItem($key, $closure, $expiresAfterInSeconds);
// Validate
Expand All @@ -270,9 +280,10 @@ public function testARememberItemCanBeStoredWithACallable(): void
// Setup
$key = 'test.key';
$callable = [$this, 'fakeCallback'];
$this->mockCacheItemIsHit($key, false);
$this->mockGetItem(2, [$key], [$key]);
$this->mockCacheItemIsHit(false);
// Expectations
$this->expectedItemSave($key, 1, 'fake result', 3600);
$this->expectedItemSave('fake result', 3600);
// Execute
$result = $this->cacheRepository->rememberItem($key, $callable);
// Validate
Expand All @@ -288,9 +299,10 @@ public function testARememberItemCanForcedToBeRefreshed(): void
// Setup
$key = 'test.key';
$callable = [$this, 'fakeCallback'];
$this->mockCacheItemIsHit($key, true);
$this->mockGetItem(2, [$key], [$key]);
$this->mockCacheItemIsHit(true);
// Expectations
$this->expectedItemSave($key, 1, 'fake result', 3600);
$this->expectedItemSave('fake result', 3600);
// Execute
$result = $this->cacheRepository->rememberItem($key, $callable, 3600, true);
// Validate
Expand All @@ -308,7 +320,8 @@ public function testSaveItemWithDefaultExpiresAfter(): void
$key = 'test.key';
$value = 'the value to cache';
// Expectations
$this->expectedItemSave($key, 0, $value, 3600);
$this->mockGetItem(1, [$key]);
$this->expectedItemSave($value, 3600);
// Execute
$this->cacheRepository->saveItem($key, $value);
}
Expand All @@ -324,7 +337,8 @@ public function testSaveItemWithAnAdjustedExpiresAfter(): void
$value = 'the value to cache';
$expiresAfterInSeconds = 600;
// Expectations
$this->expectedItemSave($key, 0, $value, $expiresAfterInSeconds);
$this->mockGetItem(1, [$key]);
$this->expectedItemSave($value, $expiresAfterInSeconds);
// Execute
$this->cacheRepository->saveItem($key, $value, $expiresAfterInSeconds);
}
Expand Down Expand Up @@ -413,37 +427,32 @@ public function fakeFindByIdWithArgument(int $id, string $argument): string
return $id . $argument;
}

private function mockGetItem(int $count, ...$keys): void
{
$this->cacheAdapter->expects($this->exactly($count))
->method('getItem')
->withConsecutive(...$keys)
->willReturn($this->cachedItem);
}

/**
* @param string $key
* @param bool $isCacheHit
*/
private function mockCacheItemIsHit(string $key, bool $isCacheHit): void
private function mockCacheItemIsHit(bool $isCacheHit): void
{
$this->cacheAdapter->expects($this->at(0))
->method('getItem')
->with($key)
->willReturn($this->cachedItem);
$this->cachedItem->expects($this->once())
->method('isHit')
->willReturn($isCacheHit);
}

/**
* @param string $key
* @param int $at
* @param string $expectedValue
* @param int $expectedExpiresAfterInSeconds
*/
private function expectedItemSave(
string $key,
int $at,
string $expectedValue,
int $expectedExpiresAfterInSeconds
): void {
$this->cacheAdapter->expects($this->at($at))
->method('getItem')
->with($key)
->willReturn($this->cachedItem);
$this->cachedItem->expects($this->once())
->method('set')
->with($expectedValue)
Expand Down

0 comments on commit 3a288fe

Please sign in to comment.