diff --git a/Makefile b/Makefile index 1d0d368..b31f823 100644 --- a/Makefile +++ b/Makefile @@ -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" @@ -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" @@ -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' diff --git a/README.md b/README.md index 799773c..54c29e1 100644 --- a/README.md +++ b/README.md @@ -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' diff --git a/backend/Dockerfile b/backend/Dockerfile index d8394e5..0b3eac1 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -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 diff --git a/backend/config/components.php b/backend/config/components.php index c3e510b..da51755 100644 --- a/backend/config/components.php +++ b/backend/config/components.php @@ -8,7 +8,9 @@ 'request' => [ 'cookieValidationKey' => file_get_contents(__DIR__ . '/cookie-validation.key'), ], - + 'errorHandler' => [ + 'errorAction' => 'site/error', + ], 'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => true, diff --git a/backend/controllers/SiteController.php b/backend/controllers/SiteController.php index bcd4d87..77fa28d 100644 --- a/backend/controllers/SiteController.php +++ b/backend/controllers/SiteController.php @@ -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'); diff --git a/codeception.yml b/codeception.yml new file mode 100644 index 0000000..e52ee21 --- /dev/null +++ b/codeception.yml @@ -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 \ No newline at end of file diff --git a/composer.json b/composer.json index e214961..b2394fb 100644 --- a/composer.json +++ b/composer.json @@ -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/", @@ -19,7 +27,9 @@ } }, "config": { - "platform": {"php": "7.1.3"}, + "platform": { + "php": "7.1.3" + }, "fxp-asset": { "enabled": false }, diff --git a/config/components-test.php b/config/components-test.php index b2eb8f2..3df93f6 100644 --- a/config/components-test.php +++ b/config/components-test.php @@ -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 : [] diff --git a/config/env.php b/config/env.php index 61c6ed0..d53ac6b 100644 --- a/config/env.php +++ b/config/env.php @@ -1,7 +1,11 @@ sendGet('/'); + $I->seeResponseCodeIs(404); + $I->seeResponseIsJson(); + } + + // TODO add more tests +} diff --git a/tests/api/bootstrap.php b/tests/api/bootstrap.php new file mode 100644 index 0000000..b55fa3b --- /dev/null +++ b/tests/api/bootstrap.php @@ -0,0 +1,3 @@ +