This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathDatabaseEventsTest.php
180 lines (145 loc) · 5.32 KB
/
DatabaseEventsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Doctrine\MongoDB\Tests;
use Doctrine\Common\EventManager;
use Doctrine\MongoDB\Database;
use Doctrine\MongoDB\Events;
use Doctrine\MongoDB\Event\CreateCollectionEventArgs;
use Doctrine\MongoDB\Event\EventArgs;
use Doctrine\MongoDB\Event\MutableEventArgs;
class DatabaseEventsTest extends TestCase
{
private $connection;
private $eventManager;
private $mongoDB;
public function setUp()
{
$this->connection = $this->getMockConnection();
$this->eventManager = $this->getMockEventManager();
$this->mongoDB = $this->getMockMongoDB();
}
public function testCreateCollection()
{
$name = 'collection';
$options = ['capped' => false, 'size' => 0, 'max' => 0];
$result = $this->getMockCollection();
$db = $this->getMockDatabase(['doCreateCollection' => $result]);
$this->expectEvents([
[Events::preCreateCollection, new CreateCollectionEventArgs($db, $name, $options)],
[Events::postCreateCollection, new EventArgs($db, $result)],
]);
$this->assertSame($result, $db->createCollection($name, $options));
}
public function testDrop()
{
$result = ['dropped' => 'databaseName', 'ok' => 1];
$this->mongoDB->expects($this->once())
->method('drop')
->will($this->returnValue($result));
$db = new Database($this->connection, $this->mongoDB, $this->eventManager);
$this->expectEvents([
[Events::preDropDatabase, new EventArgs($db)],
[Events::postDropDatabase, new EventArgs($db)],
]);
$this->assertSame($result, $db->drop());
}
public function testGetDBRef()
{
$reference = ['$ref' => 'collection', '$id' => 1];
$result = ['_id' => 1];
$db = $this->getMockDatabase(['doGetDBRef' => $result]);
$this->expectEvents([
[Events::preGetDBRef, new EventArgs($db, $reference)],
[Events::postGetDBRef, new MutableEventArgs($db, $result)],
]);
$this->assertSame($result, $db->getDBRef($reference));
}
public function testGetGridFS()
{
$prefix = 'fs';
$result = $this->getMockGridFS();
$db = $this->getMockDatabase(['doGetGridFS' => $result]);
$this->expectEvents([
[Events::preGetGridFS, new EventArgs($db, $prefix)],
[Events::postGetGridFS, new EventArgs($db, $result)],
]);
$this->assertSame($result, $db->getGridFS());
}
public function testSelectCollection()
{
$name = 'collection';
$result = $this->getMockCollection();
$db = $this->getMockDatabase(['doSelectCollection' => $result]);
$this->expectEvents([
[Events::preSelectCollection, new EventArgs($db, $name)],
[Events::postSelectCollection, new EventArgs($db, $result)],
]);
$this->assertSame($result, $db->selectCollection($name));
}
/**
* Expect events to be dispatched by the event manager in the given order.
*
* @param array $events Tuple of event name and dispatch argument
*/
private function expectEvents(array $events)
{
/* Each event should be a tuple consisting of the event name and the
* dispatched argument (e.g. EventArgs).
*
* For each event, expect a call to hasListeners() immediately followed
* by a call to dispatchEvent(). The dispatch argument is passed as-is
* to with(), so constraints may be used (e.g. callback).
*/
foreach ($events as $i => $event) {
$this->eventManager->expects($this->at($i * 2))
->method('hasListeners')
->with($event[0])
->will($this->returnValue(true));
$this->eventManager->expects($this->at($i * 2 + 1))
->method('dispatchEvent')
->with($event[0], $event[1]);
}
}
private function getMockCollection()
{
return $this->getMockBuilder('Doctrine\MongoDB\Collection')
->disableOriginalConstructor()
->getMock();
}
private function getMockConnection()
{
return $this->getMockBuilder('Doctrine\MongoDB\Connection')
->disableOriginalConstructor()
->getMock();
}
private function getMockDatabase(array $methods = [])
{
$db = $this->getMockBuilder('Doctrine\MongoDB\Database')
->setConstructorArgs([$this->connection, $this->mongoDB, $this->eventManager])
->setMethods(array_keys($methods))
->getMock();
foreach ($methods as $method => $returnValue) {
$db->expects($this->once())
->method($method)
->will($this->returnValue($returnValue));
}
return $db;
}
private function getMockEventManager()
{
return $this->getMockBuilder('Doctrine\Common\EventManager')
->disableOriginalConstructor()
->getMock();
}
private function getMockGridFS()
{
return $this->getMockBuilder('Doctrine\MongoDB\GridFS')
->disableOriginalConstructor()
->getMock();
}
private function getMockMongoDB()
{
return $this->getMockBuilder('MongoDB')
->disableOriginalConstructor()
->getMock();
}
}