-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMikronTest.php
183 lines (152 loc) · 4.95 KB
/
MikronTest.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
<?php
class Entity
{
/** @primary */
public $id;
/** @field */
public $name;
public $skip;
}
class MikronTest extends PHPUnit_Framework_TestCase
{
/**
* @var Mikron
*/
protected $mikron;
public function setup()
{
$pdo = new \Pdo(
$GLOBALS['DB_DSN'],
$GLOBALS['DB_USER'],
$GLOBALS['DB_PASSWD'],
array(
\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
)
);
$sql="
DROP TABLE IF EXISTS `entity`;
CREATE TABLE `entity` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(225) DEFAULT NULL,
PRIMARY KEY (`id`)
);
";
$stmt = $pdo->prepare($sql);
$stmt->execute();
require_once 'Mikron.php';
$this->mikron = new Mikron($pdo);
}
public function testExecute()
{
$result = $this->mikron->execute("SELECT 2");
$this->assertEquals(2, $result);
$result = $this->mikron->execute("SELECT :num", array('num'=>3));
$this->assertEquals(3, $result);
}
public function testLoad()
{
$newEntity = new Entity();
$newEntity->name = 'Andrius Putna';
$this->mikron->store($newEntity);
$entity = $this->mikron->load("entity", 1);
$this->assertInstanceOf('Entity', $entity);
$this->assertEquals(1, $entity->id);
$this->assertEquals('Andrius Putna', $entity->name);
}
public function testStoreInsert()
{
$newEntity = new Entity();
$newEntity->name = 'New Name';
$EntityWithId = $this->mikron->store($newEntity);
$this->assertFalse(is_null($EntityWithId->id));
$this->assertEquals($EntityWithId->id, $this->mikron->lastInsertId());
}
public function testStoreUpdate()
{
$newEntity = new Entity();
$newEntity->name = 'New Name';
$entity = $this->mikron->store($newEntity);
$this->assertEquals(1, $entity->id);
$entity->name = "Andrius Putna";
$entity = $this->mikron->store($entity);
$this->assertEquals(1, $entity->id);
}
public function testFindOne()
{
$newEntity = new Entity();
$newEntity->name = 'Andrius Putna';
$this->mikron->store($newEntity);
$entity = $this->mikron->findOne("entity", "name = :name", array('name'=>'Andrius Putna'));
$this->assertEquals(1, $entity->id);
}
public function testFindAll()
{
$newEntity = new Entity();
$newEntity->name = 'Name 1';
$this->mikron->store($newEntity);
$newEntity = new Entity();
$newEntity->name = 'Name 2';
$this->mikron->store($newEntity);
$entities = $this->mikron->findAll("entity", "1");
$this->assertEquals(2, count($entities));
foreach($entities as $entity) {
$this->assertInstanceOf('Entity', $entity);
}
}
public function testTrash()
{
$newEntity = new Entity();
$newEntity->name = 'Andrius Putna';
$this->mikron->store($newEntity);
$entity = $this->mikron->load("entity", 1);
$bool = $this->mikron->trash($entity);
$this->assertTrue($bool);
}
public function testGetAll()
{
$newEntity = new Entity();
$newEntity->name = 'Name';
$this->mikron->store($newEntity);
$newEntity = new Entity();
$newEntity->name = 'Name';
$this->mikron->store($newEntity);
$list = $this->mikron->getAll("SELECT * FROM entity WHERE 1");
$this->assertEquals(2, count($list));
foreach($list as $item) {
$this->assertInternalType('array', $item);
$this->assertEquals('Name', $item['name']);
}
}
public function testGetRow()
{
$newEntity = new Entity();
$newEntity->name = 'Name';
$this->mikron->store($newEntity);
$item = $this->mikron->getRow("SELECT * FROM entity WHERE id = 1");
$this->assertInternalType('array', $item);
$this->assertEquals('Name', $item['name']);
}
public function testGetCell()
{
$cell = $this->mikron->getCell("SELECT 44");
$this->assertEquals(44, $cell);
}
/*
public function testGetTableName()
{
$name = "Title";
$this->mikron->setNameResolver(function($type, $name) { return $name; });
$newName = $this->mikron->getTableName($name);
$this->assertEquals($name, $newName);
}
public function testGetEntityName()
{
$name = "title";
$this->mikron->setNameResolver(function($type, $name) { return ucfirst($name); });
$newName = $this->mikron->getClassName($name);
$this->assertEquals('Title', $newName);
}
*/
}