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
<?php
use Codeception\Module\MongoDb;
class MongoDbLegacyTest extends \PHPUnit\Framework\TestCase
{
/**
* @var array
*/
private $mongoConfig = array(
'dsn' => 'mongodb://localhost:27017/test'
);
/**
* @var MongoDb
*/
protected $module;
/**
* @var \MongoDb
*/
protected $db;
/**
* @var \MongoCollection
*/
private $userCollection;
protected function setUp()
{
if (!class_exists('Mongo')) {
$this->markTestSkipped('Mongo is not installed');
}
if (!class_exists('MongoDB\Client')) {
$this->markTestSkipped('MongoDb\Client is not installed');
}
$mongo = new \MongoClient();
$this->module = new MongoDb(make_container());
$this->module->_setConfig($this->mongoConfig);
$this->module->_initialize();
$this->db = $mongo->selectDB('test');
$this->userCollection = $this->db->createCollection('users');
$this->userCollection->insert(array('id' => 1, 'email' => 'miles@davis.com'));
}
protected function tearDown()
{
if (!is_null($this->userCollection)) {
$this->userCollection->drop();
}
}
public function testSeeInCollection()
{
$this->module->seeInCollection('users', array('email' => 'miles@davis.com'));
}
public function testDontSeeInCollection()
{
$this->module->dontSeeInCollection('users', array('email' => 'davert@davert.com'));
}
public function testHaveAndSeeInCollection()
{
$this->module->haveInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com'));
$this->module->seeInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com'));
}
public function testGrabFromCollection()
{
$user = $this->module->grabFromCollection('users', array('id' => 1));
$this->assertArrayHasKey('email', $user);
$this->assertEquals('miles@davis.com', $user['email']);
}
public function testSeeNumElementsInCollection()
{
$this->module->seeNumElementsInCollection('users', 1);
$this->module->seeNumElementsInCollection('users', 1, array('email' => 'miles@davis.com'));
$this->module->seeNumElementsInCollection('users', 0, array('name' => 'Doe'));
}
public function testGrabCollectionCount()
{
$this->userCollection->insert(array('id' => 2, 'email' => 'louis@armstrong.com'));
$this->userCollection->insert(array('id' => 3, 'email' => 'dizzy@gillespie.com'));
$this->assertEquals(1, $this->module->grabCollectionCount('users', array('id' => 3)));
$this->assertEquals(3, $this->module->grabCollectionCount('users'));
}
public function testSeeElementIsArray()
{
$this->userCollection->insert(array('id' => 4, 'trumpets' => array('piccolo', 'bass', 'slide')));
$this->module->seeElementIsArray('users', array('id' => 4), 'trumpets');
}
public function testSeeElementIsArrayThrowsError()
{
$this->setExpectedException('PHPUnit\Framework\ExpectationFailedException');
$this->userCollection->insert(array('id' => 5, 'trumpets' => array('piccolo', 'bass', 'slide')));
$this->userCollection->insert(array('id' => 6, 'trumpets' => array('piccolo', 'bass', 'slide')));
$this->module->seeElementIsArray('users', array(), 'trumpets');
}
public function testSeeElementIsObject()
{
$trumpet = new \StdClass;
$trumpet->name = 'Trumpet 1';
$trumpet->pitch = 'B♭';
$trumpet->price = array('min' => 458, 'max' => 891);
$this->userCollection->insert(array('id' => 6, 'trumpet' => $trumpet));
$this->module->seeElementIsObject('users', array('id' => 6), 'trumpet');
}
public function testSeeElementIsObjectThrowsError()
{
$trumpet = new \StdClass;
$trumpet->name = 'Trumpet 1';
$trumpet->pitch = 'B♭';
$trumpet->price = array('min' => 458, 'max' => 891);
$this->setExpectedException('PHPUnit\Framework\ExpectationFailedException');
$this->userCollection->insert(array('id' => 5, 'trumpet' => $trumpet));
$this->userCollection->insert(array('id' => 6, 'trumpet' => $trumpet));
$this->module->seeElementIsObject('users', array(), 'trumpet');
}
public function testUseDatabase()
{
$this->module->useDatabase('example');
$this->module->haveInCollection('stuff', array('name' => 'Ashley', 'email' => 'me@ashleyclarke.me'));
$this->module->seeInCollection('stuff', array('name' => 'Ashley', 'email' => 'me@ashleyclarke.me'));
$this->module->dontSeeInCollection('users', array('email' => 'miles@davis.com'));
}
}