mirror of
https://github.com/yiisoft/yii2.git
synced 2026-03-04 14:34:49 +01:00
* php-cs-fixer: PSR2 rule. * php-cs-fixer: PSR2 rule - fix views. * Travis setup refactoring. * Add php-cs-fixer to travis cs tests. * Fix tests on hhvm-3.12 * improve travis config * composer update * revert composer update * improve travis config * Fix CS. * Extract config to separate classes. * Extract config to separate classes. * Add file header. * Force short array syntax. * binary_operator_spaces fixer * Fix broken tests * cast_spaces fixer * concat_space fixer * dir_constant fixer * ereg_to_preg fixer * function_typehint_space fixer * hash_to_slash_comment fixer * is_null fixer * linebreak_after_opening_tag fixer * lowercase_cast fixer * magic_constant_casing fixer * modernize_types_casting fixer * native_function_casing fixer * new_with_braces fixer * no_alias_functions fixer * no_blank_lines_after_class_opening fixer * no_blank_lines_after_phpdoc fixer * no_empty_comment fixer * no_empty_phpdoc fixer * no_empty_statement fixer * no_extra_consecutive_blank_lines fixer * no_leading_import_slash fixer * no_leading_namespace_whitespace fixer * no_mixed_echo_print fixer * no_multiline_whitespace_around_double_arrow fixer * no_multiline_whitespace_before_semicolons fixer * no_php4_constructor fixer * no_short_bool_cast fixer * no_singleline_whitespace_before_semicolons fixer * no_spaces_around_offset fixer * no_trailing_comma_in_list_call fixer * no_trailing_comma_in_singleline_array fixer * no_unneeded_control_parentheses fixer * no_unused_imports fixer * no_useless_return fixer * no_whitespace_before_comma_in_array fixer * no_whitespace_in_blank_line fixer * not_operator_with_successor_space fixer * object_operator_without_whitespace fixer * ordered_imports fixer * php_unit_construct fixer * php_unit_dedicate_assert fixer * php_unit_fqcn_annotation fixer * phpdoc_indent fixer * phpdoc_no_access fixer * phpdoc_no_empty_return fixer * phpdoc_no_package fixer * phpdoc_no_useless_inheritdoc fixer * Fix broken tests * phpdoc_return_self_reference fixer * phpdoc_single_line_var_spacing fixer * phpdoc_single_line_var_spacing fixer * phpdoc_to_comment fixer * phpdoc_trim fixer * phpdoc_var_without_name fixer * psr4 fixer * self_accessor fixer * short_scalar_cast fixer * single_blank_line_before_namespace fixer * single_quote fixer * standardize_not_equals fixer * ternary_operator_spaces fixer * trailing_comma_in_multiline_array fixer * trim_array_spaces fixer * protected_to_private fixer * unary_operator_spaces fixer * whitespace_after_comma_in_array fixer * `parent::setRules()` -> `$this->setRules()` * blank_line_after_opening_tag fixer * Update finder config. * Revert changes for YiiRequirementChecker. * Fix array formatting. * Add missing import. * Fix CS for new code merged from master. * Fix some indentation issues.
381 lines
11 KiB
PHP
381 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* @link http://www.yiiframework.com/
|
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
|
* @license http://www.yiiframework.com/license/
|
|
*/
|
|
|
|
namespace yiiunit\framework\db;
|
|
|
|
use yii\base\Event;
|
|
use yii\db\ActiveQuery;
|
|
use yii\db\Connection;
|
|
use yii\db\QueryBuilder;
|
|
use yiiunit\data\ar\ActiveRecord;
|
|
use yiiunit\data\ar\Customer;
|
|
use yiiunit\data\ar\Profile;
|
|
|
|
/**
|
|
* Class ActiveQueryTest the base class for testing ActiveQuery
|
|
*/
|
|
abstract class ActiveQueryTest extends DatabaseTestCase
|
|
{
|
|
public function setUp()
|
|
{
|
|
parent::setUp();
|
|
ActiveRecord::$db = $this->getConnection();
|
|
}
|
|
|
|
public function testConstructor()
|
|
{
|
|
$config = [
|
|
'on' => ['a' => 'b'],
|
|
'joinWith' => ['dummy relation'],
|
|
];
|
|
$query = new ActiveQuery(Customer::className(), $config);
|
|
$this->assertEquals($query->modelClass, Customer::className());
|
|
$this->assertEquals($query->on, $config['on']);
|
|
$this->assertEquals($query->joinWith, $config['joinWith']);
|
|
}
|
|
|
|
public function testTriggerInitEvent()
|
|
{
|
|
$where = '1==1';
|
|
$callback = function (\yii\base\Event $event) use ($where) {
|
|
$event->sender->where = $where;
|
|
};
|
|
Event::on(ActiveQuery::className(), ActiveQuery::EVENT_INIT, $callback);
|
|
$result = new ActiveQuery(Customer::className());
|
|
$this->assertEquals($where, $result->where);
|
|
Event::off(ActiveQuery::className(), ActiveQuery::EVENT_INIT, $callback);
|
|
}
|
|
|
|
/**
|
|
* @todo: tests for internal logic of prepare()
|
|
*/
|
|
public function testPrepare()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$builder = new QueryBuilder(new Connection());
|
|
$result = $query->prepare($builder);
|
|
$this->assertInstanceOf('yii\db\Query', $result);
|
|
}
|
|
|
|
public function testPopulate_EmptyRows()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$rows = [];
|
|
$result = $query->populate([]);
|
|
$this->assertEquals($rows, $result);
|
|
}
|
|
|
|
/**
|
|
* @todo: tests for internal logic of populate()
|
|
*/
|
|
public function testPopulate_FilledRows()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$rows = $query->all();
|
|
$result = $query->populate($rows);
|
|
$this->assertEquals($rows, $result);
|
|
}
|
|
|
|
/**
|
|
* @todo: tests for internal logic of one()
|
|
*/
|
|
public function testOne()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$result = $query->one();
|
|
$this->assertInstanceOf('yiiunit\data\ar\Customer', $result);
|
|
}
|
|
|
|
/**
|
|
* @todo: test internal logic of createCommand()
|
|
*/
|
|
public function testCreateCommand()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$result = $query->createCommand();
|
|
$this->assertInstanceOf('yii\db\Command', $result);
|
|
}
|
|
|
|
/**
|
|
* @todo: tests for internal logic of queryScalar()
|
|
*/
|
|
public function testQueryScalar()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$result = $this->invokeMethod($query, 'queryScalar', ['name', null]);
|
|
$this->assertEquals('user1', $result);
|
|
}
|
|
|
|
/**
|
|
* @todo: tests for internal logic of joinWith()
|
|
*/
|
|
public function testJoinWith()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$result = $query->joinWith('profile');
|
|
$this->assertEquals([
|
|
[['profile'], true, 'LEFT JOIN'],
|
|
], $result->joinWith);
|
|
}
|
|
|
|
/**
|
|
* @todo: tests for internal logic of innerJoinWith()
|
|
*/
|
|
public function testInnerJoinWith()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$result = $query->innerJoinWith('profile');
|
|
$this->assertEquals([
|
|
[['profile'], true, 'INNER JOIN'],
|
|
], $result->joinWith);
|
|
}
|
|
|
|
/**
|
|
* @todo: tests for the regex inside getQueryTableName
|
|
*/
|
|
public function testGetQueryTableName_from_not_set()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$result = $this->invokeMethod($query, 'getTableNameAndAlias');
|
|
$this->assertEquals(['customer', 'customer'], $result);
|
|
}
|
|
|
|
public function testGetQueryTableName_from_set()
|
|
{
|
|
$options = ['from' => ['alias' => 'customer']];
|
|
$query = new ActiveQuery(Customer::className(), $options);
|
|
$result = $this->invokeMethod($query, 'getTableNameAndAlias');
|
|
$this->assertEquals(['customer', 'alias'], $result);
|
|
}
|
|
|
|
public function testOnCondition()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$on = ['active' => true];
|
|
$params = ['a' => 'b'];
|
|
$result = $query->onCondition($on, $params);
|
|
$this->assertEquals($on, $result->on);
|
|
$this->assertEquals($params, $result->params);
|
|
}
|
|
|
|
public function testAndOnCondition_on_not_set()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$on = ['active' => true];
|
|
$params = ['a' => 'b'];
|
|
$result = $query->andOnCondition($on, $params);
|
|
$this->assertEquals($on, $result->on);
|
|
$this->assertEquals($params, $result->params);
|
|
}
|
|
|
|
public function testAndOnCondition_on_set()
|
|
{
|
|
$onOld = ['active' => true];
|
|
$query = new ActiveQuery(Customer::className());
|
|
$query->on = $onOld;
|
|
|
|
$on = ['active' => true];
|
|
$params = ['a' => 'b'];
|
|
$result = $query->andOnCondition($on, $params);
|
|
$this->assertEquals(['and', $onOld, $on], $result->on);
|
|
$this->assertEquals($params, $result->params);
|
|
}
|
|
|
|
public function testOrOnCondition_on_not_set()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$on = ['active' => true];
|
|
$params = ['a' => 'b'];
|
|
$result = $query->orOnCondition($on, $params);
|
|
$this->assertEquals($on, $result->on);
|
|
$this->assertEquals($params, $result->params);
|
|
}
|
|
|
|
public function testOrOnCondition_on_set()
|
|
{
|
|
$onOld = ['active' => true];
|
|
$query = new ActiveQuery(Customer::className());
|
|
$query->on = $onOld;
|
|
|
|
$on = ['active' => true];
|
|
$params = ['a' => 'b'];
|
|
$result = $query->orOnCondition($on, $params);
|
|
$this->assertEquals(['or', $onOld, $on], $result->on);
|
|
$this->assertEquals($params, $result->params);
|
|
}
|
|
|
|
/**
|
|
* @todo: tests for internal logic of viaTable()
|
|
*/
|
|
public function testViaTable()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$result = $query->viaTable(Profile::className(), ['id' => 'item_id']);
|
|
$this->assertInstanceOf('yii\db\ActiveQuery', $result);
|
|
$this->assertInstanceOf('yii\db\ActiveQuery', $result->via);
|
|
}
|
|
|
|
public function testAlias_not_set()
|
|
{
|
|
$query = new ActiveQuery(Customer::className());
|
|
$result = $query->alias('alias');
|
|
$this->assertInstanceOf('yii\db\ActiveQuery', $result);
|
|
$this->assertEquals(['alias' => 'customer'], $result->from);
|
|
}
|
|
|
|
public function testAlias_yet_set()
|
|
{
|
|
$aliasOld = ['old'];
|
|
$query = new ActiveQuery(Customer::className());
|
|
$query->from = $aliasOld;
|
|
$result = $query->alias('alias');
|
|
$this->assertInstanceOf('yii\db\ActiveQuery', $result);
|
|
$this->assertEquals(['alias' => 'old'], $result->from);
|
|
}
|
|
|
|
public function testGetTableNames_notFilledFrom()
|
|
{
|
|
$query = new ActiveQuery(Profile::className());
|
|
|
|
$tables = $query->getTablesUsedInFrom();
|
|
|
|
$this->assertEquals([
|
|
'{{' . Profile::tableName() . '}}' => '{{' . Profile::tableName() . '}}',
|
|
], $tables);
|
|
}
|
|
|
|
public function testGetTableNames_isFromArray()
|
|
{
|
|
$query = new ActiveQuery(null);
|
|
$query->from = [
|
|
'{{prf}}' => '{{profile}}',
|
|
'{{usr}}' => '{{user}}',
|
|
'{{a b}}' => '{{c d}}',
|
|
];
|
|
|
|
$tables = $query->getTablesUsedInFrom();
|
|
|
|
$this->assertEquals([
|
|
'{{prf}}' => '{{profile}}',
|
|
'{{usr}}' => '{{user}}',
|
|
'{{a b}}' => '{{c d}}',
|
|
], $tables);
|
|
}
|
|
|
|
public function testGetTableNames_isFromString()
|
|
{
|
|
$query = new ActiveQuery(null);
|
|
$query->from = 'profile AS \'prf\', user "usr", `order`, "customer", "a b" as "c d"';
|
|
|
|
$tables = $query->getTablesUsedInFrom();
|
|
|
|
$this->assertEquals([
|
|
'{{prf}}' => '{{profile}}',
|
|
'{{usr}}' => '{{user}}',
|
|
'{{order}}' => '{{order}}',
|
|
'{{customer}}' => '{{customer}}',
|
|
'{{c d}}' => '{{a b}}',
|
|
], $tables);
|
|
}
|
|
|
|
public function testGetTableNames_isFromObject_generateException()
|
|
{
|
|
$query = new ActiveQuery(null);
|
|
$query->from = new \stdClass();
|
|
|
|
$this->setExpectedException('\yii\base\InvalidConfigException');
|
|
|
|
$query->getTablesUsedInFrom();
|
|
}
|
|
|
|
public function testGetTablesAlias_notFilledFrom()
|
|
{
|
|
$query = new ActiveQuery(Profile::className());
|
|
|
|
$tables = $query->getTablesUsedInFrom();
|
|
|
|
$this->assertEquals([
|
|
'{{' . Profile::tableName() . '}}' => '{{' . Profile::tableName() . '}}',
|
|
], $tables);
|
|
}
|
|
|
|
public function testGetTablesAlias_isFromArray()
|
|
{
|
|
$query = new ActiveQuery(null);
|
|
$query->from = [
|
|
'{{prf}}' => '{{profile}}',
|
|
'{{usr}}' => '{{user}}',
|
|
'{{a b}}' => '{{c d}}',
|
|
];
|
|
|
|
$tables = $query->getTablesUsedInFrom();
|
|
|
|
$this->assertEquals([
|
|
'{{prf}}' => '{{profile}}',
|
|
'{{usr}}' => '{{user}}',
|
|
'{{a b}}' => '{{c d}}',
|
|
], $tables);
|
|
}
|
|
|
|
public function testGetTablesAlias_isFromString()
|
|
{
|
|
$query = new ActiveQuery(null);
|
|
$query->from = 'profile AS \'prf\', user "usr", service srv, order, [a b] [c d], {{something}} AS myalias';
|
|
|
|
$tables = $query->getTablesUsedInFrom();
|
|
|
|
$this->assertEquals([
|
|
'{{prf}}' => '{{profile}}',
|
|
'{{usr}}' => '{{user}}',
|
|
'{{srv}}' => '{{service}}',
|
|
'{{order}}' => '{{order}}',
|
|
'{{c d}}' => '{{a b}}',
|
|
'{{myalias}}' => '{{something}}',
|
|
], $tables);
|
|
}
|
|
|
|
public function testGetTablesAlias_isFromObject_generateException()
|
|
{
|
|
$query = new ActiveQuery(null);
|
|
$query->from = new \stdClass();
|
|
|
|
$this->setExpectedException('\yii\base\InvalidConfigException');
|
|
|
|
$query->getTablesUsedInFrom();
|
|
}
|
|
|
|
/**
|
|
* @see https://github.com/yiisoft/yii2/issues/14150
|
|
*/
|
|
public function testGetTableAliasFromPrefixedTableName()
|
|
{
|
|
$query = new ActiveQuery(null);
|
|
$query->from = '{{%order_item}}';
|
|
|
|
$tables = $query->getTablesUsedInFrom();
|
|
|
|
$this->assertEquals([
|
|
'{{%order_item}}' => '{{%order_item}}',
|
|
], $tables);
|
|
}
|
|
|
|
/**
|
|
* @see https://github.com/yiisoft/yii2/issues/14211
|
|
*/
|
|
public function testGetTableAliasFromTableNameWithDatabase()
|
|
{
|
|
$query = new ActiveQuery(null);
|
|
$query->from = 'tickets.workflows';
|
|
|
|
$tables = $query->getTablesUsedInFrom();
|
|
|
|
$this->assertEquals([
|
|
'{{tickets.workflows}}' => '{{tickets.workflows}}',
|
|
], $tables);
|
|
}
|
|
}
|