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
529af8ed
Commit
529af8ed
authored
Nov 26, 2013
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mongo "Database" and "Collection" classes introduced.
parent
3b5ee4fb
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
274 additions
and
120 deletions
+274
-120
Collection.php
extensions/mongo/Collection.php
+21
-40
Connection.php
extensions/mongo/Connection.php
+82
-53
Database.php
extensions/mongo/Database.php
+65
-0
CollectionTest.php
tests/unit/extensions/mongo/CollectionTest.php
+17
-16
ConnectionTest.php
tests/unit/extensions/mongo/ConnectionTest.php
+51
-8
DatabaseTest.php
tests/unit/extensions/mongo/DatabaseTest.php
+35
-0
MongoTestCase.php
tests/unit/extensions/mongo/MongoTestCase.php
+3
-3
No files found.
extensions/mongo/Co
mmand
.php
→
extensions/mongo/Co
llection
.php
View file @
529af8ed
...
@@ -7,61 +7,48 @@
...
@@ -7,61 +7,48 @@
namespace
yii\mongo
;
namespace
yii\mongo
;
use
\yii\base\Componen
t
;
use
yii\base\Objec
t
;
use
Yii
;
use
Yii
;
/**
/**
* C
lass Command
* C
ollection represents the Mongo collection information.
*
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
class
Co
mmand
extends
Componen
t
class
Co
llection
extends
Objec
t
{
{
/**
/**
* @var
Connection the Mongo connection that this command is associated with
* @var
\MongoCollection Mongo collection instance.
*/
*/
public
$
db
;
public
$
mongoCollection
;
/**
/**
* Drops th
e current database
* Drops th
is collection.
*/
*/
public
function
drop
Db
()
public
function
drop
()
{
{
$this
->
db
->
db
->
drop
();
$this
->
mongoCollection
->
drop
();
}
}
/**
/**
* Drops the specified collection.
* @param string $name collection name.
*/
public
function
dropCollection
(
$name
)
{
$collection
=
$this
->
db
->
getCollection
(
$name
);
$collection
->
drop
();
}
/**
* @param $collection
* @param array $query
* @param array $query
* @param array $fields
* @param array $fields
* @return \MongoCursor
* @return \MongoCursor
*/
*/
public
function
find
(
$
collection
,
$
query
=
[],
$fields
=
[])
public
function
find
(
$query
=
[],
$fields
=
[])
{
{
$collection
=
$this
->
db
->
getCollection
(
$collection
);
return
$this
->
mongoCollection
->
find
(
$query
,
$fields
);
return
$collection
->
find
(
$query
,
$fields
);
}
}
/**
/**
* @param $collection
* @param array $query
* @param array $query
* @param array $fields
* @param array $fields
* @return array
* @return array
*/
*/
public
function
findAll
(
$
collection
,
$
query
=
[],
$fields
=
[])
public
function
findAll
(
$query
=
[],
$fields
=
[])
{
{
$cursor
=
$this
->
find
(
$
collection
,
$
query
,
$fields
);
$cursor
=
$this
->
find
(
$query
,
$fields
);
$result
=
[];
$result
=
[];
foreach
(
$cursor
as
$data
)
{
foreach
(
$cursor
as
$data
)
{
$result
[]
=
$data
;
$result
[]
=
$data
;
...
@@ -71,20 +58,18 @@ class Command extends Component
...
@@ -71,20 +58,18 @@ class Command extends Component
/**
/**
* Inserts new data into collection.
* Inserts new data into collection.
* @param string $collection name of the collection.
* @param array|object $data data to be inserted.
* @param array|object $data data to be inserted.
* @param array $options list of options in format: optionName => optionValue.
* @param array $options list of options in format: optionName => optionValue.
* @return \MongoId new record id instance.
* @return \MongoId new record id instance.
* @throws Exception on failure.
* @throws Exception on failure.
*/
*/
public
function
insert
(
$
collection
,
$
data
,
$options
=
[])
public
function
insert
(
$data
,
$options
=
[])
{
{
$token
=
'Inserting data into '
.
$
collection
;
$token
=
'Inserting data into '
.
$
this
->
mongoCollection
->
getName
()
;
Yii
::
info
(
$token
,
__METHOD__
);
Yii
::
info
(
$token
,
__METHOD__
);
try
{
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$collection
=
$this
->
db
->
getCollection
(
$collection
);
$this
->
tryResultError
(
$this
->
mongoCollection
->
insert
(
$data
,
$options
));
$this
->
tryResultError
(
$collection
->
insert
(
$data
,
$options
));
Yii
::
endProfile
(
$token
,
__METHOD__
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
return
is_array
(
$data
)
?
$data
[
'_id'
]
:
$data
->
_id
;
return
is_array
(
$data
)
?
$data
[
'_id'
]
:
$data
->
_id
;
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
...
@@ -95,20 +80,18 @@ class Command extends Component
...
@@ -95,20 +80,18 @@ class Command extends Component
/**
/**
* Update the existing database data, otherwise insert this data
* Update the existing database data, otherwise insert this data
* @param string $collection name of the collection.
* @param array|object $data data to be updated/inserted.
* @param array|object $data data to be updated/inserted.
* @param array $options list of options in format: optionName => optionValue.
* @param array $options list of options in format: optionName => optionValue.
* @return \MongoId updated/new record id instance.
* @return \MongoId updated/new record id instance.
* @throws Exception on failure.
* @throws Exception on failure.
*/
*/
public
function
save
(
$
collection
,
$
data
,
$options
=
[])
public
function
save
(
$data
,
$options
=
[])
{
{
$token
=
'Saving data into '
.
$
collection
;
$token
=
'Saving data into '
.
$
this
->
mongoCollection
->
getName
()
;
Yii
::
info
(
$token
,
__METHOD__
);
Yii
::
info
(
$token
,
__METHOD__
);
try
{
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$collection
=
$this
->
db
->
getCollection
(
$collection
);
$this
->
tryResultError
(
$this
->
mongoCollection
->
save
(
$data
,
$options
));
$this
->
tryResultError
(
$collection
->
save
(
$data
,
$options
));
Yii
::
endProfile
(
$token
,
__METHOD__
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
return
is_array
(
$data
)
?
$data
[
'_id'
]
:
$data
->
_id
;
return
is_array
(
$data
)
?
$data
[
'_id'
]
:
$data
->
_id
;
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
...
@@ -119,20 +102,18 @@ class Command extends Component
...
@@ -119,20 +102,18 @@ class Command extends Component
/**
/**
* Removes data from the collection.
* Removes data from the collection.
* @param string $collection name of the collection.
* @param array $criteria description of records to remove.
* @param array $criteria description of records to remove.
* @param array $options list of options in format: optionName => optionValue.
* @param array $options list of options in format: optionName => optionValue.
* @return boolean whether operation was successful.
* @return boolean whether operation was successful.
* @throws Exception on failure.
* @throws Exception on failure.
*/
*/
public
function
remove
(
$c
ollection
,
$c
riteria
=
[],
$options
=
[])
public
function
remove
(
$criteria
=
[],
$options
=
[])
{
{
$token
=
'Removing data from '
.
$
collection
;
$token
=
'Removing data from '
.
$
this
->
mongoCollection
->
getName
()
;
Yii
::
info
(
$token
,
__METHOD__
);
Yii
::
info
(
$token
,
__METHOD__
);
try
{
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$collection
=
$this
->
db
->
getCollection
(
$collection
);
$this
->
tryResultError
(
$this
->
mongoCollection
->
remove
(
$criteria
,
$options
));
$this
->
tryResultError
(
$collection
->
remove
(
$criteria
,
$options
));
Yii
::
endProfile
(
$token
,
__METHOD__
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
return
true
;
return
true
;
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
...
...
extensions/mongo/Connection.php
View file @
529af8ed
...
@@ -24,14 +24,16 @@ use Yii;
...
@@ -24,14 +24,16 @@ use Yii;
class
Connection
extends
Component
class
Connection
extends
Component
{
{
/**
/**
* @var \MongoCollection[] list of Mongo collection available in database.
* @var string host:port
*/
*
private
$_collections
=
[];
* Correct syntax is:
* mongodb://[username:password@]host1[:port1][,host2[:port2:],...][/dbname]
/**
* For example:
* @var \MongoClient mongo client instance.
* mongodb://localhost:27017
* mongodb://developer:somepassword@localhost:27017
* mongodb://developer:somepassword@localhost:27017/mydatabase
*/
*/
public
$
client
;
public
$
dsn
;
/**
/**
* @var array connection options.
* @var array connection options.
* for example:
* for example:
...
@@ -45,34 +47,83 @@ class Connection extends Component
...
@@ -45,34 +47,83 @@ class Connection extends Component
*/
*/
public
$options
=
[];
public
$options
=
[];
/**
/**
* @var string host:port
* @var string name of the Mongo database to use by default.
*
* Correct syntax is:
* mongodb://[username:password@]host1[:port1][,host2[:port2:],...]
* For example: mongodb://localhost:27017
*/
*/
public
$dsn
;
public
$defaultDatabaseName
;
/**
* @var \MongoClient mongo client instance.
*/
public
$mongoClient
;
/**
* @var Database[] list of Mongo databases
*/
private
$_databases
=
[];
/**
* Returns the Mongo collection with the given name.
* @param string|null $name collection name, if null default one will be used.
* @param boolean $refresh whether to reload the table schema even if it is found in the cache.
* @return Database database instance.
*/
public
function
getDatabase
(
$name
=
null
,
$refresh
=
false
)
{
if
(
$name
===
null
)
{
$name
=
$this
->
fetchDefaultDatabaseName
();
}
if
(
$refresh
||
!
array_key_exists
(
$name
,
$this
->
_databases
))
{
$this
->
_databases
[
$name
]
=
$this
->
selectDatabase
(
$name
);
}
return
$this
->
_databases
[
$name
];
}
/**
/**
* @var string name of the Mongo database to use
* Returns [[defaultDatabaseName]] value, if it is not set,
* attempts to determine it from [[dsn]] value.
* @return string default database name
* @throws \yii\base\InvalidConfigException if unable to determine default database name.
*/
*/
public
$dbName
;
protected
function
fetchDefaultDatabaseName
()
{
if
(
$this
->
defaultDatabaseName
===
null
)
{
if
(
preg_match
(
'/^mongodb:\\/\\/.+\\/(.+)$/s'
,
$this
->
dsn
,
$matches
))
{
$this
->
defaultDatabaseName
=
$matches
[
1
];
}
else
{
throw
new
InvalidConfigException
(
"Unable to determine default database name from dsn."
);
}
}
return
$this
->
defaultDatabaseName
;
}
/**
/**
* @var \MongoDb Mongo database instance.
* Selects the database with given name.
* @param string $name database name.
* @return Database database instance.
*/
*/
public
$db
;
protected
function
selectDatabase
(
$name
)
{
$this
->
open
();
return
Yii
::
createObject
([
'class'
=>
'yii\mongo\Database'
,
'mongoDb'
=>
$this
->
mongoClient
->
selectDB
(
$name
)
]);
}
/**
/**
* Returns the Mongo collection with the given name.
* Returns the Mongo collection with the given name.
* @param string $name collection name
* @param string|array $name collection name. If string considered as the name of the collection
* inside the default database. If array - first element considered as the name of the database,
* second - as name of collection inside that database
* @param boolean $refresh whether to reload the table schema even if it is found in the cache.
* @param boolean $refresh whether to reload the table schema even if it is found in the cache.
* @return
\MongoCollection m
ongo collection instance.
* @return
Collection M
ongo collection instance.
*/
*/
public
function
getCollection
(
$name
,
$refresh
=
false
)
public
function
getCollection
(
$name
,
$refresh
=
false
)
{
{
if
(
$refresh
||
!
array_key_exists
(
$name
,
$this
->
_collections
))
{
if
(
is_array
(
$name
))
{
$this
->
_collections
[
$name
]
=
$this
->
client
->
selectCollection
(
$this
->
dbName
,
$name
);
list
(
$dbName
,
$collectionName
)
=
$name
;
return
$this
->
getDatabase
(
$dbName
)
->
getCollection
(
$collectionName
,
$refresh
);
}
else
{
return
$this
->
getDatabase
()
->
getCollection
(
$name
,
$refresh
);
}
}
return
$this
->
_collections
[
$name
];
}
}
/**
/**
...
@@ -81,7 +132,7 @@ class Connection extends Component
...
@@ -81,7 +132,7 @@ class Connection extends Component
*/
*/
public
function
getIsActive
()
public
function
getIsActive
()
{
{
return
is_object
(
$this
->
client
)
&&
$this
->
c
lient
->
connected
;
return
is_object
(
$this
->
mongoClient
)
&&
$this
->
mongoC
lient
->
connected
;
}
}
/**
/**
...
@@ -91,7 +142,7 @@ class Connection extends Component
...
@@ -91,7 +142,7 @@ class Connection extends Component
*/
*/
public
function
open
()
public
function
open
()
{
{
if
(
$this
->
c
lient
===
null
)
{
if
(
$this
->
mongoC
lient
===
null
)
{
if
(
empty
(
$this
->
dsn
))
{
if
(
empty
(
$this
->
dsn
))
{
throw
new
InvalidConfigException
(
$this
->
className
()
.
'::dsn cannot be empty.'
);
throw
new
InvalidConfigException
(
$this
->
className
()
.
'::dsn cannot be empty.'
);
}
}
...
@@ -101,9 +152,10 @@ class Connection extends Component
...
@@ -101,9 +152,10 @@ class Connection extends Component
Yii
::
beginProfile
(
$token
,
__METHOD__
);
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$options
=
$this
->
options
;
$options
=
$this
->
options
;
$options
[
'connect'
]
=
true
;
$options
[
'connect'
]
=
true
;
$options
[
'db'
]
=
$this
->
dbName
;
if
(
$this
->
defaultDatabaseName
!==
null
)
{
$this
->
client
=
new
\MongoClient
(
$this
->
dsn
,
$options
);
$options
[
'db'
]
=
$this
->
defaultDatabaseName
;
$this
->
db
=
$this
->
client
->
selectDB
(
$this
->
dbName
);
}
$this
->
mongoClient
=
new
\MongoClient
(
$this
->
dsn
,
$options
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
...
@@ -118,32 +170,10 @@ class Connection extends Component
...
@@ -118,32 +170,10 @@ class Connection extends Component
*/
*/
public
function
close
()
public
function
close
()
{
{
if
(
$this
->
c
lient
!==
null
)
{
if
(
$this
->
mongoC
lient
!==
null
)
{
Yii
::
trace
(
'Closing Mongo connection: '
.
$this
->
dsn
,
__METHOD__
);
Yii
::
trace
(
'Closing Mongo connection: '
.
$this
->
dsn
,
__METHOD__
);
$this
->
c
lient
=
null
;
$this
->
mongoC
lient
=
null
;
$this
->
db
=
null
;
$this
->
_databases
=
[]
;
}
}
}
}
/**
* Returns the query builder for the current DB connection.
* @return QueryBuilder the query builder for the current DB connection.
*/
public
function
getQueryBuilder
()
{
return
new
QueryBuilder
(
$this
);
}
/**
* Creates a command for execution.
* @return Command the Mongo command
*/
public
function
createCommand
()
{
$this
->
open
();
$command
=
new
Command
([
'db'
=>
$this
,
]);
return
$command
;
}
}
}
\ No newline at end of file
extensions/mongo/Database.php
0 → 100644
View file @
529af8ed
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\mongo
;
use
yii\base\Object
;
use
Yii
;
/**
* Database represents the Mongo database information.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class
Database
extends
Object
{
/**
* @var \MongoDB Mongo database instance.
*/
public
$mongoDb
;
/**
* @var Collection[] list of collections.
*/
private
$_collections
=
[];
/**
* Returns the Mongo collection with the given name.
* @param string $name collection name
* @param boolean $refresh whether to reload the table schema even if it is found in the cache.
* @return Collection mongo collection instance.
*/
public
function
getCollection
(
$name
,
$refresh
=
false
)
{
if
(
$refresh
||
!
array_key_exists
(
$name
,
$this
->
_collections
))
{
$this
->
_collections
[
$name
]
=
$this
->
selectCollection
(
$name
);
}
return
$this
->
_collections
[
$name
];
}
/**
* Selects collection with given name.
* @param string $name collection name.
* @return Collection collection instance.
*/
protected
function
selectCollection
(
$name
)
{
return
Yii
::
createObject
([
'class'
=>
'yii\mongo\Collection'
,
'mongoCollection'
=>
$this
->
mongoDb
->
selectCollection
(
$name
)
]);
}
/**
* Drops this database.
*/
public
function
drop
()
{
$this
->
mongoDb
->
drop
();
}
}
\ No newline at end of file
tests/unit/extensions/mongo/Co
mmand
Test.php
→
tests/unit/extensions/mongo/Co
llection
Test.php
View file @
529af8ed
...
@@ -5,7 +5,7 @@ namespace yiiunit\extensions\mongo;
...
@@ -5,7 +5,7 @@ namespace yiiunit\extensions\mongo;
/**
/**
* @group mongo
* @group mongo
*/
*/
class
Co
mmand
Test
extends
MongoTestCase
class
Co
llection
Test
extends
MongoTestCase
{
{
protected
function
tearDown
()
protected
function
tearDown
()
{
{
...
@@ -17,12 +17,12 @@ class CommandTest extends MongoTestCase
...
@@ -17,12 +17,12 @@ class CommandTest extends MongoTestCase
public
function
testInsert
()
public
function
testInsert
()
{
{
$co
mmand
=
$this
->
getConnection
()
->
createCommand
(
);
$co
llection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$data
=
[
$data
=
[
'name'
=>
'customer 1'
,
'name'
=>
'customer 1'
,
'address'
=>
'customer 1 address'
,
'address'
=>
'customer 1 address'
,
];
];
$id
=
$co
mmand
->
insert
(
'customer'
,
$data
);
$id
=
$co
llection
->
insert
(
$data
);
$this
->
assertTrue
(
$id
instanceof
\MongoId
);
$this
->
assertTrue
(
$id
instanceof
\MongoId
);
$this
->
assertNotEmpty
(
$id
->
__toString
());
$this
->
assertNotEmpty
(
$id
->
__toString
());
}
}
...
@@ -32,26 +32,26 @@ class CommandTest extends MongoTestCase
...
@@ -32,26 +32,26 @@ class CommandTest extends MongoTestCase
*/
*/
public
function
testFindAll
()
public
function
testFindAll
()
{
{
$co
mmand
=
$this
->
getConnection
()
->
createCommand
(
);
$co
llection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$data
=
[
$data
=
[
'name'
=>
'customer 1'
,
'name'
=>
'customer 1'
,
'address'
=>
'customer 1 address'
,
'address'
=>
'customer 1 address'
,
];
];
$id
=
$co
mmand
->
insert
(
'customer'
,
$data
);
$id
=
$co
llection
->
insert
(
$data
);
$rows
=
$co
mmand
->
findAll
(
'customer'
);
$rows
=
$co
llection
->
findAll
(
);
$this
->
assertEquals
(
1
,
count
(
$rows
));
$this
->
assertEquals
(
1
,
count
(
$rows
));
$this
->
assertEquals
(
$id
,
$rows
[
0
][
'_id'
]);
$this
->
assertEquals
(
$id
,
$rows
[
0
][
'_id'
]);
}
}
public
function
testSave
()
public
function
testSave
()
{
{
$co
mmand
=
$this
->
getConnection
()
->
createCommand
(
);
$co
llection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$data
=
[
$data
=
[
'name'
=>
'customer 1'
,
'name'
=>
'customer 1'
,
'address'
=>
'customer 1 address'
,
'address'
=>
'customer 1 address'
,
];
];
$id
=
$co
mmand
->
save
(
'customer'
,
$data
);
$id
=
$co
llection
->
save
(
$data
);
$this
->
assertTrue
(
$id
instanceof
\MongoId
);
$this
->
assertTrue
(
$id
instanceof
\MongoId
);
$this
->
assertNotEmpty
(
$id
->
__toString
());
$this
->
assertNotEmpty
(
$id
->
__toString
());
}
}
...
@@ -61,18 +61,18 @@ class CommandTest extends MongoTestCase
...
@@ -61,18 +61,18 @@ class CommandTest extends MongoTestCase
*/
*/
public
function
testUpdate
()
public
function
testUpdate
()
{
{
$co
mmand
=
$this
->
getConnection
()
->
createCommand
(
);
$co
llection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$data
=
[
$data
=
[
'name'
=>
'customer 1'
,
'name'
=>
'customer 1'
,
'address'
=>
'customer 1 address'
,
'address'
=>
'customer 1 address'
,
];
];
$newId
=
$co
mmand
->
save
(
'customer'
,
$data
);
$newId
=
$co
llection
->
save
(
$data
);
$updatedId
=
$co
mmand
->
save
(
'customer'
,
$data
);
$updatedId
=
$co
llection
->
save
(
$data
);
$this
->
assertEquals
(
$newId
,
$updatedId
,
'Unable to update data!'
);
$this
->
assertEquals
(
$newId
,
$updatedId
,
'Unable to update data!'
);
$data
[
'_id'
]
=
$newId
->
__toString
();
$data
[
'_id'
]
=
$newId
->
__toString
();
$updatedId
=
$co
mmand
->
save
(
'customer'
,
$data
);
$updatedId
=
$co
llection
->
save
(
$data
);
$this
->
assertEquals
(
$newId
,
$updatedId
,
'Unable to updated data by string id!'
);
$this
->
assertEquals
(
$newId
,
$updatedId
,
'Unable to updated data by string id!'
);
}
}
...
@@ -81,16 +81,16 @@ class CommandTest extends MongoTestCase
...
@@ -81,16 +81,16 @@ class CommandTest extends MongoTestCase
*/
*/
public
function
testRemove
()
public
function
testRemove
()
{
{
$co
mmand
=
$this
->
getConnection
()
->
createCommand
(
);
$co
llection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$data
=
[
$data
=
[
'name'
=>
'customer 1'
,
'name'
=>
'customer 1'
,
'address'
=>
'customer 1 address'
,
'address'
=>
'customer 1 address'
,
];
];
$id
=
$co
mmand
->
insert
(
'customer'
,
$data
);
$id
=
$co
llection
->
insert
(
$data
);
$co
mmand
->
remove
(
'customer'
,
[
'_id'
=>
$id
]);
$co
llection
->
remove
(
[
'_id'
=>
$id
]);
$rows
=
$co
mmand
->
findAll
(
'customer'
);
$rows
=
$co
llection
->
findAll
(
);
$this
->
assertEquals
(
0
,
count
(
$rows
));
$this
->
assertEquals
(
0
,
count
(
$rows
));
}
}
}
}
\ No newline at end of file
tests/unit/extensions/mongo/ConnectionTest.php
View file @
529af8ed
...
@@ -2,7 +2,9 @@
...
@@ -2,7 +2,9 @@
namespace
yiiunit\extensions\mongo
;
namespace
yiiunit\extensions\mongo
;
use
yii\mongo\Collection
;
use
yii\mongo\Connection
;
use
yii\mongo\Connection
;
use
yii\mongo\Database
;
/**
/**
* @group mongo
* @group mongo
...
@@ -17,7 +19,7 @@ class ConnectionTest extends MongoTestCase
...
@@ -17,7 +19,7 @@ class ConnectionTest extends MongoTestCase
$connection
->
open
();
$connection
->
open
();
$this
->
assertEquals
(
$params
[
'dsn'
],
$connection
->
dsn
);
$this
->
assertEquals
(
$params
[
'dsn'
],
$connection
->
dsn
);
$this
->
assertEquals
(
$params
[
'd
bName'
],
$connection
->
db
Name
);
$this
->
assertEquals
(
$params
[
'd
efaultDatabaseName'
],
$connection
->
defaultDatabase
Name
);
$this
->
assertEquals
(
$params
[
'options'
],
$connection
->
options
);
$this
->
assertEquals
(
$params
[
'options'
],
$connection
->
options
);
}
}
...
@@ -26,17 +28,15 @@ class ConnectionTest extends MongoTestCase
...
@@ -26,17 +28,15 @@ class ConnectionTest extends MongoTestCase
$connection
=
$this
->
getConnection
(
false
,
false
);
$connection
=
$this
->
getConnection
(
false
,
false
);
$this
->
assertFalse
(
$connection
->
isActive
);
$this
->
assertFalse
(
$connection
->
isActive
);
$this
->
assertEquals
(
null
,
$connection
->
c
lient
);
$this
->
assertEquals
(
null
,
$connection
->
mongoC
lient
);
$connection
->
open
();
$connection
->
open
();
$this
->
assertTrue
(
$connection
->
isActive
);
$this
->
assertTrue
(
$connection
->
isActive
);
$this
->
assertTrue
(
is_object
(
$connection
->
client
));
$this
->
assertTrue
(
is_object
(
$connection
->
mongoClient
));
$this
->
assertTrue
(
is_object
(
$connection
->
db
));
$connection
->
close
();
$connection
->
close
();
$this
->
assertFalse
(
$connection
->
isActive
);
$this
->
assertFalse
(
$connection
->
isActive
);
$this
->
assertEquals
(
null
,
$connection
->
client
);
$this
->
assertEquals
(
null
,
$connection
->
mongoClient
);
$this
->
assertEquals
(
null
,
$connection
->
db
);
$connection
=
new
Connection
;
$connection
=
new
Connection
;
$connection
->
dsn
=
'unknown::memory:'
;
$connection
->
dsn
=
'unknown::memory:'
;
...
@@ -44,10 +44,52 @@ class ConnectionTest extends MongoTestCase
...
@@ -44,10 +44,52 @@ class ConnectionTest extends MongoTestCase
$connection
->
open
();
$connection
->
open
();
}
}
public
function
testGetDatabase
()
{
$connection
=
$this
->
getConnection
();
$database
=
$connection
->
getDatabase
(
$connection
->
defaultDatabaseName
);
$this
->
assertTrue
(
$database
instanceof
Database
);
$this
->
assertTrue
(
$database
->
mongoDb
instanceof
\MongoDB
);
$database2
=
$connection
->
getDatabase
(
$connection
->
defaultDatabaseName
);
$this
->
assertTrue
(
$database
===
$database2
);
$databaseRefreshed
=
$connection
->
getDatabase
(
$connection
->
defaultDatabaseName
,
true
);
$this
->
assertFalse
(
$database
===
$databaseRefreshed
);
}
/**
* @depends testGetDatabase
*/
public
function
testGetDefaultDatabase
()
{
$connection
=
new
Connection
();
$connection
->
dsn
=
$this
->
mongoConfig
[
'dsn'
];
$connection
->
defaultDatabaseName
=
$this
->
mongoConfig
[
'defaultDatabaseName'
];
$database
=
$connection
->
getDatabase
();
$this
->
assertTrue
(
$database
instanceof
Database
,
'Unable to get default database!'
);
$connection
=
new
Connection
();
$connection
->
dsn
=
$this
->
mongoConfig
[
'dsn'
]
.
'/'
.
$this
->
mongoConfig
[
'defaultDatabaseName'
];
$database
=
$connection
->
getDatabase
();
$this
->
assertTrue
(
$database
instanceof
Database
,
'Unable to determine default database from dsn!'
);
}
/**
* @depends testGetDefaultDatabase
*/
public
function
testGetCollection
()
public
function
testGetCollection
()
{
{
$connection
=
$this
->
getConnection
(
false
);
$connection
=
$this
->
getConnection
();
$collection
=
$connection
->
getCollection
(
'customer'
);
$collection
=
$connection
->
getCollection
(
'customer'
);
$this
->
assertTrue
(
$collection
instanceof
\MongoCollection
);
$this
->
assertTrue
(
$collection
instanceof
Collection
);
$collection2
=
$connection
->
getCollection
(
'customer'
);
$this
->
assertTrue
(
$collection
===
$collection2
);
$collection2
=
$connection
->
getCollection
(
'customer'
,
true
);
$this
->
assertFalse
(
$collection
===
$collection2
);
}
}
}
}
\ No newline at end of file
tests/unit/extensions/mongo/DatabaseTest.php
0 → 100644
View file @
529af8ed
<?php
namespace
yiiunit\extensions\mongo
;
use
yii\mongo\Collection
;
/**
* @group mongo
*/
class
DatabaseTest
extends
MongoTestCase
{
protected
function
tearDown
()
{
$this
->
dropCollection
(
'customer'
);
parent
::
tearDown
();
}
// Tests :
public
function
testGetCollection
()
{
$database
=
$connection
=
$this
->
getConnection
()
->
getDatabase
();
$collection
=
$database
->
getCollection
(
'customer'
);
$this
->
assertTrue
(
$collection
instanceof
Collection
);
$this
->
assertTrue
(
$collection
->
mongoCollection
instanceof
\MongoCollection
);
$collection2
=
$database
->
getCollection
(
'customer'
);
$this
->
assertTrue
(
$collection
===
$collection2
);
$collectionRefreshed
=
$database
->
getCollection
(
'customer'
,
true
);
$this
->
assertFalse
(
$collection
===
$collectionRefreshed
);
}
}
\ No newline at end of file
tests/unit/extensions/mongo/MongoTestCase.php
View file @
529af8ed
...
@@ -14,7 +14,7 @@ class MongoTestCase extends TestCase
...
@@ -14,7 +14,7 @@ class MongoTestCase extends TestCase
*/
*/
protected
$mongoConfig
=
[
protected
$mongoConfig
=
[
'dsn'
=>
'mongodb://localhost:27017'
,
'dsn'
=>
'mongodb://localhost:27017'
,
'd
b
Name'
=>
'yii2test'
,
'd
efaultDatabase
Name'
=>
'yii2test'
,
];
];
/**
/**
* @var Connection Mongo connection instance.
* @var Connection Mongo connection instance.
...
@@ -76,8 +76,8 @@ class MongoTestCase extends TestCase
...
@@ -76,8 +76,8 @@ class MongoTestCase extends TestCase
}
}
$db
=
new
Connection
;
$db
=
new
Connection
;
$db
->
dsn
=
$this
->
mongoConfig
[
'dsn'
];
$db
->
dsn
=
$this
->
mongoConfig
[
'dsn'
];
if
(
isset
(
$this
->
mongoConfig
[
'd
b
Name'
]))
{
if
(
isset
(
$this
->
mongoConfig
[
'd
efaultDatabase
Name'
]))
{
$db
->
d
bName
=
$this
->
mongoConfig
[
'db
Name'
];
$db
->
d
efaultDatabaseName
=
$this
->
mongoConfig
[
'defaultDatabase
Name'
];
}
}
if
(
isset
(
$this
->
mongoConfig
[
'options'
]))
{
if
(
isset
(
$this
->
mongoConfig
[
'options'
]))
{
$db
->
options
=
$this
->
mongoConfig
[
'options'
];
$db
->
options
=
$this
->
mongoConfig
[
'options'
];
...
...
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