summaryrefslogtreecommitdiff
path: root/tests/unit/Data/TDbCommandTest.php
blob: ab94cb3f966bf815f5d3693f5af639a5d9ec40c4 (plain)
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
<?php

require_once(dirname(__FILE__).'/../phpunit2.php');

Prado::using('System.Data.*');

define('TEST_DB_FILE',dirname(__FILE__).'/db/test.db');

/**
 * @package System.Data.PDO
 */
class TDbCommandTest extends PHPUnit2_Framework_TestCase
{
	private $_connection;

	public function setUp()
	{
		@unlink(TEST_DB_FILE);
		$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 testGetText()
	{
		$sql='SELECT * FROM foo';
		$command=$this->_connection->createCommand($sql);
		$this->assertEquals($command->Text,$sql);
	}

	public function testSetText()
	{
		$sql='SELECT name FROM foo';
		$command=$this->_connection->createCommand($sql);
		$row=$command->query()->read();
		$this->assertEquals($row['name'],'my name');

		$newSql='SELECT id FROM foo';
		$command->Text=$newSql;
		$this->assertEquals($command->Text,$newSql);
		$row=$command->query()->read();

		$this->assertEquals($row['id'],'1');
	}

	public function testConnection()
	{
		$sql='SELECT name FROM foo';
		$command=$this->_connection->createCommand($sql);
		$this->assertTrue($command->Connection instanceof TDbConnection);
	}

	public function testPrepare()
	{
		$sql='SELECT name FROM foo';
		$command=$this->_connection->createCommand($sql);
		$this->assertTrue($command->PdoStatement===null);
		$command->prepare();
		$this->assertTrue($command->PdoStatement instanceof PDOStatement);

		try
		{
			$command->Text='Bad SQL';
			$command->prepare();
			$this->fail('Expected exception is not raised');
		}
		catch(TDbException $e)
		{
		}
	}

	public function testCancel()
	{
		$sql='SELECT name FROM foo';
		$command=$this->_connection->createCommand($sql);
		$command->prepare();
		$this->assertTrue($command->PdoStatement instanceof PDOStatement);
		$command->cancel();
		$this->assertEquals($command->PdoStatement,null);
	}
/*
	public function testActive()
	{
	    $this->assertFalse($this->_connection2->Active);

	    $this->_connection2->Active=true;
	    $this->assertTrue($this->_connection2->Active);
	    $pdo=$this->_connection2->PdoInstance;
	    // test setting Active repeatedly doesn't re-connect DB
	    $this->_connection2->Active=true;
	    $this->assertTrue($pdo===$this->_connection2->PdoInstance);

		$this->_connection2->Active=false;
	    $this->assertFalse($this->_connection2->Active);
	}

	public function testCreateCommand()
	{
		$sql='CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))';
		try
		{
			$this->_connection2->createCommand($sql);
			$this->fail('Expected exception is not raised');
		}
		catch(TDbException $e)
		{
		}

		$command=$this->_connection->createCommand($sql);
		$this->assertTrue($command instanceof TDbCommand);
	}

	public function testBeginTransaction()
	{
		$sql='INSERT INTO foo(id,name) VALUES (1,\'my name\')';
		$transaction=$this->_connection->beginTransaction();
		try
		{
			$this->_connection->createCommand($sql)->execute();
			$this->_connection->createCommand($sql)->execute();
			$this->fail('Expected exception not raised');
			$transaction->commit();
		}
		catch(Exception $e)
		{
			$transaction->rollBack();
			$reader=$this->_connection->createCommand('SELECT * FROM foo')->query();
			$this->assertFalse($reader->read());
		}
	}

	public function testLastInsertID()
	{
		$sql='INSERT INTO foo(name) VALUES (\'my name\')';
		$this->_connection->createCommand($sql)->execute();
		$value=$this->_connection->LastInsertID;
		$this->assertEquals($this->_connection->LastInsertID,'1');
	}

	public function testQuoteString()
	{
		$str="this is 'my' name";
		$expectedStr="'this is ''my'' name'";
		$this->assertEquals($expectedStr,$this->_connection->quoteString($str));
	}

	public function testColumnNameCase()
	{
		$this->assertEquals(TDbColumnCaseMode::Preserved,$this->_connection->ColumnCase);
		$this->_connection->ColumnCase=TDbColumnCaseMode::LowerCase;
		$this->assertEquals(TDbColumnCaseMode::LowerCase,$this->_connection->ColumnCase);
	}

	public function testNullConversion()
	{
		$this->assertEquals(TDbNullConversionMode::Preserved,$this->_connection->NullConversion);
		$this->_connection->NullConversion=TDbNullConversionMode::NullToEmptyString;
		$this->assertEquals(TDbNullConversionMode::NullToEmptyString,$this->_connection->NullConversion);
	}
	*/
}

?>