Skip to content

Commit 0804a51

Browse files
committed
Enhancement: Declare test code in same directory
1 parent dc674cf commit 0804a51

15 files changed

+555
-9
lines changed

.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/.github/ export-ignore
22
/.phive/ export-ignore
33
/.editorconfig export-ignore
4+
/src/ExampleBench.php export-ignore
5+
/src/ExampleTest.php export-ignore
6+
/src/Helper.php export-ignore
7+
/src/ValueCanNotBeBlankTest.php export-ignore
8+
/src/ValueCanNotBeEmptyTest.php export-ignore
49
/.gitattributes export-ignore
510
/.gitignore export-ignore
611
/.php-cs-fixer.php export-ignore
@@ -9,6 +14,8 @@
914
/composer-require-checker.json export-ignore
1015
/composer.lock export-ignore
1116
/Makefile export-ignore
17+
/phpbench.json export-ignore
18+
/phpunit.xml export-ignore
1219
/psalm-baseline.xml export-ignore
1320
/psalm.xml export-ignore
1421
/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: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,62 @@ 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/checkout@v3.3.0"
86+
87+
- name: "Set up PHP"
88+
uses: "shivammathur/setup-php@2.24.0"
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/determine-cache-directory@1.8.0"
102+
103+
- name: "Cache dependencies installed with composer"
104+
uses: "actions/cache@v3.2.6"
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/install@1.8.0"
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/codecov-action@v3.1.1"
122+
with:
123+
files: ".build/phpunit/logs/clover.xml"
124+
token: "${{ secrets.CODECOV_TOKEN }}"
125+
70126
coding-standards:
71127
name: "Coding Standards"
72128

@@ -190,6 +246,98 @@ jobs:
190246
- name: "Run maglnet/composer-require-checker"
191247
run: ".phive/composer-require-checker check --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=phpbench.json "
340+
193341
refactoring:
194342
name: "Refactoring"
195343

@@ -327,3 +475,53 @@ jobs:
327475

328476
- name: "Run vimeo/psalm"
329477
run: "vendor/bin/psalm --config=psalm.xml --output-format=github --shepherd --show-info=false --stats --threads=4"
478+
479+
tests:
480+
name: "Tests"
481+
482+
runs-on: "ubuntu-latest"
483+
484+
strategy:
485+
matrix:
486+
php-version:
487+
- "8.2"
488+
489+
dependencies:
490+
- "lowest"
491+
- "locked"
492+
- "highest"
493+
494+
steps:
495+
- name: "Checkout"
496+
uses: "actions/checkout@v3.3.0"
497+
498+
- name: "Set up PHP"
499+
uses: "shivammathur/setup-php@2.24.0"
500+
with:
501+
coverage: "none"
502+
extensions: "none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter"
503+
php-version: "${{ matrix.php-version }}"
504+
505+
- name: "Set up problem matchers for PHP"
506+
run: "echo \"::add-matcher::${{ runner.tool_cache }}/php.json\""
507+
508+
- name: "Set up problem matchers for phpunit/phpunit"
509+
run: "echo \"::add-matcher::${{ runner.tool_cache }}/phpunit.json\""
510+
511+
- name: "Determine composer cache directory"
512+
uses: "ergebnis/.github/actions/composer/determine-cache-directory@1.8.0"
513+
514+
- name: "Cache dependencies installed with composer"
515+
uses: "actions/cache@v3.2.6"
516+
with:
517+
path: "${{ env.COMPOSER_CACHE_DIR }}"
518+
key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
519+
restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"
520+
521+
- name: "Install ${{ matrix.dependencies }} dependencies with composer"
522+
uses: "ergebnis/.github/actions/composer/install@1.8.0"
523+
with:
524+
dependencies: "${{ matrix.dependencies }}"
525+
526+
- name: "Run tests with phpunit/phpunit"
527+
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 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: mutation-tests
30+
mutation-tests: vendor ## Runs mutation tests with infection/infection
31+
mkdir -p .build/infection
32+
vendor/bin/infection --configuration=infection.json
33+
34+
.PHONY: performance-tests
35+
performance-tests: vendor ## Runs performance tests with phpbench/phpbench
36+
vendor/bin/phpbench run --config=phpbench.json src/
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@
3434
"autoload": {
3535
"psr-4": {
3636
"Localheinz\\OrganizingTestCodeInPhp\\": "src/"
37-
}
37+
},
38+
"exclude-from-classmap": [
39+
"/src/*Bench.php",
40+
"/src/*Test.php",
41+
"/src/Helper.php"
42+
]
3843
},
3944
"config": {
4045
"allow-plugins": {

infection.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
"excludes": [
16+
"ExampleBench.php",
17+
"ExampleTest.php",
18+
"Helper.php",
19+
"ValueCanNotBeBlankTest.php",
20+
"ValueCanNotBeEmptyTest.php"
21+
]
22+
},
23+
"timeout": 10
24+
}

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+
}

0 commit comments

Comments
 (0)