1 | <?php |
||
6 | { |
||
7 | /** |
||
8 | * @var \Soupmix\MongoDB $client |
||
9 | */ |
||
10 | protected $client = null; |
||
11 | |||
12 | protected function setUp() |
||
13 | { |
||
14 | $config =[ |
||
15 | 'db_name' => 'mydb_test', |
||
16 | 'connection_string' => "mongodb://127.0.0.1", |
||
17 | 'options' => [] |
||
18 | ]; |
||
19 | $client = new \MongoDB\Client($config['connection_string'], $config['options']); |
||
20 | $this->client = new \Soupmix\MongoDB($config, $client); |
||
21 | } |
||
22 | |||
23 | public function testInsertGetDocument() |
||
24 | { |
||
25 | $docId = $this->client->insert('test', ['id' => 1, 'title' => 'test']); |
||
26 | $document = $this->client->get('test', $docId); |
||
27 | $this->assertArrayHasKey('title', $document); |
||
28 | $this->assertArrayHasKey('id', $document); |
||
29 | $result = $this->client->delete('test', ['id' => $docId]); |
||
30 | $this->assertTrue($result == 1); |
||
31 | } |
||
32 | |||
33 | |||
34 | |||
35 | public function testFindDocuments() |
||
36 | { |
||
37 | $docIds = []; |
||
38 | $data = $this->bulkData(); |
||
39 | foreach ($data as $d) { |
||
40 | $docId = $this->client->insert('test', $d); |
||
41 | $this->assertNotNull($docId, 'Document could not inserted to ES while testing find'); |
||
42 | if ($docId) { |
||
43 | $docIds[] = $docId; |
||
44 | } |
||
45 | } |
||
46 | sleep(1); // waiting to be able to be searchable on elasticsearch. |
||
47 | $results = $this->client->find('test', ['title' => 'test1']); |
||
48 | $this->assertArrayHasKey('total', $results); |
||
49 | $this->assertArrayHasKey('data', $results); |
||
50 | $this->assertGreaterThanOrEqual(1, $results['total'], 'Total not equal for field term filtering'); |
||
51 | |||
52 | $results = $this->client->find('test', ['count.max__gte' => 6]); |
||
53 | $this->assertGreaterThanOrEqual(2, $results['total'], |
||
54 | 'Total not greater than or equal to 2 on count_gte filtering'); |
||
55 | |||
56 | $results = $this->client->find('test', ['count.max__gte' => 6, 'count.min__gte' => 2]); |
||
57 | $this->assertGreaterThanOrEqual(1, $results['total'], |
||
58 | 'Total not greater than or equal to 2 on count.max__gte and count.max__gte filtering'); |
||
59 | |||
60 | $results = $this->client->find('test', [[['count.max__gte' => 6], ['count.min__gte' => 2]], 'title' => 'test4']); |
||
61 | $this->assertGreaterThanOrEqual(1, $results['total'], |
||
62 | 'Total not greater than or equal to 2 on count.max__gte and count.max__gte filtering'); |
||
63 | |||
64 | foreach ($docIds as $docId) { |
||
65 | $result = $this->client->delete('test', ['id' => $docId]); |
||
66 | $this->assertTrue($result == 1); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | |||
71 | |||
72 | public function bulkData() |
||
73 | { |
||
74 | return [ |
||
75 | ['id' => 1, 'date' => '2015-04-10 00:00:00', 'title' => 'test1', 'balance' => 100.0, 'count' => ['min' => 1, 'max' => 1]], |
||
76 | ['id' => 2, 'date' => '2015-04-11 00:00:00', 'title' => 'test2', 'balance' => 120.0, 'count' => ['min' => 1, 'max' => 1]], |
||
77 | ['id' => 3, 'date' => '2015-04-12 00:00:00', 'title' => 'test3', 'balance' => 101.5, 'count' => ['min' => 1, 'max' => 7]], |
||
78 | ['id' => 4, 'date' => '2015-04-12 00:00:00', 'title' => 'test4', 'balance' => 200.5, 'count' => ['min' => 3, 'max' => 6]], |
||
79 | ['id' => 5, 'date' => '2015-04-13 00:00:00', 'title' => 'test5', 'balance' => 150.0, 'count' => ['min' => 1, 'max' => 5]], |
||
80 | ['id' => 6, 'date' => '2015-04-14 00:00:00', 'title' => 'test6', 'balance' => 400.8, 'count' => ['min' => 1, 'max' => 4]], |
||
81 | ['id' => 7, 'date' => '2015-04-15 00:00:00', 'title' => 'test7', 'balance' => 240.0, 'count' => ['min' => 1, 'max' => 4]], |
||
82 | ['id' => 8, 'date' => '2015-04-20 00:00:00', 'title' => 'test8', 'balance' => 760.0, 'count' => ['min' => 1, 'max' => 5]], |
||
83 | ['id' => 9, 'date' => '2015-04-20 00:00:00', 'title' => 'test9', 'balance' => 50.0, 'count' => ['min' => 1, 'max' => 2]], |
||
84 | ['id' => 10, 'date' => '2015-04-21 00:00:00', 'title' => 'test0', 'balance' => 55.5, 'count' => ['min' => 1, 'max' => 2]], |
||
85 | ]; |
||
86 | } |
||
87 | } |
||
88 |