Commit f017ba35 by Carsten Brandt

refactored unit tests. put common AR tests in trait

parent 983b2286
...@@ -1169,7 +1169,7 @@ class ActiveRecord extends Model ...@@ -1169,7 +1169,7 @@ class ActiveRecord extends Model
return false; return false;
} }
foreach ($this->attributes() as $name) { foreach ($this->attributes() as $name) {
$this->_attributes[$name] = $record->_attributes[$name]; $this->_attributes[$name] = isset($record->_attributes[$name]) ? $record->_attributes[$name] : null;
} }
$this->_oldAttributes = $this->_attributes; $this->_oldAttributes = $this->_attributes;
$this->_related = []; $this->_related = [];
......
...@@ -39,11 +39,7 @@ class ActiveRelation extends ActiveQuery implements ActiveRelationInterface ...@@ -39,11 +39,7 @@ class ActiveRelation extends ActiveQuery implements ActiveRelationInterface
{ {
if ($this->primaryModel !== null) { if ($this->primaryModel !== null) {
// lazy loading // lazy loading
if ($this->via instanceof ActiveRelationInterface) { if (is_array($this->via)) {
// via pivot table
$viaModels = $this->via->findPivotRows([$this->primaryModel]);
$this->filterByModels($viaModels);
} elseif (is_array($this->via)) {
// via relation // via relation
/** @var ActiveRelation $viaQuery */ /** @var ActiveRelation $viaQuery */
list($viaName, $viaQuery) = $this->via; list($viaName, $viaQuery) = $this->via;
......
...@@ -73,13 +73,16 @@ class QueryBuilder extends \yii\base\Object ...@@ -73,13 +73,16 @@ class QueryBuilder extends \yii\base\Object
$parts['query'] = ["match_all" => (object)[]]; $parts['query'] = ["match_all" => (object)[]];
} }
$options = [];
if ($query->timeout !== null) {
$options['timeout'] = $query->timeout;
}
return [ return [
'queryParts' => $parts, 'queryParts' => $parts,
'index' => $query->index, 'index' => $query->index,
'type' => $query->type, 'type' => $query->type,
'options' => [ 'options' => $options,
'timeout' => $query->timeout
],
]; ];
} }
......
<?php <?php
namespace yiiunit\data\ar; namespace yiiunit\data\ar;
use yiiunit\framework\db\ActiveRecordTest;
/** /**
* Class Customer * Class Customer
* *
...@@ -17,9 +19,6 @@ class Customer extends ActiveRecord ...@@ -17,9 +19,6 @@ class Customer extends ActiveRecord
public $status2; public $status2;
public static $afterSaveInsert = null;
public static $afterSaveNewRecord = null;
public static function tableName() public static function tableName()
{ {
return 'tbl_customer'; return 'tbl_customer';
...@@ -37,8 +36,8 @@ class Customer extends ActiveRecord ...@@ -37,8 +36,8 @@ class Customer extends ActiveRecord
public function afterSave($insert) public function afterSave($insert)
{ {
static::$afterSaveInsert = $insert; ActiveRecordTest::$afterSaveInsert = $insert;
static::$afterSaveNewRecord = $this->isNewRecord; ActiveRecordTest::$afterSaveNewRecord = $this->isNewRecord;
parent::afterSave($insert); parent::afterSave($insert);
} }
} }
<?php <?php
namespace yiiunit\data\ar\elasticsearch; namespace yiiunit\data\ar\elasticsearch;
use yiiunit\framework\elasticsearch\ActiveRecordTest;
/** /**
* Class Customer * Class Customer
* *
...@@ -31,4 +33,11 @@ class Customer extends ActiveRecord ...@@ -31,4 +33,11 @@ class Customer extends ActiveRecord
{ {
$query->andWhere(array('status' => 1)); $query->andWhere(array('status' => 1));
} }
public function afterSave($insert)
{
ActiveRecordTest::$afterSaveInsert = $insert;
ActiveRecordTest::$afterSaveNewRecord = $this->isNewRecord;
parent::afterSave($insert);
}
} }
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace yiiunit\data\ar\redis; namespace yiiunit\data\ar\redis;
use yiiunit\framework\redis\ActiveRecordTest;
class Customer extends ActiveRecord class Customer extends ActiveRecord
{ {
const STATUS_ACTIVE = 1; const STATUS_ACTIVE = 1;
...@@ -26,4 +28,11 @@ class Customer extends ActiveRecord ...@@ -26,4 +28,11 @@ class Customer extends ActiveRecord
{ {
$query->andWhere(['status' => 1]); $query->andWhere(['status' => 1]);
} }
public function afterSave($insert)
{
ActiveRecordTest::$afterSaveInsert = $insert;
ActiveRecordTest::$afterSaveNewRecord = $this->isNewRecord;
parent::afterSave($insert);
}
} }
\ No newline at end of file
...@@ -23,7 +23,7 @@ CREATE TABLE `tbl_constraints` ...@@ -23,7 +23,7 @@ CREATE TABLE `tbl_constraints`
CREATE TABLE `tbl_customer` ( CREATE TABLE `tbl_customer` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(128) NOT NULL, `email` varchar(128) NOT NULL,
`name` varchar(128) NOT NULL, `name` varchar(128),
`address` string, `address` string,
`status` int (11) DEFAULT 0, `status` int (11) DEFAULT 0,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
......
...@@ -9,7 +9,7 @@ IF OBJECT_ID('[dbo].[tbl_null_values]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_n ...@@ -9,7 +9,7 @@ IF OBJECT_ID('[dbo].[tbl_null_values]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_n
CREATE TABLE [dbo].[tbl_customer] ( CREATE TABLE [dbo].[tbl_customer] (
[id] [int] IDENTITY(1,1) NOT NULL, [id] [int] IDENTITY(1,1) NOT NULL,
[email] [varchar](128) NOT NULL, [email] [varchar](128) NOT NULL,
[name] [varchar](128) NOT NULL, [name] [varchar](128),
[address] [text], [address] [text],
[status] [int] DEFAULT 0, [status] [int] DEFAULT 0,
CONSTRAINT [PK_customer] PRIMARY KEY CLUSTERED ( CONSTRAINT [PK_customer] PRIMARY KEY CLUSTERED (
......
...@@ -23,7 +23,7 @@ CREATE TABLE `tbl_constraints` ...@@ -23,7 +23,7 @@ CREATE TABLE `tbl_constraints`
CREATE TABLE `tbl_customer` ( CREATE TABLE `tbl_customer` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(128) NOT NULL, `email` varchar(128) NOT NULL,
`name` varchar(128) NOT NULL, `name` varchar(128),
`address` text, `address` text,
`status` int (11) DEFAULT 0, `status` int (11) DEFAULT 0,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
......
...@@ -22,7 +22,7 @@ CREATE TABLE tbl_constraints ...@@ -22,7 +22,7 @@ CREATE TABLE tbl_constraints
CREATE TABLE tbl_customer ( CREATE TABLE tbl_customer (
id serial not null primary key, id serial not null primary key,
email varchar(128) NOT NULL, email varchar(128) NOT NULL,
name varchar(128) NOT NULL, name varchar(128),
address text, address text,
status integer DEFAULT 0 status integer DEFAULT 0
); );
......
...@@ -15,7 +15,7 @@ DROP TABLE IF EXISTS tbl_null_values; ...@@ -15,7 +15,7 @@ DROP TABLE IF EXISTS tbl_null_values;
CREATE TABLE tbl_customer ( CREATE TABLE tbl_customer (
id INTEGER NOT NULL, id INTEGER NOT NULL,
email varchar(128) NOT NULL, email varchar(128) NOT NULL,
name varchar(128) NOT NULL, name varchar(128),
address text, address text,
status INTEGER DEFAULT 0, status INTEGER DEFAULT 0,
PRIMARY KEY (id) PRIMARY KEY (id)
......
...@@ -146,6 +146,9 @@ class QueryTest extends ElasticSearchTestCase ...@@ -146,6 +146,9 @@ class QueryTest extends ElasticSearchTestCase
} }
// TODO test facets
// TODO test complex where() every edge of QueryBuilder
public function testOrder() public function testOrder()
{ {
......
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