Skip to content

Commit

Permalink
Integrate Slim v3 and Codeception v4 (v1.0.0)
Browse files Browse the repository at this point in the history
* Init library
* Tests with coverage
* Integration with phpstan
* Integration with php-cs-fixer
* Added CI workflow
* Added static analysis workflow
  • Loading branch information
szhajdu authored Oct 13, 2020
1 parent 4d804c0 commit 8b41792
Show file tree
Hide file tree
Showing 18 changed files with 857 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# EditorConfig is awesome: https://editorconfig.org/

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending for every file
# Indent with 4 spaces
[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

[Makefile]
indent_style = tab

[*.yml]
indent_size = 2
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI

on: [ push, pull_request ]

jobs:
build:
name: Build
runs-on: ubuntu-latest
strategy:
max-parallel: 10
matrix:
php: [ '7.2', '7.3', '7.4' ]

steps:
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
tools: composer:v2

- name: Checkout code
uses: actions/checkout@v2

- name: Download dependencies
run: composer update --no-interaction --prefer-dist

- name: Run tests
run: make test
32 changes: 32 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Static analysis

on: [ push, pull_request ]

jobs:
phpstan:
name: PHPStan
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: PHPStan
uses: docker://oskarstark/phpstan-ga:0.12.41
env:
REQUIRE_DEV: true
with:
args: analyze --no-progress

php-cs-fixer:
name: PHP-CS-Fixer
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga:2.16.4
with:
args: --dry-run --diff-format udiff
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Files
.php_cs
.php_cs.cache
/composer.lock
/phpstan.neon
!/test/output/.gitkeep

# Directories
/test/output/*
/test/support/_generated
/vendor
66 changes: 66 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules(
[
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => [
'default' => 'align_single_space_minimal',
],
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'return', 'try'],
],
'braces' => [
'allow_single_line_closure' => true,
],
'class_attributes_separation' => [
'elements' => ['const', 'property', 'method'],
],
'declare_strict_types' => true,
'no_alternative_syntax' => true,
'no_leading_import_slash' => true,
'no_multiline_whitespace_before_semicolons' => true,
'no_short_echo_tag' => true,
'no_spaces_inside_parenthesis' => true,
'no_useless_else' => true,
'not_operator_with_space' => false,
'not_operator_with_successor_space' => false,
'ordered_imports' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_align' => [
'align' => 'vertical',
],
'phpdoc_indent' => true,
'phpdoc_no_package' => true,
'phpdoc_order' => true,
'phpdoc_scalar' => [
'types' => ['boolean', 'double', 'integer', 'real', 'str'],
],
'phpdoc_separation' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'phpdoc_var_without_name' => true,
'return_type_declaration' => [
'space_before' => 'none',
],
'single_quote' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline_array' => true,
'trim_array_spaces' => true,
'single_line_after_imports' => true,
'unary_operator_spaces' => true,
'visibility_required' => true,
'yoda_style' => false,
]
)
->setIndent(' ')
->setUsingCache(false)
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/test')
->name('*.php')
);
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " test to perform tests."
@echo " coverage to perform tests with code coverage."
@echo " static to run phpstan and php-cs-fixer check."
@echo " static-phpstan to run phpstan."
@echo " static-cs-check to run php-cs-fixer."
@echo " static-cs-fix to run php-cs-fixer, writing the changes."

.PHONY: test
test:
vendor/bin/codecept build
vendor/bin/codecept run

.PHONY: coverage
coverage:
vendor/bin/codecept build
vendor/bin/codecept run --coverage --coverage-xml --coverage-html

.PHONY: static
static: static-phpstan static-cs-check

static-phpstan:
docker run --rm -it -e REQUIRE_DEV=true -v ${PWD}:/app -w /app oskarstark/phpstan-ga:0.12.41 analyze $(PHPSTAN_PARAMS)

static-cs-fix:
docker run --rm -it -v ${PWD}:/app -w /app oskarstark/php-cs-fixer-ga:2.16.4 --diff-format udiff $(CS_PARAMS)

static-cs-check:
$(MAKE) static-cs-fix CS_PARAMS="--dry-run"
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# DoclerLabs - Codeception Slim Module

[![Build Status](https://img.shields.io/github/workflow/status/DoclerLabs/codeception-slim-module/CI?label=build&style=flat-square)](https://github.com/DoclerLabs/codeception-slim-module/actions?query=workflow%3ACI)
[![PHPStan Level](https://img.shields.io/badge/PHPStan-level%208-brightgreen.svg?style=flat-square)](https://img.shields.io/badge/PHPStan-level%208-brightgreen.svg)

This module allows you to run functional tests inside [Slim 3 Microframework](http://www.slimframework.com/docs/v3/) without HTTP calls,
so tests will be much faster and debug could be easier.

Inspiration comes from [herloct/codeception-slim-module](https://github.com/herloct/codeception-slim-module) library.

## Install

### Minimal requirements
- php: `^7.2`
- slim/slim: `^3.1`
- codeception/codeception: `^4.0`

If you don't know Codeception, please check [Quickstart Guide](https://codeception.com/quickstart) first.

If you already have [Codeception](https://github.com/Codeception/Codeception) installed in your Slim application,
you can add codeception-slim-module with a single composer command.

```shell
composer require --dev docler-labs/codeception-slim-module
```

### Configuration

**Example (`test/suite/functional.suite.yml`)**
```yaml
modules:
enabled:
- DoclerLabs\CodeceptionSlimModule\Module\Slim:
application: path/to/application.php
- REST:
depends: DoclerLabs\CodeceptionSlimModule\Module\Slim
```
The `application` property is a relative path to file which returns your `Slim\App` instance.
Here is the minimum `application.php` content:

```php
require __DIR__ . '/vendor/autoload.php';
use Slim\App;
$app = new App();
// Add routes and middlewares here.
return $app;
```

## Testing your API endpoints

```php
class UserCest
{
public function getUserReturnsWithEmail(FunctionalTester $I): void
{
$I->haveHttpHeader('Content-Type', 'application/json');
$I->sendGET('/users/John');
$I->seeResponseCodeIs(200);
$I->seeResponseContainsJson(
[
'email' => 'john.doe@example.com',
]
);
}
}
```
15 changes: 15 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace: DoclerLabs\CodeceptionSlimModule\Test
actor: Tester
paths:
tests: test/suite
log: test/output
output: test/output
data: test/data
support: test/support
extensions:
enabled:
- Codeception\Extension\RunFailed
coverage:
enabled: true
include:
- src/*php
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "docler-labs/codeception-slim-module",
"description": "Codeception Module for Slim framework.",
"keywords": [
"codeception",
"module",
"slim"
],
"type": "library",
"license": "MIT",
"require": {
"php": "^7.2",
"codeception/codeception": "^4.0",
"codeception/lib-innerbrowser": "^1.0",
"slim/slim": "^3.1"
},
"require-dev": {
"ext-json": "*",
"codeception/module-asserts": "^1.2",
"codeception/module-rest": "^1.2"
},
"autoload": {
"psr-4": {
"DoclerLabs\\CodeceptionSlimModule\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"DoclerLabs\\CodeceptionSlimModule\\Test\\": "test/support",
"DoclerLabs\\CodeceptionSlimModule\\Test\\Functional\\": "test/suite/functional"
}
},
"config": {
"sort-packages": true
}
}
7 changes: 7 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: max
checkMissingIterableValueType: false
paths:
- src
fileExtensions:
- php
Loading

0 comments on commit 8b41792

Please sign in to comment.