Initial commit,修正

This commit is contained in:
2025-07-13 19:37:38 +08:00
parent 38ce288e27
commit 65bbd06981
12384 changed files with 3985464 additions and 8 deletions
+127
View File
@@ -0,0 +1,127 @@
This directory contains various tests for the basic application.
Tests in `codeception` directory are developed with [Codeception PHP Testing Framework](http://codeception.com/).
After creating the basic application, follow these steps to prepare for the tests:
1. Install Codeception if it's not yet installed:
```
composer global require "codeception/codeception=2.0.*"
composer global require "codeception/specify=*"
composer global require "codeception/verify=*"
```
If you've never used Composer for global packages run `composer global status`. It should output:
```
Changed current directory to <directory>
```
Then add `<directory>/vendor/bin` to you `PATH` environment variable. Now we're able to use `codecept` from command
line globally.
2. Install faker extension by running the following from template root directory where `composer.json` is:
```
composer require --dev "yiisoft/yii2-faker:*"
```
3. Create `yii2_basic_tests` database and update it by applying migrations (you may skip this step if you do not have created any migrations yet):
```
codeception/bin/yii migrate
```
The command needs to be run in the `tests` directory.
The database configuration can be found at `tests/codeception/config/config.php`.
4. Build the test suites:
```
codecept build
```
5. In order to be able to run acceptance tests you need to start a webserver. The simplest way is to use PHP built in
webserver. In the `web` directory execute the following:
```
php -S localhost:8080
```
6. Now you can run the tests with the following commands:
```
# run all available tests
codecept run
# run acceptance tests
codecept run acceptance
# run functional tests
codecept run functional
# run unit tests
codecept run unit
```
Fixtures Default Configuration
------------------------------
The `fixture` commands refer to the following `ActiveFixture` configuration by default:
- Fixtures path: `@tests/unit/fixtures`
- Fixtures data path: `@tests/unit/fixtures/data`
- Template files path: `@tests/unit/templates/fixtures`
- Namespace: `tests\unit\fixtures`
Where `@tests` refers to `@mdm/admin/tests/codeception`.
Code coverage support
---------------------
By default, code coverage is disabled in `codeception.yml` configuration file, you should uncomment needed rows to be able
to collect code coverage. You can run your tests and collect coverage with the following command:
```
#collect coverage for all tests
codecept run --coverage-html --coverage-xml
#collect coverage only for unit tests
codecept run unit --coverage-html --coverage-xml
#collect coverage for unit and functional tests
codecept run functional,unit --coverage-html --coverage-xml
```
You can see code coverage output under the `tests/_output` directory.
###Remote code coverage
When you run your tests not in the same process where code coverage is collected, then you should uncomment `remote` option and its
related options, to be able to collect code coverage correctly. To setup remote code coverage you should follow [instructions](http://codeception.com/docs/11-Codecoverage)
from codeception site.
1. install `Codeception c3` remote support `composer require "codeception/c3:*"`;
2. copy `c3.php` file under your `web` directory;
3. include `c3.php` file in your `index-test.php` file before application run, so it can catch needed requests.
4. edit `c3.php` to update config file path (~ line 55) with `$config_file = realpath(__DIR__ . '/../tests/codeception.yml');`
Configuration options that are used by remote code coverage:
- c3_url: url pointing to entry script that includes `c3.php` file, so `Codeception` will be able to produce code coverage;
- remote: whether to enable remote code coverage or not;
- remote_config: path to the `codeception.yml` configuration file, from the directory where `c3.php` file is located. This is needed
so that `Codeception` can create itself instance and collect code coverage correctly.
By default `c3_url` and `remote_config` setup correctly, you only need to copy and include `c3.php` file in your `index-test.php`
After that you should be able to collect code coverage from tests that run through `PhpBrowser` or `WebDriver` with same command
as for other tests:
```
#collect coverage from remote
codecept run acceptance --coverage-html --coverage-xml
```
Please refer to [Codeception tutorial](http://codeception.com/docs/01-Introduction) for
more details about writing and running acceptance, functional and unit tests.
+17
View File
@@ -0,0 +1,17 @@
actor: Tester
paths:
tests: codeception
log: codeception/_output
data: codeception/_data
helpers: codeception/_support
settings:
bootstrap: _bootstrap.php
suite_class: \PHPUnit_Framework_TestSuite
memory_limit: 1024M
log: true
colors: true
config:
# the entry script URL (with host info) for functional and acceptance tests
# PLEASE ADJUST IT TO THE ACTUAL ENTRY SCRIPT URL
test_entry_url: http://localhost:8080/index-test.php
+4
View File
@@ -0,0 +1,4 @@
# these files are auto generated by codeception build
/unit/UnitTester.php
/functional/FunctionalTester.php
/acceptance/AcceptanceTester.php
+16
View File
@@ -0,0 +1,16 @@
<?php
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', __DIR__ . '/../web/index-test.php');
require_once(__DIR__ . '/../../vendor/autoload.php');
require_once(__DIR__ . '/../../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';
Yii::setAlias('@tests', dirname(__DIR__));
@@ -0,0 +1,2 @@
*
!.gitignore
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace tests\codeception\_pages;
use yii\codeception\BasePage;
/**
* Represents create role or permission
* @property \AcceptanceTester|\FunctionalTester $actor
*/
class CreateItemPage extends BasePage
{
public $route = 'admin/role/create';
/**
* @param array $roleData
*/
public function submit(array $roleData)
{
$inputTypes = [
'name' => 'input',
'description' => 'textarea',
'ruleName' => 'select',
'data' => 'textarea',
];
foreach ($roleData as $field => $value) {
$inputType = isset($inputTypes[$field]) ? $inputTypes[$field] : 'input';
$this->actor->fillField($inputType . '[name="AuthItem[' . $field . ']"]', $value);
}
$this->actor->click('submit-button');
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace tests\codeception\_pages;
use yii\codeception\BasePage;
/**
* Represents about page
* @property \AcceptanceTester|\FunctionalTester $actor
*/
class RoutePage extends BasePage
{
public $route = 'admin/route/index';
/**
* @param array $route
*/
public function addRoute($route)
{
$this->actor->fillField('#inp-route', $route);
$this->actor->click('add-route');
}
}
+69
View File
@@ -0,0 +1,69 @@
<?php
namespace tests\codeception\_support;
use Codeception\Module;
use yii\test\FixtureTrait;
use tests\codeception\fixtures\AdminFixture;
/**
* This helper is used to populate the database with needed fixtures before any tests are run.
* In this example, the database is populated with the demo login user, which is used in acceptance
* and functional tests. All fixtures will be loaded before the suite is started and unloaded after it
* completes.
*/
class FixtureHelper extends Module
{
/**
* Redeclare visibility because codeception includes all public methods that do not start with "_"
* and are not excluded by module settings, in actor class.
*/
use FixtureTrait {
loadFixtures as public;
fixtures as public;
globalFixtures as public;
createFixtures as public;
unloadFixtures as protected;
getFixtures as protected;
getFixture as protected;
}
/**
* Method called before any suite tests run. Loads User fixture login user
* to use in acceptance and functional tests.
* @param array $settings
*/
public function _beforeSuite($settings = [])
{
$this->loadFixtures();
}
/**
* Method is called after all suite tests run
*/
public function _afterSuite()
{
$this->unloadFixtures();
}
/**
* @inheritdoc
*/
public function globalFixtures()
{
return [
AdminFixture::className(),
];
}
/**
* @inheritdoc
*/
public function fixtures()
{
return [
];
}
}
+28
View File
@@ -0,0 +1,28 @@
# 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:
- PhpBrowser
- tests\codeception\_support\FixtureHelper
# you can use WebDriver instead of PhpBrowser to test javascript and ajax.
# This will require you to install selenium. See http://codeception.com/docs/04-AcceptanceTests#Selenium
# "restart" option is used by the WebDriver to start each time per test-file new session and cookies,
# it is useful if you want to login in your app in each test.
# - WebDriver
config:
PhpBrowser:
# PLEASE ADJUST IT TO THE ACTUAL ENTRY POINT WITHOUT PATH INFO
url: http://localhost:8080
# WebDriver:
# url: http://localhost:8080
# browser: firefox
# restart: true
@@ -0,0 +1,31 @@
<?php
use tests\codeception\_pages\CreateItemPage;
/* @var $scenario Codeception\Scenario */
$I = new AcceptanceTester($scenario);
$I->wantTo('ensure that create role item work');
$createItemPage = CreateItemPage::openBy($I);
$I->see('Create Role', 'h1');
$I->amGoingTo('submit role form with no data');
$createItemPage->submit([]);
if (method_exists($I, 'wait')) {
$I->wait(3); // only for selenium
}
$I->expectTo('see validations errors');
$I->see('Create Role', 'h1');
$I->see('Name cannot be blank');
$I->amGoingTo('submit role form with correct data');
$createItemPage->submit([
'name' => 'roleTesterAcp',
'description' => 'Role created for test',
]);
if (method_exists($I, 'wait')) {
$I->wait(3); // only for selenium
}
$I->dontSeeElement('#item-form');
+10
View File
@@ -0,0 +1,10 @@
<?php
use tests\codeception\_pages\RoutePage;
/* @var $scenario Codeception\Scenario */
$I = new AcceptanceTester($scenario);
$I->wantTo('ensure that create route work');
RoutePage::openBy($I);
$I->see('Routes', 'h1');
+2
View File
@@ -0,0 +1,2 @@
<?php
new yii\web\Application(require(dirname(__DIR__) . '/config/acceptance.php'));
+10
View File
@@ -0,0 +1,10 @@
<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
defined('MDM_ADMIN_BASE_PATH') or define('MDM_ADMIN_BASE_PATH', dirname(dirname(dirname(__DIR__))));
require(MDM_ADMIN_BASE_PATH . '/vendor/autoload.php');
require(MDM_ADMIN_BASE_PATH . '/vendor/yiisoft/yii2/Yii.php');
Yii::setAlias('@tests', dirname(dirname(__DIR__)));
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env php
<?php
/**
* Yii console bootstrap file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
require_once __DIR__ . '/_bootstrap.php';
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../config/config.php'),
require(__DIR__ . '/../config/console.php')
);
$application = new yii\console\Application($config);
$exitCode = $application->run();
exit($exitCode);
+20
View File
@@ -0,0 +1,20 @@
@echo off
rem -------------------------------------------------------------
rem Yii command line bootstrap script for Windows.
rem
rem @author Qiang Xue <qiang.xue@gmail.com>
rem @link http://www.yiiframework.com/
rem @copyright Copyright (c) 2008 Yii Software LLC
rem @license http://www.yiiframework.com/license/
rem -------------------------------------------------------------
@setlocal
set YII_PATH=%~dp0
if "%PHP_COMMAND%" == "" set PHP_COMMAND=php.exe
"%PHP_COMMAND%" "%YII_PATH%yii" %*
@endlocal
@@ -0,0 +1,2 @@
# these files are auto generated by codeception build
/db-local.php
+11
View File
@@ -0,0 +1,11 @@
<?php
/**
* Application configuration for acceptance tests
*/
return yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/config.php'),
require(__DIR__ . '/web.php'),
[
]
);
+48
View File
@@ -0,0 +1,48 @@
<?php
/**
* Application configuration shared by all test types
*/
return [
'id' => 'mdm-admin-test',
'basePath' => dirname(dirname(__DIR__)), // @tests
'vendorPath' => dirname(dirname(dirname(__DIR__))) . '/vendor',
'language' => 'en-US',
'aliases' => [
'@mdm/admin' => dirname(dirname(dirname(__DIR__))),
],
'modules' => [
'admin' => [
'class' => 'mdm\admin\Module',
]
],
'controllerMap' => [
'fixture' => [
'class' => 'yii\console\controllers\FixtureController',
'namespace' => 'tests\codeception\fixtures',
],
],
'components' => [
'db' => require(__DIR__ . '/db.php'),
'mailer' => [
'useFileTransport' => true,
],
'urlManager' => [
'showScriptName' => true,
],
'authManager' => [
'class' => 'yii\rbac\DbManager'
],
'cache' => [
'class' => 'yii\caching\DummyCache',
],
'i18n' => [
'translations' => [
'rbac-admin' => [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '@mdm/admin/messages'
]
]
]
],
];
+5
View File
@@ -0,0 +1,5 @@
<?php
return[
'components' => [
]
];
+36
View File
@@ -0,0 +1,36 @@
<?php
/**
* This is the configuration file for the yii2-admin unit tests.
* You can override configuration values by creating a `db-local.php` file
* and manipulate the `$databases` variable.
* For example to change MySQL username and password your `db-local.php` should
* contain the following:
*
<?php
$driver = 'pgsql';
$databases[$driver]['username'] = 'myname';
$databases[$driver]['password'] = 'changeme';
*/
$databases = [
'mysql' => [
'dsn' => 'mysql:host=127.0.0.1;dbname=mdm_admin_test',
'username' => 'travis',
'password' => '',
],
'sqlite' => [
//'dsn' => 'sqlite::memory:',
'dsn' => 'sqlite:@runtime/mdm_admin_test.sqlite',
],
'pgsql' => [
'dsn' => 'pgsql:host=localhost;dbname=mdm_admin_test;port=5432;',
'username' => 'postgres',
'password' => 'postgres',
],
];
$driver = 'mysql';
if (is_file(__DIR__ . '/db-local.php')) {
include __DIR__ . '/db-local.php';
}
return array_merge(['class' => 'yii\db\Connection'], $databases[$driver]);
+25
View File
@@ -0,0 +1,25 @@
<?php
$_SERVER['SCRIPT_FILENAME'] = YII_TEST_ENTRY_FILE;
$_SERVER['SCRIPT_NAME'] = YII_TEST_ENTRY_URL;
/**
* Application configuration for functional tests
*/
return yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/config.php'),
require(__DIR__ . '/web.php'),
[
'components' => [
'request' => [
// it's not recommended to run functional tests with CSRF validation enabled
'enableCsrfValidation' => false,
// but if you absolutely need it set cookie domain to localhost
/*
'csrfCookie' => [
'domain' => 'localhost',
],
*/
],
],
]
);
+11
View File
@@ -0,0 +1,11 @@
<?php
/**
* Application configuration for unit tests
*/
return yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/config.php'),
require(__DIR__ . '/web.php'),
[
]
);
+8
View File
@@ -0,0 +1,8 @@
<?php
return[
'components' => [
'request' => [
'cookieValidationKey' => 'testme',
],
]
];
View File
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace tests\codeception\fixtures;
use Yii;
use yii\test\DbFixture;
/**
* Description of AdminFixture
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 2.5
*/
class AdminFixture extends DbFixture
{
public function load()
{
ob_start();
ob_implicit_flush(false);
include __DIR__ . '/data/admin.php';
ob_get_clean();
}
}
+58
View File
@@ -0,0 +1,58 @@
<?php
use mdm\admin\components\Configs;
/* @var $this tests\codeception\fixtures\AdminFixture */
$migrations = [
'm140506_102106_rbac_init' => '@yii/rbac/migrations',
'm140602_111327_create_menu_table' => '@mdm/admin/migrations',
'm160312_050000_create_user' => '@mdm/admin/migrations',
];
$command = $this->db->createCommand();
$schema = $this->db->schema;
// rbac
$authManager = Yii::$app->getAuthManager();
if ($this->db->driverName === 'mssql' || $this->db->driverName === 'sqlsrv' || $this->db->driverName === 'dblib') {
$sql = <<<SQL
IF EXISTS (
SELECT *
FROM sys.objects
WHERE [type] = 'TR' AND [name] = 'blablabla'
)
DROP dbo.trigger_auth_item_child;
GO
SQL;
$command->setSql($sql)->execute();
}
if ($schema->getTableSchema($authManager->assignmentTable)) {
$command->dropTable($authManager->assignmentTable)->execute();
}
if ($schema->getTableSchema($authManager->itemChildTable)) {
$command->dropTable($authManager->itemChildTable)->execute();
}
if ($schema->getTableSchema($authManager->itemTable)) {
$command->dropTable($authManager->itemTable)->execute();
}
if ($schema->getTableSchema($authManager->ruleTable)) {
$command->dropTable($authManager->ruleTable)->execute();
}
// mdm/admin
if ($schema->getTableSchema(Configs::menuTable())) {
$command->dropTable(Configs::menuTable())->execute();
}
if ($schema->getTableSchema(Configs::userTable())) {
$command->dropTable(Configs::userTable())->execute();
}
foreach ($migrations as $migration => $path) {
$file = Yii::getAlias($path . '/' . $migration . '.php');
include_once $file;
$migration = new $migration();
$migration->up();
}
+17
View File
@@ -0,0 +1,17 @@
# 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
- tests\codeception\_support\FixtureHelper
config:
Yii2:
configFile: 'codeception/config/functional.php'
@@ -0,0 +1,31 @@
<?php
use tests\codeception\_pages\CreateItemPage;
/* @var $scenario Codeception\Scenario */
$I = new FunctionalTester($scenario);
$I->wantTo('ensure that create item work');
$createItemPage = CreateItemPage::openBy($I);
$I->see('Create Role', 'h1');
$I->amGoingTo('submit contact form with no data');
$createItemPage->submit([]);
if (method_exists($I, 'wait')) {
$I->wait(3); // only for selenium
}
$I->expectTo('see validations errors');
$I->see('Create Role', 'h1');
$I->see('Name cannot be blank');
$I->amGoingTo('submit contact form with correct data');
$createItemPage->submit([
'name' => 'roleTester',
'description' => 'Role created for test',
]);
if (method_exists($I, 'wait')) {
$I->wait(3); // only for selenium
}
$I->dontSeeElement('#item-form');
+10
View File
@@ -0,0 +1,10 @@
<?php
use tests\codeception\_pages\RoutePage;
/* @var $scenario Codeception\Scenario */
$I = new FunctionalTester($scenario);
$I->wantTo('ensure that create route work');
RoutePage::openBy($I);
$I->see('Routes', 'h1');
+2
View File
@@ -0,0 +1,2 @@
<?php
new yii\web\Application(require(dirname(__DIR__) . '/config/functional.php'));
@@ -0,0 +1,2 @@
*
!.gitignore
+6
View File
@@ -0,0 +1,6 @@
# Codeception Test Suite Configuration
# suite for unit (internal) tests.
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.
class_name: UnitTester
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace tests\codeception\unit;
use tests\codeception\fixtures\AdminFixture;
/**
* Description of TestCase
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class TestCase extends \yii\codeception\TestCase
{
public $appConfig = '@tests/codeception/config/unit.php';
protected function setUp()
{
parent::setUp();
$this->loadFixtures();
}
public function fixtures()
{
return[
AdminFixture::className(),
];
}
}
+3
View File
@@ -0,0 +1,3 @@
<?php
// add unit testing specific bootstrap code here
@@ -0,0 +1,17 @@
<?php
namespace tests\codeception\unit\components;
/**
* Description of HelperTest
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class HelperTest extends \tests\codeception\unit\TestCase
{
public function testFilter()
{
$this->assertTrue(true);
}
}
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace tests\codeception\unit\models;
use Yii;
use tests\codeception\unit\TestCase;
use Codeception\Specify;
use mdm\admin\models\AuthItem;
class ItemTest extends TestCase
{
use Specify;
public function testAddNew()
{
$model = new AuthItem();
$model->attributes = [
'type' => 1,
];
// required
$this->assertFalse($model->validate());
$model = new AuthItem();
$model->attributes = [
'name' => 'Tester',
'type' => 1,
];
$this->assertTrue($model->validate());
$model->save();
$model = new AuthItem();
$model->attributes = [
'name' => 'Tester',
'type' => 1,
];
// not unique
$this->assertFalse($model->validate());
}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace tests\codeception\unit\models;
use tests\codeception\unit\TestCase;
/**
* Description of RouteTest
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 2.5
*/
class RouteTest extends TestCase
{
public function testGetRouteList()
{
$this->assertTrue(true);
}
}
+2
View File
@@ -0,0 +1,2 @@
*
!.gitignore
+36
View File
@@ -0,0 +1,36 @@
<?php
/* @var $this \yii\web\View */
/* @var $content string */
use yii\helpers\Html;
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
<meta charset="<?= Yii::$app->charset ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?= Html::csrfMetaTags() ?>
<title><?= Html::encode($this->title) ?></title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<div class="wrap">
<div class="container">
<?= $content ?>
</div>
</div>
<footer class="footer">
<div class="container">
<p class="pull-left">&copy; My Company <?= date('Y') ?></p>
<p class="pull-right"><?= Yii::powered() ?></p>
</div>
</footer>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
+2
View File
@@ -0,0 +1,2 @@
*
!.gitignore
+16
View File
@@ -0,0 +1,16 @@
<?php
// NOTE: Make sure this file is not accessible when deployed to production
if (!in_array(@$_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'])) {
die('You are not allowed to access this file.');
}
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../codeception/config/acceptance.php');
(new yii\web\Application($config))->run();