DbQueryHelperTest.php 3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

use Q2A\Database\DbQueryHelper;

class DbQueryHelperTest extends PHPUnit_Framework_TestCase
{
	/** @var DbQueryHelper */
	private $helper;

	protected function setUp()
	{
		$this->helper = new DbQueryHelper();
	}

	public function test__expandParameters_success()
	{
Scott committed
17 18
		$result = $this->helper->expandParameters('SELECT * FROM table WHERE field=1', []);
		$expected = ['SELECT * FROM table WHERE field=1', []];
19 20
		$this->assertSame($expected, $result);

Scott committed
21 22 23 24 25 26
		$result = $this->helper->expandParameters('SELECT * FROM table WHERE field=?', [1]);
		$expected = ['SELECT * FROM table WHERE field=?', [1]];
		$this->assertSame($expected, $result);

		$result = $this->helper->expandParameters('SELECT * FROM table WHERE field=#', [1]);
		$expected = ['SELECT * FROM table WHERE field=?', [1]];
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
		$this->assertSame($expected, $result);

		$result = $this->helper->expandParameters('SELECT * FROM table WHERE field IN (?)', [[1]]);
		$expected = ['SELECT * FROM table WHERE field IN (?)', [1]];
		$this->assertSame($expected, $result);

		$result = $this->helper->expandParameters('SELECT * FROM table WHERE field IN (?)', [[1, 2]]);
		$expected = ['SELECT * FROM table WHERE field IN (?, ?)', [1, 2]];
		$this->assertSame($expected, $result);

		$result = $this->helper->expandParameters('INSERT INTO table(field) VALUES ?', [[ [1] ]]);
		$expected = ['INSERT INTO table(field) VALUES (?)', [1]];
		$this->assertSame($expected, $result);

		$result = $this->helper->expandParameters('INSERT INTO table(field) VALUES ?', [[ [1], [2] ]]);
		$expected = ['INSERT INTO table(field) VALUES (?), (?)', [1, 2]];
		$this->assertSame($expected, $result);

		$result = $this->helper->expandParameters('INSERT INTO table(field1, field2) VALUES ?', [[ [1, 2] ]]);
		$expected = ['INSERT INTO table(field1, field2) VALUES (?, ?)', [1, 2]];
		$this->assertSame($expected, $result);

		$result = $this->helper->expandParameters('INSERT INTO table(field1, field2) VALUES ?', [[ [1, 2], [3, 4] ]]);
		$expected = ['INSERT INTO table(field1, field2) VALUES (?, ?), (?, ?)', [1, 2, 3, 4]];
		$this->assertSame($expected, $result);
	}

	public function test__expandParameters_incorrect_groups_error()
	{
		$this->setExpectedException('Q2A\Database\Exceptions\SelectSpecException');
		$this->helper->expandParameters('INSERT INTO table(field1, field2) VALUES ?', [[ [1, 2], [3] ]]);
	}
Scott committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

	public function test__applyTableSub()
	{
		$result = $this->helper->applyTableSub('SELECT * FROM ^options');
		$this->assertSame('SELECT * FROM qa_options', $result);

		$result = $this->helper->applyTableSub('SELECT * FROM ^users WHERE userid=?');
		$this->assertSame('SELECT * FROM qa_users WHERE userid=?', $result);
	}

	public function test__applyTableSub_users_prefix()
	{
		define('QA_MYSQL_USERS_PREFIX', 'base_');

		$result = $this->helper->applyTableSub('SELECT * FROM ^options');
		$this->assertSame('SELECT * FROM qa_options', $result);

		$result = $this->helper->applyTableSub('SELECT * FROM ^users WHERE userid=?');
		$this->assertSame('SELECT * FROM base_users WHERE userid=?', $result);
	}
79
}