1
|
|
|
<?php |
2
|
|
|
namespace tests; |
3
|
|
|
|
4
|
|
|
use Soupmix\SQL; |
5
|
|
|
use Doctrine\DBAL\DriverManager; |
6
|
|
|
class SQLTest extends \PHPUnit_Framework_TestCase |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var \Soupmix\SQL $client |
10
|
|
|
*/ |
11
|
|
|
protected $client = null; |
12
|
|
|
|
13
|
|
|
protected function setUp() |
14
|
|
|
{ |
15
|
|
|
ini_set("date.timezone", "Europe/Istanbul"); |
16
|
|
|
|
17
|
|
|
$config = [ |
18
|
|
|
'db_name' => 'test', |
19
|
|
|
'user_name' => 'root', |
20
|
|
|
'password' => '', |
21
|
|
|
'host' => '127.0.0.1', |
22
|
|
|
'port' => 3306, |
23
|
|
|
'charset' => 'utf8', |
24
|
|
|
'driver' => 'pdo_mysql', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
$client = DriverManager::getConnection($config); |
28
|
|
|
|
29
|
|
|
$this->client = new SQL($config, $client); |
30
|
|
|
$fields = [ |
31
|
|
|
['name' => 'title','type' => 'string', 'index' => true, 'index_type' => 'unique'], |
32
|
|
|
['name' => 'age','type' =>'smallint', 'maxLength' => 3, 'default' => 24, 'index' => true], |
33
|
|
|
['name' => 'count','type'=>'smallint', 'maxLength' => 3, 'default' => 0, 'index' => true ], |
34
|
|
|
['name' => 'balance','type'=>'float', 'maxLength' => 3, 'default' => 0.0], |
35
|
|
|
['name' => 'date','type'=>'datetime'] |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
$this->client->drop('test'); |
39
|
|
|
$this->client->create('test', $fields); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
public function testInsertGetDocument() |
44
|
|
|
{ |
45
|
|
|
$docId = $this->client->insert('test', ['id' => 1, 'title' => 'test','date' => date("Y-m-d H:i:s")]); |
46
|
|
|
$document = $this->client->get('test', $docId); |
47
|
|
|
$this->assertArrayHasKey('title', $document); |
48
|
|
|
$this->assertArrayHasKey('id', $document); |
49
|
|
|
$result = $this->client->delete('test', ['id' => $docId]); |
50
|
|
|
$this->assertTrue($result == 1); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
public function testFindDocuments() |
55
|
|
|
{ |
56
|
|
|
$docIds = []; |
57
|
|
|
$data = $this->bulkData(); |
58
|
|
|
foreach ($data as $d) { |
59
|
|
|
$docId = $this->client->insert('test', $d); |
60
|
|
|
$this->assertNotNull($docId, 'Document could not inserted to SQL while testing find'); |
61
|
|
|
if ($docId) { |
|
|
|
|
62
|
|
|
$docIds[] = $docId; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
$results = $this->client->find('test', ['title' => 'test1']); |
66
|
|
|
$this->assertArrayHasKey('total', $results); |
67
|
|
|
$this->assertArrayHasKey('data', $results); |
68
|
|
|
$this->assertArrayHasKey('title', $results['data'][0]); |
69
|
|
|
|
70
|
|
|
$results = $this->client->find('test', ['count__gte' => 6]); |
71
|
|
|
$this->assertGreaterThanOrEqual(2, $results['total'], |
72
|
|
|
'Total not greater than or equal to 2 on count_gte filtering'); |
73
|
|
|
|
74
|
|
|
$results = $this->client->find('test', [[['count__gte' => 6], ['count__gte' => 2]]]); |
75
|
|
|
$this->assertGreaterThanOrEqual(1, $results['total'], |
76
|
|
|
'Total not greater than or equal to 2 on count__gte and count__gte filtering'); |
77
|
|
|
|
78
|
|
|
$results = $this->client->find('test', [[['count__lte' => 6], ['count__gte' => 2]], 'title' => 'test4']); |
79
|
|
|
$this->assertGreaterThanOrEqual(1, $results['total'], |
80
|
|
|
'Total not greater than or equal to 2 on count__gte and count__gte filtering'); |
81
|
|
|
|
82
|
|
|
foreach ($docIds as $docId) { |
83
|
|
|
$result = $this->client->delete('test', ['id' => $docId]); |
84
|
|
|
$this->assertTrue($result == 1); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
|
92
|
|
|
public function bulkData() |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
['date' => '2015-04-10 00:00:00', 'title' => 'test1', 'balance' => 100.0, 'count' => 1], |
96
|
|
|
['date' => '2015-04-11 00:00:00', 'title' => 'test2', 'balance' => 120.0, 'count' => 2], |
97
|
|
|
['date' => '2015-04-12 00:00:00', 'title' => 'test3', 'balance' => 101.5, 'count' => 3], |
98
|
|
|
['date' => '2015-04-12 00:00:00', 'title' => 'test4', 'balance' => 200.5, 'count' => 4], |
99
|
|
|
['date' => '2015-04-13 00:00:00', 'title' => 'test5', 'balance' => 150.0, 'count' => 5], |
100
|
|
|
['date' => '2015-04-14 00:00:00', 'title' => 'test6', 'balance' => 400.8, 'count' => 6], |
101
|
|
|
['date' => '2015-04-15 00:00:00', 'title' => 'test7', 'balance' => 240.0, 'count' => 7], |
102
|
|
|
['date' => '2015-04-20 00:00:00', 'title' => 'test8', 'balance' => 760.0, 'count' => 8], |
103
|
|
|
['date' => '2015-04-20 00:00:00', 'title' => 'test9', 'balance' => 50.0, 'count' => 9], |
104
|
|
|
['date' => '2015-04-21 00:00:00', 'title' => 'test0', 'balance' => 55.5, 'count' => 10], |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: