summaryrefslogtreecommitdiff
path: root/tests/unit/Data/SqlMap/DynamicParameterTest.php
blob: d7f20449584e06e16d29e854323bb4e062825c74 (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
<?php


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

/**
 * @package System.Data.SqlMap
 */
class DynamicParameterTest extends PHPUnit_Framework_TestCase
{

	protected function getMysqlSqlMapManager()
	{
		static $conn;
		static $sqlMapManager;

		if(Prado::getApplication() === null)
			Prado::setApplication(new TApplication(dirname(__FILE__).'/app'));

		if($conn === null)
			$conn = new TDbConnection('mysql:host=localhost;dbname=prado_unitest', 'prado_unitest', 'prado_unitest');

		$conn->setActive(true);

		if($sqlMapManager === null)
		{
			$sqlMapManager = new TSqlMapManager($conn);
			$sqlMapManager->configureXml( dirname(__FILE__) . '/DynamicParameterTestMap.xml');
		}

		return $sqlMapManager;
	}

	public function testMysqlSelectStaticSql()
	{
		$mapper = $this->getMysqlSqlMapManager();
		$gateway = $mapper->getSqlmapGateway();

		$value = $gateway->queryForObject('SelectStaticSql1');
		self::assertEquals('staticsql1', $value);

		$value = $gateway->queryForObject('SelectStaticSql2');
		self::assertEquals('staticsql2', $value);
	}

	public function testMysqlSelectDynamicTable()
	{
		$mapper = $this->getMysqlSqlMapManager();
		$gateway = $mapper->getSqlmapGateway();

		$value = $gateway->queryForObject('SelectDynamicTable', 'dynamicparametertest1');
		self::assertEquals('dynamictableparametertest1', $value);

		$value = $gateway->queryForObject('SelectDynamicTable', 'dynamicparametertest2');
		self::assertEquals('dynamictableparametertest2', $value);
	}

	public function testMysqlSelectDynamicComplex()
	{
		$mapper = $this->getMysqlSqlMapManager();
		$gateway = $mapper->getSqlmapGateway();

		$aParams = array(
			'tablename' => 'dynamicparametertest1',
			'testname'	=> 'dynamictable'
		);
		$value = $gateway->queryForObject('SelectDynamicComplex', $aParams);
		self::assertEquals('#dynamictableparametertest1$', $value);

		$aParams = array(
			'tablename' => 'dynamicparametertest2',
			'testname'	=> 'dynamictable'
		);
		$value = $gateway->queryForObject('SelectDynamicComplex', $aParams);
		self::assertEquals('#dynamictableparametertest2$', $value);
	}

	public function testMysqlSelectNoDynamic()
	{
		$mapper = $this->getMysqlSqlMapManager();
		$gateway = $mapper->getSqlmapGateway();

		$value = $gateway->queryForObject('SelectNoDynamic', 'dynamictable');
		self::assertEquals('dynamictableparametertest1', $value);

		$value = $gateway->queryForObject('SelectNoDynamic', 'staticsql');
		self::assertEquals('staticsql1', $value);
	}

	/**
	 * Issue#209 test
	 */
	public function testMysqlInlineEscapeParam()
	{
		$mapper = $this->getMysqlSqlMapManager();
		$gateway = $mapper->getSqlmapGateway();

		$value = $gateway->queryForObject('SelectInlineEscapeParam', "'1234567*123$456789$012345' AS foobar");
		self::assertEquals('1234567*123$456789$012345', $value);

		$value = $gateway->queryForObject('SelectInlineEscapeParam', '"1234567*123$456789$012345" AS foobar');
		self::assertEquals('1234567*123$456789$012345', $value);

	}

}