summaryrefslogtreecommitdiff
path: root/tests/unit/Data/SqlMap/BaseCase.php
blob: 15bb3367965cfd8cbaff0dc0c9423ab372d6473c (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php

require_once(dirname(__FILE__).'/common.php');
Prado::using('System.Data.SqlMap.TSqlMapManager');

/**
 * @package System.Data.SqlMap
 */
class BaseCase extends PHPUnit_Framework_TestCase
{
	protected $sqlmap;
	protected $connection;
	private $mapper;
	private $config;
	protected $ScriptDirectory;

	public function testCase1()
	{
		$this->assertTrue(true);
	}

	public function testCase2()
	{
		$this->assertTrue(true);
	}

	public function __construct()
	{
		parent::__construct();
		$this->config = BaseTestConfig::createConfigInstance();
		$this->ScriptDirectory = $this->config->getScriptDir();
	}

	public function hasSupportFor($feature)
	{
		return $this->config->hasFeature($feature);
	}

	public function __destruct()
	{
		if(!is_null($this->mapper))
			$this->mapper->cacheConfiguration();
	}

	function getConnection()
	{
		if(is_null($this->connection))
			$this->connection = $this->config->getConnection();
		$this->connection->setActive(true);
		return $this->connection;
	}

	/**
	 * Initialize an sqlMap
	 */
	protected function initSqlMap()
	{
		$manager = new TSqlMapManager($this->config->getConnection());
		$manager->configureXml($this->config->getSqlMapConfigFile());
		$this->sqlmap = $manager->getSqlMapGateway();
		$manager->TypeHandlers->registerTypeHandler(new TDateTimeHandler);
	}

	/**
	 * Run a sql batch for the datasource.
	 */
	protected function initScript($script)
	{
		$runner = $this->config->getScriptRunner();
		$runner->runScript($this->getConnection(), $this->ScriptDirectory.$script);
	}

	/**
	 * Create a new account with id = 6
	 */
	protected function NewAccount6()
	{
		$account = new Account();
		$account->setID(6);
		$account->setFirstName('Calamity');
		$account->setLastName('Jane');
		$account->setEmailAddress('no_email@provided.com');
		return $account;
	}

	/**
	 * Verify that the input account is equal to the account(id=1).
	 */
	protected function assertAccount1(Account $account)
	{
		$this->assertIdentical($account->getID(), 1);
		$this->assertIdentical($account->getFirstName(), 'Joe');
		$this->assertIdentical($account->getEmailAddress(), 'Joe.Dalton@somewhere.com');
	}

	/**
	 * Verify that the input account is equal to the account(id=6).
	 */
	protected function assertAccount6(Account $account)
	{
		$this->assertIdentical($account->getID(), 6);
		$this->assertIdentical($account->getFirstName(), 'Calamity');
		$this->assertIdentical($account->getLastName(), 'Jane');
		$this->assertNull($account->getEmailAddress());
	}

	/**
	 * Verify that the input order is equal to the order(id=1).
	 */
	protected function assertOrder1(Order $order)
	{
		$date = @mktime(8,15,0,2,15,2003);

		$this->assertIdentical((int)$order->getID(), 1);
		if($order->getDate() instanceof TDateTime)
			$this->assertIdentical($order->getDate()->getTimestamp(), $date);
		else
			$this->fail();
		$this->assertIdentical($order->getCardType(), 'VISA');
		$this->assertIdentical($order->getCardNumber(), '999999999999');
		$this->assertIdentical($order->getCardExpiry(), '05/03');
		$this->assertIdentical($order->getStreet(), '11 This Street');
		$this->assertIdentical($order->getProvince(), 'BC');
		$this->assertIdentical($order->getPostalCode(), 'C4B 4F4');
	}

	function assertAccount1AsHashArray($account)
	{
		$this->assertIdentical(1, (int)$account["Id"]);
		$this->assertIdentical("Joe", $account["FirstName"]);
		$this->assertIdentical("Dalton", $account["LastName"]);
		$this->assertIdentical("Joe.Dalton@somewhere.com", $account["EmailAddress"]);
	}

	function AssertOrder1AsHashArray($order)
	{
		$date = @mktime(8,15,0,2,15,2003);

		$this->assertIdentical(1, $order["Id"]);
		if($order['Date'] instanceof TDateTime)
			$this->assertIdentical($date, $order["Date"]->getTimestamp());
		else
			$this->fail();
		$this->assertIdentical("VISA", $order["CardType"]);
		$this->assertIdentical("999999999999", $order["CardNumber"]);
		$this->assertIdentical("05/03", $order["CardExpiry"]);
		$this->assertIdentical("11 This Street", $order["Street"]);
		$this->assertIdentical("Victoria", $order["City"]);
		$this->assertIdentical("BC", $order["Province"]);
		$this->assertIdentical("C4B 4F4", $order["PostalCode"]);
	}

}

class HundredsBool extends TSqlMapTypeHandler
{
	public function getResult($string)
	{
		$value = intval($string);
		if($value == 100)
			return true;
		if($value == 200)
			return false;
		//throw new Exception('unexpected value '.$value);
	}

	public function getParameter($parameter)
	{
		if($parameter)
			return 100;
		else
			return 200;
	}

	public function createNewInstance($data=null)
	{
		throw new TDataMapperException('can not create');
	}
}

class OuiNonBool extends TSqlMapTypeHandler
{
	const YES = "Oui";
	const NO = "Non";

	public function getResult($string)
	{
		if($string === self::YES)
			return true;
		if($string === self::NO)
			return false;
		//throw new Exception('unexpected value '.$string);
	}

	public function getParameter($parameter)
	{
		if($parameter)
			return self::YES;
		else
			return self::NO;
	}

	public function createNewInstance($data=null)
	{
		throw new TDataMapperException('can not create');
	}
}

class TDateTimeHandler extends TSqlMapTypeHandler
{
	public function getType()
	{
		return 'date';
	}

	public function getResult($string)
	{
		$time = new TDateTime($string);
		return $time;
	}

	public function getParameter($parameter)
	{
		if($parameter instanceof TDateTime)
			return $parameter->getTimestamp();
		else
			return $parameter;
	}

	public function createNewInstance($data=null)
	{
		return new TDateTime;
	}
}

class TDateTime
{
	private $_datetime;

	public function __construct($datetime=null)
	{
		if(!is_null($datetime))
			$this->setDatetime($datetime);
	}

	public function getTimestamp()
	{
		return strtotime($this->getDatetime());
	}

	public function getDateTime()
	{
		return $this->_datetime;
	}

	public function setDateTime($value)
	{
		$this->_datetime = $value;
	}
}