1
|
|
|
<?php |
2
|
|
|
namespace tests; |
3
|
|
|
|
4
|
|
|
use Soupmix\MongoDB; |
5
|
|
|
|
6
|
|
|
class MongoDBTest extends \PHPUnit_Framework_TestCase |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var \Soupmix\MongoDB $client |
10
|
|
|
*/ |
11
|
|
|
protected $client = null; |
12
|
|
|
|
13
|
|
|
protected function setUp() |
14
|
|
|
{ |
15
|
|
|
$this->client = new MongoDB([ |
16
|
|
|
'db_name' => 'mydb_test', |
17
|
|
|
'connection_string' => "mongodb://soupmix:[email protected]", |
18
|
|
|
'options' => [] |
19
|
|
|
]); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testInsertGetDocument() |
23
|
|
|
{ |
24
|
|
|
$docId = $this->client->insert('test', ['id' => 1, 'title' => 'test']); |
25
|
|
|
$document = $this->client->get('test', $docId); |
26
|
|
|
$this->assertArrayHasKey('title', $document); |
27
|
|
|
$this->assertArrayHasKey('id', $document); |
28
|
|
|
$result = $this->client->delete('test', ['id' => $docId]); |
29
|
|
|
$this->assertTrue($result == 1); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testFindDocuments() |
33
|
|
|
{ |
34
|
|
|
$docId1 = $this->client->insert('test', ['id' => 1, 'title' => 'test']); |
35
|
|
|
sleep(1); // waiting to be able to be searchable on elasticsearch. |
36
|
|
|
$results = $this->client->find('test', ['title' => 'test']); |
37
|
|
|
$this->assertArrayHasKey('total', $results); |
38
|
|
|
$this->assertArrayHasKey('data', $results); |
39
|
|
|
$this->assertCount($results['total'], $results['data']); |
40
|
|
|
|
41
|
|
|
$result = $this->client->delete('test', ['id' => $docId1]); |
42
|
|
|
$this->assertTrue($result == 1); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testInsertUpdateGetDocument() |
46
|
|
|
{ |
47
|
|
|
// $docId = $this->client->insert('test', ['id' => 1, 'title' => 'test']); |
|
|
|
|
48
|
|
|
// sleep(1); // waiting to be able to be searchable on elasticsearch. |
49
|
|
|
// $modifiedCount = $this->client->update('test', ['title' => 'test'], ['title' => 'test2']); |
50
|
|
|
// $this->assertTrue($modifiedCount >= 1); |
51
|
|
|
// sleep(1); // waiting to be able to be searchable on elasticsearch. |
52
|
|
|
// $document = $this->client->get('test', $docId); |
53
|
|
|
// $this->assertArrayHasKey('title', $document); |
54
|
|
|
// $this->assertEquals('test2', $document['title']); |
55
|
|
|
// |
56
|
|
|
// $result = $this->client->delete('test', ['_id' => $docId]); |
57
|
|
|
// $this->assertTrue($result == 1); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.