Skip to content

Commit 227a153

Browse files
authored
Merge pull request #15 from cebe/codeception-tests
Added codeception for API testing
2 parents 01247be + 5459f0d commit 227a153

17 files changed

+160
-12
lines changed

Makefile

+18-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ default:
2626
## PHP runtime ##
2727

2828
# start PHP built-in webserver
29-
start: config/components-dev.local.php config/components-test.local.php backend/config/cookie-validation.key env.php
29+
start: config/components-dev.local.php backend/config/cookie-validation.key env.php
3030
@echo "Starting server for api"
3131
cd api && $(MAKE) start
3232
@echo "Starting server for backend"
@@ -45,7 +45,7 @@ bash: cli
4545
cli:
4646
$(DOCKER) bash
4747

48-
start-docker: docker-compose.override.yml runtime/build-docker config/components-dev.local.php config/components-test.local.php backend/config/cookie-validation.key env.php stop
48+
start-docker: docker-compose.override.yml runtime/build-docker config/components-dev.local.php backend/config/cookie-validation.key env.php stop
4949
docker-compose up -d
5050
docker-compose exec -T backend-php bash -c "grep '^$(shell whoami):' /etc/passwd || useradd -m '$(shell whoami)' --uid=$(shell id -u) -G www-data -s /bin/bash -d /app/runtime/home"
5151
docker-compose exec -T backend-php bash -c "sed -i 's/#force_color_prompt=yes/force_color_prompt=yes/' /app/runtime/home/.bashrc && sed -i 's~etc/bash_completion~etc/bash_completion.d/yii~' /app/runtime/home/.bashrc"
@@ -74,3 +74,19 @@ docker-compose.override.yml: docker-compose.override.dist.yml
7474
test -f $@ || cp $< $@
7575
backend/config/cookie-validation.key:
7676
test -s $@ || php -r 'echo bin2hex(random_bytes(20));' > $@
77+
78+
79+
## Docker Runtime Tests ##
80+
81+
test: tests/_data/dump.sql
82+
$(DOCKER) vendor/bin/codecept run
83+
84+
clean:
85+
rm -rf tests/_data/dump.sql
86+
87+
# generate database dump for test env
88+
tests/_data/dump.sql: $(shell find common/migrations -type f)
89+
$(DOCKER) sh -c 'YII_ENV=test ./yii migrate/fresh --interactive=0'
90+
$(DOCKER) sh -c 'mysqldump -h db-test -uapi_test -papisecret api_db_test > tests/_data/dump.sql'
91+
# for postgres you may use this command instead:
92+
#$(DOCKER) sh -c 'PGPASSWORD=apisecret pg_dump --schema-only --clean --if-exists -w -h db-test -U api_test -d api_db_test -f tests/_data/dump.sql'

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ You can now continue with [generating code](#generate-code).
7777

7878
> Note: If you don't have GNU make, you need to copy the configuration files as in the PHP directly section, then run:
7979
>
80+
> cp docker-compose.override.dist.yml docker-compose.override.yml
8081
> docker-compose up -d
8182
> docker-compose exec backend-php sh -c 'cd /app && composer install'
8283

backend/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM yiisoftware/yii2-php:7.4-apache
22

33
# install git and unzip for composer install dist/source
4-
RUN apt-get update && apt-get -y install git unzip && apt-get clean && rm -rf /var/lib/apt/lists/*
4+
RUN apt-get update && apt-get -y install git unzip mariadb-client && apt-get clean && rm -rf /var/lib/apt/lists/*
55

66
COPY config/apache.conf /etc/apache2/sites-enabled/000-default.conf

backend/config/components.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
'request' => [
99
'cookieValidationKey' => file_get_contents(__DIR__ . '/cookie-validation.key'),
1010
],
11-
11+
'errorHandler' => [
12+
'errorAction' => 'site/error',
13+
],
1214
'urlManager' => [
1315
'enablePrettyUrl' => true,
1416
'enableStrictParsing' => true,

backend/controllers/SiteController.php

+10
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@
33
namespace backend\controllers;
44

55
use yii\web\Controller;
6+
use yii\web\ErrorAction;
67

78
/**
89
*
910
*/
1011
class SiteController extends Controller
1112
{
13+
public function actions()
14+
{
15+
return array_merge(parent::actions(), [
16+
'error' => [
17+
'class' => ErrorAction::class,
18+
],
19+
]);
20+
}
21+
1222
public function actionIndex()
1323
{
1424
return $this->render('index');

codeception.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# suite config
2+
suites:
3+
api:
4+
actor: ApiTester
5+
path: api/
6+
bootstrap: 'bootstrap.php'
7+
modules:
8+
enabled:
9+
- Asserts
10+
- Db:
11+
dsn: mysql:host=db-test;dbname=api_db_test
12+
user: api_test
13+
password: apisecret
14+
# tests/_data/dump.sql is generated by Makefile: make tests/_data/dump.sql
15+
dump: '/app/tests/_data/dump.sql'
16+
populator: 'mysql -h $host -u$user -p$password $dbname < $dump'
17+
populate: true
18+
cleanup: true
19+
reconnect: true
20+
waitlock: 10
21+
# Yii2 module must be loaded after Db module to make sure fixtures are not overwritten by Db dump
22+
- Yii2:
23+
configFile: 'api/config/app-test.php'
24+
- REST:
25+
#url: http://api-php-test:80/
26+
depends: Yii2
27+
step_decorators:
28+
- \Codeception\Step\AsJson
29+
30+
paths:
31+
tests: tests
32+
output: tests/_output
33+
data: tests/_data
34+
support: tests/_support
35+
36+
settings:
37+
shuffle: false
38+
lint: true

composer.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
"cebe/yii2-openapi": "^2.0@beta",
1111
"yiisoft/yii2-gii": "^2.1"
1212
},
13+
"require-dev": {
14+
"codeception/codeception": "^4.2",
15+
"codeception/module-phpbrowser": "^1.0.0",
16+
"codeception/module-asserts": "^1.0.0",
17+
"codeception/module-rest": "^1.0.0",
18+
"codeception/module-yii2": "^1.1",
19+
"codeception/module-db": "^1.2"
20+
},
1321
"autoload": {
1422
"psr-4": {
1523
"api\\": "api/",
@@ -19,7 +27,9 @@
1927
}
2028
},
2129
"config": {
22-
"platform": {"php": "7.1.3"},
30+
"platform": {
31+
"php": "7.1.3"
32+
},
2333
"fxp-asset": {
2434
"enabled": false
2535
},

config/components-test.php

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
'class' => \yii\swiftmailer\Mailer::class,
1212
'useFileTransport' => true,
1313
],
14+
'db' => [
15+
'class' => yii\db\Connection::class,
16+
'dsn' => 'mysql:host=db-test;dbname=api_db_test', // for docker
17+
//'dsn' => 'mysql:host=localhost;dbname=api_db_test', // use this when mysql runs on your local host
18+
'username' => 'api_test',
19+
'password' => 'apisecret',
20+
'charset' => 'utf8',
21+
],
1422
],
1523

1624
file_exists($localConfig = __DIR__ . '/components-test.local.php') ? require $localConfig : []

config/env.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?php
22

33
if (!defined('YII_ENV')) {
4-
if (is_file($env = __DIR__ . '/../env.php')) {
4+
// allow setting YII ENV via environment, load it from local config if no env is set
5+
if (getenv('YII_ENV')) {
6+
define('YII_ENV', strtolower(getenv('YII_ENV')));
7+
define('YII_DEBUG', YII_ENV === 'dev' || YII_ENV === 'test');
8+
} elseif (is_file($env = __DIR__ . '/../env.php')) {
59
include($env);
610
} else {
711
define('YII_DEBUG', false);

docker-compose.yml

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ version: '2'
66
services:
77
api-php:
88
build: api
9-
ports:
10-
- '8337:80'
119
volumes:
1210
# Mount source-code for development
1311
- ./:/app
@@ -18,8 +16,6 @@ services:
1816

1917
backend-php:
2018
build: backend
21-
ports:
22-
- '8338:80'
2319
volumes:
2420
# Re-use local composer cache via host-volume
2521
- ~/.composer-docker/cache:/root/.composer/cache:delegated
@@ -38,10 +34,17 @@ services:
3834
- MARIADB_USER=api
3935
- MARIADB_PASSWORD=apisecret
4036

37+
db-test:
38+
image: mariadb:10.8
39+
environment:
40+
- MARIADB_ROOT_PASSWORD=verysecret
41+
- MARIADB_DATABASE=api_db_test
42+
- MARIADB_USER=api_test
43+
- MARIADB_PASSWORD=apisecret
44+
4145
redis:
4246
image: redis:3
4347

4448
mailcatcher:
4549
image: nisenabe/mailcatcher
46-
ports:
47-
- '8055:1080'
50+

tests/_data/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/dump.sql

tests/_output/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

tests/_support/ApiTester.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
/**
5+
* Inherited Methods
6+
* @method void wantToTest($text)
7+
* @method void wantTo($text)
8+
* @method void execute($callable)
9+
* @method void expectTo($prediction)
10+
* @method void expect($prediction)
11+
* @method void amGoingTo($argumentation)
12+
* @method void am($role)
13+
* @method void lookForwardTo($achieveValue)
14+
* @method void comment($description)
15+
* @method void pause()
16+
*
17+
* @SuppressWarnings(PHPMD)
18+
*/
19+
class ApiTester extends \Codeception\Actor
20+
{
21+
use _generated\ApiTesterActions;
22+
23+
/**
24+
* Define custom actions here
25+
*/
26+
}

tests/_support/Helper/Api.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Helper;
3+
4+
// here you can define custom actions
5+
// all public methods declared in helper class will be available in $I
6+
7+
class Api extends \Codeception\Module
8+
{
9+
10+
}

tests/_support/_generated/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

tests/api/ApiCest.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
class ApiCest
3+
{
4+
public function tryApi(ApiTester $I)
5+
{
6+
$I->sendGet('/');
7+
$I->seeResponseCodeIs(404);
8+
$I->seeResponseIsJson();
9+
}
10+
11+
// TODO add more tests
12+
}

tests/api/bootstrap.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require(__DIR__ . '/../../config/env.php');

0 commit comments

Comments
 (0)