Skip to content

Added codeception for API testing #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ default:
## PHP runtime ##

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

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
start-docker: docker-compose.override.yml runtime/build-docker config/components-dev.local.php backend/config/cookie-validation.key env.php stop
docker-compose up -d
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"
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"
Expand Down Expand Up @@ -74,3 +74,19 @@ docker-compose.override.yml: docker-compose.override.dist.yml
test -f $@ || cp $< $@
backend/config/cookie-validation.key:
test -s $@ || php -r 'echo bin2hex(random_bytes(20));' > $@


## Docker Runtime Tests ##

test: tests/_data/dump.sql
$(DOCKER) vendor/bin/codecept run

clean:
rm -rf tests/_data/dump.sql

# generate database dump for test env
tests/_data/dump.sql: $(shell find common/migrations -type f)
$(DOCKER) sh -c 'YII_ENV=test ./yii migrate/fresh --interactive=0'
$(DOCKER) sh -c 'mysqldump -h db-test -uapi_test -papisecret api_db_test > tests/_data/dump.sql'
# for postgres you may use this command instead:
#$(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'
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ You can now continue with [generating code](#generate-code).

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

Expand Down
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM yiisoftware/yii2-php:7.4-apache

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

COPY config/apache.conf /etc/apache2/sites-enabled/000-default.conf
4 changes: 3 additions & 1 deletion backend/config/components.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
'request' => [
'cookieValidationKey' => file_get_contents(__DIR__ . '/cookie-validation.key'),
],

'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
Expand Down
10 changes: 10 additions & 0 deletions backend/controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
namespace backend\controllers;

use yii\web\Controller;
use yii\web\ErrorAction;

/**
*
*/
class SiteController extends Controller
{
public function actions()
{
return array_merge(parent::actions(), [
'error' => [
'class' => ErrorAction::class,
],
]);
}

public function actionIndex()
{
return $this->render('index');
Expand Down
38 changes: 38 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# suite config
suites:
api:
actor: ApiTester
path: api/
bootstrap: 'bootstrap.php'
modules:
enabled:
- Asserts
- Db:
dsn: mysql:host=db-test;dbname=api_db_test
user: api_test
password: apisecret
# tests/_data/dump.sql is generated by Makefile: make tests/_data/dump.sql
dump: '/app/tests/_data/dump.sql'
populator: 'mysql -h $host -u$user -p$password $dbname < $dump'
populate: true
cleanup: true
reconnect: true
waitlock: 10
# Yii2 module must be loaded after Db module to make sure fixtures are not overwritten by Db dump
- Yii2:
configFile: 'api/config/app-test.php'
- REST:
#url: http://api-php-test:80/
depends: Yii2
step_decorators:
- \Codeception\Step\AsJson

paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support

settings:
shuffle: false
lint: true
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
"cebe/yii2-openapi": "^2.0@beta",
"yiisoft/yii2-gii": "^2.1"
},
"require-dev": {
"codeception/codeception": "^4.2",
"codeception/module-phpbrowser": "^1.0.0",
"codeception/module-asserts": "^1.0.0",
"codeception/module-rest": "^1.0.0",
"codeception/module-yii2": "^1.1",
"codeception/module-db": "^1.2"
},
"autoload": {
"psr-4": {
"api\\": "api/",
Expand All @@ -19,7 +27,9 @@
}
},
"config": {
"platform": {"php": "7.1.3"},
"platform": {
"php": "7.1.3"
},
"fxp-asset": {
"enabled": false
},
Expand Down
8 changes: 8 additions & 0 deletions config/components-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
'class' => \yii\swiftmailer\Mailer::class,
'useFileTransport' => true,
],
'db' => [
'class' => yii\db\Connection::class,
'dsn' => 'mysql:host=db-test;dbname=api_db_test', // for docker
//'dsn' => 'mysql:host=localhost;dbname=api_db_test', // use this when mysql runs on your local host
'username' => 'api_test',
'password' => 'apisecret',
'charset' => 'utf8',
],
],

file_exists($localConfig = __DIR__ . '/components-test.local.php') ? require $localConfig : []
Expand Down
6 changes: 5 additions & 1 deletion config/env.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

if (!defined('YII_ENV')) {
if (is_file($env = __DIR__ . '/../env.php')) {
// allow setting YII ENV via environment, load it from local config if no env is set
if (getenv('YII_ENV')) {
define('YII_ENV', strtolower(getenv('YII_ENV')));
define('YII_DEBUG', YII_ENV === 'dev' || YII_ENV === 'test');
} elseif (is_file($env = __DIR__ . '/../env.php')) {
include($env);
} else {
define('YII_DEBUG', false);
Expand Down
15 changes: 9 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ version: '2'
services:
api-php:
build: api
ports:
- '8337:80'
volumes:
# Mount source-code for development
- ./:/app
Expand All @@ -18,8 +16,6 @@ services:

backend-php:
build: backend
ports:
- '8338:80'
volumes:
# Re-use local composer cache via host-volume
- ~/.composer-docker/cache:/root/.composer/cache:delegated
Expand All @@ -38,10 +34,17 @@ services:
- MARIADB_USER=api
- MARIADB_PASSWORD=apisecret

db-test:
image: mariadb:10.8
environment:
- MARIADB_ROOT_PASSWORD=verysecret
- MARIADB_DATABASE=api_db_test
- MARIADB_USER=api_test
- MARIADB_PASSWORD=apisecret

redis:
image: redis:3

mailcatcher:
image: nisenabe/mailcatcher
ports:
- '8055:1080'

1 change: 1 addition & 0 deletions tests/_data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dump.sql
2 changes: 2 additions & 0 deletions tests/_output/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
26 changes: 26 additions & 0 deletions tests/_support/ApiTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method void pause()
*
* @SuppressWarnings(PHPMD)
*/
class ApiTester extends \Codeception\Actor
{
use _generated\ApiTesterActions;

/**
* Define custom actions here
*/
}
10 changes: 10 additions & 0 deletions tests/_support/Helper/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Helper;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

class Api extends \Codeception\Module
{

}
2 changes: 2 additions & 0 deletions tests/_support/_generated/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
12 changes: 12 additions & 0 deletions tests/api/ApiCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
class ApiCest
{
public function tryApi(ApiTester $I)
{
$I->sendGet('/');
$I->seeResponseCodeIs(404);
$I->seeResponseIsJson();
}

// TODO add more tests
}
3 changes: 3 additions & 0 deletions tests/api/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require(__DIR__ . '/../../config/env.php');