Skip to content

Commit 93a865f

Browse files
committed
Enhancement: Declare test code in separate directories
1 parent e7c76a8 commit 93a865f

File tree

14 files changed

+496
-2
lines changed

14 files changed

+496
-2
lines changed

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/.github/ export-ignore
22
/.phive/ export-ignore
3+
/test/ export-ignore
34
/.editorconfig export-ignore
45
/.gitattributes export-ignore
56
/.gitignore export-ignore
@@ -9,6 +10,8 @@
910
/composer-require-checker.json export-ignore
1011
/composer.lock export-ignore
1112
/Makefile export-ignore
13+
/phpbench.json export-ignore
14+
/phpunit.xml export-ignore
1215
/psalm-baseline.xml export-ignore
1316
/psalm.xml export-ignore
1417
/rector.php 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: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,61 @@ jobs:
6767
- name: "Test autoloader for production"
6868
run: "php autoloader.php"
6969

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

@@ -190,6 +245,53 @@ jobs:
190245
- name: "Run maglnet/composer-require-checker"
191246
run: ".phive/composer-require-checker check --config-file=$(pwd)/composer-require-checker.json"
192247

248+
mutation-tests:
249+
name: "Mutation Tests"
250+
251+
runs-on: "ubuntu-latest"
252+
253+
strategy:
254+
matrix:
255+
php-version:
256+
- "8.2"
257+
258+
dependencies:
259+
- "locked"
260+
261+
steps:
262+
- name: "Checkout"
263+
uses: "actions/[email protected]"
264+
265+
- name: "Set up PHP"
266+
uses: "shivammathur/[email protected]"
267+
with:
268+
coverage: "xdebug"
269+
extensions: "none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter"
270+
php-version: "${{ matrix.php-version }}"
271+
272+
- name: "Set up problem matchers for PHP"
273+
run: "echo \"::add-matcher::${{ runner.tool_cache }}/php.json\""
274+
275+
- name: "Determine composer cache directory"
276+
uses: "ergebnis/.github/actions/composer/[email protected]"
277+
278+
- name: "Cache dependencies installed with composer"
279+
uses: "actions/[email protected]"
280+
with:
281+
path: "${{ env.COMPOSER_CACHE_DIR }}"
282+
key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
283+
restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"
284+
285+
- name: "Install ${{ matrix.dependencies }} dependencies with composer"
286+
uses: "ergebnis/.github/actions/composer/[email protected]"
287+
with:
288+
dependencies: "${{ matrix.dependencies }}"
289+
290+
- name: "Run mutation tests with Xdebug and infection/infection"
291+
env:
292+
XDEBUG_MODE: "coverage"
293+
run: "vendor/bin/infection --ansi --configuration=infection.json --logger-github"
294+
193295
refactoring:
194296
name: "Refactoring"
195297

@@ -327,3 +429,53 @@ jobs:
327429

328430
- name: "Run vimeo/psalm"
329431
run: "vendor/bin/psalm --config=psalm.xml --output-format=github --shepherd --show-info=false --stats --threads=4"
432+
433+
tests:
434+
name: "Tests"
435+
436+
runs-on: "ubuntu-latest"
437+
438+
strategy:
439+
matrix:
440+
php-version:
441+
- "8.2"
442+
443+
dependencies:
444+
- "lowest"
445+
- "locked"
446+
- "highest"
447+
448+
steps:
449+
- name: "Checkout"
450+
uses: "actions/[email protected]"
451+
452+
- name: "Set up PHP"
453+
uses: "shivammathur/[email protected]"
454+
with:
455+
coverage: "none"
456+
extensions: "none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter"
457+
php-version: "${{ matrix.php-version }}"
458+
459+
- name: "Set up problem matchers for PHP"
460+
run: "echo \"::add-matcher::${{ runner.tool_cache }}/php.json\""
461+
462+
- name: "Set up problem matchers for phpunit/phpunit"
463+
run: "echo \"::add-matcher::${{ runner.tool_cache }}/phpunit.json\""
464+
465+
- name: "Determine composer cache directory"
466+
uses: "ergebnis/.github/actions/composer/[email protected]"
467+
468+
- name: "Cache dependencies installed with composer"
469+
uses: "actions/[email protected]"
470+
with:
471+
path: "${{ env.COMPOSER_CACHE_DIR }}"
472+
key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
473+
restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"
474+
475+
- name: "Install ${{ matrix.dependencies }} dependencies with composer"
476+
uses: "ergebnis/.github/actions/composer/[email protected]"
477+
with:
478+
dependencies: "${{ matrix.dependencies }}"
479+
480+
- name: "Run unit tests with phpunit/phpunit"
481+
run: "vendor/bin/phpunit --colors=always --configuration=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=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 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
@@ -46,6 +60,11 @@ static-code-analysis-baseline: vendor ## Generates a baseline for static code an
4660
vendor/bin/psalm --config=psalm.xml --clear-cache
4761
vendor/bin/psalm --config=psalm.xml --set-baseline=psalm-baseline.xml
4862

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

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
"Localheinz\\OrganizingTestCode\\": "src/"
3737
}
3838
},
39+
"autoload-dev": {
40+
"psr-4": {
41+
"Localheinz\\OrganizingTestCode\\Test\\": "test/"
42+
}
43+
},
3944
"config": {
4045
"allow-plugins": {
4146
"composer/package-versions-deprecated": true,

infection.json

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

phpbench.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"$schema":"vendor/phpbench/phpbench/phpbench.schema.json",
3+
"runner.bootstrap": "vendor/autoload.php",
4+
"runner.file_pattern": "*.php"
5+
}

phpunit.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
beStrictAboutChangesToGlobalState="true"
6+
beStrictAboutCoverageMetadata="true"
7+
beStrictAboutOutputDuringTests="true"
8+
beStrictAboutTestsThatDoNotTestAnything="true"
9+
bootstrap="vendor/autoload.php"
10+
cacheDirectory=".build/phpunit/unit/"
11+
cacheResult="true"
12+
colors="true"
13+
columns="max"
14+
displayDetailsOnIncompleteTests="true"
15+
displayDetailsOnSkippedTests="true"
16+
displayDetailsOnTestsThatTriggerDeprecations="true"
17+
displayDetailsOnTestsThatTriggerErrors="true"
18+
displayDetailsOnTestsThatTriggerNotices="true"
19+
displayDetailsOnTestsThatTriggerWarnings="true"
20+
executionOrder="random"
21+
requireCoverageMetadata="true"
22+
stopOnError="false"
23+
stopOnFailure="false"
24+
stopOnIncomplete="false"
25+
stopOnSkipped="false"
26+
>
27+
<coverage includeUncoveredFiles="true">
28+
<include>
29+
<directory suffix=".php">src/</directory>
30+
</include>
31+
</coverage>
32+
<extensions>
33+
<bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
34+
</extensions>
35+
<testsuites>
36+
<testsuite name="Unit Tests">
37+
<directory>test/Unit/</directory>
38+
</testsuite>
39+
</testsuites>
40+
</phpunit>

0 commit comments

Comments
 (0)