Skip to content

Commit 953920a

Browse files
sreichelkiatng
andauthored
PhpUnit: use database for tests, added tests (OpenMage#4138)
* 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 OpenMage#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 <[email protected]>
1 parent bc7881d commit 953920a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+3847
-487
lines changed

.ddev/commands/web/phpunit

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
## Description: run PHPUnit
4+
## Usage: phpunit
5+
## Example: ddev phpunit
6+
7+
php vendor/bin/phpunit --no-coverage "$@" --testdox

.ddev/commands/web/phpunit-coverage

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
## Description: run PHPUnit with coverage
4+
## Usage: phpunit-coverage
5+
## Example: ddev phpunit-coverage
6+
7+
enable_xdebug
8+
XDEBUG_MODE=coverage php vendor/bin/phpunit --testdox
9+
disable_xdebug
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
## Description: run PHPUnit with local HTML coverage
4+
## Usage: phpunit-coverage-local
5+
## Example: ddev phpunit-coverage-local
6+
7+
enable_xdebug
8+
XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-html build/coverage --testdox
9+
disable_xdebug

.gitattributes

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
/.phpcs.php.xml.dist export-ignore
1515
/.phpcs.xml.dist export-ignore
1616
/.phpmd.dist.xml export-ignore
17-
/phpstan.dist.baseline.neon export-ignore
18-
/phpstan.dist.issues.neon export-ignore
19-
/phpstan.dist.neon export-ignore
17+
/.phpstan.dist.baseline.neon export-ignore
18+
/.phpstan.dist.neon export-ignore
2019

2120
/README.md export-ignore
2221

.github/workflows/check-files.yml

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ on:
3333
phpunit:
3434
description: "Count changed PhpUnit files"
3535
value: ${{ jobs.check.outputs.phpunit }}
36-
sonar:
37-
description: "Count changed Sonar files"
38-
value: ${{ jobs.check.outputs.sonar }}
3936
# Allow manually triggering the workflow.
4037
workflow_dispatch:
4138

@@ -54,7 +51,6 @@ jobs:
5451
phpstan: ${{ steps.changes-phpstan.outputs.phpstan }}
5552
phpunit-test: ${{ steps.changes-phpunit-test.outputs.phpunit-test }}
5653
phpunit: ${{ steps.changes-phpunit.outputs.phpunit }}
57-
sonar: ${{ steps.changes-sonar.outputs.sonar }}
5854

5955
steps:
6056
- name: Checkout code
@@ -87,9 +83,8 @@ jobs:
8783
**phpcs**
8884
**php-cs-fixer**
8985
**phpstan**
90-
dev/tests/
91-
dev/phpunit*
92-
dev/sonar*
86+
tests/
87+
phpunit*
9388
9489
- name: Check if composer files changed
9590
id: changes-composer
@@ -161,22 +156,14 @@ jobs:
161156
id: changes-phpunit-test
162157
if: steps.changed-files-specific.outputs.any_modified == 'true'
163158
run: |
164-
count="$(grep -oE "dev/tests/" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)"
159+
count="$(grep -oE "tests/" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)"
165160
echo "$count UnitTest test file(s) changed"
166161
echo "phpunit-test=$count" >> $GITHUB_OUTPUT
167162
168163
- name: Check if PHPUnit files changed
169164
id: changes-phpunit
170165
if: steps.changed-files-specific.outputs.any_modified == 'true'
171166
run: |
172-
count="$(grep -oE "dev/phpunit*" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)"
167+
count="$(grep -oE "phpunit*" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)"
173168
echo "$count PHPUnit file(s) changed"
174169
echo "phpunit=$count" >> $GITHUB_OUTPUT
175-
176-
- name: Check if Sonar files changed
177-
id: changes-sonar
178-
if: steps.changed-files-specific.outputs.any_modified == 'true'
179-
run: |
180-
count="$(grep -oE "dev/sonar*" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)"
181-
echo "$count Sonar file(s) changed"
182-
echo "sonar=$count" >> $GITHUB_OUTPUT

.github/workflows/phpunit.yml

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,39 @@ on:
1010

1111
jobs:
1212
unit-tests:
13-
runs-on: [ubuntu-latest]
13+
runs-on: ${{ matrix.operating-system }}
14+
strategy:
15+
matrix:
16+
operating-system: [ubuntu-latest]
17+
php-versions: ['7.4', '8.3']
18+
mysql-version: ['5.7', '8.0']
19+
20+
services:
21+
mysql:
22+
image: mysql:${{ matrix.mysql-version }}
23+
env:
24+
MYSQL_ROOT_PASSWORD: root
25+
MYSQL_DATABASE: db
26+
ports:
27+
- 3306:3306
28+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
1429

1530
steps:
16-
- uses: actions/checkout@v4
31+
- name: Validate mysql service
32+
run: |
33+
echo "Checking mysql service"
34+
sudo apt-get install -y mysql-client
35+
mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -uroot -proot -e "SHOW DATABASES"
36+
37+
- name: Setup PHP
38+
uses: shivammathur/setup-php@v2
1739
with:
18-
repository: OpenMage/Testfield
19-
path: ./
40+
php-version: ${{ matrix.php-versions }}
41+
coverage: pcov #optional, setup coverage driver
42+
env:
43+
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2044

21-
- name: Validate composer
22-
run: composer validate
45+
- uses: actions/checkout@v4
2346

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

3558
- name: Install dependencies
36-
run: composer install --prefer-dist --no-progress --ignore-platform-reqs
37-
38-
- name: Checkout OpenMage repo
39-
uses: actions/checkout@v4
40-
with:
41-
path: openmage
59+
run: composer install --prefer-dist --no-progress --ignore-platform-req=ext-*
4260

43-
- name: Install OpenMage dependencies
44-
working-directory: ./openmage
45-
run: composer install --prefer-dist --no-progress --ignore-platform-reqs --no-dev
61+
- name: Install OpenMage
62+
run: |
63+
php -f install.php -- \
64+
--license_agreement_accepted 'yes' \
65+
--locale 'en_US' \
66+
--timezone 'America/New_York' \
67+
--db_host '127.0.0.1' \
68+
--db_name 'db' \
69+
--db_user 'root' \
70+
--db_pass 'root' \
71+
--db_prefix '' \
72+
--url 'http://openmage.local' \
73+
--use_rewrites 'yes' \
74+
--use_secure 'yes' \
75+
--secure_base_url 'http://openmage.local' \
76+
--use_secure_admin 'yes' \
77+
--admin_username 'admin' \
78+
--admin_lastname 'Administrator' \
79+
--admin_firstname 'OpenMage' \
80+
--admin_email '[email protected]' \
81+
--admin_password 'veryl0ngpassw0rd' \
82+
--session_save 'files' \
83+
--admin_frontname 'admin' \
84+
--backend_frontname 'admin' \
85+
--default_currency 'USD' \
86+
--enable_charts 'yes' \
87+
--skip_url_validation 'yes'
4688
47-
- name: run phpUnit
48-
run: bash ./run_unit_tests.sh
89+
- name: Run phpUnit
90+
run: php -f vendor/bin/phpunit
4991

5092
- name: Publish Unit Test Results
51-
uses: EnricoMi/publish-unit-test-result-action@v2.7
93+
uses: EnricoMi/publish-unit-test-result-action@v2
5294
if: always()
53-
continue-on-error: true
5495
with:
5596
github_token: ${{ secrets.GITHUB_TOKEN }}
56-
files: output/*.xml
97+
files: tests/logging/*.xml
98+
99+
- name: prepare SonarCloud Scan Data
100+
if: ${{ (matrix.php-versions == '7.4') && (matrix.mysql-version == '5.7') }}
101+
run: |
102+
head tests/coverage/clover.xml
103+
sed -i 's@'$GITHUB_WORKSPACE'/@/github/workspace/@g' tests/logging/junit.xml
104+
sed -i 's@'$GITHUB_WORKSPACE'/@/github/workspace/@g' tests/coverage/clover.xml
105+
head ./tests/coverage/clover.xml
106+
107+
- name: SonarCloud Scan
108+
uses: SonarSource/sonarcloud-github-action@master
109+
continue-on-error: true
110+
if: ${{ (matrix.php-versions == '7.4') && (matrix.mysql-version == '5.7') }} && SONAR_TOKEN
111+
with:
112+
args: >
113+
-Dproject.settings=tests/sonar-project.properties
114+
env:
115+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
116+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

.github/workflows/sonar.yml

Lines changed: 0 additions & 71 deletions
This file was deleted.

.github/workflows/workflow.yml

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ jobs:
9393
uses: ./.github/workflows/phpstan.yml
9494

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

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

109109
# DOES NOT run by default
110-
# runs on schedule or when worklfow or unit tests changed
111-
sonar:
112-
name: Unit Tests (Sonar)
113-
needs: [check, phpcs, php-cs-fixer]
114-
if: |
115-
needs.check.outputs.phpunit-test > 0 ||
116-
needs.check.outputs.phpunit > 0 ||
117-
needs.check.outputs.sonar > 0 ||
118-
needs.check.outputs.workflow > 0
119-
uses: ./.github/workflows/sonar.yml
120-
121-
# DOES NOT run by default
122-
# runs on schedule or when worklfow or unit tests changed
110+
# runs on schedule or when workflow or unit tests changed
123111
unit_tests:
124112
name: Unit Tests (OpenMage)
125-
needs: [check, sonar]
113+
needs: [check, php-cs-fixer]
126114
if: |
115+
needs.check.outputs.php > 0 ||
127116
needs.check.outputs.phpunit-test > 0 ||
128117
needs.check.outputs.phpunit > 0 ||
129118
needs.check.outputs.workflow > 0

.gitignore

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@
2323
/app/etc/modules/Cm_RedisSession.xml
2424
/lib/Credis
2525

26-
# Add a base setup for running unit Tests with code coverage and send them to SonarCloud
27-
# https://github.com/OpenMage/magento-lts/pull/1836
28-
/dev/testfield
29-
/dev/tests/clover.xml
30-
/dev/tests/crap4j.xml
31-
/dev/tests/junit.xml
32-
3326
# Add Gitpod online IDE config
3427
# https://github.com/OpenMage/magento-lts/pull/1836
3528
/dev/gitpod/docker-magento
@@ -76,6 +69,16 @@ phpstan*.neon
7669
!.phpstan.dist.neon
7770
!.phpstan.dist.*.neon
7871

72+
# PhpUnit
73+
tests/coverage
74+
tests/logging
75+
.phpunit.result.cache
76+
phpunit.xml
77+
!phpunit.xml.dist
78+
79+
# build
80+
/build
81+
7982
# dev scripts loaded via composer
8083
/shell/update-copyright.php
8184
/shell/translations.php

.php-cs-fixer.dist.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
'lib/Magento/',
115115
'lib/Varien/',
116116
'shell/',
117+
'tests/unit/',
117118
])
118119
->name('*.php')
119120
->ignoreDotFiles(true)

.phpcs.php.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<file>lib/Magento/</file>
1313
<file>lib/Varien/</file>
1414
<file>shell/</file>
15+
<file>tests/unit/</file>
1516
<rule ref="PHPCompatibility" />
1617
<rule ref="PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.MethodDoubleUnderscore">
1718
<exclude-pattern>*/Varien/Object.php*</exclude-pattern>

.phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<file>lib/Magento/</file>
1313
<file>lib/Varien/</file>
1414
<file>shell/</file>
15+
<file>tests/unit/</file>
1516
<!-- file contains include/include_once/... -->
1617
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
1718
<!-- controllers -->

.phpstan.dist.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ parameters:
1717
- lib/Magento
1818
- lib/Varien
1919
- shell
20+
- tests/unit
2021
excludePaths:
2122
#incompatible interfaces
2223
- app/code/core/Mage/Admin/Model/Acl/Assert/Ip.php

0 commit comments

Comments
 (0)