Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 83 additions & 15 deletions Tests/QueryPostgresqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class QueryPostgresqlTest extends \PHPUnit_Framework_TestCase
*/
protected $dbo;

/**
* The instance of the object to test.
*
* @var PostgresqlQuery
* @since __DEPLOY_VERSION__
*/
private $instance;

/**
* Data for the testNullDate test.
*
Expand Down Expand Up @@ -106,6 +114,27 @@ public function mockQuoteName($text)
return '"' . $text . '"';
}

/**
* Callback for the dbo getQuery method.
*
* @param boolean $new True to get a new query, false to get the last query.
*
* @return PostgresqlQuery
*
* @since __DEPLOY_VERSION__
*/
public function mockGetQuery($new = false)
{
if ($new)
{
return new PostgresqlQuery($this->dbo);
}
else
{
return $this->$lastQuery;
}
}

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
Expand All @@ -124,6 +153,24 @@ protected function setUp()
$this,
array('escape' => array($this, 'mockEscape'))
);

$this->instance = new PostgresqlQuery($this->dbo);
}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*
* @return void
*
* @see PHPUnit_Framework_TestCase::tearDown()
* @since __DEPLOY_VERSION__
*/
protected function tearDown()
{
unset($this->dbo);
unset($this->instance);
parent::tearDown();
}

/**
Expand All @@ -142,7 +189,7 @@ public function test__toStringSelect()
->innerJoin('b ON b.id = a.id')
->where('b.id = 1')
->group('a.id')
->having('COUNT(a.id) > 3')
->having('COUNT(a.id) > 3')
->order('a.id');

$this->assertThat(
Expand All @@ -169,22 +216,43 @@ public function test__toStringSelect()
*/
public function test__toStringUpdate()
{
$q = new PostgresqlQuery($this->dbo);
// Test on ugly query
$this->instance
->update('#__foo AS a')
->join('INNER', "b\roN\nb.id = a.id")
->set('a.hits = 0');

$string = (string) $this->instance;

$this->assertEquals(
PHP_EOL . "UPDATE #__foo AS a" .
PHP_EOL . "SET a.hits = 0" .
PHP_EOL . "FROM b" .
PHP_EOL . "WHERE b.id = a.id",
$string
);

$q->update('#__foo AS a')
$this->instance
->clear()
->update('#__foo AS a')
->join('INNER', 'b ON b.id = a.id')
->set('a.id = 2')
->where('b.id = 1');

$this->assertThat(
(string) $q,
$this->equalTo(
PHP_EOL . "UPDATE #__foo AS a" .
PHP_EOL . "SET a.id = 2" .
PHP_EOL . "FROM b" .
PHP_EOL . "WHERE b.id = 1 AND b.id = a.id"
),
'Tests for correct rendering.'
$string = (string) $this->instance;

$this->assertEquals(
PHP_EOL . "UPDATE #__foo AS a" .
PHP_EOL . "SET a.id = 2" .
PHP_EOL . "FROM b" .
PHP_EOL . "WHERE b.id = 1 AND b.id = a.id",
$string
);

// Run method __toString() again on the same query
$this->assertEquals(
$string,
(string) $this->instance
);
}

Expand Down Expand Up @@ -1076,7 +1144,7 @@ public function testEscape()
*
* @since 1.0
*/
public function testForUpdate ()
public function testForUpdate()
{
$q = new PostgresqlQuery($this->dbo);

Expand Down Expand Up @@ -1117,7 +1185,7 @@ public function testForUpdate ()
*
* @since 1.0
*/
public function testForShare ()
public function testForShare()
{
$q = new PostgresqlQuery($this->dbo);

Expand Down Expand Up @@ -1158,7 +1226,7 @@ public function testForShare ()
*
* @since 1.0
*/
public function testNoWait ()
public function testNoWait()
{
$q = new PostgresqlQuery($this->dbo);

Expand Down
23 changes: 18 additions & 5 deletions src/Postgresql/PostgresqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,23 +213,36 @@ public function __toString()

if ($this->join)
{
$onWord = ' ON ';
$tmpFrom = $this->from;
$tmpWhere = $this->where ? clone $this->where : null;
$this->from = null;

// Workaround for special case of JOIN with UPDATE
foreach ($this->join as $join)
{
$joinElem = $join->getElements();

$joinArray = explode($onWord, $joinElem[0]);
$joinArray = preg_split('/\sON\s/i', $joinElem[0], 2);

$this->from($joinArray[0]);
$this->where($joinArray[1]);

if (isset($joinArray[1]))
{
$this->where($joinArray[1]);
}
}

$query .= (string) $this->from;
}

if ($this->where)
if ($this->where)
{
$query .= (string) $this->where;
}

$this->from = $tmpFrom;
$this->where = $tmpWhere;
}
elseif ($this->where)
{
$query .= (string) $this->where;
}
Expand Down