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
325fc281
Commit
325fc281
authored
Nov 24, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
several fixes and cleanup
parent
47705f0b
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
134 additions
and
111 deletions
+134
-111
ActiveRecord.php
framework/yii/elasticsearch/ActiveRecord.php
+6
-1
Command.php
framework/yii/elasticsearch/Command.php
+7
-0
Order.php
tests/unit/data/ar/elasticsearch/Order.php
+1
-1
ActiveRecordTestTrait.php
tests/unit/framework/ar/ActiveRecordTestTrait.php
+16
-2
ActiveRecordTest.php
tests/unit/framework/elasticsearch/ActiveRecordTest.php
+99
-104
ElasticSearchConnectionTest.php
...t/framework/elasticsearch/ElasticSearchConnectionTest.php
+3
-0
ElasticSearchTestCase.php
tests/unit/framework/elasticsearch/ElasticSearchTestCase.php
+1
-1
QueryTest.php
tests/unit/framework/elasticsearch/QueryTest.php
+1
-2
No files found.
framework/yii/elasticsearch/ActiveRecord.php
View file @
325fc281
...
...
@@ -51,7 +51,12 @@ class ActiveRecord extends \yii\db\ActiveRecord
$query
=
static
::
createQuery
();
if
(
is_array
(
$q
))
{
if
(
count
(
$q
)
==
1
&&
(
array_key_exists
(
ActiveRecord
::
PRIMARY_KEY_NAME
,
$q
)))
{
return
static
::
get
(
$q
[
ActiveRecord
::
PRIMARY_KEY_NAME
]);
$pk
=
$q
[
ActiveRecord
::
PRIMARY_KEY_NAME
];
if
(
is_array
(
$pk
))
{
return
static
::
mget
(
$pk
);
}
else
{
return
static
::
get
(
$pk
);
}
}
return
$query
->
where
(
$q
)
->
one
();
}
elseif
(
$q
!==
null
)
{
...
...
framework/yii/elasticsearch/Command.php
View file @
325fc281
...
...
@@ -87,11 +87,18 @@ class Command extends Component
public
function
insert
(
$index
,
$type
,
$data
,
$id
=
null
,
$options
=
[])
{
$body
=
is_array
(
$data
)
?
Json
::
encode
(
$data
)
:
$data
;
try
{
if
(
$id
!==
null
)
{
$response
=
$this
->
db
->
http
()
->
put
(
$this
->
createUrl
([
$index
,
$type
,
$id
],
$options
),
null
,
$body
)
->
send
();
}
else
{
$response
=
$this
->
db
->
http
()
->
post
(
$this
->
createUrl
([
$index
,
$type
],
$options
),
null
,
$body
)
->
send
();
}
}
catch
(
ClientErrorResponseException
$e
)
{
throw
new
Exception
(
"elasticsearch error:
\n\n
"
.
$body
.
"
\n\n
"
.
$e
->
getMessage
()
.
print_r
(
Json
::
decode
(
$e
->
getResponse
()
->
getBody
(
true
)),
true
),
[],
0
,
$e
);
}
return
Json
::
decode
(
$response
->
getBody
(
true
));
}
...
...
tests/unit/data/ar/elasticsearch/Order.php
View file @
325fc281
...
...
@@ -30,7 +30,7 @@ class Order extends ActiveRecord
public
function
getItems
()
{
return
$this
->
hasMany
(
Item
::
className
(),
[
ActiveRecord
::
PRIMARY_KEY_NAME
=>
'item_id'
])
->
via
(
'orderItems'
)
->
orderBy
(
'
name
'
);
->
via
(
'orderItems'
)
->
orderBy
(
'
id
'
);
}
public
function
getItemsInOrder1
()
...
...
tests/unit/framework/ar/ActiveRecordTestTrait.php
View file @
325fc281
...
...
@@ -134,6 +134,8 @@ trait ActiveRecordTestTrait
$this
->
assertNull
(
$customer
);
$customer
=
$this
->
callCustomerFind
([
'id'
=>
5
]);
$this
->
assertNull
(
$customer
);
$customer
=
$this
->
callCustomerFind
([
'name'
=>
'user5'
]);
$this
->
assertNull
(
$customer
);
// find by attributes
$customer
=
$this
->
callCustomerFind
()
->
where
([
'name'
=>
'user2'
])
->
one
();
...
...
@@ -141,6 +143,7 @@ trait ActiveRecordTestTrait
$this
->
assertEquals
(
2
,
$customer
->
id
);
// scope
$this
->
assertEquals
(
2
,
count
(
$this
->
callCustomerFind
()
->
active
()
->
all
()));
$this
->
assertEquals
(
2
,
$this
->
callCustomerFind
()
->
active
()
->
count
());
// asArray
...
...
@@ -158,8 +161,14 @@ trait ActiveRecordTestTrait
{
/** @var TestCase|ActiveRecordTestTrait $this */
// query scalar
$customerName
=
$this
->
callCustomerFind
()
->
where
(
array
(
'id'
=>
2
)
)
->
scalar
(
'name'
);
$customerName
=
$this
->
callCustomerFind
()
->
where
(
[
'id'
=>
2
]
)
->
scalar
(
'name'
);
$this
->
assertEquals
(
'user2'
,
$customerName
);
$customerName
=
$this
->
callCustomerFind
()
->
where
([
'status'
=>
2
])
->
scalar
(
'name'
);
$this
->
assertEquals
(
'user3'
,
$customerName
);
$customerName
=
$this
->
callCustomerFind
()
->
where
([
'status'
=>
2
])
->
scalar
(
'noname'
);
$this
->
assertNull
(
$customerName
);
$customerId
=
$this
->
callCustomerFind
()
->
where
([
'status'
=>
2
])
->
scalar
(
'id'
);
$this
->
assertEquals
(
3
,
$customerId
);
}
public
function
testFindColumn
()
...
...
@@ -365,7 +374,12 @@ trait ActiveRecordTestTrait
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
1
,
$order
->
items
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$order
->
items
[
1
]
->
id
);
}
public
function
testFindLazyVia2
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
/** @var Order $order */
$order
=
$this
->
callOrderFind
(
1
);
$order
->
id
=
100
;
$this
->
assertEquals
([],
$order
->
items
);
...
...
@@ -551,7 +565,7 @@ trait ActiveRecordTestTrait
$customer
->
save
();
$this
->
afterSave
();
$this
->
assert
Equals
(
4
,
$customer
->
id
);
$this
->
assert
NotNull
(
$customer
->
id
);
$this
->
assertFalse
(
static
::
$afterSaveNewRecord
);
$this
->
assertTrue
(
static
::
$afterSaveInsert
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
...
...
tests/unit/framework/elasticsearch/ActiveRecordTest.php
View file @
325fc281
...
...
@@ -4,6 +4,7 @@ namespace yiiunit\framework\elasticsearch;
use
yii\elasticsearch\Connection
;
use
yii\elasticsearch\ActiveQuery
;
use
yii\helpers\Json
;
use
yiiunit\framework\ar\ActiveRecordTestTrait
;
use
yiiunit\data\ar\elasticsearch\ActiveRecord
;
use
yiiunit\data\ar\elasticsearch\Customer
;
...
...
@@ -11,6 +12,9 @@ use yiiunit\data\ar\elasticsearch\OrderItem;
use
yiiunit\data\ar\elasticsearch\Order
;
use
yiiunit\data\ar\elasticsearch\Item
;
/**
* @group elasticsearch
*/
class
ActiveRecordTest
extends
ElasticSearchTestCase
{
use
ActiveRecordTestTrait
;
...
...
@@ -43,6 +47,30 @@ class ActiveRecordTest extends ElasticSearchTestCase
// delete all indexes
$db
->
http
()
->
delete
(
'_all'
)
->
send
();
$db
->
http
()
->
post
(
'items'
,
null
,
Json
::
encode
([
'mappings'
=>
[
"item"
=>
[
"_source"
=>
[
"enabled"
=>
true
],
"properties"
=>
[
// allow proper sorting by name
"name"
=>
[
"type"
=>
"string"
,
"index"
=>
"not_analyzed"
],
]
]
],
]))
->
send
();
$db
->
http
()
->
post
(
'customers'
,
null
,
Json
::
encode
([
'mappings'
=>
[
"item"
=>
[
"_source"
=>
[
"enabled"
=>
true
],
"properties"
=>
[
// this is for the boolean test
"status"
=>
[
"type"
=>
"boolean"
],
]
]
],
]))
->
send
();
$customer
=
new
Customer
();
$customer
->
id
=
1
;
$customer
->
setAttributes
([
'email'
=>
'user1@example.com'
,
'name'
=>
'user1'
,
'address'
=>
'address1'
,
'status'
=>
1
],
false
);
...
...
@@ -115,110 +143,6 @@ class ActiveRecordTest extends ElasticSearchTestCase
Customer
::
getDb
()
->
createCommand
()
->
flushIndex
();
}
public
function
testFind
()
{
// find one
$result
=
Customer
::
find
();
$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 all asArray
$customers
=
Customer
::
find
()
->
asArray
()
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertArrayHasKey
(
ActiveRecord
::
PRIMARY_KEY_NAME
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'name'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'email'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'address'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
'status'
,
$customers
[
0
]);
$this
->
assertArrayHasKey
(
ActiveRecord
::
PRIMARY_KEY_NAME
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'name'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'email'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'address'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
'status'
,
$customers
[
1
]);
$this
->
assertArrayHasKey
(
ActiveRecord
::
PRIMARY_KEY_NAME
,
$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
=
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
([
'status'
=>
2
])
->
scalar
(
'name'
);
$this
->
assertEquals
(
'user3'
,
$customerName
);
$customerName
=
Customer
::
find
()
->
where
([
'status'
=>
2
])
->
scalar
(
'noname'
);
$this
->
assertNull
(
$customerName
);
$customerId
=
Customer
::
find
()
->
where
([
'status'
=>
2
])
->
scalar
(
ActiveRecord
::
PRIMARY_KEY_NAME
);
$this
->
assertEquals
(
3
,
$customerId
);
// find by column values
$customer
=
Customer
::
find
([
'name'
=>
'user2'
]);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
=
Customer
::
find
([
'name'
=>
'user1'
,
ActiveRecord
::
PRIMARY_KEY_NAME
=>
2
]);
$this
->
assertNull
(
$customer
);
$customer
=
Customer
::
find
([
ActiveRecord
::
PRIMARY_KEY_NAME
=>
5
]);
$this
->
assertNull
(
$customer
);
$customer
=
Customer
::
find
([
'name'
=>
'user5'
]);
$this
->
assertNull
(
$customer
);
// find by attributes
$customer
=
Customer
::
find
()
->
where
([
'name'
=>
'user2'
])
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
// find count
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
());
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
where
([
'or'
,
[
ActiveRecord
::
PRIMARY_KEY_NAME
=>
1
],
[
ActiveRecord
::
PRIMARY_KEY_NAME
=>
2
]])
->
count
());
// $this->assertEquals(6, Customer::find()->sum('id'));
// $this->assertEquals(2, Customer::find()->average('id'));
// $this->assertEquals(1, Customer::find()->min('id'));
// $this->assertEquals(3, Customer::find()->max('id'));
// scope
$this
->
assertEquals
(
2
,
count
(
Customer
::
find
()
->
active
()
->
all
()));
// $this->assertEquals(2, Customer::find()->active()->count());
// asArray
$customer
=
Customer
::
find
()
->
where
([
'name'
=>
'user2'
])
->
asArray
()
->
one
();
$this
->
assertEquals
(
array
(
'email'
=>
'user2@example.com'
,
'name'
=>
'user2'
,
'address'
=>
'address2'
,
'status'
=>
'1'
,
ActiveRecord
::
PRIMARY_KEY_NAME
=>
2
,
),
$customer
);
// indexBy
$customers
=
Customer
::
find
()
->
indexBy
(
'name'
)
->
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
->
status
.
'-'
.
$customer
->
name
;
})
->
orderBy
(
'name'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
'1-user1'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'1-user2'
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
'2-user3'
]
instanceof
Customer
);
}
public
function
testGetDb
()
{
$this
->
mockApplication
([
'components'
=>
[
'elasticsearch'
=>
Connection
::
className
()]]);
...
...
@@ -331,4 +255,74 @@ class ActiveRecordTest extends ElasticSearchTestCase
$orderItem
->
$pkName
=
13
;
$orderItem
->
save
();
}
public
function
testFindLazyVia2
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
/** @var Order $order */
$orderClass
=
$this
->
getOrderClass
();
$pkName
=
ActiveRecord
::
PRIMARY_KEY_NAME
;
$order
=
new
$orderClass
();
$order
->
$pkName
=
100
;
$this
->
assertEquals
([],
$order
->
items
);
}
public
function
testUpdateCounters
()
{
// Update Counters is not supported by elasticsearch
// $this->setExpectedException('yii\base\NotSupportedException');
// ActiveRecordTestTrait::testUpdateCounters();
}
/**
* Some PDO implementations(e.g. cubrid) do not support boolean values.
* Make sure this does not affect AR layer.
*/
public
function
testBooleanAttribute
()
{
$db
=
$this
->
getConnection
();
$db
->
createCommand
()
->
deleteIndex
(
'customers'
);
$db
->
http
()
->
post
(
'customers'
,
null
,
Json
::
encode
([
'mappings'
=>
[
"item"
=>
[
"_source"
=>
[
"enabled"
=>
true
],
"properties"
=>
[
// this is for the boolean test
"status"
=>
[
"type"
=>
"boolean"
],
]
]
],
]))
->
send
();
$customerClass
=
$this
->
getCustomerClass
();
$customer
=
new
$customerClass
();
$customer
->
name
=
'boolean customer'
;
$customer
->
email
=
'mail@example.com'
;
$customer
->
status
=
true
;
$customer
->
save
(
false
);
$customer
->
refresh
();
$this
->
assertEquals
(
true
,
$customer
->
status
);
$customer
->
status
=
false
;
$customer
->
save
(
false
);
$customer
->
refresh
();
$this
->
assertEquals
(
false
,
$customer
->
status
);
$customer
=
new
Customer
();
$customer
->
setAttributes
([
'email'
=>
'user2b@example.com'
,
'name'
=>
'user2b'
,
'status'
=>
true
],
false
);
$customer
->
save
(
false
);
$customer
=
new
Customer
();
$customer
->
setAttributes
([
'email'
=>
'user3b@example.com'
,
'name'
=>
'user3b'
,
'status'
=>
false
],
false
);
$customer
->
save
(
false
);
$this
->
afterSave
();
$customers
=
$this
->
callCustomerFind
()
->
where
([
'status'
=>
true
])
->
all
();
$this
->
assertEquals
(
1
,
count
(
$customers
));
$customers
=
$this
->
callCustomerFind
()
->
where
([
'status'
=>
false
])
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
}
}
\ No newline at end of file
tests/unit/framework/elasticsearch/ElasticSearchConnectionTest.php
View file @
325fc281
...
...
@@ -4,6 +4,9 @@ namespace yiiunit\framework\elasticsearch;
use
yii\redis\Connection
;
/**
* @group elasticsearch
*/
class
ElasticSearchConnectionTest
extends
ElasticSearchTestCase
{
/**
...
...
tests/unit/framework/elasticsearch/ElasticSearchTestCase.php
View file @
325fc281
...
...
@@ -6,7 +6,7 @@ use yii\elasticsearch\Connection;
use
yiiunit\TestCase
;
/**
*
RedisTestCase is the base class for all redis
related test cases
*
ElasticSearchTestCase is the base class for all elasticsearch
related test cases
*/
class
ElasticSearchTestCase
extends
TestCase
{
...
...
tests/unit/framework/elasticsearch/QueryTest.php
View file @
325fc281
...
...
@@ -5,8 +5,7 @@ namespace yiiunit\framework\elasticsearch;
use
yii\elasticsearch\Query
;
/**
* @group db
* @group mysql
* @group elasticsearch
*/
class
QueryTest
extends
ElasticSearchTestCase
{
...
...
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