Skip to content

Commit a7c32b2

Browse files
authored
Merge pull request #1022 from newfold-labs/update/reusable-workflows-codecoverage-playwright
Use reusable codecoverage workflow and add Playwright
2 parents c6b0b32 + 01729f9 commit a7c32b2

18 files changed

+503
-226
lines changed

.env.testing.example

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# WP-Browser configuration
2+
# https://wpbrowser.wptestkit.dev/
3+
# Copy to .env.testing and adjust for your environment.
4+
# These defaults match the reusable codecoverage workflow (MySQL service on 33306, MYSQL_DATABASE=tests-wordpress).
5+
6+
WP_ROOT_FOLDER="wordpress"
7+
8+
TEST_DB_HOST="127.0.0.1"
9+
TEST_DB_PORT="33306"
10+
TEST_DB_USER="root"
11+
TEST_DB_PASSWORD="password"
12+
13+
TEST_DB_NAME="tests-wordpress"
14+
TEST_TABLE_PREFIX="wp_"
15+
16+
TEST_SITE_WP_DOMAIN="localhost:8888"
17+
TEST_SITE_ADMIN_EMAIL="admin@example.org"

.github/workflows/codecoverage-main.yml

Lines changed: 33 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -4,194 +4,38 @@ name: Codecoverage-Main
44
# GitHub Pages, generates a README badge with the coverage percentage.
55

66
on:
7-
push:
8-
branches:
9-
- trunk
10-
pull_request:
11-
types: [ opened, reopened, ready_for_review, synchronize ]
12-
branches:
13-
- trunk
14-
workflow_dispatch:
7+
push:
8+
branches:
9+
- trunk
10+
pull_request:
11+
types: [ opened, reopened, ready_for_review, synchronize ]
12+
branches:
13+
- trunk
14+
workflow_dispatch:
15+
16+
permissions:
17+
contents: read
1518

1619
jobs:
17-
18-
codecoverage:
19-
runs-on: ubuntu-latest
20-
21-
services:
22-
mysql:
23-
image: mysql:5.7 # Password auth did not work on 8.0 on PHP 7.3, it did seem to work for PHP 7.4+
24-
env: # These are the same username and password as the wp-env container defaults
25-
MYSQL_ROOT_PASSWORD: password
26-
MYSQL_DATABASE: tests-wordpress
27-
ports: # This mapping matches the .wp-env.json configuration
28-
- 33306:3306
29-
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
30-
31-
strategy:
32-
matrix: # Supported PHP versions
33-
php: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
34-
35-
steps:
36-
- name: Checkout
37-
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
38-
with:
39-
fetch-depth: 0 # attempting to get all branch names.
40-
41-
# TODO: a DEPLOY_KEY is needed to enable gh-pages for the first time (the branch can be created and pushed but
42-
# it will not be reachable as a website).
43-
# Consider @see https://github.com/peaceiris/actions-gh-pages
44-
# - name: Check does gh-pages branch need to be created
45-
# run: |
46-
# git branch -l;
47-
# if [[ $(git branch -l gh-pages) == "" ]]; then
48-
# gh_pages_branch_needed=true;
49-
# echo "gh-pages branch is needed";
50-
# else
51-
# gh_pages_branch_needed=false
52-
# echo "gh-pages branch already exists";
53-
# fi
54-
# echo "GH_PAGES_BRANCH_NEEDED=$gh_pages_branch_needed" >> $GITHUB_ENV;
55-
# mkdir gh-pages
56-
#
57-
# - name: Maybe create gh-pages branch
58-
# if: ${{ env.GH_PAGES_BRANCH_NEEDED }}
59-
# uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4.0.0
60-
# with:
61-
# github_token: ${{ secrets.GITHUB_TOKEN }}
62-
# publish_dir: ./gh-pages
63-
# force_orphan: true
64-
# allow_empty_commit: true
65-
# commit_message: "🤖 Creating gh-pages branch"
66-
67-
- name: Checkout GitHub Pages branch for code coverage report
68-
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
69-
with:
70-
ref: gh-pages
71-
path: gh-pages
72-
73-
- name: Install PHP
74-
uses: shivammathur/setup-php@44454db4f0199b8b9685a5d763dc37cbf79108e1 # v2.36.0
75-
with:
76-
php-version: ${{ matrix.php }}
77-
coverage: xdebug
78-
tools: composer, jaschilz/php-coverage-badger
79-
extensions: zip
80-
81-
- name: Read .env.testing
82-
uses: c-py/action-dotenv-to-setenv@925b5d99a3f1e4bd7b4e9928be4e2491e29891d9 # v5
83-
with:
84-
env-file: .env.testing
85-
86-
- name: Run composer install
87-
continue-on-error: true
88-
run: composer install -v
89-
90-
- name: Allow writing to wp-content
91-
if: ${{ hashFiles('wp-content') != '' }} # TODO: This may be incorrect since it's a directory.
92-
run: sudo chmod -R a+w wp-content
93-
94-
- name: Print refs
95-
run: |
96-
echo "ref: ${{ github.ref }}"
97-
echo "head_ref: ${{ github.head_ref }}"
98-
echo "base_ref: ${{ github.base_ref }}"
99-
100-
# On trunk, there will be a previous coverage report. On PRs we create a new directory using the commit SHA.
101-
- name: Clear previous code coverage
102-
if: ${{ (matrix.php == '7.3') && (github.ref == 'refs/heads/trunk') }}
103-
run: |
104-
rm -rf gh-pages/phpunit || true;
105-
mkdir gh-pages/phpunit || true;
106-
107-
# - name: Run unit tests
108-
# if: ${{ hashFiles('tests/phpunit/bootstrap.php') != '' }} # Only run unit tests if they are present.
109-
# run: XDEBUG_MODE=coverage vendor/bin/phpunit --bootstrap tests/phpunit/bootstrap.php --coverage-php tests/_output/unit.cov --debug
110-
111-
- name: Run wpunit tests
112-
run: XDEBUG_MODE=coverage vendor/bin/codecept run wpunit --coverage tests/_output/wpunit.cov --debug
113-
114-
# For PRs, we'll generate the coverage report on each pushed commit
115-
- name: Merge code coverage for PR
116-
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
117-
run: |
118-
vendor/bin/phpcov merge --clover clover.xml tests/_output/;
119-
vendor/bin/phpcov merge --clover gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/clover.xml --php gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/phpunit.cov --html gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/html/ tests/_output/;
120-
121-
# On main, we want it output to a different path
122-
- name: Merge code coverage for main
123-
if: ${{ (matrix.php == '7.3') && (github.ref == 'refs/heads/trunk') }}
124-
run: |
125-
vendor/bin/phpcov merge --clover clover.xml tests/_output/;
126-
vendor/bin/phpcov merge --clover gh-pages/phpunit/clover.xml --php gh-pages/phpunit/phpunit.cov --html gh-pages/phpunit/html/ tests/_output/;
127-
128-
# This makes the coverage percentage available in `{{ steps.coverage-percentage.outputs.coverage-rounded }}`.
129-
- name: Check test coverage
130-
if: ${{ matrix.php == '7.3' }}
131-
uses: johanvanhelden/gha-clover-test-coverage-check@2543c79a701f179bd63aa14c16c6938c509b2cec # v1
132-
id: coverage-percentage
133-
with:
134-
percentage: 25
135-
exit: false
136-
filename: clover.xml
137-
rounded-precision: "0"
138-
139-
# See: https://github.blog/2009-12-29-bypassing-jekyll-on-github-pages/
140-
- name: Add `.nojekyll` file so code coverage report successfully deploys to gh-pages
141-
if: ${{ (matrix.php == '7.3') }}
142-
working-directory: gh-pages
143-
run: |
144-
touch .nojekyll
145-
git add -- .nojekyll *
146-
147-
- name: Update README coverage badge
148-
if: ${{ (matrix.php == '7.3') && (github.ref == 'refs/heads/trunk') }} # only commit on trunk, on the PHP version we're using in production.
149-
run: php-coverage-badger clover.xml gh-pages/phpunit/coverage.svg
150-
151-
- name: Generate PR coverage badge
152-
if: ${{ (matrix.php == '7.3') && github.event_name == 'pull_request' }}
153-
run: php-coverage-badger clover.xml gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/coverage.svg
154-
155-
- name: Commit code coverage to gh-pages
156-
if: ${{ matrix.php == '7.3' }}
157-
uses: stefanzweifel/git-auto-commit-action@28e16e81777b558cc906c8750092100bbb34c5e3 # v7.0.0
158-
with:
159-
repository: gh-pages
160-
branch: gh-pages
161-
commit_message: ${{ format('🤖 Save code coverage report to gh-pages {0}%', steps.coverage-percentage.outputs.coverage-rounded) }}
162-
commit_options: ""
163-
env:
164-
GITHUB_TOKEN: "${{ github.token }}"
165-
166-
- name: Add coverage badge to PR comment
167-
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
168-
run: |
169-
echo "[![Code Coverage ](https://newfold-labs.github.io/wp-module-onboarding/${{ github.event.pull_request.head.sha }}/phpunit/coverage.svg)](https://newfold-labs.github.io/wp-module-onboarding/${{ github.event.pull_request.head.sha }}/phpunit/html/)" >> coverage-comment.md
170-
echo "" >> coverage-comment.md
171-
echo "" >> coverage-comment.md
172-
173-
- name: Add coverage report link to PR comment
174-
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
175-
run: |
176-
echo "${{ format('[project coverage report {0}%](https://newfold-labs.github.io/wp-module-onboarding/{1}/phpunit/html/) @ {2}', steps.coverage-percentage.outputs.coverage-rounded, github.event.pull_request.head.sha, github.event.pull_request.head.sha) }}" >> coverage-comment.md
177-
echo "" >> coverage-comment.md
178-
echo "" >> coverage-comment.md
179-
180-
- name: Add phpcov uncovered lines report to PR comment
181-
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
182-
continue-on-error: true # phpcov can fail if there are no uncovered lines
183-
run: |
184-
BRANCHED_COMMIT=$(git rev-list origin..HEAD | tail -n 1);
185-
echo "BRANCHED_COMMIT=$BRANCHED_COMMIT"
186-
git diff $BRANCHED_COMMIT...${{ github.event.pull_request.head.sha }} > branch.diff;
187-
cat branch.diff;
188-
OUTPUT=${vendor/bin/phpcov patch-coverage --path-prefix $(pwd) ./gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/phpunit.cov branch.diff || true}
189-
echo $OUTPUT;
190-
echo "$OUTPUT" >> coverage-comment.md
191-
192-
- name: Add coverage PR comment
193-
uses: mshick/add-pr-comment@b8f338c590a895d50bcbfa6c5859251edc8952fc # v2.8.2
194-
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
195-
with:
196-
message-id: coverage-report
197-
message-path: coverage-comment.md
20+
get-repo-name:
21+
runs-on: ubuntu-latest
22+
outputs:
23+
repository-name: ${{ steps.repo-name.outputs.name }}
24+
steps:
25+
- name: Extract repository name
26+
id: repo-name
27+
run: echo "name=$(echo ${{ github.repository }} | cut -d'/' -f2)" >> $GITHUB_OUTPUT
28+
29+
codecoverage:
30+
needs: get-repo-name
31+
permissions:
32+
contents: write
33+
pull-requests: write
34+
uses: newfold-labs/workflows/.github/workflows/reusable-codecoverage.yml@main
35+
with:
36+
php-versions: '["7.4", "8.0", "8.1", "8.2", "8.3", "8.4"]'
37+
coverage-php-version: '7.4'
38+
repository-name: ${{ needs.get-repo-name.outputs.repository-name }}
39+
minimum-coverage: 25
40+
secrets:
41+
repo_token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
.vscode
33
.env
4+
.env.testing
45
/node_modules
56
/tests/_output
67
/tests/_support/_generated

codeception.dist.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ params:
1515
coverage:
1616
enabled: true
1717
include:
18-
- /includes/*
19-
# - /upgrades/*
20-
bootstrap: bootstrap.php
18+
- includes/*
19+
exclude:
20+
- tests/*
21+
- vendor/*
22+
- build/*
23+
- src/*

phpunit.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
>
99
<php>
1010
<ini name="display_errors" value="true"/>
11-
<env name="WP_PHPUNIT__TESTS_CONFIG" value="tests/phpunit/wp-tests-config.php" />
1211
</php>
1312
<testsuites>
1413
<testsuite name="Application Test Suite">

tests/_data/.gitkeep

Whitespace-only changes.

tests/phpunit/ColorTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace NewfoldLabs\WP\Module\Onboarding;
4+
5+
use NewfoldLabs\WP\Module\Onboarding\Types\Color;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* PHPUnit tests for Types\Color (no WordPress required).
10+
*
11+
* @coversDefaultClass \NewfoldLabs\WP\Module\Onboarding\Types\Color
12+
*/
13+
class ColorTest extends TestCase {
14+
15+
/**
16+
* Color constructor and getters work.
17+
*
18+
* @return void
19+
*/
20+
public function test_constructor_and_getters() {
21+
$color = new Color( 'Accent 1', 'accent_1', '#F27121' );
22+
$this->assertSame( 'Accent 1', $color->get_name() );
23+
$this->assertSame( 'accent_1', $color->get_slug() );
24+
$this->assertSame( '#F27121', $color->get_color() );
25+
}
26+
27+
/**
28+
* To_array returns expected keys.
29+
*
30+
* @return void
31+
*/
32+
public function test_to_array() {
33+
$color = new Color( 'Accent 1', 'accent_1', '#F27121' );
34+
$arr = $color->to_array();
35+
$this->assertSame(
36+
array(
37+
'name' => 'Accent 1',
38+
'slug' => 'accent_1',
39+
'color' => '#F27121',
40+
),
41+
$arr
42+
);
43+
}
44+
45+
/**
46+
* From_array creates equivalent Color.
47+
*
48+
* @return void
49+
*/
50+
public function test_from_array() {
51+
$data = array(
52+
'name' => 'Accent 1',
53+
'slug' => 'accent_1',
54+
'color' => '#F27121',
55+
);
56+
$color = Color::from_array( $data );
57+
$this->assertInstanceOf( Color::class, $color );
58+
$this->assertSame( 'Accent 1', $color->get_name() );
59+
$this->assertSame( 'accent_1', $color->get_slug() );
60+
$this->assertSame( '#F27121', $color->get_color() );
61+
}
62+
63+
/**
64+
* From_array throws when keys are missing.
65+
*
66+
* @return void
67+
*/
68+
public function test_from_array_throws_when_keys_missing() {
69+
$this->expectException( \InvalidArgumentException::class );
70+
$this->expectExceptionMessage( 'Array must contain name, slug, and color keys' );
71+
Color::from_array(
72+
array(
73+
'slug' => 'accent_1',
74+
'color' => '#F27121',
75+
)
76+
);
77+
}
78+
79+
/**
80+
* Constructor trims whitespace.
81+
*
82+
* @return void
83+
*/
84+
public function test_trims_whitespace() {
85+
$color = new Color( ' Accent 1 ', ' accent_1 ', ' #F27121 ' );
86+
$this->assertSame( 'Accent 1', $color->get_name() );
87+
$this->assertSame( 'accent_1', $color->get_slug() );
88+
$this->assertSame( '#F27121', $color->get_color() );
89+
}
90+
91+
/**
92+
* Constructor throws when name is empty.
93+
*
94+
* @return void
95+
*/
96+
public function test_throws_when_name_empty() {
97+
$this->expectException( \InvalidArgumentException::class );
98+
$this->expectExceptionMessage( 'Name cannot be empty' );
99+
new Color( ' ', 'accent_1', '#F27121' );
100+
}
101+
}

tests/phpunit/ExampleTest.php

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

0 commit comments

Comments
 (0)