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
3fd6d95a
Commit
3fd6d95a
authored
Nov 29, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Unit test for Mongo Active Relation added.
parent
479fbf77
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
118 additions
and
0 deletions
+118
-0
Customer.php
tests/unit/data/ar/mongo/Customer.php
+6
-0
CustomerOrder.php
tests/unit/data/ar/mongo/CustomerOrder.php
+28
-0
ActiveRelationTest.php
tests/unit/extensions/mongo/ActiveRelationTest.php
+84
-0
No files found.
tests/unit/data/ar/mongo/Customer.php
View file @
3fd6d95a
...
...
@@ -24,4 +24,9 @@ class Customer extends ActiveRecord
{
$query
->
andWhere
([
'status'
=>
2
]);
}
public
function
getOrders
()
{
return
$this
->
hasMany
(
CustomerOrder
::
className
(),
[
'customer_id'
=>
'id'
]);
}
}
\ No newline at end of file
tests/unit/data/ar/mongo/CustomerOrder.php
0 → 100644
View file @
3fd6d95a
<?php
namespace
yiiunit\data\ar\mongo
;
class
CustomerOrder
extends
ActiveRecord
{
public
static
function
collectionName
()
{
return
'customer_order'
;
}
public
function
attributes
()
{
return
[
'_id'
,
'number'
,
'customer_id'
,
'items'
,
];
}
public
function
getCustomer
()
{
return
$this
->
hasOne
(
Customer
::
className
(),
[
'id'
=>
'customer_id'
]);
}
}
\ No newline at end of file
tests/unit/extensions/mongo/ActiveRelationTest.php
0 → 100644
View file @
3fd6d95a
<?php
namespace
yiiunit\extensions\mongo
;
use
yiiunit\data\ar\mongo\ActiveRecord
;
use
yiiunit\data\ar\mongo\Customer
;
use
yiiunit\data\ar\mongo\CustomerOrder
;
/**
* @group mongo
*/
class
ActiveRelationTest
extends
MongoTestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
ActiveRecord
::
$db
=
$this
->
getConnection
();
$this
->
setUpTestRows
();
}
protected
function
tearDown
()
{
$this
->
dropCollection
(
Customer
::
collectionName
());
$this
->
dropCollection
(
CustomerOrder
::
collectionName
());
parent
::
tearDown
();
}
/**
* Sets up test rows.
*/
protected
function
setUpTestRows
()
{
$customerCollection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$customers
=
[];
for
(
$i
=
1
;
$i
<=
5
;
$i
++
)
{
$customers
[]
=
[
'name'
=>
'name'
.
$i
,
'email'
=>
'email'
.
$i
,
'address'
=>
'address'
.
$i
,
'status'
=>
$i
,
];
}
$customerCollection
->
batchInsert
(
$customers
);
$customerOrderCollection
=
$this
->
getConnection
()
->
getCollection
(
'customer_order'
);
$customerOrders
=
[];
foreach
(
$customers
as
$customer
)
{
$customerOrders
[]
=
[
'customer_id'
=>
$customer
[
'_id'
],
'number'
=>
$customer
[
'status'
],
];
$customerOrders
[]
=
[
'customer_id'
=>
$customer
[
'_id'
],
'number'
=>
$customer
[
'status'
]
+
1
,
];
}
$customerOrderCollection
->
batchInsert
(
$customerOrders
);
}
// Tests :
public
function
testFindLazy
()
{
/** @var CustomerOrder $order */
$order
=
CustomerOrder
::
find
([
'number'
=>
2
]);
$this
->
assertFalse
(
$order
->
isRelationPopulated
(
'customer'
));
$index
=
$order
->
customer
;
$this
->
assertTrue
(
$order
->
isRelationPopulated
(
'customer'
));
$this
->
assertTrue
(
$index
instanceof
Customer
);
$this
->
assertEquals
(
1
,
count
(
$order
->
populatedRelations
));
}
public
function
testFindEager
()
{
$orders
=
CustomerOrder
::
find
()
->
with
(
'customer'
)
->
all
();
$this
->
assertEquals
(
10
,
count
(
$orders
));
$this
->
assertTrue
(
$orders
[
0
]
->
isRelationPopulated
(
'customer'
));
$this
->
assertTrue
(
$orders
[
1
]
->
isRelationPopulated
(
'customer'
));
$this
->
assertTrue
(
$orders
[
0
]
->
index
instanceof
ArticleIndex
);
$this
->
assertTrue
(
$orders
[
1
]
->
index
instanceof
ArticleIndex
);
}
}
\ 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