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
use Prado\Data\TDbConnection;
use Prado\Exceptions\TDbException;
use Prado\TApplication;
if(!defined('TEST_DB_FILE'))
define('TEST_DB_FILE',dirname(__FILE__).'/db/test.db');
class FooRecord extends TComponent
{
public $id;
private $_name;
public $param;
public function __construct($param)
{
$this->param=$param;
}
public function getName()
{
return $this->_name;
}
public function setName($value)
{
$this->_name=$value;
}
}
/**
* @package System.Data.PDO
*/
class TDbDataReaderTest extends PHPUnit_Framework_TestCase
{
private $_connection;
public function setUp()
{
@unlink(TEST_DB_FILE);
// create application just to provide application mode
new TApplication(__DIR__, false ,TApplication::CONFIG_TYPE_PHP);
$this->_connection=new TDbConnection('sqlite:'.TEST_DB_FILE);
$this->_connection->Active=true;
$this->_connection->createCommand('CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))')->execute();
$this->_connection->createCommand('INSERT INTO foo (name) VALUES (\'my name\')')->execute();
$this->_connection->createCommand('INSERT INTO foo (name) VALUES (\'my name 2\')')->execute();
}
public function tearDown()
{
$this->_connection=null;
}
public function testRead()
{
$reader=$this->_connection->createCommand('SELECT * FROM foo')->query();
$row=$reader->read();
$this->assertTrue($row['id']==='1' && $row['name']==='my name');
$row=$reader->read();
$this->assertTrue($row['id']==='2' && $row['name']==='my name 2');
$row=$reader->read();
$this->assertFalse($row);
}
public function testReadColumn()
{
$reader=$this->_connection->createCommand('SELECT * FROM foo')->query();
$this->assertEquals($reader->readColumn(0),'1');
$this->assertEquals($reader->readColumn(1),'my name 2');
$this->assertFalse($reader->readColumn(0));
}
public function testReadObject()
{
$reader=$this->_connection->createCommand('SELECT * FROM foo')->query();
$object=$reader->readObject('FooRecord',array('object'));
$this->assertEquals($object->id,'1');
$this->assertEquals($object->Name,'my name');
$this->assertEquals($object->param,'object');
}
public function testReadAll()
{
$reader=$this->_connection->createCommand('SELECT * FROM foo')->query();
$rows=$reader->readAll();
$this->assertEquals(count($rows),2);
$row=$rows[0];
$this->assertTrue($row['id']==='1' && $row['name']==='my name');
$row=$rows[1];
$this->assertTrue($row['id']==='2' && $row['name']==='my name 2');
$reader=$this->_connection->createCommand('SELECT * FROM foo WHERE id=3')->query();
$rows=$reader->readAll();
$this->assertEquals($rows,array());
}
public function testClose()
{
$reader=$this->_connection->createCommand('SELECT * FROM foo')->query();
$row=$reader->read();
$this->assertFalse($reader->IsClosed);
$reader->close();
$this->assertTrue($reader->IsClosed);
try
{
$reader->read();
$this->fail('Expected exception is not raised');
}
catch(Exception $e)
{
}
}
public function testRowCount()
{
// unable to test because SQLite doesn't support row count
}
public function testColumnCount()
{
$reader=$this->_connection->createCommand('SELECT * FROM foo')->query();
$this->assertEquals($reader->ColumnCount,2);
$reader=$this->_connection->createCommand('SELECT * FROM foo WHERE id=11')->query();
$this->assertEquals($reader->ColumnCount,2);
}
public function testForeach()
{
$ids=array();
$reader=$this->_connection->createCommand('SELECT * FROM foo')->query();
foreach($reader as $row)
$ids[]=$row['id'];
$this->assertEquals(count($ids),2);
$this->assertTrue($ids[0]==='1' && $ids[1]==='2');
try
{
foreach($reader as $row)
$ids[]=$row['id'];
$this->fail('Expected exception is not raised');
}
catch(TDbException $e)
{
}
}
public function testFetchMode()
{
$reader=$this->_connection->createCommand('SELECT * FROM foo')->query();
$reader->FetchMode=PDO::FETCH_NUM;
$row=$reader->read();
$this->assertFalse(isset($row['id']));
$this->assertTrue(isset($row[0]));
$reader->FetchMode=PDO::FETCH_ASSOC;
$row=$reader->read();
$this->assertTrue(isset($row['id']));
$this->assertFalse(isset($row[0]));
}
public function testBindColumn()
{
$reader=$this->_connection->createCommand('SELECT * FROM foo')->query();
$reader->bindColumn(1,$id);
$reader->bindColumn(2,$name);
$reader->read();
$this->assertEquals($id,'1');
$this->assertEquals($name,'my name');
$reader->read();
$this->assertEquals($id,'2');
$this->assertEquals($name,'my name 2');
$reader->read();
$this->assertEquals($id,'2');
$this->assertEquals($name,'my name 2');
}
}
|