Skip to content

Commit

Permalink
Added toString to SQLHelper.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabordemooij committed Oct 30, 2013
1 parent 1fbd1c8 commit 41bb293
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
15 changes: 15 additions & 0 deletions RedBean/SQLHelper.php
Expand Up @@ -289,4 +289,19 @@ public function getNew()
{
return new self( $this->adapter );
}

/**
* When cast to string, simply print the query and its bindings.
*
* @return string
*/
public function __toString()
{
list( $query, $params ) = $this->getQuery();

return print_r( array(
'query' => $query,
'params' => $params
), true );
}
}
20 changes: 20 additions & 0 deletions testing/RedUNIT/Mysql/Mix.php
Expand Up @@ -116,6 +116,26 @@ public function testQueryBuilderMix()

asrt( ( $value == 2 ), TRUE );
}

/**
* Test the __toString method of the SQLHelper.
*/
public function testToString()
{
$toolbox = R::$toolbox;
$adapter = $toolbox->getDatabaseAdapter();
$sqlHelper = new RedBean_SQLHelper( $adapter );
$sqlHelper->begin()
->select( '*' )
->from( 'table' )
->where( 'name = ?' )
->put( 'name' );
$str = (string) $sqlHelper;
asrt( ( strpos( $str, 'query' ) !== false ), true );
asrt( ( strpos( $str, 'select * from table where name = ?' ) !== false ), true );
asrt( ( strpos( $str, '=> Array') !== false ), true );
asrt( ( strpos( $str, 'params') !== false ), true );
}
}


Expand Down

0 comments on commit 41bb293

Please sign in to comment.