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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
f7a6cb9f
Commit
f7a6cb9f
authored
Mar 30, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored AR tests.
parent
338b6aa7
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
99 deletions
+42
-99
ActiveRecordTest.php
tests/unit/extensions/elasticsearch/ActiveRecordTest.php
+13
-33
ActiveRecordTest.php
tests/unit/extensions/redis/ActiveRecordTest.php
+19
-36
ActiveRecordTestTrait.php
tests/unit/framework/ar/ActiveRecordTestTrait.php
+0
-0
ActiveRecordTest.php
tests/unit/framework/db/ActiveRecordTest.php
+10
-30
No files found.
tests/unit/extensions/elasticsearch/ActiveRecordTest.php
View file @
f7a6cb9f
...
...
@@ -20,26 +20,6 @@ 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
();
...
...
@@ -164,7 +144,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
public
function
testFindAsArray
()
{
// asArray
$customer
=
$this
->
callCustomerF
ind
()
->
where
([
'id'
=>
2
])
->
asArray
()
->
one
();
$customer
=
Customer
::
f
ind
()
->
where
([
'id'
=>
2
])
->
asArray
()
->
one
();
$this
->
assertEquals
([
'id'
=>
2
,
'email'
=>
'user2@example.com'
,
...
...
@@ -177,7 +157,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
public
function
testSearch
()
{
$customers
=
$this
->
callCustomerF
ind
()
->
search
()[
'hits'
];
$customers
=
Customer
::
f
ind
()
->
search
()[
'hits'
];
$this
->
assertEquals
(
3
,
$customers
[
'total'
]);
$this
->
assertEquals
(
3
,
count
(
$customers
[
'hits'
]));
$this
->
assertTrue
(
$customers
[
'hits'
][
0
]
instanceof
Customer
);
...
...
@@ -185,12 +165,12 @@ class ActiveRecordTest extends ElasticSearchTestCase
$this
->
assertTrue
(
$customers
[
'hits'
][
2
]
instanceof
Customer
);
// limit vs. totalcount
$customers
=
$this
->
callCustomerF
ind
()
->
limit
(
2
)
->
search
()[
'hits'
];
$customers
=
Customer
::
f
ind
()
->
limit
(
2
)
->
search
()[
'hits'
];
$this
->
assertEquals
(
3
,
$customers
[
'total'
]);
$this
->
assertEquals
(
2
,
count
(
$customers
[
'hits'
]));
// asArray
$result
=
$this
->
callCustomerF
ind
()
->
asArray
()
->
search
()[
'hits'
];
$result
=
Customer
::
f
ind
()
->
asArray
()
->
search
()[
'hits'
];
$this
->
assertEquals
(
3
,
$result
[
'total'
]);
$customers
=
$result
[
'hits'
];
$this
->
assertEquals
(
3
,
count
(
$customers
));
...
...
@@ -213,7 +193,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
// TODO test asArray() + fields() + indexBy()
// find by attributes
$result
=
$this
->
callCustomerF
ind
()
->
where
([
'name'
=>
'user2'
])
->
search
()[
'hits'
];
$result
=
Customer
::
f
ind
()
->
where
([
'name'
=>
'user2'
])
->
search
()[
'hits'
];
$customer
=
reset
(
$result
[
'hits'
]);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
2
,
$customer
->
id
);
...
...
@@ -223,7 +203,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
public
function
testSearchFacets
()
{
$result
=
$this
->
callCustomerF
ind
()
->
addStatisticalFacet
(
'status_stats'
,
[
'field'
=>
'status'
])
->
search
();
$result
=
Customer
::
f
ind
()
->
addStatisticalFacet
(
'status_stats'
,
[
'field'
=>
'status'
])
->
search
();
$this
->
assertArrayHasKey
(
'facets'
,
$result
);
$this
->
assertEquals
(
3
,
$result
[
'facets'
][
'status_stats'
][
'count'
]);
$this
->
assertEquals
(
4
,
$result
[
'facets'
][
'status_stats'
][
'total'
]);
// sum of values
...
...
@@ -400,10 +380,10 @@ class ActiveRecordTest extends ElasticSearchTestCase
$customer
->
save
(
false
);
$this
->
afterSave
();
$customers
=
$this
->
callCustomerF
ind
()
->
where
([
'status'
=>
true
])
->
all
();
$customers
=
Customer
::
f
ind
()
->
where
([
'status'
=>
true
])
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$customers
=
$this
->
callCustomerF
ind
()
->
where
([
'status'
=>
false
])
->
all
();
$customers
=
Customer
::
f
ind
()
->
where
([
'status'
=>
false
])
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
}
...
...
@@ -411,7 +391,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
{
/** @var TestCase|ActiveRecordTestTrait $this */
// indexBy + asArray
$customers
=
$this
->
callCustomerF
ind
()
->
asArray
()
->
fields
([
'id'
,
'name'
])
->
all
();
$customers
=
Customer
::
f
ind
()
->
asArray
()
->
fields
([
'id'
,
'name'
])
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertArrayHasKey
(
'id'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'name'
,
$customers
[
0
]);
...
...
@@ -435,7 +415,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
$customerClass
=
$this
->
getCustomerClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
// indexBy + asArray
$customers
=
$this
->
callCustomerF
ind
()
->
indexBy
(
'name'
)
->
fields
(
'id'
,
'name'
)
->
all
();
$customers
=
Customer
::
f
ind
()
->
indexBy
(
'name'
)
->
fields
(
'id'
,
'name'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
'user1'
]
instanceof
$customerClass
);
$this
->
assertTrue
(
$customers
[
'user2'
]
instanceof
$customerClass
);
...
...
@@ -457,7 +437,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
$this
->
assertNull
(
$customers
[
'user3'
]
->
status
);
// indexBy callable + asArray
$customers
=
$this
->
callCustomerF
ind
()
->
indexBy
(
function
(
$customer
)
{
$customers
=
Customer
::
f
ind
()
->
indexBy
(
function
(
$customer
)
{
return
$customer
->
id
.
'-'
.
$customer
->
name
;
})
->
fields
(
'id'
,
'name'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
...
...
@@ -485,7 +465,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
{
/** @var TestCase|ActiveRecordTestTrait $this */
// indexBy + asArray
$customers
=
$this
->
callCustomerF
ind
()
->
indexBy
(
'name'
)
->
asArray
()
->
fields
(
'id'
,
'name'
)
->
all
();
$customers
=
Customer
::
f
ind
()
->
indexBy
(
'name'
)
->
asArray
()
->
fields
(
'id'
,
'name'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertArrayHasKey
(
'id'
,
$customers
[
'user1'
]);
$this
->
assertArrayHasKey
(
'name'
,
$customers
[
'user1'
]);
...
...
@@ -504,7 +484,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
$this
->
assertArrayNotHasKey
(
'status'
,
$customers
[
'user3'
]);
// indexBy callable + asArray
$customers
=
$this
->
callCustomerF
ind
()
->
indexBy
(
function
(
$customer
)
{
$customers
=
Customer
::
f
ind
()
->
indexBy
(
function
(
$customer
)
{
return
$customer
[
'id'
]
.
'-'
.
$customer
[
'name'
];
})
->
asArray
()
->
fields
(
'id'
,
'name'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
...
...
tests/unit/extensions/redis/ActiveRecordTest.php
View file @
f7a6cb9f
...
...
@@ -16,26 +16,6 @@ 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
();
...
...
@@ -142,7 +122,7 @@ class ActiveRecordTest extends RedisTestCase
$this
->
markTestSkipped
(
'Redis does not support orderBy.'
);
}
public
function
testSatisticalFind
()
public
function
testS
t
atisticalFind
()
{
// find count, sum, average, min, max, scalar
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
());
...
...
@@ -155,19 +135,19 @@ class ActiveRecordTest extends RedisTestCase
$this
->
assertEquals
(
7
,
OrderItem
::
find
()
->
sum
(
'quantity'
));
}
public
function
test
f
indIndexBy
()
public
function
test
F
indIndexBy
()
{
$customerClass
=
$this
->
getCustomerClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
// indexBy
$customers
=
$this
->
callCustomerF
ind
()
->
indexBy
(
'name'
)
/*->orderBy('id')*/
->
all
();
$customers
=
Customer
::
f
ind
()
->
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
->
callCustomerF
ind
()
->
indexBy
(
function
(
$customer
)
{
$customers
=
Customer
::
f
ind
()
->
indexBy
(
function
(
$customer
)
{
return
$customer
->
id
.
'-'
.
$customer
->
name
;
})
/*->orderBy('id')*/
->
all
();
// TODO this test is duplicated because of missing orderBy support in redis
$this
->
assertEquals
(
3
,
count
(
$customers
));
...
...
@@ -181,50 +161,53 @@ class ActiveRecordTest extends RedisTestCase
// TODO this test is duplicated because of missing orderBy support in redis
/** @var TestCase|ActiveRecordTestTrait $this */
// all()
$customers
=
$this
->
callCustomerF
ind
()
->
all
();
$customers
=
Customer
::
f
ind
()
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$customers
=
$this
->
callCustomerF
ind
()
/*->orderBy('id')*/
->
limit
(
1
)
->
all
();
$customers
=
Customer
::
f
ind
()
/*->orderBy('id')*/
->
limit
(
1
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$this
->
assertEquals
(
'user1'
,
$customers
[
0
]
->
name
);
$customers
=
$this
->
callCustomerF
ind
()
/*->orderBy('id')*/
->
limit
(
1
)
->
offset
(
1
)
->
all
();
$customers
=
Customer
::
f
ind
()
/*->orderBy('id')*/
->
limit
(
1
)
->
offset
(
1
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$this
->
assertEquals
(
'user2'
,
$customers
[
0
]
->
name
);
$customers
=
$this
->
callCustomerF
ind
()
/*->orderBy('id')*/
->
limit
(
1
)
->
offset
(
2
)
->
all
();
$customers
=
Customer
::
f
ind
()
/*->orderBy('id')*/
->
limit
(
1
)
->
offset
(
2
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$this
->
assertEquals
(
'user3'
,
$customers
[
0
]
->
name
);
$customers
=
$this
->
callCustomerF
ind
()
/*->orderBy('id')*/
->
limit
(
2
)
->
offset
(
1
)
->
all
();
$customers
=
Customer
::
f
ind
()
/*->orderBy('id')*/
->
limit
(
2
)
->
offset
(
1
)
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
$this
->
assertEquals
(
'user2'
,
$customers
[
0
]
->
name
);
$this
->
assertEquals
(
'user3'
,
$customers
[
1
]
->
name
);
$customers
=
$this
->
callCustomerF
ind
()
->
limit
(
2
)
->
offset
(
3
)
->
all
();
$customers
=
Customer
::
f
ind
()
->
limit
(
2
)
->
offset
(
3
)
->
all
();
$this
->
assertEquals
(
0
,
count
(
$customers
));
// one()
$customer
=
$this
->
callCustomerF
ind
()
/*->orderBy('id')*/
->
one
();
$customer
=
Customer
::
f
ind
()
/*->orderBy('id')*/
->
one
();
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
$customer
=
$this
->
callCustomerF
ind
()
/*->orderBy('id')*/
->
offset
(
0
)
->
one
();
$customer
=
Customer
::
f
ind
()
/*->orderBy('id')*/
->
offset
(
0
)
->
one
();
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
$customer
=
$this
->
callCustomerF
ind
()
/*->orderBy('id')*/
->
offset
(
1
)
->
one
();
$customer
=
Customer
::
f
ind
()
/*->orderBy('id')*/
->
offset
(
1
)
->
one
();
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
=
$this
->
callCustomerF
ind
()
/*->orderBy('id')*/
->
offset
(
2
)
->
one
();
$customer
=
Customer
::
f
ind
()
/*->orderBy('id')*/
->
offset
(
2
)
->
one
();
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
$customer
=
$this
->
callCustomerF
ind
()
->
offset
(
3
)
->
one
();
$customer
=
Customer
::
f
ind
()
->
offset
(
3
)
->
one
();
$this
->
assertNull
(
$customer
);
}
public
function
testFindEagerViaRelation
()
{
/** @var \yii\db\ActiveRecordInterface $orderClass */
$orderClass
=
$this
->
getOrderClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
$orders
=
$
this
->
callOrderF
ind
()
->
with
(
'items'
)
/*->orderBy('id')*/
->
all
();
// TODO this test is duplicated because of missing orderBy support in redis
$orders
=
$
orderClass
::
f
ind
()
->
with
(
'items'
)
/*->orderBy('id')*/
->
all
();
// TODO this test is duplicated because of missing orderBy support in redis
$this
->
assertEquals
(
3
,
count
(
$orders
));
$order
=
$orders
[
0
];
$this
->
assertEquals
(
1
,
$order
->
id
);
...
...
tests/unit/framework/ar/ActiveRecordTestTrait.php
View file @
f7a6cb9f
This diff is collapsed.
Click to expand it.
tests/unit/framework/db/ActiveRecordTest.php
View file @
f7a6cb9f
...
...
@@ -26,26 +26,6 @@ class ActiveRecordTest extends DatabaseTestCase
ActiveRecord
::
$db
=
$this
->
getConnection
();
}
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
();
...
...
@@ -69,7 +49,7 @@ class ActiveRecordTest extends DatabaseTestCase
public
function
testCustomColumns
()
{
// find custom column
$customer
=
$this
->
callCustomerF
ind
()
->
select
([
'*'
,
'(status*2) AS status2'
])
$customer
=
Customer
::
f
ind
()
->
select
([
'*'
,
'(status*2) AS status2'
])
->
where
([
'name'
=>
'user3'
])
->
one
();
$this
->
assertEquals
(
3
,
$customer
->
id
);
$this
->
assertEquals
(
4
,
$customer
->
status2
);
...
...
@@ -78,19 +58,19 @@ class ActiveRecordTest extends DatabaseTestCase
public
function
testStatisticalFind
()
{
// find count, sum, average, min, max, scalar
$this
->
assertEquals
(
3
,
$this
->
callCustomerF
ind
()
->
count
());
$this
->
assertEquals
(
2
,
$this
->
callCustomerF
ind
()
->
where
(
'id=1 OR id=2'
)
->
count
());
$this
->
assertEquals
(
6
,
$this
->
callCustomerF
ind
()
->
sum
(
'id'
));
$this
->
assertEquals
(
2
,
$this
->
callCustomerF
ind
()
->
average
(
'id'
));
$this
->
assertEquals
(
1
,
$this
->
callCustomerF
ind
()
->
min
(
'id'
));
$this
->
assertEquals
(
3
,
$this
->
callCustomerF
ind
()
->
max
(
'id'
));
$this
->
assertEquals
(
3
,
$this
->
callCustomerF
ind
()
->
select
(
'COUNT(*)'
)
->
scalar
());
$this
->
assertEquals
(
3
,
Customer
::
f
ind
()
->
count
());
$this
->
assertEquals
(
2
,
Customer
::
f
ind
()
->
where
(
'id=1 OR id=2'
)
->
count
());
$this
->
assertEquals
(
6
,
Customer
::
f
ind
()
->
sum
(
'id'
));
$this
->
assertEquals
(
2
,
Customer
::
f
ind
()
->
average
(
'id'
));
$this
->
assertEquals
(
1
,
Customer
::
f
ind
()
->
min
(
'id'
));
$this
->
assertEquals
(
3
,
Customer
::
f
ind
()
->
max
(
'id'
));
$this
->
assertEquals
(
3
,
Customer
::
f
ind
()
->
select
(
'COUNT(*)'
)
->
scalar
());
}
public
function
testFindScalar
()
{
// query scalar
$customerName
=
$this
->
callCustomerF
ind
()
->
where
([
'id'
=>
2
])
->
select
(
'name'
)
->
scalar
();
$customerName
=
Customer
::
f
ind
()
->
where
([
'id'
=>
2
])
->
select
(
'name'
)
->
scalar
();
$this
->
assertEquals
(
'user2'
,
$customerName
);
}
...
...
@@ -168,7 +148,7 @@ class ActiveRecordTest extends DatabaseTestCase
public
function
testDeeplyNestedTableRelation
()
{
/** @var Customer $customer */
$customer
=
$this
->
callCustomerF
ind
(
1
);
$customer
=
Customer
::
f
ind
(
1
);
$this
->
assertNotNull
(
$customer
);
$items
=
$customer
->
orderItems
;
...
...
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