Skip to content

Commit

Permalink
Bump deps
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed Apr 9, 2023
1 parent f8d6395 commit 883d70d
Show file tree
Hide file tree
Showing 10 changed files with 570 additions and 724 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
run: composer install

- name: Run unit tests
run: vendor/bin/phpunit --verbose
run: vendor/bin/phpunit

- name: Run static code analysis
run: vendor/bin/psalm
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ vendor
phpunit.xml
.vscode
.php-cs-fixer.cache
.phpunit.result.cache
.phpunit.cache
.psalm
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"ext-memcached": "*",
"imbo/imbo": "dev-main",
"imbo/imbo-coding-standard": "^2.0",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^10.0",
"psalm/plugin-phpunit": "^0.18.4",
"vimeo/psalm": "^5.5"
},
Expand All @@ -42,8 +42,8 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit --verbose",
"test:coverage": "vendor/bin/phpunit --verbose --coverage-html build/coverage",
"test": "vendor/bin/phpunit",
"test:coverage": "vendor/bin/phpunit --coverage-html build/coverage",
"cs": "php-cs-fixer fix --dry-run --diff",
"sa": "vendor/bin/psalm",
"ci": [
Expand Down
1,075 changes: 439 additions & 636 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: '3.1'
services:
memcached:
image: memcached
image: memcached:1.6-alpine
ports:
- 11211:11211
18 changes: 4 additions & 14 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<coverage processUncoveredFiles="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" bootstrap="vendor/autoload.php" executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache" requireCoverageMetadata="true" beStrictAboutCoverageMetadata="false">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>

<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<php>
<env name="MEMCACHED_HOST" value="localhost" />
<env name="MEMCACHED_PORT" value="11211" />
<env name="MEMCACHED_HOST" value="localhost"/>
<env name="MEMCACHED_PORT" value="11211"/>
</php>
</phpunit>
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedCode="false"
findUnusedBaselineEntry="true"
>
<projectFiles>
<directory name="src"/>
Expand Down
55 changes: 30 additions & 25 deletions src/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ public function loadFromCache(EventInterface $event): void
{
$request = $event->getRequest();
$response = $event->getResponse();
$user = $request->getUser();

$cacheKey = $this->getCacheKey(
$request->getUser(),
$request->getImageIdentifier(),
);
if (null === $user) {
return;
}

$cacheKey = $this->getCacheKey($user, $request->getImageIdentifier());

/** @var mixed */
$result = $this->cache->get($cacheKey);
Expand Down Expand Up @@ -100,28 +102,31 @@ public function loadFromCache(EventInterface $event): void
*/
public function storeInCache(EventInterface $event): void
{
$request = $event->getRequest();
$response = $event->getResponse();

$cacheKey = $this->getCacheKey(
$request->getUser(),
$request->getImageIdentifier(),
);
if ($response->getStatusCode() !== 200) {
return;
}

// Store the response in the cache for later use
if ($response->getStatusCode() === 200) {
$metadata = [];
$request = $event->getRequest();
$user = $request->getUser();

if ($model = $response->getModel()) {
/** @var mixed */
$metadata = $model->getData();
}
if (null === $user) {
return;
}

$cacheKey = $this->getCacheKey($user, $request->getImageIdentifier());
$metadata = [];

$this->cache->set($cacheKey, [
'lastModified' => $response->getLastModified(),
'metadata' => $metadata,
]);
if ($model = $response->getModel()) {
/** @var mixed */
$metadata = $model->getData();
}

$this->cache->set($cacheKey, [
'lastModified' => $response->getLastModified(),
'metadata' => $metadata,
]);
}

/**
Expand All @@ -132,13 +137,13 @@ public function storeInCache(EventInterface $event): void
public function deleteFromCache(EventInterface $event): void
{
$request = $event->getRequest();
$user = $request->getUser();

$cacheKey = $this->getCacheKey(
$request->getUser(),
$request->getImageIdentifier(),
);
if (null === $user) {
return;
}

$this->cache->delete($cacheKey);
$this->cache->delete($this->getCacheKey($user, $request->getImageIdentifier()));
}

/**
Expand Down
54 changes: 26 additions & 28 deletions tests/Cache/CacheTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,14 @@ abstract class CacheTests extends TestCase
{
abstract protected function getAdapter(): CacheInterface;

/**
* @return array<string,array{key:string,value:mixed}>
*/
public function getCacheData(): array
{
return [
'string value' => [
'key' => 'key1',
'value' => 'value',
],
'numeric value' => [
'key' => 'key2',
'value' => 123,
],
'list value' => [
'key' => 'key3',
'value' => [1, 2, 3],
],
'object value' => [
'key' => 'key4',
'value' => new stdClass(),
],
];
}

/**
* @dataProvider getCacheData
* @param string $key The cache key
* @param mixed $value The cache value
* @covers ::__construct
* @covers ::get
* @covers ::set
* @covers ::delete
*/
public function testSetGetAndDelete(string $key, $value): void
public function testSetGetAndDelete(string $key, mixed $value): void
{
$adapter = $this->getAdapter();

Expand All @@ -68,4 +41,29 @@ public function testSetGetAndDelete(string $key, $value): void
'Value has not been removed from cache',
);
}

/**
* @return array<string,array{key:string,value:mixed}>
*/
public static function getCacheData(): array
{
return [
'string value' => [
'key' => 'key1',
'value' => 'value',
],
'numeric value' => [
'key' => 'key2',
'value' => 123,
],
'list value' => [
'key' => 'key3',
'value' => [1, 2, 3],
],
'object value' => [
'key' => 'key4',
'value' => new stdClass(),
],
];
}
}
78 changes: 63 additions & 15 deletions tests/EventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,11 @@
*/
class EventListenerTest extends TestCase
{
/** @var EventInterface&MockObject */
private EventInterface $event;

/** @var Request&MockObject */
private Request $request;

/** @var Response&MockObject */
private Response $response;

/** @var CacheInterface&MockObject */
private $cache;

/** @var ResponseHeaderBag&MockObject */
private $responseHeaders;

private EventInterface&MockObject $event;
private Request&MockObject $request;
private Response&MockObject $response;
private CacheInterface&MockObject $cache;
private ResponseHeaderBag&MockObject $responseHeaders;
private string $user = 'user';
private string $imageIdentifier = 'imageid';
private EventListener $listener;
Expand Down Expand Up @@ -62,6 +52,63 @@ protected function getListener(): EventListener
return $this->listener;
}

/**
* @covers ::loadFromCache
*/
public function testLoadFromCacheReturnsEarlyOnMissingUser(): void
{
$this->cache
->expects($this->never())
->method('get');

$this->listener->loadFromCache(
$this->createConfiguredMock(EventInterface::class, [
'getRequest' => $this->createMock(Request::class),
]),
);
}

/**
* @covers ::storeInCache
*/
public function testStoreInCacheReturnsEarlyOnMissingUser(): void
{
$this->cache
->expects($this->never())
->method('set');

/** @var Request&MockObject */
$request = $this->createMock(Request::class);
$request
->expects($this->never())
->method('getImageIdentifier');

$this->listener->storeInCache(
$this->createConfiguredMock(EventInterface::class, [
'getRequest' => $request,
'getResponse' => $this->createConfiguredMock(Response::class, [
'getStatusCode' => 200,
]),
]),
);
}

/**
* @covers ::deleteFromCache
*/
public function testDeleteFromCacheReturnsEarlyOnMissingUser(): void
{
$this->cache
->expects($this->never())
->method('delete');

$this->listener->deleteFromCache(
$this->createConfiguredMock(EventInterface::class, [
'getRequest' => $this->createMock(Request::class),
]),
);
}

/**
* @covers ::loadFromCache
*/
Expand Down Expand Up @@ -152,6 +199,7 @@ public function testStoresDataInCacheWhenResponseCodeIs200(): void
'metadata' => $data,
]);

/** @var ArrayModel&MockObject */
$model = $this->createMock(ArrayModel::class);
$model
->expects($this->once())
Expand Down

0 comments on commit 883d70d

Please sign in to comment.