From 899a6aad66f0b39a5ea9e54a72741651e7c0348a Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Tue, 30 Jan 2024 17:51:17 +0100 Subject: [PATCH] Improve tests --- .docker/php81.Dockerfile | 6 ---- .docker/php82.Dockerfile | 6 ---- .docker/php83.Dockerfile | 6 ---- .docker/xdebug.ci.ini | 4 +++ .docker/xdebug.ini | 5 +++ .gitattributes | 19 +++++----- .github/CONTRIBUTING.md | 3 +- .github/workflows/ci.yml | 52 ++++++++++++--------------- .github/workflows/static-analysis.yml | 26 +++++++++----- docker-compose.ci.yml | 36 +++++++++++++++++++ docker-compose.yml | 43 +++++++++++++++------- tests/config/database.ci.php | 23 ------------ 12 files changed, 128 insertions(+), 101 deletions(-) delete mode 100644 .docker/php81.Dockerfile delete mode 100644 .docker/php82.Dockerfile delete mode 100644 .docker/php83.Dockerfile create mode 100644 .docker/xdebug.ci.ini create mode 100644 .docker/xdebug.ini create mode 100644 docker-compose.ci.yml delete mode 100644 tests/config/database.ci.php diff --git a/.docker/php81.Dockerfile b/.docker/php81.Dockerfile deleted file mode 100644 index 6964018..0000000 --- a/.docker/php81.Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -FROM php:8.1-cli - -RUN apt-get update && \ - apt-get install -y unzip - -COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer diff --git a/.docker/php82.Dockerfile b/.docker/php82.Dockerfile deleted file mode 100644 index dbf5dec..0000000 --- a/.docker/php82.Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -FROM php:8.2-cli - -RUN apt-get update && \ - apt-get install -y unzip - -COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer diff --git a/.docker/php83.Dockerfile b/.docker/php83.Dockerfile deleted file mode 100644 index 351731a..0000000 --- a/.docker/php83.Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -FROM php:8.3-cli - -RUN apt-get update && \ - apt-get install -y unzip - -COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer diff --git a/.docker/xdebug.ci.ini b/.docker/xdebug.ci.ini new file mode 100644 index 0000000..56b3946 --- /dev/null +++ b/.docker/xdebug.ci.ini @@ -0,0 +1,4 @@ +zend_extension=xdebug + +[xdebug] +xdebug.mode=coverage diff --git a/.docker/xdebug.ini b/.docker/xdebug.ini new file mode 100644 index 0000000..d191809 --- /dev/null +++ b/.docker/xdebug.ini @@ -0,0 +1,5 @@ +zend_extension=xdebug + +[xdebug] +xdebug.mode=coverage,debug,develop +xdebug.client_host=host.docker.internal diff --git a/.gitattributes b/.gitattributes index 90c7705..169a96a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,9 +1,10 @@ -/.docker export-ignore -/.github export-ignore -/tests export-ignore -.gitattributes export-ignore -.gitignore export-ignore -.scrutinizer.yml export-ignore -docker-compose.yml export-ignore -phpstan.neon.dist export-ignore -phpunit.xml.dist export-ignore +/.docker export-ignore +/.github export-ignore +/tests export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.scrutinizer.yml export-ignore +docker-compose.ci.yml export-ignore +docker-compose.yml export-ignore +phpstan.neon.dist export-ignore +phpunit.xml.dist export-ignore diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 7e257de..439c117 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -23,5 +23,6 @@ We accept contributions via Pull Requests on [GitHub](https://github.com/stauden ## Running Tests ``` -vendor/bin/phpunit +docker compose run --rm php8.3 composer install +docker compose run --rm php8.3 vendor/bin/phpunit ``` diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index afcfc78..7ef664a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,40 +14,32 @@ jobs: include: - php: 8.3 release: stable - coverage: xdebug - - services: - mysql: - image: mysql:5.7 - env: - MYSQL_ROOT_PASSWORD: password - MYSQL_DATABASE: test - ports: - - 3306 - options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 + coverage: true steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 2 - - uses: actions/cache@v3 + - name: Check out code + uses: actions/checkout@v4 + - name: Cache Composer dependencies + uses: actions/cache@v4 with: path: ~/.cache/composer/files - key: php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} - - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php }} - extensions: bcmath, ctype, json, mbstring, openssl, pdo, pdo_${{ matrix.database }}, tokenizer, xml - coverage: ${{ matrix.coverage }} - - run: composer update --no-interaction --no-progress --prefer-dist --prefer-${{ matrix.release }} - - run: cp tests/config/database.ci.php tests/config/database.php - - run: | - PHPUNIT_FLAGS=$([ "${{ matrix.coverage }}" == "xdebug" ] && echo "--coverage-clover=coverage.xml" || echo "") - vendor/bin/phpunit $PHPUNIT_FLAGS + key: php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + - name: Start database container + run: docker compose -f docker-compose.yml -f docker-compose.ci.yml up --wait ${{ matrix.database }} + if: matrix.database != 'sqlite' + - name: Install dependencies + run: | + docker compose -f docker-compose.yml -f docker-compose.ci.yml run --rm php${{ matrix.php }} \ + composer update --no-interaction --no-progress --prefer-dist --prefer-${{ matrix.release }} + - name: Run tests + run: | + docker compose -f docker-compose.yml -f docker-compose.ci.yml run -e DB_CONNECTION=${{ matrix.database }} \ + --rm php${{ matrix.php }}${{ env.XDEBUG }} vendor/bin/phpunit ${{ env.COVERAGE }} env: - DATABASE: ${{ matrix.database }} - MYSQL_PORT: ${{ job.services.mysql.ports[3306] }} - - uses: codecov/codecov-action@v3 + XDEBUG: ${{ matrix.coverage && '-xdebug' || '' }} + COVERAGE: ${{ matrix.coverage && '--coverage-clover=coverage.xml' || '' }} + - name: Upload code coverage + uses: codecov/codecov-action@v3 env: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} - if: matrix.coverage == 'xdebug' && env.CODECOV_TOKEN + if: matrix.coverage && env.CODECOV_TOKEN diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index de43a9f..56f2939 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -6,14 +6,24 @@ jobs: phpstan: runs-on: ubuntu-latest + strategy: + matrix: + php: [ 8.3 ] + release: [ stable ] + steps: - - uses: actions/checkout@v3 - - uses: actions/cache@v3 + - name: Check out code + uses: actions/checkout@v4 + - name: Cache Composer dependencies + uses: actions/cache@v4 with: path: ~/.cache/composer/files - key: php-8.3-composer-${{ hashFiles('**/composer.json') }} - - uses: shivammathur/setup-php@v2 - with: - php-version: 8.3 - - run: composer update --no-interaction --no-progress --prefer-dist - - run: vendor/bin/phpstan analyse --error-format=github + key: php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + - name: Install dependencies + run: | + docker compose -f docker-compose.yml -f docker-compose.ci.yml run --rm php${{ matrix.php }} \ + composer update --no-interaction --no-progress --prefer-dist --prefer-${{ matrix.release }} + - name: Analyse code + run: | + docker compose -f docker-compose.yml -f docker-compose.ci.yml run --rm php${{ matrix.php }} \ + vendor/bin/phpstan analyse --error-format=github --memory-limit=-1 diff --git a/docker-compose.ci.yml b/docker-compose.ci.yml new file mode 100644 index 0000000..2fdbe12 --- /dev/null +++ b/docker-compose.ci.yml @@ -0,0 +1,36 @@ +version: '3.8' + +services: + php8.1: + image: ghcr.io/staudenmeir/php:8.1 + working_dir: /var/www/html + networks: + - test + volumes: + - .:/var/www/html:delegated + - ~/.cache/composer/files:/root/.composer/cache/files + php8.2: + image: ghcr.io/staudenmeir/php:8.2 + working_dir: /var/www/html + networks: + - test + volumes: + - .:/var/www/html:delegated + - ~/.cache/composer/files:/root/.composer/cache/files + php8.3: + image: ghcr.io/staudenmeir/php:8.3 + working_dir: /var/www/html + networks: + - test + volumes: + - .:/var/www/html:delegated + - ~/.cache/composer/files:/root/.composer/cache/files + php8.3-xdebug: + image: ghcr.io/staudenmeir/php:8.3 + working_dir: /var/www/html + networks: + - test + volumes: + - .:/var/www/html:delegated + - .docker/xdebug.ci.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini + - ~/.cache/composer/files:/root/.composer/cache/files diff --git a/docker-compose.yml b/docker-compose.yml index 8e7a58e..49fce6c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,24 +1,43 @@ version: '3.8' services: - php81: - build: - context: . - dockerfile: .docker/php81.Dockerfile + php8.1: + image: ghcr.io/staudenmeir/php:8.1 working_dir: /var/www/html + networks: + - test volumes: - .:/var/www/html:delegated - php82: - build: - context: . - dockerfile: .docker/php82.Dockerfile + - .docker/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini + php8.2: + image: ghcr.io/staudenmeir/php:8.2 working_dir: /var/www/html + networks: + - test volumes: - .:/var/www/html:delegated - php83: - build: - context: . - dockerfile: .docker/php83.Dockerfile + - .docker/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini + php8.3: + image: ghcr.io/staudenmeir/php:8.3 working_dir: /var/www/html + networks: + - test volumes: - .:/var/www/html:delegated + - .docker/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini + mysql: + image: 'mysql:latest' + environment: + MYSQL_ROOT_PASSWORD: password + MYSQL_DATABASE: test + healthcheck: + test: mysqladmin ping --silent + interval: 5s + timeout: 3s + retries: 10 + networks: + - test + +networks: + test: + driver: bridge diff --git a/tests/config/database.ci.php b/tests/config/database.ci.php deleted file mode 100644 index 80458be..0000000 --- a/tests/config/database.ci.php +++ /dev/null @@ -1,23 +0,0 @@ - [ - 'driver' => 'mysql', - 'host' => '127.0.0.1', - 'port' => getenv('MYSQL_PORT'), - 'database' => 'test', - 'username' => 'root', - 'password' => 'password', - 'unix_socket' => '', - 'charset' => 'utf8mb4', - 'collation' => 'utf8mb4_unicode_ci', - 'prefix' => '', - 'strict' => true, - 'engine' => null, - ], - 'sqlite' => [ - 'driver' => 'sqlite', - 'database' => ':memory:', - 'prefix' => '', - ], -];