Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
f017ba35
Commit
f017ba35
authored
Nov 24, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored unit tests. put common AR tests in trait
parent
983b2286
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
803 additions
and
1037 deletions
+803
-1037
ActiveRecord.php
framework/yii/db/ActiveRecord.php
+1
-1
ActiveRelation.php
framework/yii/elasticsearch/ActiveRelation.php
+1
-5
QueryBuilder.php
framework/yii/elasticsearch/QueryBuilder.php
+6
-3
Customer.php
tests/unit/data/ar/Customer.php
+4
-5
Customer.php
tests/unit/data/ar/elasticsearch/Customer.php
+9
-0
Customer.php
tests/unit/data/ar/redis/Customer.php
+10
-0
cubrid.sql
tests/unit/data/cubrid.sql
+1
-1
mssql.sql
tests/unit/data/mssql.sql
+1
-1
mysql.sql
tests/unit/data/mysql.sql
+1
-1
postgres.sql
tests/unit/data/postgres.sql
+1
-1
sqlite.sql
tests/unit/data/sqlite.sql
+1
-1
ActiveRecordTestTrait.php
tests/unit/framework/ar/ActiveRecordTestTrait.php
+620
-0
ActiveRecordTest.php
tests/unit/framework/db/ActiveRecordTest.php
+32
-370
ActiveRecordTest.php
tests/unit/framework/elasticsearch/ActiveRecordTest.php
+62
-284
QueryTest.php
tests/unit/framework/elasticsearch/QueryTest.php
+3
-0
ActiveRecordTest.php
tests/unit/framework/redis/ActiveRecordTest.php
+50
-364
No files found.
framework/yii/db/ActiveRecord.php
View file @
f017ba35
...
@@ -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
=
[];
...
...
framework/yii/elasticsearch/ActiveRelation.php
View file @
f017ba35
...
@@ -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
;
...
...
framework/yii/elasticsearch/QueryBuilder.php
View file @
f017ba35
...
@@ -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
],
];
];
}
}
...
...
tests/unit/data/ar/Customer.php
View file @
f017ba35
<?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
);
}
}
}
}
tests/unit/data/ar/elasticsearch/Customer.php
View file @
f017ba35
<?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
);
}
}
}
tests/unit/data/ar/redis/Customer.php
View file @
f017ba35
...
@@ -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
tests/unit/data/cubrid.sql
View file @
f017ba35
...
@@ -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`
)
...
...
tests/unit/data/mssql.sql
View file @
f017ba35
...
@@ -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
(
...
...
tests/unit/data/mysql.sql
View file @
f017ba35
...
@@ -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`
)
...
...
tests/unit/data/postgres.sql
View file @
f017ba35
...
@@ -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
);
);
...
...
tests/unit/data/sqlite.sql
View file @
f017ba35
...
@@ -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
)
...
...
tests/unit/framework/ar/ActiveRecordTestTrait.php
0 → 100644
View file @
f017ba35
<?php
/**
*
*
* @author Carsten Brandt <mail@cebe.cc>
*/
namespace
yiiunit\framework\ar
;
use
yii\db\ActiveQueryInterface
;
use
yiiunit\TestCase
;
use
yiiunit\data\ar\Customer
;
use
yiiunit\data\ar\Order
;
/**
* This trait provides unit tests shared by the differen AR implementations
*
* @var TestCase $this
*/
trait
ActiveRecordTestTrait
{
/**
* This method should call Customer::find($q)
* @param $q
* @return mixed
*/
public
abstract
function
callCustomerFind
(
$q
=
null
);
/**
* This method should call Order::find($q)
* @param $q
* @return mixed
*/
public
abstract
function
callOrderFind
(
$q
=
null
);
/**
* This method should call OrderItem::find($q)
* @param $q
* @return mixed
*/
public
abstract
function
callOrderItemFind
(
$q
=
null
);
/**
* This method should call Item::find($q)
* @param $q
* @return mixed
*/
public
abstract
function
callItemFind
(
$q
=
null
);
/**
* This method should return the classname of Customer class
* @return string
*/
public
abstract
function
getCustomerClass
();
/**
* This method should return the classname of Order class
* @return string
*/
public
abstract
function
getOrderClass
();
/**
* This method should return the classname of OrderItem class
* @return string
*/
public
abstract
function
getOrderItemClass
();
/**
* This method should return the classname of Item class
* @return string
*/
public
abstract
function
getItemClass
();
/**
* can be overridden to do things after save()
*/
public
function
afterSave
()
{
}
public
function
testFind
()
{
$customerClass
=
$this
->
getCustomerClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
// find one
$result
=
$this
->
callCustomerFind
();
$this
->
assertTrue
(
$result
instanceof
ActiveQueryInterface
);
$customer
=
$result
->
one
();
$this
->
assertTrue
(
$customer
instanceof
$customerClass
);
// find all
$customers
=
$this
->
callCustomerFind
()
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
0
]
instanceof
$customerClass
);
$this
->
assertTrue
(
$customers
[
1
]
instanceof
$customerClass
);
$this
->
assertTrue
(
$customers
[
2
]
instanceof
$customerClass
);
// find all asArray
$customers
=
$this
->
callCustomerFind
()
->
asArray
()
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertArrayHasKey
(
'id'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'name'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'email'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'address'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'status'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'id'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'name'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'email'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'address'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'status'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'id'
,
$customers
[
2
]);
$this
->
assertArrayHasKey
(
'name'
,
$customers
[
2
]);
$this
->
assertArrayHasKey
(
'email'
,
$customers
[
2
]);
$this
->
assertArrayHasKey
(
'address'
,
$customers
[
2
]);
$this
->
assertArrayHasKey
(
'status'
,
$customers
[
2
]);
// find by a single primary key
$customer
=
$this
->
callCustomerFind
(
2
);
$this
->
assertTrue
(
$customer
instanceof
$customerClass
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
=
$this
->
callCustomerFind
(
5
);
$this
->
assertNull
(
$customer
);
$customer
=
$this
->
callCustomerFind
([
'id'
=>
[
5
,
6
,
1
]]);
$this
->
assertEquals
(
1
,
count
(
$customer
));
$customer
=
$this
->
callCustomerFind
()
->
where
([
'id'
=>
[
5
,
6
,
1
]])
->
one
();
$this
->
assertNotNull
(
$customer
);
// find by column values
$customer
=
$this
->
callCustomerFind
([
'id'
=>
2
,
'name'
=>
'user2'
]);
$this
->
assertTrue
(
$customer
instanceof
$customerClass
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
=
$this
->
callCustomerFind
([
'id'
=>
2
,
'name'
=>
'user1'
]);
$this
->
assertNull
(
$customer
);
$customer
=
$this
->
callCustomerFind
([
'id'
=>
5
]);
$this
->
assertNull
(
$customer
);
// find by attributes
$customer
=
$this
->
callCustomerFind
()
->
where
([
'name'
=>
'user2'
])
->
one
();
$this
->
assertTrue
(
$customer
instanceof
$customerClass
);
$this
->
assertEquals
(
2
,
$customer
->
id
);
// scope
$this
->
assertEquals
(
2
,
$this
->
callCustomerFind
()
->
active
()
->
count
());
// asArray
$customer
=
$this
->
callCustomerFind
()
->
where
([
'id'
=>
2
])
->
asArray
()
->
one
();
$this
->
assertEquals
([
'id'
=>
'2'
,
'email'
=>
'user2@example.com'
,
'name'
=>
'user2'
,
'address'
=>
'address2'
,
'status'
=>
'1'
,
],
$customer
);
}
public
function
testFindScalar
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
// query scalar
$customerName
=
$this
->
callCustomerFind
()
->
where
(
array
(
'id'
=>
2
))
->
scalar
(
'name'
);
$this
->
assertEquals
(
'user2'
,
$customerName
);
}
public
function
testFindColumn
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
$this
->
assertEquals
([
'user1'
,
'user2'
,
'user3'
],
Customer
::
find
()
->
column
(
'name'
));
$this
->
assertEquals
([
'user3'
,
'user2'
,
'user1'
],
Customer
::
find
()
->
orderBy
([
'name'
=>
SORT_DESC
])
->
column
(
'name'
));
}
public
function
testfindIndexBy
()
{
$customerClass
=
$this
->
getCustomerClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
// indexBy
$customers
=
$this
->
callCustomerFind
()
->
indexBy
(
'name'
)
->
orderBy
(
'id'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
'user1'
]
instanceof
$customerClass
);
$this
->
assertTrue
(
$customers
[
'user2'
]
instanceof
$customerClass
);
$this
->
assertTrue
(
$customers
[
'user3'
]
instanceof
$customerClass
);
// indexBy callable
$customers
=
$this
->
callCustomerFind
()
->
indexBy
(
function
(
$customer
)
{
return
$customer
->
id
.
'-'
.
$customer
->
name
;
})
->
orderBy
(
'id'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
'1-user1'
]
instanceof
$customerClass
);
$this
->
assertTrue
(
$customers
[
'2-user2'
]
instanceof
$customerClass
);
$this
->
assertTrue
(
$customers
[
'3-user3'
]
instanceof
$customerClass
);
}
public
function
testRefresh
()
{
$customerClass
=
$this
->
getCustomerClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
$customer
=
new
$customerClass
();
$this
->
assertFalse
(
$customer
->
refresh
());
$customer
=
$this
->
callCustomerFind
(
1
);
$customer
->
name
=
'to be refreshed'
;
$this
->
assertTrue
(
$customer
->
refresh
());
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
}
public
function
testEquals
()
{
$customerClass
=
$this
->
getCustomerClass
();
$itemClass
=
$this
->
getItemClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
$customerA
=
new
$customerClass
();
$customerB
=
new
$customerClass
();
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
$customerA
=
new
$customerClass
();
$customerB
=
new
$itemClass
();
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
$customerA
=
$this
->
callCustomerFind
(
1
);
$customerB
=
$this
->
callCustomerFind
(
2
);
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
$customerB
=
$this
->
callCustomerFind
(
1
);
$this
->
assertTrue
(
$customerA
->
equals
(
$customerB
));
$customerA
=
$this
->
callCustomerFind
(
1
);
$customerB
=
$this
->
callItemFind
(
1
);
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
}
public
function
testFindCount
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
$this
->
assertEquals
(
3
,
$this
->
callCustomerFind
()
->
count
());
// TODO should limit have effect on count()
// $this->assertEquals(1, $this->callCustomerFind()->limit(1)->count());
// $this->assertEquals(2, $this->callCustomerFind()->limit(2)->count());
// $this->assertEquals(1, $this->callCustomerFind()->offset(2)->limit(2)->count());
}
public
function
testFindLimit
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
// all()
$customers
=
$this
->
callCustomerFind
()
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$customers
=
$this
->
callCustomerFind
()
->
limit
(
1
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$this
->
assertEquals
(
'user1'
,
$customers
[
0
]
->
name
);
$customers
=
$this
->
callCustomerFind
()
->
limit
(
1
)
->
offset
(
1
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$this
->
assertEquals
(
'user2'
,
$customers
[
0
]
->
name
);
$customers
=
$this
->
callCustomerFind
()
->
limit
(
1
)
->
offset
(
2
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$this
->
assertEquals
(
'user3'
,
$customers
[
0
]
->
name
);
$customers
=
$this
->
callCustomerFind
()
->
limit
(
2
)
->
offset
(
1
)
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
$this
->
assertEquals
(
'user2'
,
$customers
[
0
]
->
name
);
$this
->
assertEquals
(
'user3'
,
$customers
[
1
]
->
name
);
$customers
=
$this
->
callCustomerFind
()
->
limit
(
2
)
->
offset
(
3
)
->
all
();
$this
->
assertEquals
(
0
,
count
(
$customers
));
// one()
$customer
=
$this
->
callCustomerFind
()
->
one
();
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
$customer
=
$this
->
callCustomerFind
()
->
offset
(
0
)
->
one
();
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
$customer
=
$this
->
callCustomerFind
()
->
offset
(
1
)
->
one
();
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
=
$this
->
callCustomerFind
()
->
offset
(
2
)
->
one
();
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
$customer
=
$this
->
callCustomerFind
()
->
offset
(
3
)
->
one
();
$this
->
assertNull
(
$customer
);
}
public
function
testFindComplexCondition
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
$this
->
assertEquals
(
2
,
$this
->
callCustomerFind
()
->
where
([
'OR'
,
[
'name'
=>
'user1'
],
[
'name'
=>
'user2'
]])
->
count
());
$this
->
assertEquals
(
2
,
count
(
$this
->
callCustomerFind
()
->
where
([
'OR'
,
[
'name'
=>
'user1'
],
[
'name'
=>
'user2'
]])
->
all
()));
$this
->
assertEquals
(
2
,
$this
->
callCustomerFind
()
->
where
([
'name'
=>
[
'user1'
,
'user2'
]])
->
count
());
$this
->
assertEquals
(
2
,
count
(
$this
->
callCustomerFind
()
->
where
([
'name'
=>
[
'user1'
,
'user2'
]])
->
all
()));
$this
->
assertEquals
(
1
,
$this
->
callCustomerFind
()
->
where
([
'AND'
,
[
'name'
=>
[
'user2'
,
'user3'
]],
[
'BETWEEN'
,
'status'
,
2
,
4
]])
->
count
());
$this
->
assertEquals
(
1
,
count
(
$this
->
callCustomerFind
()
->
where
([
'AND'
,
[
'name'
=>
[
'user2'
,
'user3'
]],
[
'BETWEEN'
,
'status'
,
2
,
4
]])
->
all
()));
}
public
function
testFindNullValues
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
$customer
=
$this
->
callCustomerFind
(
2
);
$customer
->
name
=
null
;
$customer
->
save
(
false
);
$this
->
afterSave
();
$result
=
$this
->
callCustomerFind
()
->
where
([
'name'
=>
null
])
->
all
();
$this
->
assertEquals
(
1
,
count
(
$result
));
$this
->
assertEquals
(
2
,
reset
(
$result
)
->
primaryKey
);
}
public
function
testExists
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
$this
->
assertTrue
(
$this
->
callCustomerFind
()
->
where
([
'id'
=>
2
])
->
exists
());
$this
->
assertFalse
(
$this
->
callCustomerFind
()
->
where
([
'id'
=>
5
])
->
exists
());
$this
->
assertTrue
(
$this
->
callCustomerFind
()
->
where
([
'name'
=>
'user1'
])
->
exists
());
$this
->
assertFalse
(
$this
->
callCustomerFind
()
->
where
([
'name'
=>
'user5'
])
->
exists
());
}
public
function
testFindLazy
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
$customer
=
$this
->
callCustomerFind
(
2
);
$this
->
assertFalse
(
$customer
->
isRelationPopulated
(
'orders'
));
$orders
=
$customer
->
orders
;
$this
->
assertTrue
(
$customer
->
isRelationPopulated
(
'orders'
));
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertEquals
(
1
,
count
(
$customer
->
populatedRelations
));
/** @var Customer $customer */
$customer
=
$this
->
callCustomerFind
(
2
);
$this
->
assertFalse
(
$customer
->
isRelationPopulated
(
'orders'
));
$orders
=
$customer
->
getOrders
()
->
where
([
'id'
=>
3
])
->
all
();
$this
->
assertFalse
(
$customer
->
isRelationPopulated
(
'orders'
));
$this
->
assertEquals
(
0
,
count
(
$customer
->
populatedRelations
));
$this
->
assertEquals
(
1
,
count
(
$orders
));
$this
->
assertEquals
(
3
,
$orders
[
0
]
->
id
);
}
public
function
testFindEager
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
$customers
=
$this
->
callCustomerFind
()
->
with
(
'orders'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
0
]
->
isRelationPopulated
(
'orders'
));
$this
->
assertTrue
(
$customers
[
1
]
->
isRelationPopulated
(
'orders'
));
$this
->
assertEquals
(
1
,
count
(
$customers
[
0
]
->
orders
));
$this
->
assertEquals
(
2
,
count
(
$customers
[
1
]
->
orders
));
$customer
=
$this
->
callCustomerFind
()
->
with
(
'orders'
)
->
one
();
$this
->
assertTrue
(
$customer
->
isRelationPopulated
(
'orders'
));
$this
->
assertEquals
(
1
,
count
(
$customer
->
orders
));
$this
->
assertEquals
(
1
,
count
(
$customer
->
populatedRelations
));
}
public
function
testFindLazyVia
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
/** @var Order $order */
$order
=
$this
->
callOrderFind
(
1
);
$this
->
assertEquals
(
1
,
$order
->
id
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
1
,
$order
->
items
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$order
->
items
[
1
]
->
id
);
$order
=
$this
->
callOrderFind
(
1
);
$order
->
id
=
100
;
$this
->
assertEquals
([],
$order
->
items
);
}
public
function
testFindEagerViaRelation
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
$orders
=
$this
->
callOrderFind
()
->
with
(
'items'
)
->
orderBy
(
'id'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$orders
));
$order
=
$orders
[
0
];
$this
->
assertEquals
(
1
,
$order
->
id
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
1
,
$order
->
items
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$order
->
items
[
1
]
->
id
);
}
public
function
testFindNestedRelation
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
$customers
=
$this
->
callCustomerFind
()
->
with
(
'orders'
,
'orders.items'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertEquals
(
1
,
count
(
$customers
[
0
]
->
orders
));
$this
->
assertEquals
(
2
,
count
(
$customers
[
1
]
->
orders
));
// TODO check is populated
$this
->
assertEquals
(
0
,
count
(
$customers
[
2
]
->
orders
));
$this
->
assertEquals
(
2
,
count
(
$customers
[
0
]
->
orders
[
0
]
->
items
));
$this
->
assertEquals
(
3
,
count
(
$customers
[
1
]
->
orders
[
0
]
->
items
));
$this
->
assertEquals
(
1
,
count
(
$customers
[
1
]
->
orders
[
1
]
->
items
));
}
public
function
testLink
()
{
$orderClass
=
$this
->
getOrderClass
();
$orderItemClass
=
$this
->
getOrderItemClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
$customer
=
$this
->
callCustomerFind
(
2
);
$this
->
assertEquals
(
2
,
count
(
$customer
->
orders
));
// has many
$order
=
new
$orderClass
;
$order
->
total
=
100
;
$this
->
assertTrue
(
$order
->
isNewRecord
);
$customer
->
link
(
'orders'
,
$order
);
$this
->
afterSave
();
$this
->
assertEquals
(
3
,
count
(
$customer
->
orders
));
$this
->
assertFalse
(
$order
->
isNewRecord
);
$this
->
assertEquals
(
3
,
count
(
$customer
->
getOrders
()
->
all
()));
$this
->
assertEquals
(
2
,
$order
->
customer_id
);
// belongs to
$order
=
new
$orderClass
;
$order
->
total
=
100
;
$this
->
assertTrue
(
$order
->
isNewRecord
);
$customer
=
$this
->
callCustomerFind
(
1
);
$this
->
assertNull
(
$order
->
customer
);
$order
->
link
(
'customer'
,
$customer
);
$this
->
assertFalse
(
$order
->
isNewRecord
);
$this
->
assertEquals
(
1
,
$order
->
customer_id
);
$this
->
assertEquals
(
1
,
$order
->
customer
->
primaryKey
);
// via model
$order
=
$this
->
callOrderFind
(
1
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
2
,
count
(
$order
->
orderItems
));
$orderItem
=
$this
->
callOrderItemFind
([
'order_id'
=>
1
,
'item_id'
=>
3
]);
$this
->
assertNull
(
$orderItem
);
$item
=
$this
->
callItemFind
(
3
);
$order
->
link
(
'items'
,
$item
,
[
'quantity'
=>
10
,
'subtotal'
=>
100
]);
$this
->
afterSave
();
$this
->
assertEquals
(
3
,
count
(
$order
->
items
));
$this
->
assertEquals
(
3
,
count
(
$order
->
orderItems
));
$orderItem
=
$this
->
callOrderItemFind
([
'order_id'
=>
1
,
'item_id'
=>
3
]);
$this
->
assertTrue
(
$orderItem
instanceof
$orderItemClass
);
$this
->
assertEquals
(
10
,
$orderItem
->
quantity
);
$this
->
assertEquals
(
100
,
$orderItem
->
subtotal
);
}
public
function
testUnlink
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
// has many
$customer
=
$this
->
callCustomerFind
(
2
);
$this
->
assertEquals
(
2
,
count
(
$customer
->
orders
));
$customer
->
unlink
(
'orders'
,
$customer
->
orders
[
1
],
true
);
$this
->
afterSave
();
$this
->
assertEquals
(
1
,
count
(
$customer
->
orders
));
$this
->
assertNull
(
$this
->
callOrderFind
(
3
));
// via model
$order
=
$this
->
callOrderFind
(
2
);
$this
->
assertEquals
(
3
,
count
(
$order
->
items
));
$this
->
assertEquals
(
3
,
count
(
$order
->
orderItems
));
$order
->
unlink
(
'items'
,
$order
->
items
[
2
],
true
);
$this
->
afterSave
();
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
2
,
count
(
$order
->
orderItems
));
}
public
static
$afterSaveNewRecord
;
public
static
$afterSaveInsert
;
public
function
testInsert
()
{
$customerClass
=
$this
->
getCustomerClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
$customer
=
new
$customerClass
;
$customer
->
email
=
'user4@example.com'
;
$customer
->
name
=
'user4'
;
$customer
->
address
=
'address4'
;
$this
->
assertNull
(
$customer
->
id
);
$this
->
assertTrue
(
$customer
->
isNewRecord
);
static
::
$afterSaveNewRecord
=
null
;
static
::
$afterSaveInsert
=
null
;
$customer
->
save
();
$this
->
afterSave
();
$this
->
assertEquals
(
4
,
$customer
->
id
);
$this
->
assertFalse
(
static
::
$afterSaveNewRecord
);
$this
->
assertTrue
(
static
::
$afterSaveInsert
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
}
public
function
testUpdate
()
{
$customerClass
=
$this
->
getCustomerClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
// save
$customer
=
$this
->
callCustomerFind
(
2
);
$this
->
assertTrue
(
$customer
instanceof
$customerClass
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
static
::
$afterSaveNewRecord
=
null
;
static
::
$afterSaveInsert
=
null
;
$customer
->
name
=
'user2x'
;
$customer
->
save
();
$this
->
afterSave
();
$this
->
assertEquals
(
'user2x'
,
$customer
->
name
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
$this
->
assertFalse
(
static
::
$afterSaveNewRecord
);
$this
->
assertFalse
(
static
::
$afterSaveInsert
);
$customer2
=
$this
->
callCustomerFind
(
2
);
$this
->
assertEquals
(
'user2x'
,
$customer2
->
name
);
// updateAll
$customer
=
$this
->
callCustomerFind
(
3
);
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
$ret
=
$customerClass
::
updateAll
([
'name'
=>
'temp'
],
[
'id'
=>
3
]);
$this
->
afterSave
();
$this
->
assertEquals
(
1
,
$ret
);
$customer
=
$this
->
callCustomerFind
(
3
);
$this
->
assertEquals
(
'temp'
,
$customer
->
name
);
$ret
=
$customerClass
::
updateAll
([
'name'
=>
'tempX'
]);
$this
->
afterSave
();
$this
->
assertEquals
(
3
,
$ret
);
$ret
=
$customerClass
::
updateAll
([
'name'
=>
'temp'
],
[
'name'
=>
'user6'
]);
$this
->
afterSave
();
$this
->
assertEquals
(
0
,
$ret
);
}
public
function
testUpdateCounters
()
{
$orderItemClass
=
$this
->
getOrderItemClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
// updateCounters
$pk
=
[
'order_id'
=>
2
,
'item_id'
=>
4
];
$orderItem
=
$this
->
callOrderItemFind
(
$pk
);
$this
->
assertEquals
(
1
,
$orderItem
->
quantity
);
$ret
=
$orderItem
->
updateCounters
([
'quantity'
=>
-
1
]);
$this
->
afterSave
();
$this
->
assertTrue
(
$ret
);
$this
->
assertEquals
(
0
,
$orderItem
->
quantity
);
$orderItem
=
$this
->
callOrderItemFind
(
$pk
);
$this
->
assertEquals
(
0
,
$orderItem
->
quantity
);
// updateAllCounters
$pk
=
[
'order_id'
=>
1
,
'item_id'
=>
2
];
$orderItem
=
$this
->
callOrderItemFind
(
$pk
);
$this
->
assertEquals
(
2
,
$orderItem
->
quantity
);
$ret
=
$orderItemClass
::
updateAllCounters
([
'quantity'
=>
3
,
'subtotal'
=>
-
10
,
],
$pk
);
$this
->
afterSave
();
$this
->
assertEquals
(
1
,
$ret
);
$orderItem
=
$this
->
callOrderItemFind
(
$pk
);
$this
->
assertEquals
(
5
,
$orderItem
->
quantity
);
$this
->
assertEquals
(
30
,
$orderItem
->
subtotal
);
}
public
function
testDelete
()
{
$customerClass
=
$this
->
getCustomerClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
// delete
$customer
=
$this
->
callCustomerFind
(
2
);
$this
->
assertTrue
(
$customer
instanceof
$customerClass
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
->
delete
();
$this
->
afterSave
();
$customer
=
$this
->
callCustomerFind
(
2
);
$this
->
assertNull
(
$customer
);
// deleteAll
$customers
=
$this
->
callCustomerFind
()
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
$ret
=
$customerClass
::
deleteAll
();
$this
->
afterSave
();
$this
->
assertEquals
(
2
,
$ret
);
$customers
=
$this
->
callCustomerFind
()
->
all
();
$this
->
assertEquals
(
0
,
count
(
$customers
));
$ret
=
$customerClass
::
deleteAll
();
$this
->
afterSave
();
$this
->
assertEquals
(
0
,
$ret
);
}
/**
* Some PDO implementations(e.g. cubrid) do not support boolean values.
* Make sure this does not affect AR layer.
*/
public
function
testBooleanAttribute
()
{
$customerClass
=
$this
->
getCustomerClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
$customer
=
new
$customerClass
();
$customer
->
name
=
'boolean customer'
;
$customer
->
email
=
'mail@example.com'
;
$customer
->
status
=
true
;
$customer
->
save
(
false
);
$customer
->
refresh
();
$this
->
assertEquals
(
1
,
$customer
->
status
);
$customer
->
status
=
false
;
$customer
->
save
(
false
);
$customer
->
refresh
();
$this
->
assertEquals
(
0
,
$customer
->
status
);
$customers
=
$this
->
callCustomerFind
()
->
where
([
'status'
=>
true
])
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
$customers
=
$this
->
callCustomerFind
()
->
where
([
'status'
=>
false
])
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
}
}
\ No newline at end of file
tests/unit/framework/db/ActiveRecordTest.php
View file @
f017ba35
...
@@ -8,6 +8,7 @@ use yiiunit\data\ar\NullValues;
...
@@ -8,6 +8,7 @@ use yiiunit\data\ar\NullValues;
use
yiiunit\data\ar\OrderItem
;
use
yiiunit\data\ar\OrderItem
;
use
yiiunit\data\ar\Order
;
use
yiiunit\data\ar\Order
;
use
yiiunit\data\ar\Item
;
use
yiiunit\data\ar\Item
;
use
yiiunit\framework\ar\ActiveRecordTestTrait
;
/**
/**
* @group db
* @group db
...
@@ -15,126 +16,57 @@ use yiiunit\data\ar\Item;
...
@@ -15,126 +16,57 @@ use yiiunit\data\ar\Item;
*/
*/
class
ActiveRecordTest
extends
DatabaseTestCase
class
ActiveRecordTest
extends
DatabaseTestCase
{
{
use
ActiveRecordTestTrait
;
protected
function
setUp
()
protected
function
setUp
()
{
{
parent
::
setUp
();
parent
::
setUp
();
ActiveRecord
::
$db
=
$this
->
getConnection
();
ActiveRecord
::
$db
=
$this
->
getConnection
();
}
}
public
function
testFind
()
public
function
callCustomerFind
(
$q
=
null
)
{
return
Customer
::
find
(
$q
);
}
{
public
function
callOrderFind
(
$q
=
null
)
{
return
Order
::
find
(
$q
);
}
// find one
public
function
callOrderItemFind
(
$q
=
null
)
{
return
OrderItem
::
find
(
$q
);
}
$result
=
Customer
::
find
();
public
function
callItemFind
(
$q
=
null
)
{
return
Item
::
find
(
$q
);
}
$this
->
assertTrue
(
$result
instanceof
ActiveQuery
);
$customer
=
$result
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
// find all
$customers
=
Customer
::
find
()
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
0
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
1
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
2
]
instanceof
Customer
);
// find by a single primary key
$customer
=
Customer
::
find
(
2
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
=
Customer
::
find
(
5
);
$this
->
assertNull
(
$customer
);
// query scalar
$customerName
=
Customer
::
find
()
->
where
(
array
(
'id'
=>
2
))
->
select
(
'name'
)
->
scalar
();
$this
->
assertEquals
(
'user2'
,
$customerName
);
// find by column values
$customer
=
Customer
::
find
([
'id'
=>
2
,
'name'
=>
'user2'
]);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
=
Customer
::
find
([
'id'
=>
2
,
'name'
=>
'user1'
]);
$this
->
assertNull
(
$customer
);
// find by attributes
public
function
getCustomerClass
()
{
return
Customer
::
className
();
}
$customer
=
Customer
::
find
()
->
where
([
'name'
=>
'user2'
])
->
one
();
public
function
getItemClass
()
{
return
Item
::
className
();
}
$this
->
assertTrue
(
$customer
instanceof
Customer
);
public
function
getOrderClass
()
{
return
Order
::
className
();
}
$this
->
assertEquals
(
2
,
$customer
->
id
);
public
function
getOrderItemClass
()
{
return
OrderItem
::
className
();
}
public
function
testCustomColumns
()
{
// find custom column
// find custom column
$customer
=
Customer
::
f
ind
()
->
select
([
'*'
,
'(status*2) AS status2'
])
$customer
=
$this
->
callCustomerF
ind
()
->
select
([
'*'
,
'(status*2) AS status2'
])
->
where
([
'name'
=>
'user3'
])
->
one
();
->
where
([
'name'
=>
'user3'
])
->
one
();
$this
->
assertEquals
(
3
,
$customer
->
id
);
$this
->
assertEquals
(
3
,
$customer
->
id
);
$this
->
assertEquals
(
4
,
$customer
->
status2
);
$this
->
assertEquals
(
4
,
$customer
->
status2
);
}
public
function
testSatisticalFind
()
{
// find count, sum, average, min, max, scalar
// find count, sum, average, min, max, scalar
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
());
$this
->
assertEquals
(
3
,
$this
->
callCustomerFind
()
->
count
());
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
where
(
'id=1 OR id=2'
)
->
count
());
$this
->
assertEquals
(
2
,
$this
->
callCustomerFind
()
->
where
(
'id=1 OR id=2'
)
->
count
());
$this
->
assertEquals
(
6
,
Customer
::
find
()
->
sum
(
'id'
));
$this
->
assertEquals
(
6
,
$this
->
callCustomerFind
()
->
sum
(
'id'
));
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
average
(
'id'
));
$this
->
assertEquals
(
2
,
$this
->
callCustomerFind
()
->
average
(
'id'
));
$this
->
assertEquals
(
1
,
Customer
::
find
()
->
min
(
'id'
));
$this
->
assertEquals
(
1
,
$this
->
callCustomerFind
()
->
min
(
'id'
));
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
max
(
'id'
));
$this
->
assertEquals
(
3
,
$this
->
callCustomerFind
()
->
max
(
'id'
));
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
select
(
'COUNT(*)'
)
->
scalar
());
$this
->
assertEquals
(
3
,
$this
->
callCustomerFind
()
->
select
(
'COUNT(*)'
)
->
scalar
());
// scope
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
active
()
->
count
());
// asArray
$customer
=
Customer
::
find
()
->
where
(
'id=2'
)
->
asArray
()
->
one
();
$this
->
assertEquals
([
'id'
=>
'2'
,
'email'
=>
'user2@example.com'
,
'name'
=>
'user2'
,
'address'
=>
'address2'
,
'status'
=>
'1'
,
],
$customer
);
// indexBy
$customers
=
Customer
::
find
()
->
indexBy
(
'name'
)
->
orderBy
(
'id'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
'user1'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'user2'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'user3'
]
instanceof
Customer
);
// indexBy callable
$customers
=
Customer
::
find
()
->
indexBy
(
function
(
$customer
)
{
return
$customer
->
id
.
'-'
.
$customer
->
name
;
})
->
orderBy
(
'id'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
'1-user1'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'2-user2'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'3-user3'
]
instanceof
Customer
);
}
}
public
function
test
Refresh
()
public
function
test
FindScalar
()
{
{
$customer
=
new
Customer
();
// query scalar
$this
->
assertFalse
(
$customer
->
refresh
());
$customerName
=
$this
->
callCustomerFind
()
->
where
(
array
(
'id'
=>
2
))
->
select
(
'name'
)
->
scalar
();
$this
->
assertEquals
(
'user2'
,
$customerName
);
$customer
=
Customer
::
find
(
1
);
$customer
->
name
=
'to be refreshed'
;
$this
->
assertTrue
(
$customer
->
refresh
());
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
}
}
public
function
test
Equals
()
public
function
test
FindColumn
()
{
{
$customerA
=
new
Customer
();
/** @var TestCase|ActiveRecordTestTrait $this */
$customerB
=
new
Customer
();
$this
->
assertEquals
([
'user1'
,
'user2'
,
'user3'
],
Customer
::
find
()
->
select
(
'name'
)
->
column
());
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
$this
->
assertEquals
([
'user3'
,
'user2'
,
'user1'
],
Customer
::
find
()
->
orderBy
([
'name'
=>
SORT_DESC
])
->
select
(
'name'
)
->
column
());
$customerA
=
new
Customer
();
$customerB
=
new
Item
();
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
$customerA
=
Customer
::
find
(
1
);
$customerB
=
Customer
::
find
(
2
);
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
$customerB
=
Customer
::
find
(
1
);
$this
->
assertTrue
(
$customerA
->
equals
(
$customerB
));
$customerA
=
Customer
::
find
(
1
);
$customerB
=
Item
::
find
(
1
);
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
}
}
public
function
testFindBySql
()
public
function
testFindBySql
()
...
@@ -154,67 +86,6 @@ class ActiveRecordTest extends DatabaseTestCase
...
@@ -154,67 +86,6 @@ class ActiveRecordTest extends DatabaseTestCase
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
}
}
public
function
testFindLazy
()
{
/** @var Customer $customer */
$customer
=
Customer
::
find
(
2
);
$this
->
assertFalse
(
$customer
->
isRelationPopulated
(
'orders'
));
$orders
=
$customer
->
orders
;
$this
->
assertTrue
(
$customer
->
isRelationPopulated
(
'orders'
));
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertEquals
(
1
,
count
(
$customer
->
populatedRelations
));
/** @var Customer $customer */
$customer
=
Customer
::
find
(
2
);
$this
->
assertFalse
(
$customer
->
isRelationPopulated
(
'orders'
));
$orders
=
$customer
->
getOrders
()
->
where
(
'id=3'
)
->
all
();
$this
->
assertFalse
(
$customer
->
isRelationPopulated
(
'orders'
));
$this
->
assertEquals
(
0
,
count
(
$customer
->
populatedRelations
));
$this
->
assertEquals
(
1
,
count
(
$orders
));
$this
->
assertEquals
(
3
,
$orders
[
0
]
->
id
);
}
public
function
testFindEager
()
{
$customers
=
Customer
::
find
()
->
with
(
'orders'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
0
]
->
isRelationPopulated
(
'orders'
));
$this
->
assertTrue
(
$customers
[
1
]
->
isRelationPopulated
(
'orders'
));
$this
->
assertEquals
(
1
,
count
(
$customers
[
0
]
->
orders
));
$this
->
assertEquals
(
2
,
count
(
$customers
[
1
]
->
orders
));
$customer
=
Customer
::
find
()
->
with
(
'orders'
)
->
one
();
$this
->
assertTrue
(
$customer
->
isRelationPopulated
(
'orders'
));
$this
->
assertEquals
(
1
,
count
(
$customer
->
orders
));
$this
->
assertEquals
(
1
,
count
(
$customer
->
populatedRelations
));
}
public
function
testFindLazyVia
()
{
/** @var Order $order */
$order
=
Order
::
find
(
1
);
$this
->
assertEquals
(
1
,
$order
->
id
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
1
,
$order
->
items
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$order
->
items
[
1
]
->
id
);
$order
=
Order
::
find
(
1
);
$order
->
id
=
100
;
$this
->
assertEquals
([],
$order
->
items
);
}
public
function
testFindEagerViaRelation
()
{
$orders
=
Order
::
find
()
->
with
(
'items'
)
->
orderBy
(
'id'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$orders
));
$order
=
$orders
[
0
];
$this
->
assertEquals
(
1
,
$order
->
id
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
1
,
$order
->
items
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$order
->
items
[
1
]
->
id
);
}
public
function
testFindLazyViaTable
()
public
function
testFindLazyViaTable
()
{
{
/** @var Order $order */
/** @var Order $order */
...
@@ -250,187 +121,6 @@ class ActiveRecordTest extends DatabaseTestCase
...
@@ -250,187 +121,6 @@ class ActiveRecordTest extends DatabaseTestCase
$this
->
assertEquals
(
2
,
$order
->
books
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$order
->
books
[
0
]
->
id
);
}
}
public
function
testFindNestedRelation
()
{
$customers
=
Customer
::
find
()
->
with
(
'orders'
,
'orders.items'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertEquals
(
1
,
count
(
$customers
[
0
]
->
orders
));
$this
->
assertEquals
(
2
,
count
(
$customers
[
1
]
->
orders
));
$this
->
assertEquals
(
0
,
count
(
$customers
[
2
]
->
orders
));
$this
->
assertEquals
(
2
,
count
(
$customers
[
0
]
->
orders
[
0
]
->
items
));
$this
->
assertEquals
(
3
,
count
(
$customers
[
1
]
->
orders
[
0
]
->
items
));
$this
->
assertEquals
(
1
,
count
(
$customers
[
1
]
->
orders
[
1
]
->
items
));
}
public
function
testLink
()
{
$customer
=
Customer
::
find
(
2
);
$this
->
assertEquals
(
2
,
count
(
$customer
->
orders
));
// has many
$order
=
new
Order
;
$order
->
total
=
100
;
$this
->
assertTrue
(
$order
->
isNewRecord
);
$customer
->
link
(
'orders'
,
$order
);
$this
->
assertEquals
(
3
,
count
(
$customer
->
orders
));
$this
->
assertFalse
(
$order
->
isNewRecord
);
$this
->
assertEquals
(
3
,
count
(
$customer
->
getOrders
()
->
all
()));
$this
->
assertEquals
(
2
,
$order
->
customer_id
);
// belongs to
$order
=
new
Order
;
$order
->
total
=
100
;
$this
->
assertTrue
(
$order
->
isNewRecord
);
$customer
=
Customer
::
find
(
1
);
$this
->
assertNull
(
$order
->
customer
);
$order
->
link
(
'customer'
,
$customer
);
$this
->
assertFalse
(
$order
->
isNewRecord
);
$this
->
assertEquals
(
1
,
$order
->
customer_id
);
$this
->
assertEquals
(
1
,
$order
->
customer
->
id
);
// via table
$order
=
Order
::
find
(
2
);
$this
->
assertEquals
(
0
,
count
(
$order
->
books
));
$orderItem
=
OrderItem
::
find
([
'order_id'
=>
2
,
'item_id'
=>
1
]);
$this
->
assertNull
(
$orderItem
);
$item
=
Item
::
find
(
1
);
$order
->
link
(
'books'
,
$item
,
[
'quantity'
=>
10
,
'subtotal'
=>
100
]);
$this
->
assertEquals
(
1
,
count
(
$order
->
books
));
$orderItem
=
OrderItem
::
find
([
'order_id'
=>
2
,
'item_id'
=>
1
]);
$this
->
assertTrue
(
$orderItem
instanceof
OrderItem
);
$this
->
assertEquals
(
10
,
$orderItem
->
quantity
);
$this
->
assertEquals
(
100
,
$orderItem
->
subtotal
);
// via model
$order
=
Order
::
find
(
1
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
2
,
count
(
$order
->
orderItems
));
$orderItem
=
OrderItem
::
find
([
'order_id'
=>
1
,
'item_id'
=>
3
]);
$this
->
assertNull
(
$orderItem
);
$item
=
Item
::
find
(
3
);
$order
->
link
(
'items'
,
$item
,
[
'quantity'
=>
10
,
'subtotal'
=>
100
]);
$this
->
assertEquals
(
3
,
count
(
$order
->
items
));
$this
->
assertEquals
(
3
,
count
(
$order
->
orderItems
));
$orderItem
=
OrderItem
::
find
([
'order_id'
=>
1
,
'item_id'
=>
3
]);
$this
->
assertTrue
(
$orderItem
instanceof
OrderItem
);
$this
->
assertEquals
(
10
,
$orderItem
->
quantity
);
$this
->
assertEquals
(
100
,
$orderItem
->
subtotal
);
}
public
function
testUnlink
()
{
// has many
$customer
=
Customer
::
find
(
2
);
$this
->
assertEquals
(
2
,
count
(
$customer
->
orders
));
$customer
->
unlink
(
'orders'
,
$customer
->
orders
[
1
],
true
);
$this
->
assertEquals
(
1
,
count
(
$customer
->
orders
));
$this
->
assertNull
(
Order
::
find
(
3
));
// via model
$order
=
Order
::
find
(
2
);
$this
->
assertEquals
(
3
,
count
(
$order
->
items
));
$this
->
assertEquals
(
3
,
count
(
$order
->
orderItems
));
$order
->
unlink
(
'items'
,
$order
->
items
[
2
],
true
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
2
,
count
(
$order
->
orderItems
));
// via table
$order
=
Order
::
find
(
1
);
$this
->
assertEquals
(
2
,
count
(
$order
->
books
));
$order
->
unlink
(
'books'
,
$order
->
books
[
1
],
true
);
$this
->
assertEquals
(
1
,
count
(
$order
->
books
));
$this
->
assertEquals
(
1
,
count
(
$order
->
orderItems
));
}
public
function
testInsert
()
{
$customer
=
new
Customer
;
$customer
->
email
=
'user4@example.com'
;
$customer
->
name
=
'user4'
;
$customer
->
address
=
'address4'
;
$this
->
assertNull
(
$customer
->
id
);
$this
->
assertTrue
(
$customer
->
isNewRecord
);
Customer
::
$afterSaveNewRecord
=
null
;
Customer
::
$afterSaveInsert
=
null
;
$customer
->
save
();
$this
->
assertEquals
(
4
,
$customer
->
id
);
$this
->
assertFalse
(
Customer
::
$afterSaveNewRecord
);
$this
->
assertTrue
(
Customer
::
$afterSaveInsert
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
}
public
function
testUpdate
()
{
// save
$customer
=
Customer
::
find
(
2
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
Customer
::
$afterSaveNewRecord
=
null
;
Customer
::
$afterSaveInsert
=
null
;
$customer
->
name
=
'user2x'
;
$customer
->
save
();
$this
->
assertEquals
(
'user2x'
,
$customer
->
name
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
$this
->
assertFalse
(
Customer
::
$afterSaveNewRecord
);
$this
->
assertFalse
(
Customer
::
$afterSaveInsert
);
$customer2
=
Customer
::
find
(
2
);
$this
->
assertEquals
(
'user2x'
,
$customer2
->
name
);
// updateCounters
$pk
=
[
'order_id'
=>
2
,
'item_id'
=>
4
];
$orderItem
=
OrderItem
::
find
(
$pk
);
$this
->
assertEquals
(
1
,
$orderItem
->
quantity
);
$ret
=
$orderItem
->
updateCounters
([
'quantity'
=>
-
1
]);
$this
->
assertTrue
(
$ret
);
$this
->
assertEquals
(
0
,
$orderItem
->
quantity
);
$orderItem
=
OrderItem
::
find
(
$pk
);
$this
->
assertEquals
(
0
,
$orderItem
->
quantity
);
// updateAll
$customer
=
Customer
::
find
(
3
);
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
$ret
=
Customer
::
updateAll
([
'name'
=>
'temp'
],
[
'id'
=>
3
]);
$this
->
assertEquals
(
1
,
$ret
);
$customer
=
Customer
::
find
(
3
);
$this
->
assertEquals
(
'temp'
,
$customer
->
name
);
// updateCounters
$pk
=
[
'order_id'
=>
1
,
'item_id'
=>
2
];
$orderItem
=
OrderItem
::
find
(
$pk
);
$this
->
assertEquals
(
2
,
$orderItem
->
quantity
);
$ret
=
OrderItem
::
updateAllCounters
([
'quantity'
=>
3
,
'subtotal'
=>
-
10
,
],
$pk
);
$this
->
assertEquals
(
1
,
$ret
);
$orderItem
=
OrderItem
::
find
(
$pk
);
$this
->
assertEquals
(
5
,
$orderItem
->
quantity
);
$this
->
assertEquals
(
30
,
$orderItem
->
subtotal
);
}
public
function
testDelete
()
{
// delete
$customer
=
Customer
::
find
(
2
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
->
delete
();
$customer
=
Customer
::
find
(
2
);
$this
->
assertNull
(
$customer
);
// deleteAll
$customers
=
Customer
::
find
()
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
$ret
=
Customer
::
deleteAll
();
$this
->
assertEquals
(
2
,
$ret
);
$customers
=
Customer
::
find
()
->
all
();
$this
->
assertEquals
(
0
,
count
(
$customers
));
}
public
function
testStoreNull
()
public
function
testStoreNull
()
{
{
...
@@ -501,34 +191,6 @@ class ActiveRecordTest extends DatabaseTestCase
...
@@ -501,34 +191,6 @@ class ActiveRecordTest extends DatabaseTestCase
$this
->
assertTrue
(
$record
->
var2
===
$record
->
var3
);
$this
->
assertTrue
(
$record
->
var2
===
$record
->
var3
);
}
}
/**
* Some PDO implementations(e.g. cubrid) do not support boolean values.
* Make sure this does not affect AR layer.
*/
public
function
testBooleanAttribute
()
{
$customer
=
new
Customer
();
$customer
->
name
=
'boolean customer'
;
$customer
->
email
=
'mail@example.com'
;
$customer
->
status
=
true
;
$customer
->
save
(
false
);
$customer
->
refresh
();
$this
->
assertEquals
(
1
,
$customer
->
status
);
$customer
->
status
=
false
;
$customer
->
save
(
false
);
$customer
->
refresh
();
$this
->
assertEquals
(
0
,
$customer
->
status
);
$customers
=
Customer
::
find
()
->
where
([
'status'
=>
true
])
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
$customers
=
Customer
::
find
()
->
where
([
'status'
=>
false
])
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
}
public
function
testIsPrimaryKey
()
public
function
testIsPrimaryKey
()
{
{
$this
->
assertFalse
(
Customer
::
isPrimaryKey
([]));
$this
->
assertFalse
(
Customer
::
isPrimaryKey
([]));
...
...
tests/unit/framework/elasticsearch/ActiveRecordTest.php
View file @
f017ba35
...
@@ -4,7 +4,7 @@ namespace yiiunit\framework\elasticsearch;
...
@@ -4,7 +4,7 @@ namespace yiiunit\framework\elasticsearch;
use
yii\elasticsearch\Connection
;
use
yii\elasticsearch\Connection
;
use
yii\elasticsearch\ActiveQuery
;
use
yii\elasticsearch\ActiveQuery
;
use
yii
\helpers\Json
;
use
yii
unit\framework\ar\ActiveRecordTestTrait
;
use
yiiunit\data\ar\elasticsearch\ActiveRecord
;
use
yiiunit\data\ar\elasticsearch\ActiveRecord
;
use
yiiunit\data\ar\elasticsearch\Customer
;
use
yiiunit\data\ar\elasticsearch\Customer
;
use
yiiunit\data\ar\elasticsearch\OrderItem
;
use
yiiunit\data\ar\elasticsearch\OrderItem
;
...
@@ -13,6 +13,26 @@ use yiiunit\data\ar\elasticsearch\Item;
...
@@ -13,6 +13,26 @@ use yiiunit\data\ar\elasticsearch\Item;
class
ActiveRecordTest
extends
ElasticSearchTestCase
class
ActiveRecordTest
extends
ElasticSearchTestCase
{
{
use
ActiveRecordTestTrait
;
public
function
callCustomerFind
(
$q
=
null
)
{
return
Customer
::
find
(
$q
);
}
public
function
callOrderFind
(
$q
=
null
)
{
return
Order
::
find
(
$q
);
}
public
function
callOrderItemFind
(
$q
=
null
)
{
return
OrderItem
::
find
(
$q
);
}
public
function
callItemFind
(
$q
=
null
)
{
return
Item
::
find
(
$q
);
}
public
function
getCustomerClass
()
{
return
Customer
::
className
();
}
public
function
getItemClass
()
{
return
Item
::
className
();
}
public
function
getOrderClass
()
{
return
Order
::
className
();
}
public
function
getOrderItemClass
()
{
return
OrderItem
::
className
();
}
/**
* can be overridden to do things after save()
*/
public
function
afterSave
()
{
$this
->
getConnection
()
->
createCommand
()
->
flushIndex
();
}
public
function
setUp
()
public
function
setUp
()
{
{
parent
::
setUp
();
parent
::
setUp
();
...
@@ -110,6 +130,25 @@ class ActiveRecordTest extends ElasticSearchTestCase
...
@@ -110,6 +130,25 @@ class ActiveRecordTest extends ElasticSearchTestCase
$this
->
assertTrue
(
$customers
[
1
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
1
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
2
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
2
]
instanceof
Customer
);
// find all asArray
$customers
=
Customer
::
find
()
->
asArray
()
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertArrayHasKey
(
'primaryKey'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'name'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'email'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'address'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'status'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'primaryKey'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'name'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'email'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'address'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'status'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'primaryKey'
,
$customers
[
2
]);
$this
->
assertArrayHasKey
(
'name'
,
$customers
[
2
]);
$this
->
assertArrayHasKey
(
'email'
,
$customers
[
2
]);
$this
->
assertArrayHasKey
(
'address'
,
$customers
[
2
]);
$this
->
assertArrayHasKey
(
'status'
,
$customers
[
2
]);
// find by a single primary key
// find by a single primary key
$customer
=
Customer
::
find
(
2
);
$customer
=
Customer
::
find
(
2
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
...
@@ -120,12 +159,16 @@ class ActiveRecordTest extends ElasticSearchTestCase
...
@@ -120,12 +159,16 @@ class ActiveRecordTest extends ElasticSearchTestCase
// query scalar
// query scalar
$customerName
=
Customer
::
find
()
->
where
([
'status'
=>
2
])
->
scalar
(
'name'
);
$customerName
=
Customer
::
find
()
->
where
([
'status'
=>
2
])
->
scalar
(
'name'
);
$this
->
assertEquals
(
'user3'
,
$customerName
);
$this
->
assertEquals
(
'user3'
,
$customerName
);
$customerName
=
Customer
::
find
()
->
where
([
'status'
=>
2
])
->
scalar
(
'noname'
);
$this
->
assertNull
(
$customerName
);
$customerId
=
Customer
::
find
()
->
where
([
'status'
=>
2
])
->
scalar
(
'primaryKey'
);
$this
->
assertEquals
(
3
,
$customerId
);
// find by column values
// find by column values
$customer
=
Customer
::
find
([
'name'
=>
'user2'
]);
$customer
=
Customer
::
find
([
'name'
=>
'user2'
]);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
=
Customer
::
find
([
'name'
=>
'user1'
,
'
id
'
=>
2
]);
$customer
=
Customer
::
find
([
'name'
=>
'user1'
,
'
primaryKey
'
=>
2
]);
$this
->
assertNull
(
$customer
);
$this
->
assertNull
(
$customer
);
$customer
=
Customer
::
find
([
'primaryKey'
=>
5
]);
$customer
=
Customer
::
find
([
'primaryKey'
=>
5
]);
$this
->
assertNull
(
$customer
);
$this
->
assertNull
(
$customer
);
...
@@ -137,8 +180,9 @@ class ActiveRecordTest extends ElasticSearchTestCase
...
@@ -137,8 +180,9 @@ class ActiveRecordTest extends ElasticSearchTestCase
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
// find count
, sum, average, min, max, scalar
// find count
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
());
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
());
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
where
([
'or'
,
[
'primaryKey'
=>
1
],
[
'primaryKey'
=>
2
]])
->
count
());
// $this->assertEquals(6, Customer::find()->sum('id'));
// $this->assertEquals(6, Customer::find()->sum('id'));
// $this->assertEquals(2, Customer::find()->average('id'));
// $this->assertEquals(2, Customer::find()->average('id'));
// $this->assertEquals(1, Customer::find()->min('id'));
// $this->assertEquals(1, Customer::find()->min('id'));
...
@@ -168,14 +212,19 @@ class ActiveRecordTest extends ElasticSearchTestCase
...
@@ -168,14 +212,19 @@ class ActiveRecordTest extends ElasticSearchTestCase
// indexBy callable
// indexBy callable
$customers
=
Customer
::
find
()
->
indexBy
(
function
(
$customer
)
{
$customers
=
Customer
::
find
()
->
indexBy
(
function
(
$customer
)
{
return
$customer
->
status
.
'-'
.
$customer
->
name
;
return
$customer
->
status
.
'-'
.
$customer
->
name
;
// })->orderBy('id')->all();
})
->
orderBy
(
'name'
)
->
all
();
})
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
'1-user1'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'1-user1'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'1-user2'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'1-user2'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'2-user3'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'2-user3'
]
instanceof
Customer
);
}
}
public
function
testGetDb
()
{
$this
->
mockApplication
([
'components'
=>
[
'elasticsearch'
=>
Connection
::
className
()]]);
$this
->
assertInstanceOf
(
Connection
::
className
(),
ActiveRecord
::
getDb
());
}
public
function
testGet
()
public
function
testGet
()
{
{
$this
->
assertInstanceOf
(
Customer
::
className
(),
Customer
::
get
(
1
));
$this
->
assertInstanceOf
(
Customer
::
className
(),
Customer
::
get
(
1
));
...
@@ -199,127 +248,6 @@ class ActiveRecordTest extends ElasticSearchTestCase
...
@@ -199,127 +248,6 @@ class ActiveRecordTest extends ElasticSearchTestCase
$this
->
assertInstanceOf
(
Customer
::
className
(),
$records
[
1
]);
$this
->
assertInstanceOf
(
Customer
::
className
(),
$records
[
1
]);
}
}
public
function
testRefresh
()
{
$customer
=
new
Customer
();
$this
->
assertFalse
(
$customer
->
refresh
());
$customer
=
Customer
::
get
(
1
);
$customer
->
name
=
'to be refreshed'
;
$this
->
assertTrue
(
$customer
->
refresh
());
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
}
public
function
testEquals
()
{
$customerA
=
new
Customer
();
$customerB
=
new
Customer
();
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
$customerA
=
new
Customer
();
$customerB
=
new
Item
();
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
$customerA
=
Customer
::
find
(
1
);
$customerB
=
Customer
::
find
(
2
);
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
$customerB
=
Customer
::
find
(
1
);
$this
->
assertTrue
(
$customerA
->
equals
(
$customerB
));
$customerA
=
Customer
::
find
(
1
);
$customerB
=
Item
::
find
(
1
);
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
}
public
function
testFindCount
()
{
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
());
$this
->
assertEquals
(
1
,
Customer
::
find
()
->
limit
(
1
)
->
count
());
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
limit
(
2
)
->
count
());
$this
->
assertEquals
(
1
,
Customer
::
find
()
->
offset
(
2
)
->
limit
(
2
)
->
count
());
}
public
function
testFindLimit
()
{
// all()
$customers
=
Customer
::
find
()
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$customers
=
Customer
::
find
()
->
limit
(
1
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$this
->
assertEquals
(
'user1'
,
$customers
[
0
]
->
name
);
$customers
=
Customer
::
find
()
->
limit
(
1
)
->
offset
(
1
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$this
->
assertEquals
(
'user2'
,
$customers
[
0
]
->
name
);
$customers
=
Customer
::
find
()
->
limit
(
1
)
->
offset
(
2
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$this
->
assertEquals
(
'user3'
,
$customers
[
0
]
->
name
);
$customers
=
Customer
::
find
()
->
limit
(
2
)
->
offset
(
1
)
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
$this
->
assertEquals
(
'user2'
,
$customers
[
0
]
->
name
);
$this
->
assertEquals
(
'user3'
,
$customers
[
1
]
->
name
);
$customers
=
Customer
::
find
()
->
limit
(
2
)
->
offset
(
3
)
->
all
();
$this
->
assertEquals
(
0
,
count
(
$customers
));
// one()
$customer
=
Customer
::
find
()
->
one
();
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
$customer
=
Customer
::
find
()
->
offset
(
0
)
->
one
();
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
$customer
=
Customer
::
find
()
->
offset
(
1
)
->
one
();
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
=
Customer
::
find
()
->
offset
(
2
)
->
one
();
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
$customer
=
Customer
::
find
()
->
offset
(
3
)
->
one
();
$this
->
assertNull
(
$customer
);
}
public
function
testFindComplexCondition
()
{
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
where
([
'OR'
,
[
'name'
=>
'user1'
],
[
'name'
=>
'user2'
]])
->
count
());
$this
->
assertEquals
(
2
,
count
(
Customer
::
find
()
->
where
([
'OR'
,
[
'name'
=>
'user1'
],
[
'name'
=>
'user2'
]])
->
all
()));
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
where
([
'name'
=>
[
'user1'
,
'user2'
]])
->
count
());
$this
->
assertEquals
(
2
,
count
(
Customer
::
find
()
->
where
([
'name'
=>
[
'user1'
,
'user2'
]])
->
all
()));
$this
->
assertEquals
(
1
,
Customer
::
find
()
->
where
([
'AND'
,
[
'name'
=>
[
'user2'
,
'user3'
]],
[
'BETWEEN'
,
'status'
,
2
,
4
]])
->
count
());
$this
->
assertEquals
(
1
,
count
(
Customer
::
find
()
->
where
([
'AND'
,
[
'name'
=>
[
'user2'
,
'user3'
]],
[
'BETWEEN'
,
'status'
,
2
,
4
]])
->
all
()));
}
public
function
testFindNullValues
()
{
$customer
=
Customer
::
find
(
2
);
$customer
->
name
=
null
;
$customer
->
save
(
false
);
Customer
::
getDb
()
->
createCommand
()
->
flushIndex
(
'customers'
);
$result
=
Customer
::
find
()
->
where
([
'name'
=>
null
])
->
all
();
$this
->
assertEquals
(
1
,
count
(
$result
));
$this
->
assertEquals
(
2
,
reset
(
$result
)
->
primaryKey
);
}
public
function
testFindColumn
()
{
$this
->
assertEquals
([
'user1'
,
'user2'
,
'user3'
],
Customer
::
find
()
->
column
(
'name'
));
$this
->
assertEquals
([
'user3'
,
'user2'
,
'user1'
],
Customer
::
find
()
->
orderBy
([
'name'
=>
SORT_DESC
])
->
column
(
'name'
));
}
public
function
testExists
()
{
$this
->
assertTrue
(
Customer
::
find
()
->
where
([
'name'
=>
'user1'
])
->
exists
());
$this
->
assertFalse
(
Customer
::
find
()
->
where
([
'name'
=>
'user5'
])
->
exists
());
}
public
function
testFindLazy
()
public
function
testFindLazy
()
{
{
/** @var $customer Customer */
/** @var $customer Customer */
...
@@ -332,114 +260,18 @@ class ActiveRecordTest extends ElasticSearchTestCase
...
@@ -332,114 +260,18 @@ class ActiveRecordTest extends ElasticSearchTestCase
$this
->
assertEquals
(
2
,
$orders
[
0
]
->
primaryKey
);
$this
->
assertEquals
(
2
,
$orders
[
0
]
->
primaryKey
);
}
}
public
function
testFindEager
()
{
$customers
=
Customer
::
find
()
->
with
(
'orders'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertEquals
(
1
,
count
(
$customers
[
0
]
->
orders
));
$this
->
assertEquals
(
2
,
count
(
$customers
[
1
]
->
orders
));
}
public
function
testFindLazyVia
()
{
/** @var $order Order */
$order
=
Order
::
find
(
1
);
$this
->
assertEquals
(
1
,
$order
->
primaryKey
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
2
,
$order
->
items
[
0
]
->
primaryKey
);
$this
->
assertEquals
(
1
,
$order
->
items
[
1
]
->
primaryKey
);
$order
=
new
Order
();
$order
->
primaryKey
=
100
;
$this
->
assertEquals
([],
$order
->
items
);
}
public
function
testFindEagerViaRelation
()
public
function
testFindEagerViaRelation
()
{
{
$orders
=
Order
::
find
()
->
with
(
'items'
)
->
all
();
// this test is currently failing randomly because of https://github.com/yiisoft/yii2/issues/1310
$orders
=
Order
::
find
()
->
with
(
'items'
)
->
orderBy
(
'create_time'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$orders
));
$this
->
assertEquals
(
3
,
count
(
$orders
));
$order
=
$orders
[
0
];
$order
=
$orders
[
0
];
$this
->
assertEquals
(
1
,
$order
->
primaryKey
);
$this
->
assertEquals
(
1
,
$order
->
primaryKey
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
2
,
$order
->
items
[
0
]
->
primaryKey
);
$this
->
assertEquals
(
1
,
$order
->
items
[
0
]
->
primaryKey
);
$this
->
assertEquals
(
1
,
$order
->
items
[
1
]
->
primaryKey
);
$this
->
assertEquals
(
2
,
$order
->
items
[
1
]
->
primaryKey
);
}
}
public
function
testFindNestedRelation
()
{
$customers
=
Customer
::
find
()
->
with
(
'orders'
,
'orders.items'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertEquals
(
1
,
count
(
$customers
[
0
]
->
orders
));
$this
->
assertEquals
(
2
,
count
(
$customers
[
1
]
->
orders
));
$this
->
assertEquals
(
0
,
count
(
$customers
[
2
]
->
orders
));
$this
->
assertEquals
(
2
,
count
(
$customers
[
0
]
->
orders
[
0
]
->
items
));
$this
->
assertEquals
(
3
,
count
(
$customers
[
1
]
->
orders
[
0
]
->
items
));
$this
->
assertEquals
(
1
,
count
(
$customers
[
1
]
->
orders
[
1
]
->
items
));
}
public
function
testLink
()
{
$customer
=
Customer
::
find
(
2
);
$this
->
assertEquals
(
2
,
count
(
$customer
->
orders
));
// has many
$order
=
new
Order
;
$order
->
total
=
100
;
$this
->
assertTrue
(
$order
->
isNewRecord
);
$customer
->
link
(
'orders'
,
$order
);
$this
->
assertEquals
(
3
,
count
(
$customer
->
orders
));
$this
->
assertFalse
(
$order
->
isNewRecord
);
Customer
::
getDb
()
->
createCommand
()
->
flushIndex
();
$this
->
assertEquals
(
3
,
count
(
$customer
->
getOrders
()
->
all
()));
$this
->
assertEquals
(
2
,
$order
->
customer_id
);
// belongs to
$order
=
new
Order
;
$order
->
total
=
100
;
$this
->
assertTrue
(
$order
->
isNewRecord
);
$customer
=
Customer
::
find
(
1
);
$this
->
assertNull
(
$order
->
customer
);
$order
->
link
(
'customer'
,
$customer
);
$this
->
assertFalse
(
$order
->
isNewRecord
);
$this
->
assertEquals
(
1
,
$order
->
customer_id
);
$this
->
assertEquals
(
1
,
$order
->
customer
->
primaryKey
);
// via model
$order
=
Order
::
find
(
1
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
2
,
count
(
$order
->
orderItems
));
$orderItem
=
OrderItem
::
find
([
'order_id'
=>
1
,
'item_id'
=>
3
]);
$this
->
assertNull
(
$orderItem
);
$item
=
Item
::
find
(
3
);
$order
->
link
(
'items'
,
$item
,
[
'quantity'
=>
10
,
'subtotal'
=>
100
]);
Customer
::
getDb
()
->
createCommand
()
->
flushIndex
();
$this
->
assertEquals
(
3
,
count
(
$order
->
items
));
$this
->
assertEquals
(
3
,
count
(
$order
->
orderItems
));
$orderItem
=
OrderItem
::
find
([
'order_id'
=>
1
,
'item_id'
=>
3
]);
$this
->
assertTrue
(
$orderItem
instanceof
OrderItem
);
$this
->
assertEquals
(
10
,
$orderItem
->
quantity
);
$this
->
assertEquals
(
100
,
$orderItem
->
subtotal
);
}
public
function
testUnlink
()
{
// has many
$customer
=
Customer
::
find
(
2
);
$this
->
assertEquals
(
2
,
count
(
$customer
->
orders
));
$customer
->
unlink
(
'orders'
,
$customer
->
orders
[
1
],
true
);
Customer
::
getDb
()
->
createCommand
()
->
flushIndex
();
$this
->
assertEquals
(
1
,
count
(
$customer
->
orders
));
$this
->
assertNull
(
Order
::
find
(
3
));
// via model
$order
=
Order
::
find
(
2
);
$this
->
assertEquals
(
3
,
count
(
$order
->
items
));
$this
->
assertEquals
(
3
,
count
(
$order
->
orderItems
));
$order
->
unlink
(
'items'
,
$order
->
items
[
2
],
true
);
Customer
::
getDb
()
->
createCommand
()
->
flushIndex
();
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
2
,
count
(
$order
->
orderItems
));
}
public
function
testInsertNoPk
()
public
function
testInsertNoPk
()
{
{
...
@@ -451,11 +283,14 @@ class ActiveRecordTest extends ElasticSearchTestCase
...
@@ -451,11 +283,14 @@ class ActiveRecordTest extends ElasticSearchTestCase
$customer
->
address
=
'address4'
;
$customer
->
address
=
'address4'
;
$this
->
assertNull
(
$customer
->
primaryKey
);
$this
->
assertNull
(
$customer
->
primaryKey
);
$this
->
assertNull
(
$customer
->
oldPrimaryKey
);
$this
->
assertTrue
(
$customer
->
isNewRecord
);
$this
->
assertTrue
(
$customer
->
isNewRecord
);
$customer
->
save
();
$customer
->
save
();
$this
->
assertNotNull
(
$customer
->
primaryKey
);
$this
->
assertNotNull
(
$customer
->
primaryKey
);
$this
->
assertNotNull
(
$customer
->
oldPrimaryKey
);
$this
->
assertEquals
(
$customer
->
primaryKey
,
$customer
->
oldPrimaryKey
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
}
}
...
@@ -475,70 +310,14 @@ class ActiveRecordTest extends ElasticSearchTestCase
...
@@ -475,70 +310,14 @@ class ActiveRecordTest extends ElasticSearchTestCase
$this
->
assertFalse
(
$customer
->
isNewRecord
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
}
}
public
function
testUpdate
()
{
// save
$customer
=
Customer
::
find
(
2
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
$customer
->
name
=
'user2x'
;
$customer
->
save
();
$this
->
assertEquals
(
'user2x'
,
$customer
->
name
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
$customer2
=
Customer
::
find
(
2
);
$this
->
assertEquals
(
'user2x'
,
$customer2
->
name
);
// updateAll
$customer
=
Customer
::
find
(
3
);
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
$ret
=
Customer
::
updateAll
([
'name'
=>
'temp'
,
],
[
'name'
=>
'user3'
]);
$this
->
assertEquals
(
1
,
$ret
);
$customer
=
Customer
::
find
(
3
);
$this
->
assertEquals
(
'temp'
,
$customer
->
name
);
$ret
=
Customer
::
updateAll
([
'name'
=>
'temp'
]);
$this
->
assertEquals
(
3
,
$ret
);
}
public
function
testUpdatePk
()
public
function
testUpdatePk
()
{
{
$this
->
setExpectedException
(
'yii\base\InvalidCallException'
);
$pk
=
[
'primaryKey'
=>
2
];
$pk
=
[
'primaryKey'
=>
2
];
$orderItem
=
Order
::
find
(
$pk
);
$orderItem
=
Order
::
find
(
$pk
);
$this
->
assertEquals
(
2
,
$orderItem
->
primaryKey
);
$this
->
assertEquals
(
2
,
$orderItem
->
primaryKey
);
$this
->
setExpectedException
(
'yii\base\InvalidCallException'
);
$orderItem
->
primaryKey
=
13
;
$orderItem
->
primaryKey
=
13
;
$orderItem
->
save
();
$orderItem
->
save
();
}
}
public
function
testDelete
()
{
// delete
$customer
=
Customer
::
find
(
2
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
->
delete
();
$customer
=
Customer
::
find
(
2
);
$this
->
assertNull
(
$customer
);
Customer
::
getDb
()
->
createCommand
()
->
flushIndex
(
'customers'
);
// deleteAll
$customers
=
Customer
::
find
()
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
$ret
=
Customer
::
deleteAll
([
'name'
=>
[
'user1'
,
'user3'
]]);
$this
->
assertEquals
(
2
,
$ret
);
Customer
::
getDb
()
->
createCommand
()
->
flushIndex
(
'customers'
);
$customers
=
Customer
::
find
()
->
all
();
$this
->
assertEquals
(
0
,
count
(
$customers
));
$ret
=
Customer
::
deleteAll
();
$this
->
assertEquals
(
0
,
$ret
);
}
}
}
\ No newline at end of file
tests/unit/framework/elasticsearch/QueryTest.php
View file @
f017ba35
...
@@ -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
()
{
{
...
...
tests/unit/framework/redis/ActiveRecordTest.php
View file @
f017ba35
...
@@ -9,12 +9,26 @@ use yiiunit\data\ar\redis\Customer;
...
@@ -9,12 +9,26 @@ use yiiunit\data\ar\redis\Customer;
use
yiiunit\data\ar\redis\OrderItem
;
use
yiiunit\data\ar\redis\OrderItem
;
use
yiiunit\data\ar\redis\Order
;
use
yiiunit\data\ar\redis\Order
;
use
yiiunit\data\ar\redis\Item
;
use
yiiunit\data\ar\redis\Item
;
use
yiiunit\framework\ar\ActiveRecordTestTrait
;
/**
/**
* @group redis
* @group redis
*/
*/
class
ActiveRecordTest
extends
RedisTestCase
class
ActiveRecordTest
extends
RedisTestCase
{
{
use
ActiveRecordTestTrait
;
public
function
callCustomerFind
(
$q
=
null
)
{
return
Customer
::
find
(
$q
);
}
public
function
callOrderFind
(
$q
=
null
)
{
return
Order
::
find
(
$q
);
}
public
function
callOrderItemFind
(
$q
=
null
)
{
return
OrderItem
::
find
(
$q
);
}
public
function
callItemFind
(
$q
=
null
)
{
return
Item
::
find
(
$q
);
}
public
function
getCustomerClass
()
{
return
Customer
::
className
();
}
public
function
getItemClass
()
{
return
Item
::
className
();
}
public
function
getOrderClass
()
{
return
Order
::
className
();
}
public
function
getOrderItemClass
()
{
return
OrderItem
::
className
();
}
public
function
setUp
()
public
function
setUp
()
{
{
parent
::
setUp
();
parent
::
setUp
();
...
@@ -79,50 +93,21 @@ class ActiveRecordTest extends RedisTestCase
...
@@ -79,50 +93,21 @@ class ActiveRecordTest extends RedisTestCase
$orderItem
->
save
(
false
);
$orderItem
->
save
(
false
);
}
}
public
function
testFind
()
public
function
testFind
NullValues
()
{
{
// find one
// https://github.com/yiisoft/yii2/issues/1311
$result
=
Customer
::
find
();
$this
->
markTestSkipped
(
'Redis does not store/find null values correctly.'
);
$this
->
assertTrue
(
$result
instanceof
ActiveQuery
);
}
$customer
=
$result
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
// find all
$customers
=
Customer
::
find
()
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
0
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
1
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
2
]
instanceof
Customer
);
// find by a single primary key
$customer
=
Customer
::
find
(
2
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
=
Customer
::
find
(
5
);
$this
->
assertNull
(
$customer
);
$customer
=
Customer
::
find
([
'id'
=>
[
5
,
6
,
1
]]);
$this
->
assertEquals
(
1
,
count
(
$customer
));
$customer
=
Customer
::
find
()
->
where
([
'id'
=>
[
5
,
6
,
1
]])
->
one
();
$this
->
assertNotNull
(
$customer
);
// query scalar
$customerName
=
Customer
::
find
()
->
where
([
'id'
=>
2
])
->
scalar
(
'name'
);
$this
->
assertEquals
(
'user2'
,
$customerName
);
// find by column values
public
function
testBooleanAttribute
()
$customer
=
Customer
::
find
([
'id'
=>
2
,
'name'
=>
'user2'
]);
{
$this
->
assertTrue
(
$customer
instanceof
Customer
);
// https://github.com/yiisoft/yii2/issues/1311
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$this
->
markTestSkipped
(
'Redis does not store/find boolean values correctly.'
);
$customer
=
Customer
::
find
([
'id'
=>
2
,
'name'
=>
'user1'
]);
}
$this
->
assertNull
(
$customer
);
$customer
=
Customer
::
find
([
'id'
=>
5
]);
$this
->
assertNull
(
$customer
);
// find by attributes
$customer
=
Customer
::
find
()
->
where
([
'name'
=>
'user2'
])
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
2
,
$customer
->
id
);
public
function
testSatisticalFind
()
{
// find count, sum, average, min, max, scalar
// find count, sum, average, min, max, scalar
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
());
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
());
$this
->
assertEquals
(
6
,
Customer
::
find
()
->
sum
(
'id'
));
$this
->
assertEquals
(
6
,
Customer
::
find
()
->
sum
(
'id'
));
...
@@ -130,68 +115,41 @@ class ActiveRecordTest extends RedisTestCase
...
@@ -130,68 +115,41 @@ class ActiveRecordTest extends RedisTestCase
$this
->
assertEquals
(
1
,
Customer
::
find
()
->
min
(
'id'
));
$this
->
assertEquals
(
1
,
Customer
::
find
()
->
min
(
'id'
));
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
max
(
'id'
));
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
max
(
'id'
));
// scope
$this
->
assertEquals
(
6
,
OrderItem
::
find
()
->
count
());
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
active
()
->
count
());
$this
->
assertEquals
(
7
,
OrderItem
::
find
()
->
sum
(
'quantity'
));
}
// asArray
$customer
=
Customer
::
find
()
->
where
([
'id'
=>
2
])
->
asArray
()
->
one
();
$this
->
assertEquals
(
array
(
'id'
=>
'2'
,
'email'
=>
'user2@example.com'
,
'name'
=>
'user2'
,
'address'
=>
'address2'
,
'status'
=>
'1'
,
),
$customer
);
public
function
testfindIndexBy
()
{
$customerClass
=
$this
->
getCustomerClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
// indexBy
// indexBy
$customers
=
Customer
::
find
()
->
indexBy
(
'name'
)
->
all
();
$customers
=
$this
->
callCustomerFind
()
->
indexBy
(
'name'
)
/*->orderBy('id')*/
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
'user1'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'user1'
]
instanceof
$customerClass
);
$this
->
assertTrue
(
$customers
[
'user2'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'user2'
]
instanceof
$customerClass
);
$this
->
assertTrue
(
$customers
[
'user3'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'user3'
]
instanceof
$customerClass
);
// indexBy callable
// indexBy callable
$customers
=
Customer
::
f
ind
()
->
indexBy
(
function
(
$customer
)
{
$customers
=
$this
->
callCustomerF
ind
()
->
indexBy
(
function
(
$customer
)
{
return
$customer
->
id
.
'-'
.
$customer
->
name
;
return
$customer
->
id
.
'-'
.
$customer
->
name
;
// })->orderBy('id')->all();
})
/*->orderBy('id')*/
->
all
();
// TODO this test is duplicated because of missing orderBy support in redis
})
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
'1-user1'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'1-user1'
]
instanceof
$customerClass
);
$this
->
assertTrue
(
$customers
[
'2-user2'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'2-user2'
]
instanceof
$customerClass
);
$this
->
assertTrue
(
$customers
[
'3-user3'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'3-user3'
]
instanceof
$customerClass
);
}
public
function
testRefresh
()
{
$customer
=
new
Customer
();
$this
->
assertFalse
(
$customer
->
refresh
());
$customer
=
Customer
::
find
(
1
);
$customer
->
name
=
'to be refreshed'
;
$this
->
assertTrue
(
$customer
->
refresh
());
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
}
}
public
function
test
Equals
()
public
function
test
FindEagerViaRelation
()
{
{
$customerA
=
new
Customer
();
/** @var TestCase|ActiveRecordTestTrait $this */
$customerB
=
new
Customer
();
$orders
=
$this
->
callOrderFind
()
->
with
(
'items'
)
/*->orderBy('id')*/
->
all
();
// TODO this test is duplicated because of missing orderBy support in redis
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
$this
->
assertEquals
(
3
,
count
(
$orders
));
$order
=
$orders
[
0
];
$customerA
=
new
Customer
();
$this
->
assertEquals
(
1
,
$order
->
id
);
$customerB
=
new
Item
();
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
$this
->
assertEquals
(
1
,
$order
->
items
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$order
->
items
[
1
]
->
id
);
$customerA
=
Customer
::
find
(
1
);
$customerB
=
Customer
::
find
(
2
);
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
$customerB
=
Customer
::
find
(
1
);
$this
->
assertTrue
(
$customerA
->
equals
(
$customerB
));
$customerA
=
Customer
::
find
(
1
);
$customerB
=
Item
::
find
(
1
);
$this
->
assertFalse
(
$customerA
->
equals
(
$customerB
));
}
}
public
function
testFindCount
()
public
function
testFindCount
()
...
@@ -202,266 +160,14 @@ class ActiveRecordTest extends RedisTestCase
...
@@ -202,266 +160,14 @@ class ActiveRecordTest extends RedisTestCase
$this
->
assertEquals
(
1
,
Customer
::
find
()
->
offset
(
2
)
->
limit
(
2
)
->
count
());
$this
->
assertEquals
(
1
,
Customer
::
find
()
->
offset
(
2
)
->
limit
(
2
)
->
count
());
}
}
public
function
testFindLimit
()
{
// all()
$customers
=
Customer
::
find
()
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$customers
=
Customer
::
find
()
->
limit
(
1
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$this
->
assertEquals
(
'user1'
,
$customers
[
0
]
->
name
);
$customers
=
Customer
::
find
()
->
limit
(
1
)
->
offset
(
1
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$this
->
assertEquals
(
'user2'
,
$customers
[
0
]
->
name
);
$customers
=
Customer
::
find
()
->
limit
(
1
)
->
offset
(
2
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$this
->
assertEquals
(
'user3'
,
$customers
[
0
]
->
name
);
$customers
=
Customer
::
find
()
->
limit
(
2
)
->
offset
(
1
)
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
$this
->
assertEquals
(
'user2'
,
$customers
[
0
]
->
name
);
$this
->
assertEquals
(
'user3'
,
$customers
[
1
]
->
name
);
$customers
=
Customer
::
find
()
->
limit
(
2
)
->
offset
(
3
)
->
all
();
$this
->
assertEquals
(
0
,
count
(
$customers
));
// one()
$customer
=
Customer
::
find
()
->
one
();
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
$customer
=
Customer
::
find
()
->
offset
(
0
)
->
one
();
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
$customer
=
Customer
::
find
()
->
offset
(
1
)
->
one
();
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
=
Customer
::
find
()
->
offset
(
2
)
->
one
();
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
$customer
=
Customer
::
find
()
->
offset
(
3
)
->
one
();
$this
->
assertNull
(
$customer
);
}
public
function
testFindComplexCondition
()
{
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
where
([
'OR'
,
[
'id'
=>
1
],
[
'id'
=>
2
]])
->
count
());
$this
->
assertEquals
(
2
,
count
(
Customer
::
find
()
->
where
([
'OR'
,
[
'id'
=>
1
],
[
'id'
=>
2
]])
->
all
()));
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
where
([
'id'
=>
[
1
,
2
]])
->
count
());
$this
->
assertEquals
(
2
,
count
(
Customer
::
find
()
->
where
([
'id'
=>
[
1
,
2
]])
->
all
()));
$this
->
assertEquals
(
1
,
Customer
::
find
()
->
where
([
'AND'
,
[
'id'
=>
[
2
,
3
]],
[
'BETWEEN'
,
'status'
,
2
,
4
]])
->
count
());
$this
->
assertEquals
(
1
,
count
(
Customer
::
find
()
->
where
([
'AND'
,
[
'id'
=>
[
2
,
3
]],
[
'BETWEEN'
,
'status'
,
2
,
4
]])
->
all
()));
}
public
function
testSum
()
{
$this
->
assertEquals
(
6
,
OrderItem
::
find
()
->
count
());
$this
->
assertEquals
(
7
,
OrderItem
::
find
()
->
sum
(
'quantity'
));
}
public
function
testFindColumn
()
public
function
testFindColumn
()
{
{
$this
->
assertEquals
([
'user1'
,
'user2'
,
'user3'
],
Customer
::
find
()
->
column
(
'name'
));
$this
->
assertEquals
([
'user1'
,
'user2'
,
'user3'
],
Customer
::
find
()
->
column
(
'name'
));
// TODO $this->assertEquals(['user3', 'user2', 'user1'], Customer::find()->orderBy(['name' => SORT_DESC])->column('name'));
// TODO $this->assertEquals(['user3', 'user2', 'user1'], Customer::find()->orderBy(['name' => SORT_DESC])->column('name'));
}
}
public
function
testExists
()
{
$this
->
assertTrue
(
Customer
::
find
()
->
where
([
'id'
=>
2
])
->
exists
());
$this
->
assertFalse
(
Customer
::
find
()
->
where
([
'id'
=>
5
])
->
exists
());
}
public
function
testFindLazy
()
{
/** @var $customer Customer */
$customer
=
Customer
::
find
(
2
);
$orders
=
$customer
->
orders
;
$this
->
assertEquals
(
2
,
count
(
$orders
));
$orders
=
$customer
->
getOrders
()
->
where
([
'id'
=>
3
])
->
all
();
$this
->
assertEquals
(
1
,
count
(
$orders
));
$this
->
assertEquals
(
3
,
$orders
[
0
]
->
id
);
}
public
function
testFindEager
()
{
$customers
=
Customer
::
find
()
->
with
(
'orders'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertEquals
(
1
,
count
(
$customers
[
0
]
->
orders
));
$this
->
assertEquals
(
2
,
count
(
$customers
[
1
]
->
orders
));
}
public
function
testFindLazyVia
()
{
/** @var $order Order */
$order
=
Order
::
find
(
1
);
$this
->
assertEquals
(
1
,
$order
->
id
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
1
,
$order
->
items
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$order
->
items
[
1
]
->
id
);
$order
=
Order
::
find
(
1
);
$order
->
id
=
100
;
$this
->
assertEquals
([],
$order
->
items
);
}
public
function
testFindEagerViaRelation
()
{
$orders
=
Order
::
find
()
->
with
(
'items'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$orders
));
$order
=
$orders
[
0
];
$this
->
assertEquals
(
1
,
$order
->
id
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
1
,
$order
->
items
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$order
->
items
[
1
]
->
id
);
}
public
function
testFindNestedRelation
()
{
$customers
=
Customer
::
find
()
->
with
(
'orders'
,
'orders.items'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertEquals
(
1
,
count
(
$customers
[
0
]
->
orders
));
$this
->
assertEquals
(
2
,
count
(
$customers
[
1
]
->
orders
));
$this
->
assertEquals
(
0
,
count
(
$customers
[
2
]
->
orders
));
$this
->
assertEquals
(
2
,
count
(
$customers
[
0
]
->
orders
[
0
]
->
items
));
$this
->
assertEquals
(
3
,
count
(
$customers
[
1
]
->
orders
[
0
]
->
items
));
$this
->
assertEquals
(
1
,
count
(
$customers
[
1
]
->
orders
[
1
]
->
items
));
}
public
function
testLink
()
{
$customer
=
Customer
::
find
(
2
);
$this
->
assertEquals
(
2
,
count
(
$customer
->
orders
));
// has many
$order
=
new
Order
;
$order
->
total
=
100
;
$this
->
assertTrue
(
$order
->
isNewRecord
);
$customer
->
link
(
'orders'
,
$order
);
$this
->
assertEquals
(
3
,
count
(
$customer
->
orders
));
$this
->
assertFalse
(
$order
->
isNewRecord
);
$this
->
assertEquals
(
3
,
count
(
$customer
->
getOrders
()
->
all
()));
$this
->
assertEquals
(
2
,
$order
->
customer_id
);
// belongs to
$order
=
new
Order
;
$order
->
total
=
100
;
$this
->
assertTrue
(
$order
->
isNewRecord
);
$customer
=
Customer
::
find
(
1
);
$this
->
assertNull
(
$order
->
customer
);
$order
->
link
(
'customer'
,
$customer
);
$this
->
assertFalse
(
$order
->
isNewRecord
);
$this
->
assertEquals
(
1
,
$order
->
customer_id
);
$this
->
assertEquals
(
1
,
$order
->
customer
->
id
);
// via model
$order
=
Order
::
find
(
1
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
2
,
count
(
$order
->
orderItems
));
$orderItem
=
OrderItem
::
find
([
'order_id'
=>
1
,
'item_id'
=>
3
]);
$this
->
assertNull
(
$orderItem
);
$item
=
Item
::
find
(
3
);
$order
->
link
(
'items'
,
$item
,
[
'quantity'
=>
10
,
'subtotal'
=>
100
]);
$this
->
assertEquals
(
3
,
count
(
$order
->
items
));
$this
->
assertEquals
(
3
,
count
(
$order
->
orderItems
));
$orderItem
=
OrderItem
::
find
([
'order_id'
=>
1
,
'item_id'
=>
3
]);
$this
->
assertTrue
(
$orderItem
instanceof
OrderItem
);
$this
->
assertEquals
(
10
,
$orderItem
->
quantity
);
$this
->
assertEquals
(
100
,
$orderItem
->
subtotal
);
}
public
function
testUnlink
()
{
// has many
$customer
=
Customer
::
find
(
2
);
$this
->
assertEquals
(
2
,
count
(
$customer
->
orders
));
$customer
->
unlink
(
'orders'
,
$customer
->
orders
[
1
],
true
);
$this
->
assertEquals
(
1
,
count
(
$customer
->
orders
));
$this
->
assertNull
(
Order
::
find
(
3
));
// via model
$order
=
Order
::
find
(
2
);
$this
->
assertEquals
(
3
,
count
(
$order
->
items
));
$this
->
assertEquals
(
3
,
count
(
$order
->
orderItems
));
$order
->
unlink
(
'items'
,
$order
->
items
[
2
],
true
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
2
,
count
(
$order
->
orderItems
));
}
public
function
testInsert
()
{
$customer
=
new
Customer
;
$customer
->
email
=
'user4@example.com'
;
$customer
->
name
=
'user4'
;
$customer
->
address
=
'address4'
;
$this
->
assertNull
(
$customer
->
id
);
$this
->
assertTrue
(
$customer
->
isNewRecord
);
$customer
->
save
();
$this
->
assertEquals
(
4
,
$customer
->
id
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
}
// TODO test serial column incr
// TODO test serial column incr
public
function
testUpdate
()
{
// save
$customer
=
Customer
::
find
(
2
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
$customer
->
name
=
'user2x'
;
$customer
->
save
();
$this
->
assertEquals
(
'user2x'
,
$customer
->
name
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
$customer2
=
Customer
::
find
(
2
);
$this
->
assertEquals
(
'user2x'
,
$customer2
->
name
);
// updateAll
$customer
=
Customer
::
find
(
3
);
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
$ret
=
Customer
::
updateAll
(
array
(
'name'
=>
'temp'
,
),
[
'id'
=>
3
]);
$this
->
assertEquals
(
1
,
$ret
);
$customer
=
Customer
::
find
(
3
);
$this
->
assertEquals
(
'temp'
,
$customer
->
name
);
}
public
function
testUpdateCounters
()
{
// updateCounters
$pk
=
[
'order_id'
=>
2
,
'item_id'
=>
4
];
$orderItem
=
OrderItem
::
find
(
$pk
);
$this
->
assertEquals
(
1
,
$orderItem
->
quantity
);
$ret
=
$orderItem
->
updateCounters
([
'quantity'
=>
-
1
]);
$this
->
assertTrue
(
$ret
);
$this
->
assertEquals
(
0
,
$orderItem
->
quantity
);
$orderItem
=
OrderItem
::
find
(
$pk
);
$this
->
assertEquals
(
0
,
$orderItem
->
quantity
);
// updateAllCounters
$pk
=
[
'order_id'
=>
1
,
'item_id'
=>
2
];
$orderItem
=
OrderItem
::
find
(
$pk
);
$this
->
assertEquals
(
2
,
$orderItem
->
quantity
);
$ret
=
OrderItem
::
updateAllCounters
(
array
(
'quantity'
=>
3
,
'subtotal'
=>
-
10
,
),
$pk
);
$this
->
assertEquals
(
1
,
$ret
);
$orderItem
=
OrderItem
::
find
(
$pk
);
$this
->
assertEquals
(
5
,
$orderItem
->
quantity
);
$this
->
assertEquals
(
30
,
$orderItem
->
subtotal
);
}
public
function
testUpdatePk
()
public
function
testUpdatePk
()
{
{
// updateCounters
// updateCounters
...
@@ -477,23 +183,4 @@ class ActiveRecordTest extends RedisTestCase
...
@@ -477,23 +183,4 @@ class ActiveRecordTest extends RedisTestCase
$this
->
assertNull
(
OrderItem
::
find
(
$pk
));
$this
->
assertNull
(
OrderItem
::
find
(
$pk
));
$this
->
assertNotNull
(
OrderItem
::
find
([
'order_id'
=>
2
,
'item_id'
=>
10
]));
$this
->
assertNotNull
(
OrderItem
::
find
([
'order_id'
=>
2
,
'item_id'
=>
10
]));
}
}
public
function
testDelete
()
{
// delete
$customer
=
Customer
::
find
(
2
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
->
delete
();
$customer
=
Customer
::
find
(
2
);
$this
->
assertNull
(
$customer
);
// deleteAll
$customers
=
Customer
::
find
()
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
$ret
=
Customer
::
deleteAll
();
$this
->
assertEquals
(
2
,
$ret
);
$customers
=
Customer
::
find
()
->
all
();
$this
->
assertEquals
(
0
,
count
(
$customers
));
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment