Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

explicitly display mysqli error #269

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion src/Adapter/Driver/Mysqli/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ public function prepare($sql = null)
$this->resource = $this->mysqli->prepare($sql);
if (!$this->resource instanceof \mysqli_stmt) {
throw new Exception\InvalidQueryException(
'Statement couldn\'t be produced with sql: ' . $sql,
sprintf(
' Error #%d: %s. Statement couldn\'t be produced with sql: %s',
$this->mysqli->errno, $this->mysqli->error, $sql
),
null,
new Exception\ErrorException($this->mysqli->error, $this->mysqli->errno)
);
Expand Down
56 changes: 56 additions & 0 deletions test/Adapter/Driver/Mysqli/StatementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Db\Adapter\Driver\Mysqli;

use Zend\Db\Adapter\Driver\Mysqli\Statement;

use Zend\Db\Adapter\Driver\Mysqli\Connection;
use Zend\Db\Adapter\Driver\StatementInterface;
use Zend\Db\Adapter\Exception;
use Zend\Db\Adapter\ParameterContainer;
use Zend\Db\Adapter\Profiler;

class MysliTest extends \PHPUnit_Framework_TestCase
{

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->statement = new Statement();
}

public function testPrepare()
{
$this->statement->initialize(new TestAsset\MysqliMock());
$this->assertNull($this->statement->prepare('SELECT 1'));
}

/**
* intentionally throw exception to test error contains valid string
*/
// public function testMysqliStatementPrepareException(){
// $this->statement = new Statement(new Connection());
// // $this->statement->initialize(new mysqli("localhost", "test", "test", "test", 3306));

// // $this->_connection = mysqli_connect('localhost', 'root', '');
// // $this->_execute('USE playpen');
// $this->statement->setSql('SELECT * FROM test WHERE;');
// try{
// $this->statement->prepare();
// // } catch (InvalidQueryException $e){
// } catch (Exception $e){
// fwrite(STDOUT, $e->getMessage());
// }
// // fwrite(STDOUT, $this->statement->getSql());
// }
}
23 changes: 23 additions & 0 deletions test/Adapter/Driver/Mysqli/TestAsset/MysqliMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Db\Adapter\Driver\Mysqli\TestAsset;

class MysqliMock extends \mysqli
{
protected $mockStatement;

public function __construct()
{
// not sure what to do here
// all I really need is mysqli->prepare so line 206 in Statement.php throws the correct exception
// $this->resource = $this->mysqli->prepare($sql);
parent::__construct();
}
}