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
d5375e8c
Commit
d5375e8c
authored
Jul 20, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added elasticsearch test case for array attribute relations
parent
1a07485e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
4 deletions
+63
-4
Order.php
tests/unit/data/ar/elasticsearch/Order.php
+9
-1
ActiveRecordTest.php
tests/unit/extensions/elasticsearch/ActiveRecordTest.php
+54
-3
No files found.
tests/unit/data/ar/elasticsearch/Order.php
View file @
d5375e8c
...
@@ -21,7 +21,7 @@ class Order extends ActiveRecord
...
@@ -21,7 +21,7 @@ class Order extends ActiveRecord
public
function
attributes
()
public
function
attributes
()
{
{
return
[
'id'
,
'customer_id'
,
'created_at'
,
'total'
];
return
[
'id'
,
'customer_id'
,
'created_at'
,
'total'
,
'itemsArray'
];
}
}
public
function
getCustomer
()
public
function
getCustomer
()
...
@@ -34,6 +34,14 @@ class Order extends ActiveRecord
...
@@ -34,6 +34,14 @@ class Order extends ActiveRecord
return
$this
->
hasMany
(
OrderItem
::
className
(),
[
'order_id'
=>
'id'
]);
return
$this
->
hasMany
(
OrderItem
::
className
(),
[
'order_id'
=>
'id'
]);
}
}
/**
* A relation to Item defined via array valued attribute
*/
public
function
getItemsByArrayValue
()
{
return
$this
->
hasMany
(
Item
::
className
(),
[
'id'
=>
'itemsArray'
])
->
indexBy
(
'id'
);
}
public
function
getItems
()
public
function
getItems
()
{
{
return
$this
->
hasMany
(
Item
::
className
(),
[
'id'
=>
'item_id'
])
return
$this
->
hasMany
(
Item
::
className
(),
[
'id'
=>
'item_id'
])
...
...
tests/unit/extensions/elasticsearch/ActiveRecordTest.php
View file @
d5375e8c
...
@@ -121,15 +121,15 @@ class ActiveRecordTest extends ElasticSearchTestCase
...
@@ -121,15 +121,15 @@ class ActiveRecordTest extends ElasticSearchTestCase
$order
=
new
Order
();
$order
=
new
Order
();
$order
->
id
=
1
;
$order
->
id
=
1
;
$order
->
setAttributes
([
'customer_id'
=>
1
,
'created_at'
=>
1325282384
,
'total'
=>
110.0
],
false
);
$order
->
setAttributes
([
'customer_id'
=>
1
,
'created_at'
=>
1325282384
,
'total'
=>
110.0
,
'itemsArray'
=>
[
1
,
2
]
],
false
);
$order
->
save
(
false
);
$order
->
save
(
false
);
$order
=
new
Order
();
$order
=
new
Order
();
$order
->
id
=
2
;
$order
->
id
=
2
;
$order
->
setAttributes
([
'customer_id'
=>
2
,
'created_at'
=>
1325334482
,
'total'
=>
33.0
],
false
);
$order
->
setAttributes
([
'customer_id'
=>
2
,
'created_at'
=>
1325334482
,
'total'
=>
33.0
,
'itemsArray'
=>
[
4
,
5
,
3
]
],
false
);
$order
->
save
(
false
);
$order
->
save
(
false
);
$order
=
new
Order
();
$order
=
new
Order
();
$order
->
id
=
3
;
$order
->
id
=
3
;
$order
->
setAttributes
([
'customer_id'
=>
2
,
'created_at'
=>
1325502201
,
'total'
=>
40.0
],
false
);
$order
->
setAttributes
([
'customer_id'
=>
2
,
'created_at'
=>
1325502201
,
'total'
=>
40.0
,
'itemsArray'
=>
[
2
]
],
false
);
$order
->
save
(
false
);
$order
->
save
(
false
);
$orderItem
=
new
OrderItem
();
$orderItem
=
new
OrderItem
();
...
@@ -696,6 +696,57 @@ class ActiveRecordTest extends ElasticSearchTestCase
...
@@ -696,6 +696,57 @@ class ActiveRecordTest extends ElasticSearchTestCase
$this
->
assertEquals
(
0
,
count
(
$orderItems
));
$this
->
assertEquals
(
0
,
count
(
$orderItems
));
}
}
public
function
testArrayAttributes
()
{
$this
->
assertTrue
(
is_array
(
Order
::
findOne
(
1
)
->
itemsArray
));
$this
->
assertTrue
(
is_array
(
Order
::
findOne
(
2
)
->
itemsArray
));
$this
->
assertTrue
(
is_array
(
Order
::
findOne
(
3
)
->
itemsArray
));
}
public
function
testArrayAttributeRelationLazy
()
{
$order
=
Order
::
findOne
(
1
);
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
2
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
1
]));
$this
->
assertTrue
(
isset
(
$items
[
2
]));
$this
->
assertTrue
(
$items
[
1
]
instanceof
Item
);
$this
->
assertTrue
(
$items
[
2
]
instanceof
Item
);
$order
=
Order
::
findOne
(
2
);
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
3
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
3
]));
$this
->
assertTrue
(
isset
(
$items
[
4
]));
$this
->
assertTrue
(
isset
(
$items
[
5
]));
$this
->
assertTrue
(
$items
[
3
]
instanceof
Item
);
$this
->
assertTrue
(
$items
[
4
]
instanceof
Item
);
$this
->
assertTrue
(
$items
[
5
]
instanceof
Item
);
}
public
function
testArrayAttributeRelationEager
()
{
/* @var $order Order */
$order
=
Order
::
find
()
->
with
(
'itemsByArrayValue'
)
->
where
([
'id'
=>
1
])
->
one
();
$this
->
assertTrue
(
$order
->
isRelationPopulated
(
'itemsByArrayValue'
));
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
2
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
1
]));
$this
->
assertTrue
(
isset
(
$items
[
2
]));
$this
->
assertTrue
(
$items
[
1
]
instanceof
Item
);
$this
->
assertTrue
(
$items
[
2
]
instanceof
Item
);
/* @var $order Order */
$order
=
Order
::
find
()
->
with
(
'itemsByArrayValue'
)
->
where
([
'id'
=>
2
])
->
one
();
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
3
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
3
]));
$this
->
assertTrue
(
isset
(
$items
[
4
]));
$this
->
assertTrue
(
isset
(
$items
[
5
]));
$this
->
assertTrue
(
$items
[
3
]
instanceof
Item
);
$this
->
assertTrue
(
$items
[
4
]
instanceof
Item
);
$this
->
assertTrue
(
$items
[
5
]
instanceof
Item
);
}
// TODO test AR with not mapped PK
// TODO test AR with not mapped PK
}
}
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