/
DbTest.php
190 lines (162 loc) · 6.44 KB
/
DbTest.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
181
182
183
184
185
186
187
188
189
190
<?php
namespace Microweber\tests;
class DbTest extends TestCase
{
private $save = array(
'content_type' => 'page',
'subtype' => 'static',
'title' => 'one page',
'parent' => '0',
'is_deleted' => '0',
);
private $save_post = array(
'content_type' => 'post',
'subtype' => 'static',
'title' => 'one post',
'parent' => '0',
'is_deleted' => '0',
);
private $content;
private $content5;
public function setUp()
{
parent::setUp();
$this->content = db_save('content', $this->save);
$this->content5 = db_save('content', $this->save_post);
}
public function testSimpleSave()
{
$save = $this->save;
$save_post = $this->save_post;
$content = $this->content;
$content5 = $this->content5;
//$content = db_save('content', $save);
$content2 = db_save('content', $save);
$content3 = db_save('content', $save);
$content4 = db_save('content', $save);
//$content5 = db_save('content', $save_post);
$content6 = db_save('content', $save_post);
$this->assertEquals(2, $content);
$this->assertEquals(4, $content2);
$this->assertTrue($content != $content2);
$this->assertTrue($content2 != $content3);
$this->assertTrue($content3 != $content4);
$this->assertTrue($content4 != $content5);
$this->assertTrue($content5 != $content6);
}
public function testSimpleGet()
{
$content = db_get('content', 'limit=2');
$this->assertTrue(!empty($content));
$this->assertEquals(2, count($content));
}
public function testSimpleCount()
{
$content_count = db_get('content', 'count=true');
$this->assertTrue($content_count > 0);
$this->assertTrue(is_int($content_count));
}
public function testPageCount()
{
$content_count = db_get('content', 'count=true');
$pages_count = db_get('content', 'limit=2&count_paging=1');
$must_be = intval(floor($content_count / 2));
$this->assertEquals($pages_count, $must_be);
}
public function testOrderBy()
{
$content = db_get('content', 'limit=1&single=1&order_by=id desc');
$content2 = db_get('content', 'limit=1&single=1&order_by=id asc');
$this->assertTrue(isset($content['id']));
$this->assertTrue(isset($content2['id']));
$this->assertNotEquals($content['id'], $content2['id']);
$this->assertTrue(($content['id'] > $content2['id']));
}
public function testLimitAndPaging()
{
$pages_count = db_get('content', 'limit=2&count_paging=1');
$first_page = db_get('content', 'limit=2');
$second_page = db_get('content', 'limit=2¤t_page=2');
$first_page_items = count($first_page);
$second_page_items = count($second_page);
$ids_on_first_page = array();
foreach ($first_page as $item) {
$this->assertTrue(isset($item['id']));
$ids_on_first_page[] = $item['id'];
}
foreach ($second_page as $item) {
$this->assertTrue(isset($item['id']));
$this->assertFalse(in_array($item['id'], $ids_on_first_page));
}
$this->assertEquals($first_page_items, $second_page_items + 1, 'First page item count: ' . $first_page_items . ', second page item count: ' . $second_page_items);
$this->assertFalse(($pages_count > 1));
$this->assertTrue(is_int($pages_count));
}
public function testIncludeExcludeIds()
{
$content = db_get('content', 'limit=10');
$this->assertTrue(is_array($content));
$some_ids = array();
foreach ($content as $item) {
$some_ids[] = $item['id'];
}
$half = round(count($some_ids) / 2);
shuffle($some_ids);
$some_ids = array_slice($some_ids, $half);
$includeString = 'ids='.implode(',', $some_ids);
$content_ids = db_get('content', $includeString);
foreach ($content_ids as $item) {
$this->assertTrue(in_array($item['id'], $some_ids));
}
$this->assertTrue(is_array($content_ids));
$excludeString = 'exclude_ids='.implode(',', $some_ids);
$content_ids = db_get('content', $excludeString);
foreach ($content_ids as $item) {
$this->assertTrue(!in_array($item['id'], $some_ids));
}
$this->assertTrue(is_array($content_ids));
}
public function testMinMaxAvg()
{
$content = db_get('content', 'content_type=page&min=id');
$content_max = db_get('content', 'content_type=page&max=id');
$content_avg = db_get('content', 'content_type=page&avg=id');
$this->assertFalse(is_int($content));
$content = intval($content);
$this->assertFalse(is_int($content_max));
$content_max = intval($content_max);
$this->assertFalse(is_int($content_avg));
$content_avg = intval($content_avg);
$this->assertTrue(($content < $content_max), "Content: " . $content . ", Content_max: " . $content_max);
$this->assertTrue(($content_avg < $content_max), "Content_avg: " . $content_avg . ", Content_max: " . $content_max);
$this->assertTrue(($content <= $content_avg), "Content: " . $content . ", Content_avg: " . $content_avg);
}
public function testShorthandFilters()
{
$content = db_get('content', 'limit=1&content_type=[eq]page');
foreach ($content as $item) {
$this->assertTrue(($item['content_type'] == 'page'));
}
$content = db_get('content', 'limit=1&content_type=[neq]page');
foreach ($content as $item) {
$this->assertTrue(($item['content_type'] != 'page'));
}
$content = db_get('content', 'limit=1&content_type=[like]post');
foreach ($content as $item) {
$this->assertTrue(($item['content_type'] == 'post'));
}
$content = db_get('content', 'limit=1&content_type=[not_like]post');
foreach ($content as $item) {
$this->assertTrue(($item['content_type'] != 'post'));
}
}
public function testSelectOnlyFields()
{
$content = db_get('content', 'limit=2&fields=id,position&order_by=id desc');
foreach ($content as $item) {
$this->assertTrue(count($item) == 2);
$this->assertTrue(isset($item['id']));
$this->assertFalse(isset($item['position']));
}
}
}