From b786aa81596f94f96d50063c321221ac4195fd09 Mon Sep 17 00:00:00 2001 From: s1lver Date: Fri, 7 Jul 2023 12:43:53 +0300 Subject: [PATCH] (#7) Fixed psalm issues and build image --- Makefile | 52 +++++++++++++++------------- psalm.xml | 2 ++ src/RedisCache.php | 4 +++ tests/RedisClusterCacheTest.php | 35 ++----------------- tests/docker/docker-compose.yml | 27 +++++---------- tests/docker/php/81/Dockerfile | 16 --------- tests/docker/php/{80 => }/Dockerfile | 7 ++-- 7 files changed, 48 insertions(+), 95 deletions(-) delete mode 100644 tests/docker/php/81/Dockerfile rename tests/docker/php/{80 => }/Dockerfile (65%) diff --git a/Makefile b/Makefile index c650ab5..911d10d 100644 --- a/Makefile +++ b/Makefile @@ -1,38 +1,40 @@ -build: - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose up -d --build +help: ## Display help information + @fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//' -down: - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose down +build: ## Build an image from a docker-compose file. Params: {{ v=8.1 }}. Default latest PHP 8.1 + PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml up -d --build -start: - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose up -d +down: ## Stop and remove containers, networks + docker-compose -f tests/docker/docker-compose.yml down -test: test80 test81 +start: ## Start services + docker-compose -f tests/docker/docker-compose.yml up -d -test80: - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose build --pull php80 - make create-cluster - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose run php80 vendor/bin/phpunit --colors=always - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose down +sh: ## Enter the container with the application + docker exec -it cache-redis-php sh -test81: - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose build --pull php81 +test: ## Run tests. Params: {{ v=8.1 }}. Default latest PHP 8.1 + PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml build --pull cache-redis-php make create-cluster - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose run php81 vendor/bin/phpunit --colors=always - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose down + PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml run cache-redis-php vendor/bin/phpunit --colors=always + docker-compose -f tests/docker/docker-compose.yml down -create-cluster: +create-cluster: ## Create Redis cluster docker exec redis1 sh -c "redis-cli -p 6381 -a Password --cluster create 172.20.128.2:6381 172.20.128.3:6382 172.20.128.4:6383 172.20.128.5:6384 172.20.128.6:6385 172.20.128.7:6386 --cluster-replicas 1 --no-auth-warning --cluster-yes" -connect-cluster: +connect-cluster: ## Connect to Redis cluster docker exec -it redis1 sh -c "redis-cli -c -p 6381 -a Password --no-auth-warning" -mutation-test: - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose build --pull php$(v) +mutation-test: ## Run mutation tests. Params: {{ v=8.1 }}. Default latest PHP 8.1 + PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml build --pull cache-redis-php make create-cluster - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose run php$(v) php -dpcov.enabled=1 -dpcov.directory=. vendor/bin/roave-infection-static-analysis-plugin -j2 --ignore-msi-with-no-mutations --only-covered - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose down + PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml run cache-redis-php php -dpcov.enabled=1 -dpcov.directory=. vendor/bin/roave-infection-static-analysis-plugin -j2 --ignore-msi-with-no-mutations --only-covered + make down + +coverage: ## Run code coverage. Params: {{ v=8.1 }}. Default latest PHP 8.1 + PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml run cache-redis-php vendor/bin/phpunit --coverage-clover coverage.xml + make down -coverage: - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose run php80 vendor/bin/phpunit --coverage-clover coverage.xml - COMPOSE_FILE=tests/docker/docker-compose.yml docker-compose down +static-analyze: ## Run code static analyze. Params: {{ v=8.1 }}. Default latest PHP 8.1 + PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml run cache-redis-php vendor/bin/psalm --config=psalm.xml --shepherd --stats --php-version=$(v) + make down diff --git a/psalm.xml b/psalm.xml index 5a95a3a..f4ceced 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,8 @@ connections as $connection) { /** @var StreamConnection $connection */ $cluster = (new Client($connection->getParameters()))->info('Cluster'); + /** @psalm-suppress MixedArrayAccess */ if (isset($cluster['Cluster']['cluster_enabled']) && 1 === (int)$cluster['Cluster']['cluster_enabled']) { return true; } @@ -73,6 +74,7 @@ public function isCluster(): bool public function get(string $key, mixed $default = null): mixed { $this->validateKey($key); + /** @var null|string $value */ $value = $this->client->get($key); return $value === null ? $default : unserialize($value); } @@ -201,6 +203,7 @@ public function setMultiple(iterable $values, null|int|DateInterval $ttl = null) $this->client->expire($key, (int)$ttl); } + /** @var null|array $results */ $results = $this->client->exec(); return !in_array(null, (array)$results, true); @@ -238,6 +241,7 @@ public function deleteMultiple(iterable $keys): bool public function has(string $key): bool { $this->validateKey($key); + /** @var int $ttl */ $ttl = $this->client->ttl($key); /** "-1" - if the key exists but has no associated expire {@see https://redis.io/commands/ttl}. */ return $ttl > 0 || $ttl === -1; diff --git a/tests/RedisClusterCacheTest.php b/tests/RedisClusterCacheTest.php index 9bcc37e..9f05b6e 100644 --- a/tests/RedisClusterCacheTest.php +++ b/tests/RedisClusterCacheTest.php @@ -276,39 +276,6 @@ public function testNormalizeTtl($ttl, $expectedResult): void $this->assertSameExceptObject($expectedResult, $result); } - /** - * @return array - */ - public function iterableProvider(): array - { - return [ - 'array' => [ - ['a' => 1, 'b' => 2,], - ['a' => 1, 'b' => 2,], - ], - 'ArrayIterator' => [ - ['a' => 1, 'b' => 2,], - new ArrayIterator(['a' => 1, 'b' => 2,]), - ], - 'IteratorAggregate' => [ - ['a' => 1, 'b' => 2,], - new class () implements IteratorAggregate { - public function getIterator(): ArrayIterator - { - return new ArrayIterator(['a' => 1, 'b' => 2,]); - } - }, - ], - 'generator' => [ - ['a' => 1, 'b' => 2,], - (static function () { - yield 'a' => 1; - yield 'b' => 2; - })(), - ], - ]; - } - /** * @return array */ @@ -447,4 +414,6 @@ private function assertSameExceptObject(mixed $expected, mixed $actual): void } } } + + } diff --git a/tests/docker/docker-compose.yml b/tests/docker/docker-compose.yml index 4cdf825..85f9d2e 100644 --- a/tests/docker/docker-compose.yml +++ b/tests/docker/docker-compose.yml @@ -1,31 +1,20 @@ version: '3.8' services: - php80: - container_name: cache-redis-php80 - hostname: cache-redis-php80 - build: ./php/80 + cache-redis-php: + container_name: cache-redis-php + hostname: cache-redis-php + build: + context: ../.. + dockerfile: tests/docker/php/Dockerfile + args: + PHP_VERSION: ${PHP_VERSION:-8.1} volumes: - ../runtime/.composer80:/root/.composer - ../..:/var/www - dns: - - 8.8.8.8 - - 4.4.4.4 networks: &network - cache-redis-network - php81: - container_name: cache-redis-php81 - hostname: cache-redis-php81 - build: ./php/81 - volumes: - - ../runtime/.composer81:/root/.composer - - ../..:/var/www - dns: - - 8.8.8.8 - - 4.4.4.4 - networks: *network - redis-single: image: redis:7.0.4-alpine container_name: redis-single diff --git a/tests/docker/php/81/Dockerfile b/tests/docker/php/81/Dockerfile deleted file mode 100644 index f05d928..0000000 --- a/tests/docker/php/81/Dockerfile +++ /dev/null @@ -1,16 +0,0 @@ -FROM --platform=linux/amd64 php:8.1.16-fpm-alpine - -RUN apk add unzip zlib-dev libzip-dev autoconf g++ make - -RUN docker-php-ext-install zip -RUN pecl install pcov-1.0.11 -RUN docker-php-ext-enable pcov - -COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer - -ENV COMPOSER_ALLOW_SUPERUSER 1 - -WORKDIR /var/www - -ENTRYPOINT ["sh", "tests/docker/php/entrypoint.sh"] -CMD ["sleep", "infinity"] diff --git a/tests/docker/php/80/Dockerfile b/tests/docker/php/Dockerfile similarity index 65% rename from tests/docker/php/80/Dockerfile rename to tests/docker/php/Dockerfile index f056757..89f4929 100644 --- a/tests/docker/php/80/Dockerfile +++ b/tests/docker/php/Dockerfile @@ -1,8 +1,11 @@ -FROM --platform=linux/amd64 php:8.0.28-fpm-alpine +# Important! Do not use this image in production! + +ARG PHP_VERSION +FROM --platform=linux/amd64 php:${PHP_VERSION}-cli-alpine RUN apk add unzip zlib-dev libzip-dev autoconf g++ make -RUN docker-php-ext-install zip +RUN docker-php-ext-install zip opcache RUN pecl install pcov-1.0.11 RUN docker-php-ext-enable pcov