diff --git a/.gitignore b/.gitignore index 166758b..c067386 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,16 @@ workspace.code-workspace vendor -.phpunit.result.cache -.phpunit.cache -.vscode/launch.json +.vscode +auth.json composer.lock -build +build/ +*/temp/ +.phpunit.result.cache +.phpunit.cache/ +coverage/ +tests/coverage/ +junit.xml +coverage.xml +*-report.txt +test-output.txt +release_notes.md diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2c93b54..83b20ef 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,26 +1,141 @@ -# This file is a template, and might need editing before it works on your project. -# To contribute improvements to CI/CD templates, please follow the Development guide at: -# https://docs.gitlab.com/ee/development/cicd/templates.html -# This specific template is located at: -# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Composer.gitlab-ci.yml +include: + - project: "ldc-web/shared-ci" + ref: "main" + file: "lint/phpcs.yml" + - project: "ldc-web/shared-ci" + ref: "main" + file: "lint/phpstan.yml" + - project: "ldc-web/shared-ci" + ref: "main" + file: "security/composer.yml" + - project: "ldc-web/shared-ci" + ref: "main" + file: "test/test.yml" + +stages: + - lint + - security + - test + - publish + - release + +phplint: + stage: lint + image: $CI_REGISTRY/ldc-web/projets/ldc-web-images-docker/uwil-ci-8.5:latest + variables: + TEXT_REPORT: php-l-report.txt + before_script: + - composer config gitlab-token.lab.s-web.fr $CI_DEPLOY_USER $CI_DEPLOY_PASSWORD + - composer install --no-scripts --no-interaction --no-ansi --no-progress --prefer-dist + script: + - composer run-script lint:php > "$TEXT_REPORT" 2>&1; status=$? + - cat "$TEXT_REPORT" + - if [ $status -ne 0 ] || grep -qE "Parse error|Errors parsing" "$TEXT_REPORT"; then echo "PHP lint failed"; exit 1; fi + artifacts: + when: always + expose_as: "php-l report" + paths: + - php-l-report.txt + allow_failure: false + rules: + - if: $CI_PIPELINE_SOURCE == "merge_request_event" + +pint: + stage: lint + image: $CI_REGISTRY/ldc-web/projets/ldc-web-images-docker/uwil-ci-8.5:latest + variables: + PINT_REPORT: pint-report.txt + before_script: + - composer config gitlab-token.lab.s-web.fr $CI_DEPLOY_USER $CI_DEPLOY_PASSWORD + - composer install --no-scripts --no-interaction --no-ansi --no-progress --prefer-dist + script: + - vendor/bin/pint --test > "$PINT_REPORT" 2>&1; status=$? + - cat "$PINT_REPORT" + - if [ $status -ne 0 ] || grep -qE "ERROR|ERRORS|ERROR:" "$PINT_REPORT"; then echo "Pint reported errors"; exit 1; fi + artifacts: + when: always + expose_as: "pint report" + paths: + - pint-report.txt + allow_failure: false + rules: + - if: $CI_PIPELINE_SOURCE == "merge_request_event" + +pest: + stage: test + image: $CI_REGISTRY/ldc-web/projets/ldc-web-images-docker/uwil-ci-8.5:latest + variables: + JUNIT_REPORT: junit.xml + TEXT_REPORT: test-output.txt + COVERAGE_REPORT: coverage.xml + before_script: + - composer config gitlab-token.lab.s-web.fr $CI_DEPLOY_USER $CI_DEPLOY_PASSWORD + - composer install --no-scripts --no-interaction --no-ansi --no-progress --prefer-dist + script: + - rm -f "$JUNIT_REPORT" "$COVERAGE_REPORT" + - composer run-script test:coverage -- --min=85 > "$TEXT_REPORT" 2>&1; status=$? + - cat "$TEXT_REPORT" + - if [ -f "$COVERAGE_REPORT" ]; then echo "Coverage report generated $COVERAGE_REPORT"; else echo "No coverage report found"; fi + - exit $status + artifacts: + when: always + expose_as: "pest report" + paths: + - junit.xml + - test-output.txt + - coverage.xml + reports: + junit: junit.xml + allow_failure: false + rules: + - if: $CI_PIPELINE_SOURCE == "merge_request_event" -# Publishes a tag/branch to Composer Packages of the current project package-publish: - image: curlimages/curl:latest - stage: build + image: $CI_REGISTRY/ldc-web/projets/ldc-web-images-docker/uwil-ci-8.5:latest + stage: publish variables: URL: "$CI_SERVER_PROTOCOL://$CI_SERVER_HOST:$CI_SERVER_PORT/api/v4/projects/$CI_PROJECT_ID/packages/composer?job_token=$CI_JOB_TOKEN" script: + # Check that the tag is on the main branch + - git fetch origin main + - | + if ! git branch -r --contains "$CI_COMMIT_SHA" | grep -q "origin/main"; then + echo "Erreur : Le tag $CI_COMMIT_TAG n'a pas été créé sur la branche main !" + exit 1 + fi + # Publish Composer package - insecure=$([ "$CI_SERVER_PROTOCOL" = "http" ] && echo "--insecure" || echo "") - - response=$(curl -s -w "\n%{http_code}" $insecure --data tag=$CI_COMMIT_TAG $URL) - - code=$(echo "$response" | tail -n 1) - - body=$(echo "$response" | head -n 1) - # Output state information - - if [ $code -eq 201 ]; then - echo "Package created - Code $code - $body"; + - | + response=$(curl -s -w "\n%{http_code}" $insecure --data tag=$CI_COMMIT_TAG $URL) + code=$(echo "$response" | tail -n 1) + body=$(echo "$response" | head -n 1) + if [ $code -eq 201 ]; then + echo "Package created - Code $code - $body" else - echo "Could not create package - Code $code - $body"; - exit 1; + echo "Could not create package - Code $code - $body" + exit 1 fi rules: - - if: '$CI_COMMIT_TAG =~ /^\d+.\d+.\d+/' + - if: '$CI_COMMIT_TAG =~ /^v?\d+\.\d+\.\d+/' + +release: + stage: release + image: registry.gitlab.com/gitlab-org/cli:latest + needs: + - job: package-publish + rules: + - if: '$CI_COMMIT_TAG =~ /^v?\d+\.\d+\.\d+/' + before_script: + - | + tag_clean=$(echo "$CI_COMMIT_TAG" | sed 's/^v//') + sed -n "/## .*\[v\?${tag_clean}\]/,/## .*\[/p" CHANGELOG.md | sed '$d' > release_notes.md || true + if [ ! -s release_notes.md ]; then + echo "Release $CI_COMMIT_TAG du package uwil/storage-laravel." > release_notes.md + fi + script: + - echo "Création de la release $CI_COMMIT_TAG via glab CLI..." + release: + name: "Release $CI_COMMIT_TAG" + tag_name: "$CI_COMMIT_TAG" + ref: "$CI_COMMIT_TAG" + description: "./release_notes.md" diff --git a/composer.json b/composer.json index fbec5bb..5bdb7d3 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "laravel/pint": "^1.29", "php-parallel-lint/php-parallel-lint": "^1.4", "slevomat/coding-standard": "^8.22", - "pestphp/pest": "^5.3" + "pestphp/pest": "^5.3", + "orchestra/testbench": "^11.3" }, "autoload": { "psr-4": { @@ -27,7 +28,7 @@ }, "autoload-dev": { "psr-4": { - "Uwil\\Storage\\Tests\\": "tests" + "Uwil\\Storage\\Tests\\": "tests/" } }, "extra": { @@ -41,7 +42,16 @@ } }, "scripts": { - "test": "vendor/bin/pest" + "analyze": "./vendor/bin/phpstan analyse", + "format": "./vendor/bin/pint", + "lint": "./vendor/bin/phpcs && ./vendor/bin/pint --test", + "lint:fix": "./vendor/bin/phpcbf && ./vendor/bin/pint", + "lint:php": "./vendor/bin/parallel-lint --exclude vendor .", + "lint:phpcs": "./vendor/bin/phpcs", + "lint:phpstan": "./vendor/bin/phpstan analyse", + "lint:pint": "./vendor/bin/pint --test", + "test": "./vendor/bin/pest", + "test:coverage": "./vendor/bin/pest --log-junit=junit.xml --coverage --coverage-html=tests/coverage --coverage-clover=coverage.xml" }, "config": { "allow-plugins": { diff --git a/phpunit.xml b/phpunit.xml index d0db21f..f118e18 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,25 +1,41 @@ - - - - - - - - - - - ./tests/ - - - - - - - - - - src/ - - + + + + ./tests/Feature + + + ./tests/Integration + + + ./tests/Unit + + + + + ./src + + + ./src/StorageServiceProvider.php + + + + + + + + + diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..2dbce1b --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,7 @@ +in(__DIR__); diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..ef448ec --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,42 @@ +set('database.default', 'testing'); + $app['config']->set('database.connections.testing', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + + $app['config']->set('storage-service.default', 'local'); + $app['config']->set('storage-service.eligibleDisks', ['local']); + $app['config']->set('storage-service.allowedExtensions', [ + 'jpg', 'jpeg', 'png', 'gif', 'pdf', 'txt', 'doc', 'docx', 'csv', 'mp4', 'webm', + ]); + } +} diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php new file mode 100644 index 0000000..f807f5b --- /dev/null +++ b/tests/Unit/ExampleTest.php @@ -0,0 +1,7 @@ +not->toBeNull(); +});