Skip to content

Commit

Permalink
Refactor tests for PHPUnit 10.x compat
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmullie committed Jun 28, 2023
1 parent d49068f commit 24d5b28
Show file tree
Hide file tree
Showing 145 changed files with 3,719 additions and 2,629 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/healthcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install latest Docker Compose
run: |
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
- name: Test (twice, if needed)
run: make test || make test
- name: Healthcheck ping
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install latest Docker Compose
run: |
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m)" -o docker-compose
chmod +x docker-compose
mv docker-compose /usr/local/bin/docker-compose
- name: Test PHP 8.2
run: make test PHP=8.2
- name: Test PHP 8.1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/docs
/vendor
/composer.lock
.phpunit.result.cache
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ ARG version=cli
FROM php:$version

WORKDIR /var/www
COPY makefile makefile
COPY composer.json composer.json
COPY docker docker
COPY . .

RUN docker/php.sh
25 changes: 4 additions & 21 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,6 @@ services:
dockerfile: Dockerfile
args:
version: cli
volumes:
- ./src:/var/www/src
- ./tests:/var/www/tests
- ./build:/var/www/build
- ./.php-cs-fixer.php:/var/www/.php-cs-fixer.php
- ./phpunit.xml:/var/www/phpunit.xml
- ./ruleset.xml:/var/www/ruleset.xml
depends_on:
couchbase:
condition: service_healthy
memcached:
condition: service_started
mysql:
condition: service_healthy
postgresql:
condition: service_healthy
redis:
condition: service_healthy
php-8.0:
<<: *default-php
build:
Expand Down Expand Up @@ -60,11 +42,12 @@ services:
image: mysql
command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci','--default-authentication-plugin=mysql_native_password']
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_ROOT_PASSWORD=
- MYSQL_ROOT_PASSWORD=pass
- MYSQL_USER=user
- MYSQL_PASSWORD=pass
- MYSQL_DATABASE=cache
healthcheck:
test: ["CMD", "mysql" ,"-h", "mysql", "-P", "3306", "-u", "root", "-e", "SELECT 1", "cache"]
test: ["CMD", "mysqladmin", "ping", "-hmysql", "-P3306", "-uuser", "-ppass"]
interval: 3s
timeout: 3s
retries: 30
Expand Down
12 changes: 9 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# defaults for `make test`
PHP ?=
ADAPTER ?= Apc,Couchbase,Flysystem,Memcached,MemoryStore,MySQL,PostgreSQL,Redis,SQLite
GROUP ?= adapter,buffered,collections,keyvaluestore,psr6,psr16,shard,transactional,stampede
VOLUME_BINDS ?= src,tests,build,.php-cs-fixer.php,phpunit.xml,ruleset.xml

install:
wget -q -O - https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Expand Down Expand Up @@ -33,16 +35,20 @@ test:
# Usage:
# make test - tests all adapters on latest PHP version
# make test PHP=8.0 ADAPTER=Memcached - tests Memcached on PHP 8.0
VOLUMES=""
for VOLUME in $$(echo "$(VOLUME_BINDS)" | tr "," "\n"); do VOLUMES="$$VOLUMES -v $$(pwd)/$$VOLUME:/var/www/$$VOLUME"; done;\
test "$(PHP)" && TEST_CONTAINER=php-$(PHP) || TEST_CONTAINER=php;\
DEPENDENT_CONTAINERS="$(filter-out apc flysystem memorystore sqlite, $(shell echo $(ADAPTER) | tr 'A-Z,' 'a-z '))";\
RELEVANT_CONTAINERS="$$TEST_CONTAINER $(filter-out apc flysystem memorystore sqlite, $(shell echo $(ADAPTER) | tr 'A-Z,' 'a-z '))";\
docker-compose up --no-deps -d $$DEPENDENT_CONTAINERS;\
docker-compose run --no-deps $$TEST_CONTAINER env XDEBUG_MODE=coverage vendor/bin/phpunit --group $(ADAPTER) --coverage-clover build/coverage-$(PHP)-$(ADAPTER).clover;\
docker-compose up --no-deps --wait -d $$DEPENDENT_CONTAINERS;\
docker-compose run --no-deps $$VOLUMES $$TEST_CONTAINER env XDEBUG_MODE=coverage vendor/bin/phpunit --group $(GROUP) --testsuite $(ADAPTER) --coverage-clover build/coverage-$(PHP)-$(ADAPTER).clover;\
TEST_STATUS=$$?;\
docker-compose stop -t0 $$RELEVANT_CONTAINERS;\
exit $$TEST_STATUS

format:
docker-compose run --no-deps php sh -c "vendor/bin/php-cs-fixer fix && vendor/bin/phpcbf --standard=ruleset.xml"
VOLUMES=""
for VOLUME in $$(echo "$(VOLUME_BINDS)" | tr "," "\n"); do VOLUMES="$$VOLUMES -v $$(pwd)/$$VOLUME:/var/www/$$VOLUME"; done;\
docker-compose run --no-deps $$VOLUMES php sh -c "vendor/bin/php-cs-fixer fix && vendor/bin/phpcbf --standard=ruleset.xml"

.PHONY: docs
56 changes: 41 additions & 15 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<clover outputFile="build/coverage.clover"/>
</report>
</coverage>
<testsuites>
<testsuite name="scrapbook">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<logging/>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd">
<coverage>
<report>
<clover outputFile="build/coverage.clover"/>
</report>
</coverage>
<testsuites>
<testsuite name="Apc">
<directory suffix="Test.php">tests/Adapters/Apc</directory>
</testsuite>
<testsuite name="Couchbase">
<directory suffix="Test.php">tests/Adapters/Couchbase</directory>
</testsuite>
<testsuite name="Flysystem">
<directory suffix="Test.php">tests/Adapters/Flysystem</directory>
</testsuite>
<testsuite name="Memcached">
<directory suffix="Test.php">tests/Adapters/Memcached</directory>
</testsuite>
<testsuite name="MemoryStore">
<directory suffix="Test.php">tests/Adapters/MemoryStore</directory>
</testsuite>
<testsuite name="MySQL">
<directory suffix="Test.php">tests/Adapters/MySQL</directory>
</testsuite>
<testsuite name="PostgreSQL">
<directory suffix="Test.php">tests/Adapters/PostgreSQL</directory>
</testsuite>
<testsuite name="Redis">
<directory suffix="Test.php">tests/Adapters/Redis</directory>
</testsuite>
<testsuite name="SQLite">
<directory suffix="Test.php">tests/Adapters/SQLite</directory>
</testsuite>
</testsuites>
<logging/>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
10 changes: 6 additions & 4 deletions src/Adapters/Couchbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,14 @@ protected function deleteIfExpired(string|array $key, int $expire): bool
* Couchbase expects an integer TTL (under 1576800000) for relative
* times, or a \DateTimeInterface for absolute times.
*
* @return int|DateTime expiration in seconds or \DateTimeInterface
* @return int|\DateTime expiration in seconds or \DateTimeInterface
*/
protected function expire(int $expire): int|DateTime
protected function expire(int $expire): int|\DateTime
{
// relative time in seconds, <30 days
if ($expire < 30 * 24 * 60 * 60) {flush();
if ($expire < 30 * 24 * 60 * 60) {
flush();

return $expire;
}

Expand All @@ -411,7 +413,7 @@ protected function expire(int $expire): int|DateTime
return -1; // @todo this if statement should be useless; this case should be fine as DateTime
}

return (new DateTime())->setTimestamp($expire);
return (new \DateTime())->setTimestamp($expire);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Psr16/SimpleCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function get(string $key, mixed $default = null): mixed
return $multi[$key] ?? $default;
}

public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
public function set(string $key, mixed $value, int|\DateInterval $ttl = null): bool
{
$this->assertValidKey($key);
$ttl = $this->ttl($ttl);
Expand Down Expand Up @@ -83,7 +83,7 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable
return array_merge($nulls, $results);
}

public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
public function setMultiple(iterable $values, int|\DateInterval $ttl = null): bool
{
if ($values instanceof \Traversable) {
// we also need the keys, and an array is stricter about what it can
Expand Down
2 changes: 0 additions & 2 deletions src/Psr6/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ public function getItem(string $key): CacheItemInterface
}

/**
* {@inheritdoc}
*
* @return Item[]
*/
public function getItems(array $keys = []): iterable
Expand Down
26 changes: 26 additions & 0 deletions tests/AbstractAdapterTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace MatthiasMullie\Scrapbook\Tests;

use MatthiasMullie\Scrapbook\KeyValueStore;
use PHPUnit\Framework\TestCase;

abstract class AbstractAdapterTestCase extends TestCase
{
protected KeyValueStore $adapterKeyValueStore;
protected string $collectionName;

protected function setUp(): void
{
parent::setUp();

$this->collectionName = $this->getCollectionName();
$this->adapterKeyValueStore = $this->getAdapterKeyValueStore();
}

abstract public function getAdapterKeyValueStore(): KeyValueStore;

abstract public function getCollectionName(): string;
}
Loading

0 comments on commit 24d5b28

Please sign in to comment.