Skip to content

Commit

Permalink
PhpUnit: use database for tests, added tests (#4138)
Browse files Browse the repository at this point in the history
* Rector: CQ - UnusedForeachValueToArrayKeysRector (#1)

* Rector: CQ - UnusedForeachValueToArrayKeysRector

See Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector

* fixes + phpstan

See fix at rector: rectorphp/rector-src#6164

* Updated test

* Added test

* Added test

* Renamed and updated test

* Updated test

* Updated test

* Updated test

* Updated test

* Revert "Rector: CQ - UnusedForeachValueToArrayKeysRector (#1)"

This reverts commit 3d7eaf6.

* Updated test (phpstan L9)

* Updated test (fixed namespace)

* Updated test (phpstan L9)

* Added unit test to code style check and phpstan

* Some cleanup

* Some cleanup

* Some cleanup

* Updated workflow

* ddev shortcut [ski ci]

* Moved phphunit config file

* Merged phpunit & sonar workflow

* PhpUnit fix

 - PHP Deprecated:  str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/runner/work/magento-lts/magento-lts/lib/Varien/Object.php on line 594

* Fix linefeed (?)

* Fix config

* Fixed invalid php version

* Use minimum version

* Use minimum version for SonarScan

* Fix command [ski ci]

* Enable xdebug

* Added test, ref #4123

* Revert "Some cleanup"

This reverts commit 6d45ed6.

* Revert "PhpUnit fix"

This reverts commit f4935f1.

* Updated test

* Updated docblocks

* Revert "Fix linefeed (?)"

This reverts commit dee0422.

* Added tests

* Added tests

* Added tests [skip ci]

* Reverted changes to Mage_Core_Model_Website

* Updated config

* Updated .gitignore

* Updated workflow

* Updated workflow typo

* Updated workflow continue on error

* Updated workflow if condition

* Updated workflow [skip ci]

* Moved tests to root (as common)

* Fixed paths

* Fixed paths

* Fixed paths

* CS fix

* Added test

* Added test

* Added groups/suites

* Updated test

* CS fix

* Updated .gitignore

* Updated phpunit.xml.dist

* Added test

* Added tests

* Added tests

* Added tests

* Added tests

* CS fix

* CS fix

* Added tests

* Added tests

* Added tests

* Added tests

* CS/PhpStan fixes

* Update

- some tests commented - need files changes

* Nicer output?

* Don't wait for phpcs

* Revert output

* Updated workflows

* Added test

* Added phpunit command to composer.json

* Updated DDEV commands

* Updated composer scripts

---------

Co-authored-by: Ng Kiat Siong <kiatsiong.ng@gmail.com>
  • Loading branch information
sreichel and kiatng committed Sep 17, 2024
1 parent bc7881d commit 953920a
Show file tree
Hide file tree
Showing 69 changed files with 3,847 additions and 487 deletions.
7 changes: 7 additions & 0 deletions .ddev/commands/web/phpunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

## Description: run PHPUnit
## Usage: phpunit
## Example: ddev phpunit

php vendor/bin/phpunit --no-coverage "$@" --testdox
9 changes: 9 additions & 0 deletions .ddev/commands/web/phpunit-coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

## Description: run PHPUnit with coverage
## Usage: phpunit-coverage
## Example: ddev phpunit-coverage

enable_xdebug
XDEBUG_MODE=coverage php vendor/bin/phpunit --testdox
disable_xdebug
9 changes: 9 additions & 0 deletions .ddev/commands/web/phpunit-coverage-local
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

## Description: run PHPUnit with local HTML coverage
## Usage: phpunit-coverage-local
## Example: ddev phpunit-coverage-local

enable_xdebug
XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-html build/coverage --testdox
disable_xdebug
5 changes: 2 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
/.phpcs.php.xml.dist export-ignore
/.phpcs.xml.dist export-ignore
/.phpmd.dist.xml export-ignore
/phpstan.dist.baseline.neon export-ignore
/phpstan.dist.issues.neon export-ignore
/phpstan.dist.neon export-ignore
/.phpstan.dist.baseline.neon export-ignore
/.phpstan.dist.neon export-ignore

/README.md export-ignore

Expand Down
21 changes: 4 additions & 17 deletions .github/workflows/check-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ on:
phpunit:
description: "Count changed PhpUnit files"
value: ${{ jobs.check.outputs.phpunit }}
sonar:
description: "Count changed Sonar files"
value: ${{ jobs.check.outputs.sonar }}
# Allow manually triggering the workflow.
workflow_dispatch:

Expand All @@ -54,7 +51,6 @@ jobs:
phpstan: ${{ steps.changes-phpstan.outputs.phpstan }}
phpunit-test: ${{ steps.changes-phpunit-test.outputs.phpunit-test }}
phpunit: ${{ steps.changes-phpunit.outputs.phpunit }}
sonar: ${{ steps.changes-sonar.outputs.sonar }}

steps:
- name: Checkout code
Expand Down Expand Up @@ -87,9 +83,8 @@ jobs:
**phpcs**
**php-cs-fixer**
**phpstan**
dev/tests/
dev/phpunit*
dev/sonar*
tests/
phpunit*
- name: Check if composer files changed
id: changes-composer
Expand Down Expand Up @@ -161,22 +156,14 @@ jobs:
id: changes-phpunit-test
if: steps.changed-files-specific.outputs.any_modified == 'true'
run: |
count="$(grep -oE "dev/tests/" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)"
count="$(grep -oE "tests/" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)"
echo "$count UnitTest test file(s) changed"
echo "phpunit-test=$count" >> $GITHUB_OUTPUT
- name: Check if PHPUnit files changed
id: changes-phpunit
if: steps.changed-files-specific.outputs.any_modified == 'true'
run: |
count="$(grep -oE "dev/phpunit*" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)"
count="$(grep -oE "phpunit*" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)"
echo "$count PHPUnit file(s) changed"
echo "phpunit=$count" >> $GITHUB_OUTPUT
- name: Check if Sonar files changed
id: changes-sonar
if: steps.changed-files-specific.outputs.any_modified == 'true'
run: |
count="$(grep -oE "dev/sonar*" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)"
echo "$count Sonar file(s) changed"
echo "sonar=$count" >> $GITHUB_OUTPUT
100 changes: 80 additions & 20 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,39 @@ on:

jobs:
unit-tests:
runs-on: [ubuntu-latest]
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.4', '8.3']
mysql-version: ['5.7', '8.0']

services:
mysql:
image: mysql:${{ matrix.mysql-version }}
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: db
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- uses: actions/checkout@v4
- name: Validate mysql service
run: |
echo "Checking mysql service"
sudo apt-get install -y mysql-client
mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -uroot -proot -e "SHOW DATABASES"
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
repository: OpenMage/Testfield
path: ./
php-version: ${{ matrix.php-versions }}
coverage: pcov #optional, setup coverage driver
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Validate composer
run: composer validate
- uses: actions/checkout@v4

- name: Get composer cache directory
id: composer-cache
Expand All @@ -33,24 +56,61 @@ jobs:
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer install --prefer-dist --no-progress --ignore-platform-reqs

- name: Checkout OpenMage repo
uses: actions/checkout@v4
with:
path: openmage
run: composer install --prefer-dist --no-progress --ignore-platform-req=ext-*

- name: Install OpenMage dependencies
working-directory: ./openmage
run: composer install --prefer-dist --no-progress --ignore-platform-reqs --no-dev
- name: Install OpenMage
run: |
php -f install.php -- \
--license_agreement_accepted 'yes' \
--locale 'en_US' \
--timezone 'America/New_York' \
--db_host '127.0.0.1' \
--db_name 'db' \
--db_user 'root' \
--db_pass 'root' \
--db_prefix '' \
--url 'http://openmage.local' \
--use_rewrites 'yes' \
--use_secure 'yes' \
--secure_base_url 'http://openmage.local' \
--use_secure_admin 'yes' \
--admin_username 'admin' \
--admin_lastname 'Administrator' \
--admin_firstname 'OpenMage' \
--admin_email 'admin@example.com' \
--admin_password 'veryl0ngpassw0rd' \
--session_save 'files' \
--admin_frontname 'admin' \
--backend_frontname 'admin' \
--default_currency 'USD' \
--enable_charts 'yes' \
--skip_url_validation 'yes'
- name: run phpUnit
run: bash ./run_unit_tests.sh
- name: Run phpUnit
run: php -f vendor/bin/phpunit

- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v2.7
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
continue-on-error: true
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
files: output/*.xml
files: tests/logging/*.xml

- name: prepare SonarCloud Scan Data
if: ${{ (matrix.php-versions == '7.4') && (matrix.mysql-version == '5.7') }}
run: |
head tests/coverage/clover.xml
sed -i 's@'$GITHUB_WORKSPACE'/@/github/workspace/@g' tests/logging/junit.xml
sed -i 's@'$GITHUB_WORKSPACE'/@/github/workspace/@g' tests/coverage/clover.xml
head ./tests/coverage/clover.xml
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
continue-on-error: true
if: ${{ (matrix.php-versions == '7.4') && (matrix.mysql-version == '5.7') }} && SONAR_TOKEN
with:
args: >
-Dproject.settings=tests/sonar-project.properties
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
71 changes: 0 additions & 71 deletions .github/workflows/sonar.yml

This file was deleted.

21 changes: 5 additions & 16 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ jobs:
uses: ./.github/workflows/phpstan.yml

# DOES NOT run by default
# runs on schedule or when worklfow changed
# runs on schedule or when workflow changed
syntax_php:
name: PHP Syntax
needs: [check, phpcs, php-cs-fixer]
needs: [check, php-cs-fixer]
if: needs.check.outputs.workflow > 0
uses: ./.github/workflows/syntax-php.yml

Expand All @@ -107,23 +107,12 @@ jobs:
uses: ./.github/workflows/syntax-xml.yml

# DOES NOT run by default
# runs on schedule or when worklfow or unit tests changed
sonar:
name: Unit Tests (Sonar)
needs: [check, phpcs, php-cs-fixer]
if: |
needs.check.outputs.phpunit-test > 0 ||
needs.check.outputs.phpunit > 0 ||
needs.check.outputs.sonar > 0 ||
needs.check.outputs.workflow > 0
uses: ./.github/workflows/sonar.yml

# DOES NOT run by default
# runs on schedule or when worklfow or unit tests changed
# runs on schedule or when workflow or unit tests changed
unit_tests:
name: Unit Tests (OpenMage)
needs: [check, sonar]
needs: [check, php-cs-fixer]
if: |
needs.check.outputs.php > 0 ||
needs.check.outputs.phpunit-test > 0 ||
needs.check.outputs.phpunit > 0 ||
needs.check.outputs.workflow > 0
Expand Down
17 changes: 10 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@
/app/etc/modules/Cm_RedisSession.xml
/lib/Credis

# Add a base setup for running unit Tests with code coverage and send them to SonarCloud
# https://github.com/OpenMage/magento-lts/pull/1836
/dev/testfield
/dev/tests/clover.xml
/dev/tests/crap4j.xml
/dev/tests/junit.xml

# Add Gitpod online IDE config
# https://github.com/OpenMage/magento-lts/pull/1836
/dev/gitpod/docker-magento
Expand Down Expand Up @@ -76,6 +69,16 @@ phpstan*.neon
!.phpstan.dist.neon
!.phpstan.dist.*.neon

# PhpUnit
tests/coverage
tests/logging
.phpunit.result.cache
phpunit.xml
!phpunit.xml.dist

# build
/build

# dev scripts loaded via composer
/shell/update-copyright.php
/shell/translations.php
Expand Down
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
'lib/Magento/',
'lib/Varien/',
'shell/',
'tests/unit/',
])
->name('*.php')
->ignoreDotFiles(true)
Expand Down
1 change: 1 addition & 0 deletions .phpcs.php.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<file>lib/Magento/</file>
<file>lib/Varien/</file>
<file>shell/</file>
<file>tests/unit/</file>
<rule ref="PHPCompatibility" />
<rule ref="PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.MethodDoubleUnderscore">
<exclude-pattern>*/Varien/Object.php*</exclude-pattern>
Expand Down
1 change: 1 addition & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<file>lib/Magento/</file>
<file>lib/Varien/</file>
<file>shell/</file>
<file>tests/unit/</file>
<!-- file contains include/include_once/... -->
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<!-- controllers -->
Expand Down
1 change: 1 addition & 0 deletions .phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ parameters:
- lib/Magento
- lib/Varien
- shell
- tests/unit
excludePaths:
#incompatible interfaces
- app/code/core/Mage/Admin/Model/Acl/Assert/Ip.php
Expand Down
Loading

0 comments on commit 953920a

Please sign in to comment.