4
4
5
5
class ConnectionTests extends PHPUnit_Framework_TestCase
6
6
{
7
- protected $ connection ;
8
-
9
- public function setUp ()
10
- {
11
- if ( ! $ this ->connection )
12
- {
13
- $ this ->connection = new Connection (null , array (
14
- 'connect ' => true ,
15
- ));
16
- }
17
- }
18
-
19
- public function testInjection ()
20
- {
21
- $ mongo = $ this ->connection ->getConnection ();
22
- $ mongoHash = spl_object_hash ($ mongo );
23
- $ connection = new Connection ($ mongo );
24
- $ reflection = new ReflectionObject ($ connection );
25
- $ connectionProperty = $ reflection ->getProperty ('connection ' );
26
- $ connectionProperty ->setAccessible (true );
27
- $ this ->assertInstanceOf ('MongoClient ' , $ connectionProperty ->getValue ($ connection ));
28
- $ this ->assertEquals ($ mongoHash , spl_object_hash ($ connectionProperty ->getValue ($ connection )));
29
- }
30
-
31
- public function testDisconnect ()
32
- {
33
- $ this ->connection ->disconnect ();
34
- $ this ->assertFalse ($ this ->connection ->isConnected ());
35
- }
36
-
37
-
38
- public function testReconnectonnect ()
39
- {
40
- $ this ->connection ->disconnect ();
41
- $ this ->assertTrue ($ this ->connection ->disconnect ());
42
- $ this ->assertFalse ($ this ->connection ->isConnected ());
43
- $ this ->connection ->connect ();
44
- $ this ->assertTrue ($ this ->connection ->connect ());
45
- $ this ->assertTrue ($ this ->connection ->isConnected ());
46
- }
47
-
48
-
49
- public function testHasDatabase ()
50
- {
51
- $ this ->assertFalse ($ this ->connection ->hasDatabase ('__unknown__database__ ' ));
52
- $ this ->assertTrue ($ this ->connection ->hasDatabase ('admin ' ));
53
- }
54
-
55
- public function testDatabaseDefaultServer ()
56
- {
57
- $ connection = new Connection (null );
58
- $ host = (string ) $ connection ->getConnection ();
59
- $ this ->assertEquals ('localhost:27017 ' , $ host );
60
- }
61
-
62
- public function testDatabaseConfig ()
63
- {
64
- $ connection = new Connection (array ('connect ' => true ));
65
- $ host = (string ) $ connection ->getConnection ();
66
- $ this ->assertEquals ('localhost:27017 ' , $ host );
67
- }
68
-
69
- public function testListDatabases ()
70
- {
71
- $ list = $ this ->connection ->listDatabases ();
72
-
73
- $ this ->assertInternalType ('array ' , $ list );
74
- $ this ->assertContains ('admin ' , $ list );
75
-
76
- $ list = $ this ->connection ->listDatabases (true );
77
- $ this ->assertInternalType ('array ' , $ list );
78
- }
79
-
80
- public function testGetMongoObject ()
81
- {
82
- $ mongo = $ this ->connection ->getConnection ();
83
-
84
- $ this ->assertInstanceOf ('MongoClient ' , $ mongo );
85
- }
86
-
87
-
88
- public function testDropUnknownDatabase ()
89
- {
90
- $ result = $ this ->connection ->dropDatabase ('_unknown_ ' );
91
-
92
- $ this ->assertTrue ($ result );
93
- }
94
-
95
-
96
- public function testDropKnownDatabase ()
97
- {
98
- $ mongo = $ this ->connection ->getConnection ();
99
- $ mongo ->demo ->users ->insert (array ('test ' => true ));
100
-
101
- $ result = $ this ->connection ->dropDatabase ('demo ' );
102
-
103
- $ this ->assertTrue ($ result );
104
- }
105
-
106
-
107
- public function testGetDatabase ()
108
- {
109
- $ database = $ this ->connection ->database ('my_db ' );
110
- $ this ->assertInstanceOf ('League\Monga\Database ' , $ database );
111
- }
112
-
113
-
114
- public function testGetMongoDatabase ()
115
- {
116
- $ database = $ this ->connection ->database ('my_db ' , false );
117
- $ this ->assertInstanceOf ('MongoDB ' , $ database );
118
- }
119
-
120
-
121
- public function testGetConnection ()
122
- {
123
- $ mongo = $ this ->connection ->getConnection ();
124
- $ this ->assertInstanceOf ('MongoClient ' , $ mongo );
125
- }
126
-
127
-
128
- public function testReplaceConnection ()
129
- {
130
- $ original = $ this ->connection ->getConnection ();
131
- $ new = new MongoClient ();
132
- $ original_hash = spl_object_hash ($ original );
133
- $ new_hash = spl_object_hash ($ new );
134
- $ this ->connection ->setConnection ($ new );
135
- $ get_hash = spl_object_hash ($ this ->connection ->getConnection ());
136
- $ this ->assertEquals ($ get_hash , $ new_hash );
137
- $ this ->assertNotEquals ($ get_hash , $ original_hash );
138
- $ this ->connection ->setConnection ($ original );
139
- }
140
- }
7
+ protected $ connection ;
8
+
9
+ public function setUp ()
10
+ {
11
+ if ( ! $ this ->connection ) {
12
+ $ this ->connection = new Connection (null , array (
13
+ 'connect ' => true ,
14
+ ));
15
+ }
16
+ }
17
+
18
+ public function testInjection ()
19
+ {
20
+ $ mongo = $ this ->connection ->getConnection ();
21
+ $ mongoHash = spl_object_hash ($ mongo );
22
+ $ connection = new Connection ($ mongo );
23
+ $ reflection = new ReflectionObject ($ connection );
24
+ $ connectionProperty = $ reflection ->getProperty ('connection ' );
25
+ $ connectionProperty ->setAccessible (true );
26
+ $ this ->assertInstanceOf ('MongoClient ' , $ connectionProperty ->getValue ($ connection ));
27
+ $ this ->assertEquals ($ mongoHash , spl_object_hash ($ connectionProperty ->getValue ($ connection )));
28
+ }
29
+
30
+ public function testDisconnect ()
31
+ {
32
+ $ this ->connection ->disconnect ();
33
+ $ this ->assertFalse ($ this ->connection ->isConnected ());
34
+ }
35
+
36
+
37
+ public function testReconnectonnect ()
38
+ {
39
+ $ this ->connection ->disconnect ();
40
+ $ this ->assertTrue ($ this ->connection ->disconnect ());
41
+ $ this ->assertFalse ($ this ->connection ->isConnected ());
42
+ $ this ->connection ->connect ();
43
+ $ this ->assertTrue ($ this ->connection ->connect ());
44
+ $ this ->assertTrue ($ this ->connection ->isConnected ());
45
+ }
46
+
47
+
48
+ public function testHasDatabase ()
49
+ {
50
+ $ this ->assertFalse ($ this ->connection ->hasDatabase ('__unknown__database__ ' ));
51
+ $ this ->assertTrue ($ this ->connection ->hasDatabase ('admin ' ));
52
+ }
53
+
54
+ public function testDatabaseDefaultServer ()
55
+ {
56
+ $ connection = new Connection (null );
57
+ $ host = (string ) $ connection ->getConnection ();
58
+ $ this ->assertEquals ('localhost:27017 ' , $ host );
59
+ }
60
+
61
+ public function testDatabaseConfig ()
62
+ {
63
+ $ connection = new Connection (array ('connect ' => true ));
64
+ $ host = (string ) $ connection ->getConnection ();
65
+ $ this ->assertEquals ('localhost:27017 ' , $ host );
66
+ }
67
+
68
+ public function testListDatabases ()
69
+ {
70
+ $ list = $ this ->connection ->listDatabases ();
71
+
72
+ $ this ->assertInternalType ('array ' , $ list );
73
+ $ this ->assertContains ('admin ' , $ list );
74
+
75
+ $ list = $ this ->connection ->listDatabases (true );
76
+ $ this ->assertInternalType ('array ' , $ list );
77
+ }
78
+
79
+ public function testGetMongoObject ()
80
+ {
81
+ $ mongo = $ this ->connection ->getConnection ();
82
+
83
+ $ this ->assertInstanceOf ('MongoClient ' , $ mongo );
84
+ }
85
+
86
+
87
+ public function testDropUnknownDatabase ()
88
+ {
89
+ $ result = $ this ->connection ->dropDatabase ('_unknown_ ' );
90
+
91
+ $ this ->assertTrue ($ result );
92
+ }
93
+
94
+
95
+ public function testDropKnownDatabase ()
96
+ {
97
+ $ mongo = $ this ->connection ->getConnection ();
98
+ $ mongo ->demo ->users ->insert (array ('test ' => true ));
99
+
100
+ $ result = $ this ->connection ->dropDatabase ('demo ' );
101
+
102
+ $ this ->assertTrue ($ result );
103
+ }
104
+
105
+
106
+ public function testGetDatabase ()
107
+ {
108
+ $ database = $ this ->connection ->database ('my_db ' );
109
+ $ this ->assertInstanceOf ('League\Monga\Database ' , $ database );
110
+ }
111
+
112
+
113
+ public function testGetMongoDatabase ()
114
+ {
115
+ $ database = $ this ->connection ->database ('my_db ' , false );
116
+ $ this ->assertInstanceOf ('MongoDB ' , $ database );
117
+ }
118
+
119
+
120
+ public function testGetConnection ()
121
+ {
122
+ $ mongo = $ this ->connection ->getConnection ();
123
+ $ this ->assertInstanceOf ('MongoClient ' , $ mongo );
124
+ }
125
+
126
+
127
+ public function testReplaceConnection ()
128
+ {
129
+ $ original = $ this ->connection ->getConnection ();
130
+ $ new = new MongoClient ();
131
+ $ original_hash = spl_object_hash ($ original );
132
+ $ new_hash = spl_object_hash ($ new );
133
+ $ this ->connection ->setConnection ($ new );
134
+ $ get_hash = spl_object_hash ($ this ->connection ->getConnection ());
135
+ $ this ->assertEquals ($ get_hash , $ new_hash );
136
+ $ this ->assertNotEquals ($ get_hash , $ original_hash );
137
+ $ this ->connection ->setConnection ($ original );
138
+ }
139
+ }
0 commit comments