Commit fc8d7bb7 authored by Tobias Munk's avatar Tobias Munk

added tests

parent e4e7fa48
Pipeline #1514 failed with stage
tests/_output
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_helpers
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 1024M
modules:
config:
Db:
dsn: ''
user: ''
password: ''
dump: tests/_data/dump.sql
config:
# the entry script URL (with host info) for functional tests
test_entry_url: http://WEB:80/index.php
\ No newline at end of file
## Testing giiant
### CLI
docker-compose -f test.yml up -d
docker-compose -f test.yml run appcli bash
cd vendor/schmunk42/yii2-giiant
codecept run cli -v
\ No newline at end of file
<?php
$basePath = '/app'; // dockerized Phundament application
require($basePath . '/vendor/autoload.php');
require($basePath . '/src/config/env.php');
// TODO: currently `dev` is required for giiant to get loaded, possible workaround, use local.php (host-mount)
/*if (getenv('YII_ENV') !== 'test') {
echo "Error: YII_ENV must be set to 'test'\n";
exit;
}*/
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
defined('YII_TEST_ENTRY_URL') or define(
'YII_TEST_ENTRY_URL', parse_url(
\Codeception\Configuration::config()['config']['test_entry_url'],
PHP_URL_PATH
)
);
defined('YII_TEST_ENTRY_FILE') or define('YII_TEST_ENTRY_FILE', dirname(dirname(__DIR__)) . '/web/index.php');
require_once($basePath . '/vendor/yiisoft/yii2/Yii.php');
$_SERVER['SCRIPT_FILENAME'] = YII_TEST_ENTRY_FILE;
$_SERVER['SCRIPT_NAME'] = YII_TEST_ENTRY_URL;
$_SERVER['SERVER_NAME'] = parse_url(\Codeception\Configuration::config()['config']['test_entry_url'], PHP_URL_HOST);
$_SERVER['SERVER_PORT'] = parse_url(
\Codeception\Configuration::config()['config']['test_entry_url'],
PHP_URL_PORT
) ?: '80';
#var_dump($_SERVER);
Yii::setAlias('@tests', dirname(__DIR__) . "/tests");
<?php
$appSrcPath = '/app/src';
/**
* Application configuration for acceptance tests
*/
return yii\helpers\ArrayHelper::merge(
require($appSrcPath . '/config/main.php'),
require(__DIR__ . '/config.php'),
[
'controllerNamespace' => 'app\controllers',
'language' => 'en',
]
);
<?php
/**
* Application configuration shared by all test types
*/
return [
'bootstrap' => [
'gii'
],
'modules' => [
'gii' => [
'class' => 'yii\gii\Module',
'allowedIPs' => '*'
]
],
];
<?php
/**
* Application configuration for unit tests
*/
$_SERVER['SCRIPT_FILENAME'] = YII_TEST_ENTRY_FILE;
$_SERVER['SCRIPT_NAME'] = YII_TEST_ENTRY_URL;
$basePath = '/app';
return yii\helpers\ArrayHelper::merge(
require($basePath . '/src/config/main.php'),
require(__DIR__ . '/config.php'),
[
'controllerNamespace' => 'app\controllers',
'components' => [
'request' => [
// it's not recommended to run functional tests with CSRF validation enabled
'enableCsrfValidation' => false,
'cookieValidationKey' => uniqid('TESTING-'),
// but if you absolutely need it set cookie domain to localhost
/*
'csrfCookie' => [
'domain' => 'localhost',
],
*/
],
],
]
);
<?php
return [
/*'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => getenv('DATABASE_DSN'),
'username' => getenv('DATABASE_USER'),
'password' => getenv('DATABASE_PASSWORD'),
'charset' => 'utf8',
'tablePrefix' => getenv('DATABASE_TABLE_PREFIX'),
],
],*/
/* 'modules' => [
'crud' => [
'class' => 'app\modules\crud\Module',
'layout' => '@admin-views/layouts/main',
],
]*/
];
\ No newline at end of file
<?php
/**
* Application configuration for unit tests
*/
$basePath = '/app';
return yii\helpers\ArrayHelper::merge(
require($basePath . '/src/config/main.php'),
require(__DIR__ . '/config.php'),
[
]
);
<?php
namespace Codeception\Module;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class AcceptanceHelper extends \Codeception\Module
{
}
<?php
namespace Codeception\Module;
// here you can define custom functions for CliGuy
class CliHelper extends \Codeception\Module
{
public function runShellCmd($command, $failNonZero = true)
{
$data = array();
#exec($command, $data, $resultCode);
exec("/app/yii 'migrate'", $data, $resultCode);
$this->output = implode("\n", $data);
if ($this->output === null) {
\PHPUnit_Framework_Assert::fail("$command can't be executed");
}
if ($resultCode !== 0 && $failNonZero) {
\PHPUnit_Framework_Assert::fail("Result code was $resultCode.\n\n" . $this->output);
}
$this->debug(preg_replace('~s/\e\[\d+(?>(;\d+)*)m//g~', '', $this->output));
}
}
<?php
namespace Codeception\Module;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class FunctionalHelper extends \Codeception\Module
{
}
<?php
namespace Codeception\Module;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class UnitHelper extends \Codeception\Module
{
}
<?php
namespace schmunk42\giiant\tests\codeception\_pages;
use yii\codeception\BasePage;
/**
* Represents login page
* @property \AcceptanceTester|\FunctionalTester $actor
*/
class LoginPage extends BasePage
{
public $route = 'user/security/login';
/**
* @param string $username
* @param string $password
*/
public function login($username, $password)
{
$this->actor->fillField('input[name="login-form[login]"]', $username);
$this->actor->fillField('input[name="login-form[password]"]', $password);
$this->actor->click('Sign in');
if (method_exists($this->actor, 'waitForElement')) {
$this->actor->waitForElement('#link-logout', 5);
}
}
}
# Codeception Test Suite Configuration
# suite for acceptance tests.
# perform tests in browser using the Selenium-like tools.
# powered by Mink (http://mink.behat.org).
# (tip: that's what your customer will see).
# (tip: test your ajax and javascript by one of Mink drivers).
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.
class_name: AcceptanceTester
modules:
enabled:
- WebDriver
config:
WebDriver:
host: HUB
url: http://WEB:80/
browser: firefox
port: 4444
window_size: 1024x768
restart: true
env:
chrome:
modules:
config:
WebDriver:
browser: 'chrome'
host: HUB
window_size: 1280x720
firefox:
# nothing changed
\ No newline at end of file
This diff is collapsed.
<?php
use schmunk42\giiant\tests\_pages\LoginPage;
#new schmunk42\giiant\tests\_pages\LoginPage;
$I = new AcceptanceTester($scenario);
$I->wantTo('ensure that crud works with access rules');
$I->amOnPage('/crud/actor');
$I->dontSee('Actor', 'h2');
$I->makeScreenshot('crud-actor-login');
$I->see('Sign in', 'h3');
$I->amGoingTo('try to login with correct credentials');
// TODO: use LoginPage
$username = 'admin';
$password = 'admin';
$I->fillField('input[name="login-form[login]"]', $username);
$I->fillField('input[name="login-form[password]"]', $password);
$I->click('Sign in');
$I->waitForElement('#link-logout', 5);
#LoginPage::openBy($I);
#$loginPage = LoginPage::openBy($I);
#$loginPage->login('admin', 'admin');
$I->see('Actor', 'h2');
$I->makeScreenshot('crud-actor');
\ No newline at end of file
<?php
new yii\web\Application(require(dirname(__DIR__) . '/_config/acceptance.php'));
# Codeception Test Suite Configuration
# suite for acceptance tests.
# perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.
class_name: CliTester
modules:
enabled:
- Filesystem
- Cli
- CliHelper
config:
PhpBrowser:
url: 'http://localhost/myapp/'
<?php
$I = new CliTester($scenario);
$I->runShellCommand('/app/yii');
$I->seeInShellOutput('This is Yii version 2.0.');
<?php
$I = new CliTester($scenario);
$I->runShellCommand("/app/yii help gii");
$I->seeInShellOutput('DESCRIPTION');
$I->seeInShellOutput('SUB-COMMANDS');
$I->seeInShellOutput('gii/');
$I->runShellCommand("/app/yii help gii/giiant-crud");
$I->seeInShellOutput('DESCRIPTION');
$I->seeInShellOutput('yii gii/giiant-crud');
<?php
$I = new CliTester($scenario);
// prepare output folders
$I->runShellCommand('mkdir -p /app/src/modules/crud/controllers');
$I->runShellCommand('mkdir -p /app/src/modules/crud/models/search');
// model & crud command
$batch = <<<'CMD'
/app/yii giiant-batch \
--interactive=0 \
--overwrite=1 \
--modelDb=db \
--modelBaseClass=yii\\db\\ActiveRecord \
--modelNamespace=app\\\models \
--crudControllerNamespace=app\\modules\\crud\\controllers \
--crudSearchModelNamespace=app\\modules\\crud\\models\\search \
--crudViewPath=@app/modules/crud/views \
--crudPathPrefix= \
--crudSkipRelations=Variant,Variants \
--crudProviders=schmunk42\\giiant\\crud\\providers\\optsProvider \
--tables=actor,film,film_actor,language,film_category,category,inventory,store,rental,payment,customer,staff,address,city,country
CMD;
$I->runShellCommand($batch);
// assertions
$I->dontSeeInShellOutput('Please fix the following errors');
$I->dontSeeInShellOutput('ErrorException');
$I->seeInShellOutput('The following files will be generated');
$I->seeFileFound('/app/src/modules/crud/controllers/ActorController.php');
\ No newline at end of file
<?php
$I = new CliTester($scenario);
// prepare output folders
$I->runShellCommand('mkdir -p /app/src/controllers/crud');
$I->runShellCommand('mkdir -p /app/src/models/crud/search');
// model & crud command
$batch = <<<'CMD'
/app/yii giiant-batch \
--interactive=0 \
--overwrite=1 \
--modelDb=db \
--modelBaseClass=yii\\db\\ActiveRecord \
--modelNamespace=app\\\models \
--crudControllerNamespace=app\\controllers\\crud \
--crudSearchModelNamespace=app\\models\\crud\\search \
--crudViewPath=@app/views/crud \
--crudPathPrefix=crud/ \
--crudSkipRelations=Variant,Variants \
--crudProviders=schmunk42\\giiant\\crud\\providers\\optsProvider \
--tables=actor,film,film_actor,language,film_category,category,inventory,store,rental,payment,customer,staff,address,city,country
CMD;
$I->runShellCommand($batch);
// assertions
$I->dontSeeInShellOutput('Please fix the following errors');
$I->dontSeeInShellOutput('ErrorException');
$I->seeInShellOutput('The following files will be generated');
$I->seeFileFound('/app/src/modules/crud/controllers/ActorController.php');
\ No newline at end of file
This diff is collapsed.
<?php
#$config = require(dirname(__DIR__) . '/_config/cli.php');
#$_app = new yii\console\Application($config);
# Phundament 4 - Potemkin Testing Stack
# =====================================
# Note: Wait until `appcli` container has completed its startup.
# Spawn a new project locally
#
# docker-compose run appsrc cp -r /phundament-src/src /mnt
# docker-compose up -d
# docker-compose run appcli ../yii
# Application services
# --------------------
# container to run one-off commands - a dockerized shell
appcli:
command: 'sh /app/src/run.sh'
# should be replaced with: image: <project_name>_appsrc
image: phundament/app:4.0.0-rc1-dev
links:
- 'appnginx:WEB'
- 'mariadb:DB'
- 'seleniumhub:HUB'
volumes:
- ../:/app/vendor/schmunk42/yii2-giiant
- ../tests/_config/local.php:/app/src/config/local.php
volumes_from:
- appsrc
environment:
DB_ENV_MYSQL_DATABASE: sakila
APP_MIGRATION_LOOKUP: '@app/migrations'
appsrc:
image: phundament/app:4.0.0-rc1-dev
volumes:
- ../:/app/vendor/schmunk42/yii2-giiant
- ../tests/_config/local.php:/app/src/config/local.php
- '/app'
command: 'tail -f /dev/null'
# The php-fpm server for interpreting php code
appfpm:
environment:
DB_ENV_MYSQL_DATABASE: sakila
image: phundament/php:5.6-fpm-4.0.0-rc1-dev
# host-volume for local development
volumes:
- ../:/app/vendor/schmunk42/yii2-giiant
- ../tests/_config/local.php:/app/src/config/local.php
volumes_from:
- appsrc
links:
- 'mariadb:DB'
# The nginx server for serving static files directly
appnginx:
image: schmunk42/nginx:1.7
links:
- 'appfpm:PHPFPM'
# host-volume for local development
volumes:
- ../:/app/vendor/schmunk42/yii2-giiant
volumes_from:
- appsrc
ports:
- '80'
environment:
VIRTUAL_HOST: '~^test\.giiant\.'
# Data Services
# -------------
mariadb:
image: schmunk42/mariadb-example-databases
ports:
- '3306'
environment:
MARIADB_PASS: veryveryverysecretadmin
#redis:
# image: redis:3.0
# ports:
# - 6379
# Testing Services
# ----------------
seleniumchrome:
image: 'selenium/node-chrome-debug:2.46.0'
links:
- 'appnginx:WEB'
- 'seleniumhub:HUB'
ports:
- '5900'
seleniumfirefox:
image: 'selenium/node-firefox-debug:2.46.0'
links:
- 'appnginx:WEB'
- 'seleniumhub:HUB'
ports:
- '5900'
seleniumhub:
image: 'selenium/hub:2.46.0'
ports:
- '4444'
# Codeception Test Suite Configuration
# suite for functional (integration) tests.
# emulate web requests and make application process them.
# (tip: better to use with frameworks).
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.
#basic/web/index.php
class_name: FunctionalTester
modules:
enabled:
- Filesystem
- Yii2
config:
Yii2:
configFile: 'tests/_config/functional.php'
<?php
use schmunk42\giiant\tests\_pages\LoginPage;
$I = new FunctionalTester($scenario);
$I->wantTo('ensure that crud works with access rules');
$I->amOnPage('/crud/actor');
$I->dontSee('Actor', 'h2');
$I->see('Sign in', 'h3');
$I->amGoingTo('try to login with correct credentials');
// TODO: use LoginPage class
$username = 'admin';
$password = 'admin';
$I->fillField('input[name="login-form[login]"]', $username);
$I->fillField('input[name="login-form[password]"]', $password);
$I->click('Sign in');
$I->see('Actor', 'h2');
This diff is collapsed.
<?php
$config = require(dirname(__DIR__) . '/_config/functional.php');
new yii\web\Application($config);
#!/usr/bin/env bash
docker-compose kill
docker-compose rm
docker-compose up -d
\ No newline at end of file
#!/usr/bin/env bash
docker-compose run --rm appcli \
codecept run -c vendor/schmunk42/yii2-giiant unit,cli,functional,acceptance
# Codeception Test Suite Configuration
# suite for unit (internal) tests.
class_name: UnitTester
modules:
enabled: [Asserts, UnitHelper]
<?php
namespace tests\codeception\unit\models;
use Codeception\Specify;
use schmunk42\giiant\model\Generator as ModelGenerator;
use Yii;
use yii\codeception\TestCase;
class BasicTest extends TestCase
{
use Specify;
public $appConfig = '@tests/_config/unit.php';
protected function setUp()
{
$this->appConfig = Yii::getAlias($this->appConfig);
parent::setUp();
}
protected function tearDown()
{
parent::tearDown();
}
public function testPrefixesGenerator()
{
$generator = new ModelGenerator();
$generator->template = 'default';
$generator->tableName = 'sakila.*';
$files = $generator->generate();
if (version_compare(str_replace('-dev', '', Yii::getVersion()), '2.0.4', '<')) {
$this->markTestSkipped('This feature is only available since Yii 2.0.4.');
}
# TODO: review "52"
$this->assertEquals(51, count($files));
$this->assertEquals("Actor", basename($files[0]->path, '.php'));
$this->assertEquals("ActorInfo", basename($files[1]->path, '.php'));
}
}
<?php //[STAMP] 090c14362f4b219e3a4bc7246b5bb911
// This class was automatically generated by build task
// You should not change it manually as it will be overwritten on next build
// @codingStandardsIgnoreFile
use Codeception\Module\Asserts;
/**
* 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 haveFriend($name, $actorClass = null)
*
* @SuppressWarnings(PHPMD)
*/
class UnitTester extends \Codeception\Actor
{
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that two variables are equal.
*
* @param $expected
* @param $actual
* @param string $message
*
* @return mixed
* @see \Codeception\Module\Asserts::assertEquals()
*/
public function assertEquals($expected, $actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertEquals', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that two variables are not equal
*
* @param $expected
* @param $actual
* @param string $message
*
* @see \Codeception\Module\Asserts::assertNotEquals()
*/
public function assertNotEquals($expected, $actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertNotEquals', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that two variables are same
*
* @param $expected
* @param $actual
* @param string $message
*
* @return mixed
* @see \Codeception\Module\Asserts::assertSame()
*/
public function assertSame($expected, $actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertSame', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that two variables are not same
*
* @param $expected
* @param $actual
* @param string $message
*
* @see \Codeception\Module\Asserts::assertNotSame()
*/
public function assertNotSame($expected, $actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertNotSame', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that actual is greater than expected
*
* @param $expected
* @param $actual
* @param string $message
*
* @see \Codeception\Module\Asserts::assertGreaterThan()
*/
public function assertGreaterThan($expected, $actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertGreaterThan', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @deprecated
* @see \Codeception\Module\Asserts::assertGreaterThen()
*/
public function assertGreaterThen($expected, $actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertGreaterThen', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that actual is greater or equal than expected
*
* @param $expected
* @param $actual
* @param string $message
*
* @see \Codeception\Module\Asserts::assertGreaterThanOrEqual()
*/
public function assertGreaterThanOrEqual($expected, $actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertGreaterThanOrEqual', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @deprecated
* @see \Codeception\Module\Asserts::assertGreaterThenOrEqual()
*/
public function assertGreaterThenOrEqual($expected, $actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertGreaterThenOrEqual', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that actual is less than expected
*
* @param $expected
* @param $actual
* @param string $message
*
* @see \Codeception\Module\Asserts::assertLessThan()
*/
public function assertLessThan($expected, $actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertLessThan', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that actual is less or equal than expected
*
* @param $expected
* @param $actual
* @param string $message
*
* @see \Codeception\Module\Asserts::assertLessThanOrEqual()
*/
public function assertLessThanOrEqual($expected, $actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertLessThanOrEqual', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that haystack contains needle
*
* @param $needle
* @param $haystack
* @param string $message
*
* @see \Codeception\Module\Asserts::assertContains()
*/
public function assertContains($needle, $haystack, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertContains', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that haystack doesn't contain needle.
*
* @param $needle
* @param $haystack
* @param string $message
*
* @see \Codeception\Module\Asserts::assertNotContains()
*/
public function assertNotContains($needle, $haystack, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertNotContains', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that variable is empty.
*
* @param $actual
* @param string $message
*
* @see \Codeception\Module\Asserts::assertEmpty()
*/
public function assertEmpty($actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertEmpty', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that variable is not empty.
*
* @param $actual
* @param string $message
*
* @see \Codeception\Module\Asserts::assertNotEmpty()
*/
public function assertNotEmpty($actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertNotEmpty', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that variable is NULL
*
* @param $actual
* @param string $message
*
* @see \Codeception\Module\Asserts::assertNull()
*/
public function assertNull($actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertNull', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that variable is not NULL
*
* @param $actual
* @param string $message
*
* @see \Codeception\Module\Asserts::assertNotNull()
*/
public function assertNotNull($actual, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertNotNull', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that condition is positive.
*
* @param $condition
* @param string $message
*
* @see \Codeception\Module\Asserts::assertTrue()
*/
public function assertTrue($condition, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertTrue', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that condition is negative.
*
* @param $condition
* @param string $message
*
* @see \Codeception\Module\Asserts::assertFalse()
*/
public function assertFalse($condition, $message = null)
{
return $this->scenario->runStep(new \Codeception\Step\Action('assertFalse', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Fails the test with message.
*
* @param $message
*
* @see \Codeception\Module\Asserts::fail()
*/
public function fail($message)
{
return $this->scenario->runStep(new \Codeception\Step\Action('fail', func_get_args()));
}
}
<?php
// Here you can initialize variables that will be available to your tests
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment