Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: PHP Composer

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
php-version: ['8.1', '8.2', '8.3', '8.4', '8.5']

steps:
- uses: actions/checkout@v4

- name: Setup PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: none

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-${{ matrix.php-version }}-

- name: Install dependencies
run: composer install -o

- name: Run test suite
run: ./vendor/bin/phpunit -c phpunit.xml
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ docs/
/examples/cache/*
!examples/cache/.gitkeep
coverage/
.phpunit.result.cache
.claude/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.org/mmamedov/page-cache.svg?branch=master)](https://travis-ci.org/mmamedov/page-cache) [![Latest Stable Version](http://img.shields.io/packagist/v/mmamedov/page-cache.svg)](https://packagist.org/packages/mmamedov/page-cache) [![License](https://img.shields.io/packagist/l/mmamedov/page-cache.svg)](https://packagist.org/packages/mmamedov/page-cache)
[![Latest Stable Version](http://img.shields.io/packagist/v/mmamedov/page-cache.svg)](https://packagist.org/packages/mmamedov/page-cache) [![License](https://img.shields.io/packagist/l/mmamedov/page-cache.svg)](https://packagist.org/packages/mmamedov/page-cache)

Full-page PHP Caching library
----
Expand Down Expand Up @@ -27,7 +27,7 @@ Version 2.0 is not backwards compatible with versions starting with v1.0. Versio
was refactored to enable us deliver more features.

When upgrading to version 2.0, please note the followings:
- PHP requirements >= 5.6.
- PHP requirements >= 8.1.
- Your config file must be like this `return [...]` and not `$config = array(...);` like in previous version.
- Config `expiration` setting was renamed to `cache_expiration_in_seconds`
- Use `try/catch` to ensure proper page load in case of PageCache error.
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
}
],
"require": {
"php": ">=5.6.0",
"php": ">=8.1",
"psr/simple-cache": "^1.0",
"psr/log": "^1.0"
},
"require-dev": {
"mobiledetect/mobiledetectlib": "^2.8",
"monolog/monolog": "^1.23",
"mikey179/vfsStream": "^1.6",
"guzzlehttp/guzzle": "^6.3",
"monolog/monolog": "^2.0 || ^3.0",
"mikey179/vfsstream": "^1.6",
"guzzlehttp/guzzle": "^7.0",
"cache/integration-tests": "dev-master",
"symfony/cache": "^3.4 || ^4.0",
"symfony/process": "^3.4 || ^4.0",
"phpunit/phpunit": "^5"
"symfony/cache": "^6.0 || ^7.0",
"symfony/process": "^6.0 || ^7.0",
"phpunit/phpunit": "^10.0"
},
"suggest": {
"monolog/monolog": "Allows using monolog for logging",
Expand Down
11 changes: 4 additions & 7 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<source>
<include>
<directory suffix=".php">src</directory>
<exclude>
<file>examples/config.php</file>
</exclude>
</whitelist>
</filter>
</include>
</source>
</phpunit>
2 changes: 1 addition & 1 deletion src/DefaultLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function log($level, $message, array $context = [])
$exception = isset($context['exception']) ? $context['exception'] : null;
$microTime = microtime(true);
$micro = sprintf("%06d", ($microTime - floor($microTime)) * 1000000);
$logTime = (new \DateTime(date('Y-m-d H:i:s.' . $micro, $microTime)))->format('Y-m-d H:i:s.u');
$logTime = (new \DateTime(date('Y-m-d H:i:s.' . $micro, (int)$microTime)))->format('Y-m-d H:i:s.u');
error_log(
'[' . $logTime . '] '
.$message.($exception ? ' {Exception: '.$exception->getMessage().'}' : '')."\n",
Expand Down
6 changes: 5 additions & 1 deletion src/HttpHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ public function getSentHeaders()
*/
private function setHeader($name, $value = null, $httpResponseCode = null)
{
header($name . ($value ? ': ' . $value : ''), true, $httpResponseCode);
if ($httpResponseCode !== null) {
header($name . ($value ? ': ' . $value : ''), true, $httpResponseCode);
} else {
header($name . ($value ? ': ' . $value : ''), true);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Storage/CacheItemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ private function randomizeExpirationTime(CacheItemInterface $item)

// Slightly random offset
$offset = log10(mt_rand(10, 1000)) * mt_rand(-2, 2);
$item->setExpiresAt((new DateTime())->setTimestamp($expiresAtTimestamp + $offset));
$item->setExpiresAt((new DateTime())->setTimestamp((int)round($expiresAtTimestamp + $offset)));
}
}
39 changes: 17 additions & 22 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ConfigTest extends \PHPUnit\Framework\TestCase
*/
private $config;

public function setUp()
public function setUp(): void
{
date_default_timezone_set('UTC');
$this->config = new Config();
Expand All @@ -40,11 +40,9 @@ public function testPageCacheIntegrationWithSetPath()
$this->assertSame($dir, $this->config->getCachePath());
}

/**
* @expectedException \Exception
*/
public function testSetPath2()
{
$this->expectException(\Exception::class);
$this->config->setCachePath('nonexistant_dir');
}

Expand All @@ -64,50 +62,47 @@ public function testSetExpirationException()
public function testSetEnableLogIsEnableLog()
{
$this->config->setEnableLog(true);
$this->assertAttributeSame(true, 'enableLog', $this->config);
$this->assertSame(true, $this->config->isEnableLog());
$this->assertTrue($this->config->isEnableLog());

$this->config->setEnableLog(false);
$this->assertAttributeSame(false, 'enableLog', $this->config);
$this->assertSame(false, $this->config->isEnableLog());
$this->assertFalse($this->config->isEnableLog());
}


public function testSetGetMinCacheFileSize()
{
$this->config->setMinCacheFileSize(0);
$this->assertAttributeSame(0, 'minCacheFileSize', $this->config);
$this->assertSame(0, $this->config->getMinCacheFileSize());

$this->config->setMinCacheFileSize(10000);
$this->assertAttributeSame(10000, 'minCacheFileSize', $this->config);
$this->assertSame(10000, $this->config->getMinCacheFileSize());
}

public function testSetUseSessionIsUseSession()
{
//initially useSession is false
$this->assertAttributeSame(false, 'useSession', $this->config);
$this->assertSame(false, $this->config->isUseSession());
$this->config->setUseSession(true);
$this->assertAttributeSame(true, 'useSession', $this->config);
$this->assertSame(true, $this->config->isUseSession());
$this->assertTrue($this->config->isUseSession());
}

public function testSetGetSessionExclude()
{
$this->assertAttributeSame([], 'sessionExcludeKeys', $this->config);
$this->assertSame([], $this->config->getSessionExcludeKeys());
$this->config->setSessionExcludeKeys([1, 2, 3]);
$this->assertAttributeSame([1,2,3], 'sessionExcludeKeys', $this->config);
$this->assertSame([1,2,3], $this->config->getSessionExcludeKeys());
}

public function testSetFileLock()
{
$this->config->setFileLock(LOCK_EX);
$this->assertAttributeEquals(LOCK_EX, 'fileLock', $this->config);
$this->assertEquals(LOCK_EX, $this->config->getFileLock());

$this->config->setFileLock(LOCK_EX | LOCK_NB);
$this->assertAttributeEquals(LOCK_EX | LOCK_NB, 'fileLock', $this->config);
$this->assertEquals(LOCK_EX | LOCK_NB, $this->config->getFileLock());
}

public function testGetFileLock()
Expand Down Expand Up @@ -141,7 +136,7 @@ public function testGetLogFilePath()

public function testGetMinCacheFileSize()
{
$this->assertAttributeSame(10, 'minCacheFileSize', $this->config);
$this->assertSame(10, $this->config->getMinCacheFileSize());
$this->config->setMinCacheFileSize(10240);
$this->assertSame(10240, $this->config->getMinCacheFileSize());
}
Expand All @@ -150,15 +145,15 @@ public function testLoadConfigurationFile()
{
$config = new Config(__DIR__.'/config_test.php');

$this->assertAttributeSame(1, 'minCacheFileSize', $config);
$this->assertAttributeSame(false, 'enableLog', $config);
$this->assertAttributeSame(600, 'cacheExpirationInSeconds', $config);
$this->assertAttributeContains('/tmp/cache/', 'cachePath', $config);
$this->assertAttributeContains('/tmp', 'logFilePath', $config);
$this->assertSame(1, $config->getMinCacheFileSize());
$this->assertSame(false, $config->isEnableLog());
$this->assertSame(600, $config->getCacheExpirationInSeconds());
$this->assertStringContainsString('/tmp/cache/', $config->getCachePath());
$this->assertStringContainsString('/tmp', $config->getLogFilePath());
$this->assertSame(false, $config->isUseSession());
$this->assertSame([], $config->getSessionExcludeKeys());
$this->assertAttributeSame(LOCK_EX | LOCK_NB, 'fileLock', $config);
$this->assertAttributeSame(false, 'forwardHeaders', $config);
$this->assertSame(LOCK_EX | LOCK_NB, $config->getFileLock());
$this->assertSame(false, $config->isForwardHeaders());
}

/**
Expand Down
26 changes: 10 additions & 16 deletions tests/Integration/IntegrationWebServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use GuzzleHttp\Psr7\Uri;
use PHPUnit\Framework\ExpectationFailedException;
use Symfony\Component\Process\Process;
use function GuzzleHttp\Psr7\build_query;

/**
* Class IntegrationWebServerTest
Expand Down Expand Up @@ -78,7 +77,7 @@ class IntegrationWebServerTest extends \PHPUnit\Framework\TestCase
],
];

protected function setUp()
protected function setUp(): void
{
if ($this->canConnectToServer()) {
throw new \RuntimeException('Something is already running on '.$this->serverHost.':'.$this->serverPort.'. Aborting tests.');
Expand Down Expand Up @@ -117,7 +116,7 @@ protected function setUp()
$this->clearCacheDirectory();
}

protected function tearDown()
protected function tearDown(): void
{
$this->stopBuiltInServer();

Expand Down Expand Up @@ -350,20 +349,15 @@ private function getRandomContentFileName()
private function startBuiltInServer()
{
// Build the command
$command = sprintf('%s -S %s:%d -t %s %s', // >/dev/null 2>&1
$this->serverProcess = new Process([
PHP_BINARY,
$this->serverHost,
$this->serverPort,
'-S',
$this->serverHost.':'.$this->serverPort,
'-t',
$this->documentRoot,
$this->documentRoot.DIRECTORY_SEPARATOR.'index.php'
);

if ('\\' !== DIRECTORY_SEPARATOR) {
// exec is mandatory to deal with sending a signal to the process
$command = 'exec '.$command;
}

$this->serverProcess = new Process($command);
$this->documentRoot.DIRECTORY_SEPARATOR.'index.php',
]);
$this->serverProcess->disableOutput();
$this->serverProcess->start();

if (!$this->serverProcess->isRunning()) {
Expand Down Expand Up @@ -440,7 +434,7 @@ private function makeRequest($contentFileName, array $queryParams = null)
self::REDIRECT_KEY => false,
], $queryParams ?: []);

$uri = $uri->withQuery(build_query($queryParams));
$uri = $uri->withQuery(\GuzzleHttp\Psr7\Query::build($queryParams));

return new Request('GET', $uri);
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Integration/www/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Monolog\Logger;
use PageCache\PageCache;
use PageCache\Tests\Integration\IntegrationWebServerTest;
use Symfony\Component\Cache\Simple\FilesystemCache;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Psr16Cache;

ini_set('display_errors', 1);

Expand Down Expand Up @@ -97,7 +98,7 @@ function setCacheImplementation(PageCache $pc, $name, $cacheDirectory)
case IntegrationWebServerTest::CACHE_TYPE_SYMFONY_FILESYSTEM:
// Using basic symfony/cache
$ttl = $pc->config()->getCacheExpirationInSeconds() * 2;
$cacheAdapter = new FilesystemCache('symfony-cache', $ttl, $cacheDirectory);
$cacheAdapter = new Psr16Cache(new FilesystemAdapter('symfony-cache', $ttl, $cacheDirectory));
$pc->setCacheAdapter($cacheAdapter);
break;

Expand Down
Loading
Loading