aboutsummaryrefslogtreecommitdiffstats
path: root/tests/dbal/case_test.php
blob: 5f1c7d61fad7f9995834e4723251635e414b91a1 (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
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

class phpbb_dbal_case_test extends phpbb_database_test_case
{
	public function getDataSet()
	{
		return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml');
	}

	public function test_case_int()
	{
		$db = $this->new_dbal();

		$sql = 'SELECT ' . $db->sql_case('1 = 1', '1', '2') . ' AS test_num
			FROM phpbb_config';
		$result = $db->sql_query_limit($sql, 1);

		$this->assertEquals(1, (int) $db->sql_fetchfield('test_num'));

		$sql = 'SELECT ' . $db->sql_case('1 = 0', '1', '2') . ' AS test_num
			FROM phpbb_config';
		$result = $db->sql_query_limit($sql, 1);

		$this->assertEquals(2, (int) $db->sql_fetchfield('test_num'));
	}

	public function test_case_string()
	{
		$db = $this->new_dbal();

		$sql = 'SELECT ' . $db->sql_case('1 = 1', "'foo'", "'bar'") . ' AS test_string
			FROM phpbb_config';
		$result = $db->sql_query_limit($sql, 1);

		$this->assertEquals('foo', $db->sql_fetchfield('test_string'));

		$sql = 'SELECT ' . $db->sql_case('1 = 0', "'foo'", "'bar'") . ' AS test_string
			FROM phpbb_config';
		$result = $db->sql_query_limit($sql, 1);

		$this->assertEquals('bar', $db->sql_fetchfield('test_string'));
	}

	public function test_case_column()
	{
		$db = $this->new_dbal();

		$sql = 'SELECT ' . $db->sql_case("config_name = 'config1'", 'config_name', 'config_value') . " AS test_string
			FROM phpbb_config
			WHERE config_name = 'config1'";
		$result = $db->sql_query_limit($sql, 1);

		$this->assertEquals('config1', $db->sql_fetchfield('test_string'));

		$sql = 'SELECT ' . $db->sql_case("config_name = 'config1'", 'config_name', 'config_value') . " AS test_string
			FROM phpbb_config
			WHERE config_value = 'bar'";
		$result = $db->sql_query_limit($sql, 1);

		$this->assertEquals('bar', $db->sql_fetchfield('test_string'));
	}
}