Skip to content

Commit af257fa

Browse files
Merge pull request #94 from julienloizelet/feat/coding-standards-rework
Feat/coding standards rework
2 parents 5ccafe8 + 1e29d58 commit af257fa

37 files changed

+721
-413
lines changed

.github/workflows/test-suite.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828

2929
- name: Install DDEV
3030
env:
31-
DDEV_VERSION: v1.19.1
31+
DDEV_VERSION: v1.19.3
3232
run: |
3333
# @see https://ddev.readthedocs.io/en/stable/#installationupgrade-script-linux-and-macos-armarm64-and-amd64-architectures
3434
sudo apt-get -qq update
@@ -90,11 +90,11 @@ jobs:
9090
9191
- name: Run PHP UNIT tests (IP verification)
9292
run: |
93-
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} LAPI_URL=http://crowdsec:8080 MEMCACHED_DSN=memcached://memcached:11211 REDIS_DSN=redis://redis:6379 /usr/bin/php ./my-own-modules/crowdsec-php-lib/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./my-own-modules/crowdsec-php-lib/tests/IpVerificationTest.php
93+
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} LAPI_URL=http://crowdsec:8080 MEMCACHED_DSN=memcached://memcached:11211 REDIS_DSN=redis://redis:6379 /usr/bin/php ./my-own-modules/crowdsec-php-lib/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./my-own-modules/crowdsec-php-lib/tests/Integration/IpVerificationTest.php
9494
9595
- name: Run PHP UNIT tests (Geolocation)
9696
run: |
97-
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} LAPI_URL=http://crowdsec:8080 /usr/bin/php ./my-own-modules/crowdsec-php-lib/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./my-own-modules/crowdsec-php-lib/tests/GeolocationTest.php
97+
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} LAPI_URL=http://crowdsec:8080 /usr/bin/php ./my-own-modules/crowdsec-php-lib/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./my-own-modules/crowdsec-php-lib/tests/Integration/GeolocationTest.php
9898
9999
- name: Prepare Standalone Bouncer end-to-end tests
100100
run: |

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Composer
22
vendor
3-
/composer.lock
3+
composer.lock
44

55
# Systems
66
.DS_Store
@@ -12,7 +12,7 @@ super-linter.log
1212
tools/php-cs-fixer/composer.lock
1313

1414
# App
15-
/var/
15+
var/
1616
.bouncer-key
1717
.cache
1818

composer.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "crowdsec/bouncer",
3-
"description": "The official PHP bouncer library for the CrowdSec LAPI/CAPI",
3+
"description": "The official PHP bouncer library for the CrowdSec LAPI",
44
"type": "library",
55
"license": "MIT",
66
"minimum-stability": "stable",
@@ -22,6 +22,11 @@
2222
"CrowdSecBouncer\\": "src/"
2323
}
2424
},
25+
"autoload-dev": {
26+
"psr-4": {
27+
"CrowdSecBouncer\\Tests\\": "tests/"
28+
}
29+
},
2530
"authors": [
2631
{
2732
"name": "Lucas Cherifi",
@@ -43,8 +48,6 @@
4348
"geoip2/geoip2": "^2.12.2"
4449
},
4550
"require-dev": {
46-
"phpunit/phpunit": "8.5.21",
47-
"phpmd/phpmd": "@stable",
48-
"squizlabs/php_codesniffer": "^3.6.2"
51+
"phpunit/phpunit": "8.5.27"
4952
}
5053
}

docs/DEVELOPER.md

+64-16
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ For a quick start, follow the below steps.
6969

7070
#### DDEV installation
7171

72-
This project is fully compatible with DDEV 1.19.1, and it is recommended to use this specific version.
72+
This project is fully compatible with DDEV 1.19.3, and it is recommended to use this specific version.
7373
For the DDEV installation, please follow the [official instructions](https://ddev.readthedocs.io/en/stable/#installation).
7474
On a Linux distribution, you can run:
7575
```
7676
sudo apt-get -qq update
7777
sudo apt-get -qq -y install libnss3-tools
7878
curl -LO https://raw.githubusercontent.com/drud/ddev/master/scripts/install_ddev.sh
79-
bash install_ddev.sh v1.19.1
79+
bash install_ddev.sh v1.19.3
8080
rm install_ddev.sh
8181
```
8282

@@ -86,18 +86,18 @@ rm install_ddev.sh
8686
The final structure of the project will look like below.
8787

8888
```
89-
php-project-sources
89+
php-project-sources (choose the name you want for this folder)
9090
9191
│ (your php project sources; could be a simple index.php file)
9292
93-
└───.ddev
93+
└───.ddev (do not change this folder name)
9494
│ │
9595
│ │ (Cloned sources of a PHP specific ddev repo)
9696
97-
└───my-own-modules
98-
97+
└───my-own-modules (do not change this folder name)
9998
100-
└───crowdsec-php-lib
99+
100+
└───crowdsec-php-lib (do not change this folder name)
101101
102102
│ (Clone of this repo)
103103
@@ -212,7 +212,7 @@ Finally, run
212212

213213

214214
```bash
215-
ddev exec BOUNCER_KEY=your-bouncer-key LAPI_URL=http://crowdsec:8080 MEMCACHED_DSN=memcached://memcached:11211 REDIS_DSN=redis://redis:6379 /usr/bin/php ./my-own-modules/crowdsec-php-lib/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./my-own-modules/crowdsec-php-lib/tests/IpVerificationTest.php
215+
ddev exec BOUNCER_KEY=your-bouncer-key LAPI_URL=http://crowdsec:8080 MEMCACHED_DSN=memcached://memcached:11211 REDIS_DSN=redis://redis:6379 /usr/bin/php ./my-own-modules/crowdsec-php-lib/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./my-own-modules/crowdsec-php-lib/tests/Integration/IpVerificationTest.php
216216
```
217217

218218
For geolocation Unit Test, you should first put 2 free MaxMind databases in the `tests` folder : `GeoLite2-City.mmdb`
@@ -222,7 +222,7 @@ and`GeoLite2-Country.mmdb`. You can download these databases by creating a maxmi
222222
Then, you can run:
223223

224224
```bash
225-
ddev exec BOUNCER_KEY=your-bouncer-key LAPI_URL=http://crowdsec:8080 /usr/bin/php ./my-own-modules/crowdsec-php-lib/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./my-own-modules/crowdsec-php-lib/tests/GeolocationTest.php
225+
ddev exec BOUNCER_KEY=your-bouncer-key LAPI_URL=http://crowdsec:8080 /usr/bin/php ./my-own-modules/crowdsec-php-lib/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./my-own-modules/crowdsec-php-lib/tests/Integration/GeolocationTest.php
226226

227227
```
228228

@@ -295,30 +295,42 @@ yarn global add cross-env
295295

296296
#### Coding standards
297297

298+
We set up some coding standards tools that you will find in the `tools/coding-standards` folder.
299+
In order to use these, you will need to work with a PHP version >= 7.4 and run first:
300+
301+
```
302+
ddev composer update --working-dir=./my-own-modules/crowdsec-php-lib/tools/coding-standards
303+
```
304+
298305
##### PHPCS Fixer
299306

300307
We are using the [PHP Coding Standards Fixer](https://cs.symfony.com/)
301308

302309
With ddev, you can do the following:
303310

311+
304312
```bash
305-
ddev composer update --working-dir=./my-own-modules/crowdsec-php-lib/tools/php-cs-fixer
313+
ddev phpcsfixer my-own-modules/crowdsec-php-lib/tools/coding-standards/php-cs-fixer ../
314+
306315
```
307-
And then:
316+
317+
##### PHPSTAN
318+
319+
To use the [PHPSTAN](https://github.com/phpstan/phpstan) tool, you can run:
320+
308321

309322
```bash
310-
ddev phpcsfixer my-own-modules/crowdsec-php-lib tools/php-cs-fixer
323+
ddev phpstan /var/www/html/my-own-modules/crowdsec-php-lib/tools/coding-standards phpstan/phpstan.neon /var/www/html/my-own-modules/crowdsec-php-lib/src
311324

312325
```
313326

314-
**N.B**: to use PHPCS Fixer, you will need to work with a PHP version >= 7.4.
315327

316328
##### PHP Mess Detector
317329

318330
To use the [PHPMD](https://github.com/phpmd/phpmd) tool, you can run:
319331

320332
```bash
321-
ddev phpmd ./my-own-modules/crowdsec-php-lib tools/phpmd/rulesets.xml src
333+
ddev phpmd ./my-own-modules/crowdsec-php-lib/tools/coding-standards phpmd/rulesets.xml ../../src
322334

323335
```
324336

@@ -327,15 +339,51 @@ ddev phpmd ./my-own-modules/crowdsec-php-lib tools/phpmd/rulesets.xml src
327339
To use [PHP Code Sniffer](https://github.com/squizlabs/PHP_CodeSniffer) tools, you can run:
328340

329341
```bash
330-
ddev phpcs ./my-own-modules/crowdsec-php-lib/vendor/bin/phpcs my-own-modules/crowdsec-php-lib/src
342+
ddev phpcs ./my-own-modules/crowdsec-php-lib/tools/coding-standards my-own-modules/crowdsec-php-lib/src PSR12
331343
```
332344

333345
and:
334346

335347
```bash
336-
ddev phpcbf ./my-own-modules/crowdsec-php-lib/vendor/bin/phpcs my-own-modules/crowdsec-php-lib/src
348+
ddev phpcbf ./my-own-modules/crowdsec-php-lib/tools/coding-standards my-own-modules/crowdsec-php-lib/src PSR12
349+
```
350+
351+
352+
##### PSALM
353+
354+
To use [PSALM](https://github.com/vimeo/psalm) tools, you can run:
355+
356+
```bash
357+
ddev psalm ./my-own-modules/crowdsec-php-lib/tools/coding-standards ./my-own-modules/crowdsec-php-lib/tools/coding-standards/psalm
337358
```
338359

360+
##### PHP Unit Code coverage
361+
362+
In order to generate a code coverage report, you have to:
363+
364+
- Enable `xdebug`:
365+
```bash
366+
ddev xdebug
367+
```
368+
369+
To generate a html report, you can run:
370+
```bash
371+
ddev exec XDEBUG_MODE=coverage BOUNCER_KEY=your-bouncer-key LAPI_URL=http://crowdsec:8080
372+
MEMCACHED_DSN=memcached://memcached:11211 REDIS_DSN=redis://redis:6379 /usr/bin/php ./my-own-modules/crowdsec-php-lib/tools/coding-standards/vendor/bin/phpunit --configuration ./my-own-modules/crowdsec-php-lib/tools/coding-standards/phpunit/phpunit.xml
373+
374+
```
375+
376+
You should find the main report file `dashboard.html` in `tools/coding-standards/phpunit/code-coverage` folder.
377+
378+
379+
If you want to generate a text report in the same folder:
380+
381+
```bash
382+
ddev exec XDEBUG_MODE=coverage BOUNCER_KEY=your-bouncer-key LAPI_URL=http://crowdsec:8080
383+
MEMCACHED_DSN=memcached://memcached:11211 REDIS_DSN=redis://redis:6379 /usr/bin/php ./my-own-modules/crowdsec-php-lib/tools/coding-standards/vendor/bin/phpunit --configuration ./my-own-modules/crowdsec-php-lib/tools/coding-standards/phpunit/phpunit.xml --coverage-text=./my-own-modules/crowdsec-php-lib/tools/coding-standards/phpunit/code-coverage/report.txt
384+
```
385+
386+
339387

340388
#### Generate CrowdSec tools and settings on start
341389

docs/INSTALLATION_GUIDE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
## Requirements
2626

2727
- PHP >= 7.2
28-
- required php extensions : `ext-curl`, `ext-gd`
28+
- required php extensions : `ext-json`, `ext-gd`
2929

3030
## Installation
3131

docs/USER_GUIDE.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Please note that first and foremost a CrowdSec agent must be installed on a serv
4242

4343
## Features
4444

45-
- CrowdSec LAPI Support (CAPI not supported yet)
45+
- CrowdSec LAPI Support
4646
- Handle IP, IP ranges and Country scoped decisions
4747
- Clear, prune and refresh the LAPI cache
4848
- `Live mode` or `Stream mode`
@@ -73,7 +73,8 @@ On the other hand, all texts are also fully customizable. This will allow you, f
7373

7474
### Create your own bouncer
7575

76-
You can use this library to develop your own PHP application bouncer.
76+
You can use this library to develop your own PHP application bouncer. Any custom bouncer should extend the
77+
[`AbstractBounce`](../src/AbstractBounce.php) class.
7778

7879
#### Quick start
7980

@@ -234,8 +235,7 @@ Here is the list of available settings:
234235
#### The `Standalone` example
235236

236237
This library includes the [`StandaloneBounce`](../src/StandaloneBounce.php) class. You can see that class as a good
237-
example for creating your own bouncer. This class extends [`AbstractBounce`](../src/AbstractBounce.php) and
238-
implements [IBounce](../src/IBounce.php). All bouncers should do the same. In order to add the bounce logic, you
238+
example for creating your own bouncer. This class extends [`AbstractBounce`](../src/AbstractBounce.php). All bouncers should do the same. In order to add the bounce logic, you
239239
should first instantiate your bouncer:
240240

241241
```php

0 commit comments

Comments
 (0)