Skip to content

Commit 0098fa7

Browse files
committed
Enhancement: Declare test code in subdirectories with separate configuration files
1 parent 6238054 commit 0098fa7

File tree

15 files changed

+551
-9
lines changed

15 files changed

+551
-9
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/.github/ export-ignore
22
/.phive/ export-ignore
3+
/src/Test/ export-ignore
34
/.editorconfig export-ignore
45
/.gitattributes export-ignore
56
/.gitignore export-ignore

.github/CONTRIBUTING.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ make dependency-analysis
4646

4747
to run a dependency analysis.
4848

49+
## Mutation Tests
50+
51+
We are using [`infection/infection`](https://github.com/infection/infection) to ensure a minimum quality of the tests.
52+
53+
Enable `Xdebug` and run
54+
55+
```sh
56+
make mutation-tests
57+
```
58+
59+
to run mutation tests.
60+
61+
## Performance Tests
62+
63+
We are using [`phpbench/phpbench`](https://github.com/phpbench/phpbench) to control the performance of our production code.
64+
65+
```sh
66+
make performance-tests
67+
```
68+
69+
to run performance tests.
70+
4971
## Refactoring
5072

5173
We are using [`rector/rector`](https://github.com/rectorphp/rector) to automatically refactor code.
@@ -94,6 +116,17 @@ to regenerate the baseline in [`../psalm-baseline.xml`](../psalm-baseline.xml).
94116

95117
:exclamation: Ideally, the baseline should shrink over time.
96118

119+
## Tests
120+
121+
We are using [`phpunit/phpunit`](https://github.com/sebastianbergmann/phpunit) to drive the development.
122+
123+
Run
124+
125+
```sh
126+
make tests
127+
```
128+
129+
to run all the tests.
97130

98131
## Extra lazy?
99132

@@ -103,7 +136,7 @@ Run
103136
make
104137
```
105138

106-
to automatically refactor code, enforce coding standards, and run a static code analysis!
139+
to automatically refactor code, enforce coding standards, run a static code analysis, run tests, run performance tests, and run mutation tests!
107140

108141
## Help
109142

.github/settings.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ branches:
1515
required_status_checks:
1616
checks:
1717
- context: "Autoloader (8.2, locked)"
18+
- context: "Code Coverage (8.2, locked)"
1819
- context: "Coding Standards (8.2, locked)"
1920
- context: "Dependency Analysis (8.2, locked)"
21+
- context: "Mutation Tests (8.2, locked)"
22+
- context: "Performance Tests (8.2, locked)"
2023
- context: "Refactoring (8.2, locked)"
2124
- context: "Security Analysis (8.2, locked)"
2225
- context: "Static Code Analysis (8.2, locked)"
26+
- context: "Tests (8.2, locked)"
2327
strict: false
2428
restrictions: null
2529

.github/workflows/integrate.yaml

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,62 @@ jobs:
6262
- name: "Test autoloader for production"
6363
run: "php autoloader.php"
6464

65+
code-coverage:
66+
name: "Code Coverage"
67+
68+
runs-on: "ubuntu-latest"
69+
70+
strategy:
71+
matrix:
72+
php-version:
73+
- "8.2"
74+
75+
dependencies:
76+
- "locked"
77+
78+
steps:
79+
- name: "Checkout"
80+
uses: "actions/checkout@v3.3.0"
81+
82+
- name: "Set up PHP"
83+
uses: "shivammathur/setup-php@2.24.0"
84+
with:
85+
coverage: "xdebug"
86+
extensions: "none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter"
87+
php-version: "${{ matrix.php-version }}"
88+
89+
- name: "Set up problem matchers for PHP"
90+
run: "echo \"::add-matcher::${{ runner.tool_cache }}/php.json\""
91+
92+
- name: "Set up problem matchers for phpunit/phpunit"
93+
run: "echo \"::add-matcher::${{ runner.tool_cache }}/phpunit.json\""
94+
95+
- name: "Determine composer cache directory"
96+
uses: "ergebnis/.github/actions/composer/determine-cache-directory@1.8.0"
97+
98+
- name: "Cache dependencies installed with composer"
99+
uses: "actions/cache@v3.2.6"
100+
with:
101+
path: "${{ env.COMPOSER_CACHE_DIR }}"
102+
key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
103+
restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"
104+
105+
- name: "Install ${{ matrix.dependencies }} dependencies with composer"
106+
uses: "ergebnis/.github/actions/composer/install@1.8.0"
107+
with:
108+
dependencies: "${{ matrix.dependencies }}"
109+
110+
- name: "Collect code coverage with Xdebug and phpunit/phpunit"
111+
env:
112+
XDEBUG_MODE: "coverage"
113+
run: "vendor/bin/phpunit --colors=always --configuration=src/Test/Unit/phpunit.xml --coverage-clover=.build/phpunit/logs/clover.xml"
114+
115+
- name: "Send code coverage report to codecov.io"
116+
uses: "codecov/codecov-action@v3.1.1"
117+
with:
118+
files: ".build/phpunit/logs/clover.xml"
119+
token: "${{ secrets.CODECOV_TOKEN }}"
120+
65121
coding-standards:
66122
name: "Coding Standards"
67123

@@ -190,6 +246,98 @@ jobs:
190246
- name: "Run maglnet/composer-require-checker"
191247
run: ".phive/composer-require-checker check --ansi --config-file=$(pwd)/composer-require-checker.json"
192248

249+
mutation-tests:
250+
name: "Mutation Tests"
251+
252+
runs-on: "ubuntu-latest"
253+
254+
strategy:
255+
matrix:
256+
php-version:
257+
- "8.2"
258+
259+
dependencies:
260+
- "locked"
261+
262+
steps:
263+
- name: "Checkout"
264+
uses: "actions/checkout@v3.3.0"
265+
266+
- name: "Set up PHP"
267+
uses: "shivammathur/setup-php@2.24.0"
268+
with:
269+
coverage: "xdebug"
270+
extensions: "none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter"
271+
php-version: "${{ matrix.php-version }}"
272+
273+
- name: "Set up problem matchers for PHP"
274+
run: "echo \"::add-matcher::${{ runner.tool_cache }}/php.json\""
275+
276+
- name: "Determine composer cache directory"
277+
uses: "ergebnis/.github/actions/composer/determine-cache-directory@1.8.0"
278+
279+
- name: "Cache dependencies installed with composer"
280+
uses: "actions/cache@v3.2.6"
281+
with:
282+
path: "${{ env.COMPOSER_CACHE_DIR }}"
283+
key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
284+
restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"
285+
286+
- name: "Install ${{ matrix.dependencies }} dependencies with composer"
287+
uses: "ergebnis/.github/actions/composer/install@1.8.0"
288+
with:
289+
dependencies: "${{ matrix.dependencies }}"
290+
291+
- name: "Run mutation tests with Xdebug and infection/infection"
292+
env:
293+
XDEBUG_MODE: "coverage"
294+
run: "vendor/bin/infection --ansi --configuration=infection.json --logger-github"
295+
296+
performance-tests:
297+
name: "Performance Tests"
298+
299+
runs-on: "ubuntu-latest"
300+
301+
strategy:
302+
matrix:
303+
php-version:
304+
- "8.2"
305+
306+
dependencies:
307+
- "locked"
308+
309+
steps:
310+
- name: "Checkout"
311+
uses: "actions/checkout@v3.3.0"
312+
313+
- name: "Set up PHP"
314+
uses: "shivammathur/setup-php@2.24.0"
315+
with:
316+
coverage: "none"
317+
extensions: "none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter"
318+
php-version: "${{ matrix.php-version }}"
319+
320+
- name: "Set up problem matchers for PHP"
321+
run: "echo \"::add-matcher::${{ runner.tool_cache }}/php.json\""
322+
323+
- name: "Determine composer cache directory"
324+
uses: "ergebnis/.github/actions/composer/determine-cache-directory@1.8.0"
325+
326+
- name: "Cache dependencies installed with composer"
327+
uses: "actions/cache@v3.2.6"
328+
with:
329+
path: "${{ env.COMPOSER_CACHE_DIR }}"
330+
key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
331+
restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"
332+
333+
- name: "Install ${{ matrix.dependencies }} dependencies with composer"
334+
uses: "ergebnis/.github/actions/composer/install@1.8.0"
335+
with:
336+
dependencies: "${{ matrix.dependencies }}"
337+
338+
- name: "Run performance tests with phpbench/phpbench"
339+
run: "vendor/bin/phpbench run --config=src/Test/Performance/phpbench.json src/Test/Performance/"
340+
193341
refactoring:
194342
name: "Refactoring"
195343

@@ -354,3 +502,53 @@ jobs:
354502

355503
- name: "Run vimeo/psalm"
356504
run: "vendor/bin/psalm --config=psalm.xml --output-format=github --shepherd --show-info=false --stats --threads=4"
505+
506+
tests:
507+
name: "Tests"
508+
509+
runs-on: "ubuntu-latest"
510+
511+
strategy:
512+
matrix:
513+
php-version:
514+
- "8.2"
515+
516+
dependencies:
517+
- "lowest"
518+
- "locked"
519+
- "highest"
520+
521+
steps:
522+
- name: "Checkout"
523+
uses: "actions/checkout@v3.3.0"
524+
525+
- name: "Set up PHP"
526+
uses: "shivammathur/setup-php@2.24.0"
527+
with:
528+
coverage: "none"
529+
extensions: "none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter"
530+
php-version: "${{ matrix.php-version }}"
531+
532+
- name: "Set up problem matchers for PHP"
533+
run: "echo \"::add-matcher::${{ runner.tool_cache }}/php.json\""
534+
535+
- name: "Set up problem matchers for phpunit/phpunit"
536+
run: "echo \"::add-matcher::${{ runner.tool_cache }}/phpunit.json\""
537+
538+
- name: "Determine composer cache directory"
539+
uses: "ergebnis/.github/actions/composer/determine-cache-directory@1.8.0"
540+
541+
- name: "Cache dependencies installed with composer"
542+
uses: "actions/cache@v3.2.6"
543+
with:
544+
path: "${{ env.COMPOSER_CACHE_DIR }}"
545+
key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
546+
restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"
547+
548+
- name: "Install ${{ matrix.dependencies }} dependencies with composer"
549+
uses: "ergebnis/.github/actions/composer/install@1.8.0"
550+
with:
551+
dependencies: "${{ matrix.dependencies }}"
552+
553+
- name: "Run unit tests with phpunit/phpunit"
554+
run: "vendor/bin/phpunit --colors=always --configuration=src/Test/Unit/phpunit.xml"

Makefile

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
.PHONY: it
2-
it: refactoring coding-standards security-analysis static-code-analysis autoloader ## Runs the refactoring, coding-standards, security-analysis, static-code-analysis, and autoloader targets
2+
it: refactoring coding-standards security-analysis static-code-analysis tests performance-tests mutation-tests autoloader ## Runs the refactoring, coding-standards, security-analysis, static-code-analysis, tests, performance-tests, mutation-tests, and autoloader targets
33

44
.PHONY: autoloader
55
autoloader: ## Dumps the autoloader for production and verifies that it does not include classes not intended for production
66
composer dump-autoload --no-dev --optimize
77
php autoloader.php
88

9+
.PHONY: code-coverage
10+
code-coverage: vendor ## Collects coverage from running unit tests with phpunit/phpunit
11+
mkdir -p .build/phpunit
12+
vendor/bin/phpunit --configuration=src/Test/Unit/phpunit.xml --coverage-text
13+
914
.PHONY: coding-standards
1015
coding-standards: vendor ## Lints YAML files with yamllint, normalizes composer.json with ergebnis/composer-normalize, and fixes code style issues with friendsofphp/php-cs-fixer
1116
yamllint -c .yamllint.yaml --strict .
@@ -21,6 +26,15 @@ dependency-analysis: phive vendor ## Runs a dependency analysis with maglnet/com
2126
help: ## Displays this list of targets with descriptions
2227
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}'
2328

29+
.PHONY: performance-tests
30+
performance-tests: vendor ## Runs performance tests with phpbench/phpbench
31+
vendor/bin/phpbench run --config=src/Test/Performance/phpbench.json src/Test/Performance/
32+
33+
.PHONY: mutation-tests
34+
mutation-tests: vendor ## Runs mutation tests with infection/infection
35+
mkdir -p .build/infection
36+
vendor/bin/infection --configuration=infection.json
37+
2438
.PHONY: phive
2539
phive: .phive ## Installs dependencies with phive
2640
mkdir -p .build/phive/
@@ -47,6 +61,11 @@ static-code-analysis-baseline: vendor ## Generates a baseline for static code an
4761
vendor/bin/psalm --config=psalm.xml --clear-cache
4862
vendor/bin/psalm --config=psalm.xml --set-baseline=psalm-baseline.xml
4963

64+
.PHONY: tests
65+
tests: vendor ## Runs unit tests with phpunit/phpunit
66+
mkdir -p .build/phpunit
67+
vendor/bin/phpunit --configuration=src/Test/Unit/phpunit.xml
68+
5069
vendor: composer.json composer.lock
5170
composer validate --strict
5271
composer install --no-interaction --no-progress

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
"autoload": {
3535
"psr-4": {
3636
"Localheinz\\OrganizingTestCodeInPhp\\": "src/"
37-
}
37+
},
38+
"exclude-from-classmap": [
39+
"/src/Test/"
40+
]
3841
},
3942
"config": {
4043
"allow-plugins": {

infection.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"ignoreMsiWithNoMutations": true,
3+
"logs": {
4+
"text": ".build/infection/infection-log.txt"
5+
},
6+
"minCoveredMsi": 100,
7+
"minMsi": 100,
8+
"phpUnit": {
9+
"configDir": "src/Test/Unit/"
10+
},
11+
"source": {
12+
"directories": [
13+
"src"
14+
],
15+
"excludes": [
16+
"Test/"
17+
]
18+
},
19+
"timeout": 10
20+
}

psalm-baseline.xml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
3-
<file src="src/Example.php">
4-
<PossiblyUnusedMethod>
5-
<code>equals</code>
6-
<code>fromString</code>
7-
<code>toString</code>
8-
</PossiblyUnusedMethod>
3+
<file src="src/Test/Performance/ExampleBench.php">
4+
<UnusedClass>
5+
<code>ExampleBench</code>
6+
</UnusedClass>
97
</file>
108
</files>

0 commit comments

Comments
 (0)