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
bc73a248
Commit
bc73a248
authored
Mar 08, 2014
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MongoDB log fixed to handle Mongo objects correctly
parent
f5661639
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
1 deletion
+84
-1
Collection.php
extensions/mongodb/Collection.php
+59
-1
CollectionTest.php
tests/unit/extensions/mongodb/CollectionTest.php
+25
-0
No files found.
extensions/mongodb/Collection.php
View file @
bc73a248
...
@@ -107,12 +107,70 @@ class Collection extends Object
...
@@ -107,12 +107,70 @@ class Collection extends Object
{
{
$parts
=
[];
$parts
=
[];
foreach
(
$arguments
as
$argument
)
{
foreach
(
$arguments
as
$argument
)
{
$parts
[]
=
is_scalar
(
$argument
)
?
$argument
:
Json
::
encode
(
$argument
);
$parts
[]
=
is_scalar
(
$argument
)
?
$argument
:
$this
->
encodeLogData
(
$argument
);
}
}
return
$this
->
getFullName
()
.
'.'
.
$command
.
'('
.
implode
(
', '
,
$parts
)
.
')'
;
return
$this
->
getFullName
()
.
'.'
.
$command
.
'('
.
implode
(
', '
,
$parts
)
.
')'
;
}
}
/**
/**
* Encodes complex log data into JSON format string.
* @param mixed $data raw data.
* @return string encoded data string.
*/
protected
function
encodeLogData
(
$data
)
{
return
json_encode
(
$this
->
processLogData
(
$data
));
}
/**
* Pre-processes the log data before sending it to `json_encode()`.
* @param mixed $data raw data.
* @return mixed the processed data.
*/
protected
function
processLogData
(
$data
)
{
if
(
is_object
(
$data
))
{
if
(
$data
instanceof
\MongoId
||
$data
instanceof
\MongoRegex
||
$data
instanceof
\MongoDate
||
$data
instanceof
\MongoInt32
||
$data
instanceof
\MongoInt64
||
$data
instanceof
\MongoTimestamp
)
{
$data
=
get_class
(
$data
)
.
'('
.
$data
->
__toString
()
.
')'
;
}
elseif
(
$data
instanceof
\MongoCode
)
{
$data
=
'MongoCode( '
.
$data
->
__toString
()
.
' )'
;
}
elseif
(
$data
instanceof
\MongoBinData
)
{
$data
=
'MongoBinData(...)'
;
}
elseif
(
$data
instanceof
\MongoDBRef
)
{
$data
=
'MongoDBRef(...)'
;
}
elseif
(
$data
instanceof
\MongoMinKey
||
$data
instanceof
\MongoMaxKey
)
{
$data
=
get_class
(
$data
);
}
else
{
$result
=
[];
foreach
(
$data
as
$name
=>
$value
)
{
$result
[
$name
]
=
$value
;
}
$data
=
$result
;
}
if
(
$data
===
[])
{
return
new
\stdClass
();
}
}
if
(
is_array
(
$data
))
{
foreach
(
$data
as
$key
=>
$value
)
{
if
(
is_array
(
$value
)
||
is_object
(
$value
))
{
$data
[
$key
]
=
$this
->
processLogData
(
$value
);
}
}
}
return
$data
;
}
/**
* Drops this collection.
* Drops this collection.
* @throws Exception on failure.
* @throws Exception on failure.
* @return boolean whether the operation successful.
* @return boolean whether the operation successful.
...
...
tests/unit/extensions/mongodb/CollectionTest.php
View file @
bc73a248
...
@@ -420,6 +420,10 @@ class CollectionTest extends MongoDbTestCase
...
@@ -420,6 +420,10 @@ class CollectionTest extends MongoDbTestCase
$this
->
assertCount
(
2
,
$result
);
$this
->
assertCount
(
2
,
$result
);
}
}
/**
* @depends testInsert
* @depends testFind
*/
public
function
testFindByNotObjectId
()
public
function
testFindByNotObjectId
()
{
{
$collection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$collection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
...
@@ -439,4 +443,24 @@ class CollectionTest extends MongoDbTestCase
...
@@ -439,4 +443,24 @@ class CollectionTest extends MongoDbTestCase
$this
->
assertTrue
(
$cursor
instanceof
\MongoCursor
);
$this
->
assertTrue
(
$cursor
instanceof
\MongoCursor
);
$this
->
assertEquals
(
0
,
$cursor
->
count
());
$this
->
assertEquals
(
0
,
$cursor
->
count
());
}
}
/**
* @depends testInsert
*
* @see https://github.com/yiisoft/yii2/issues/2548
*/
public
function
testInsertMongoBin
()
{
$collection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$fileName
=
realpath
(
__DIR__
.
'/../../../../extensions/gii/assets/logo.png'
);
$data
=
[
'name'
=>
'customer 1'
,
'address'
=>
'customer 1 address'
,
'binData'
=>
new
\MongoBinData
(
file_get_contents
(
$fileName
),
2
),
];
$id
=
$collection
->
insert
(
$data
);
$this
->
assertTrue
(
$id
instanceof
\MongoId
);
$this
->
assertNotEmpty
(
$id
->
__toString
());
}
}
}
\ 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