summaryrefslogtreecommitdiff
path: root/tests/simple_unit/SqlMap/ActiveRecordSqlMapTest.php
blob: 63d62534859bc723cffd0bbdddcef75307dd2a77 (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
<?php

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

Prado::using('System.Data.ActiveRecord.TActiveRecord');

class ActiveAccount extends TActiveRecord
{
	public $Account_Id;
	public $Account_FirstName;
	public $Account_LastName;
	public $Account_Email;

	public $Account_Banner_Option;
	public $Account_Cart_Option;

	const TABLE='Accounts';

	public static function finder($className=__CLASS__)
	{
		return parent::finder($className);
	}
}

class ActiveRecordSqlMapTest extends BaseCase
{
	function __construct()
	{
		parent::__construct();
		$this->initSqlMap();
		TActiveRecordManager::getInstance()->setDbConnection($this->getConnection());

		//$this->initScript('account-init.sql');
	}

	function testLoadWithSqlMap()
	{
		$records = $this->sqlmap->queryForList('GetActiveRecordAccounts');
		$registry=TActiveRecordManager::getInstance()->getObjectStateRegistry();
		foreach($records as $record)
		{
			$this->assertEqual(get_class($record), 'ActiveAccount');
			$this->assertTrue($registry->getIsCleanObject($record));
		}
	}

	function testLoadWithActiveRecord()
	{
		$records = ActiveAccount::finder()->findAll();
		$registry=TActiveRecordManager::getInstance()->getObjectStateRegistry();
		foreach($records as $record)
		{
			$this->assertEqual(get_class($record), 'ActiveAccount');
			$this->assertTrue($registry->getIsCleanObject($record));
		}
	}

	function testLoadWithSqlMap_SaveWithActiveRecord()
	{
		$record = $this->sqlmap->queryForObject('GetActiveRecordAccounts');
		$registry=TActiveRecordManager::getInstance()->getObjectStateRegistry();
		$record->Account_FirstName = "Testing 123";
		$this->assertTrue($registry->getIsDirtyObject($record));

		$this->assertTrue($record->save());

		$check1 = $this->sqlmap->queryForObject('GetActiveRecordAccounts');
		$check2 = ActiveAccount::finder()->findByAccount_FirstName($record->Account_FirstName);

		$this->assertSameAccount($record,$check1);
		$this->assertSameAccount($record,$check2);

		$this->initScript('account-init.sql');
	}

	function assertSameAccount($account1,$account2)
	{
		$props = array('Account_Id', 'Account_FirstName', 'Account_LastName',
						'Account_Email', 'Account_Banner_Option', 'Account_Cart_Option');
		foreach($props as $prop)
			$this->assertEqual($account1->{$prop}, $account2->{$prop});
	}
}

?>