-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added data type validation passed to bind method
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
use Amp\Mysql\Stmt; | ||
use PHPUnit\Framework\TestCase; | ||
use Amp\Mysql\ResultProxy; | ||
|
||
class StmtTest extends TestCase | ||
{ | ||
protected $processor; | ||
protected $resultProxy; | ||
|
||
public function setUp() | ||
{ | ||
$this->processor = $this->prophesize('Amp\Mysql\Processor'); | ||
$this->resultProxy = new ResultProxy(); | ||
} | ||
|
||
/** | ||
* @dataProvider provideTestBindDataTypes | ||
*/ | ||
public function testBindDataTypes($data, $expectedException) | ||
{ | ||
// arrange | ||
$query = 'SELECT * FROM test WHERE id = ?'; | ||
$stmtId = 1; | ||
$paramId = 0; | ||
$named = []; | ||
|
||
$this->processor->alive()->willReturn(true); | ||
$this->processor->delRef()->shouldBeCalled(); | ||
$this->processor->closeStmt(\Prophecy\Argument::any())->shouldBeCalled(); | ||
$this->resultProxy->columnsToFetch = 1; | ||
$stmt = new Stmt($this->processor->reveal(), $query, $stmtId, $named, $this->resultProxy); | ||
|
||
// assert | ||
if ($expectedException) { | ||
$this->expectException($expectedException); | ||
$this->processor->bindParam($stmtId, \Prophecy\Argument::any(), $data)->shouldNotBeCalled(); | ||
} else { | ||
$this->addToAssertionCount(1); | ||
$this->processor->bindParam($stmtId, \Prophecy\Argument::any(), $data)->shouldBeCalled(); | ||
} | ||
|
||
// act | ||
$stmt->bind($paramId, $data); | ||
} | ||
|
||
public function provideTestBindDataTypes() | ||
{ | ||
return [ | ||
'test scalar' => [ | ||
'data' => 1, | ||
'expectedException' => false, | ||
], | ||
'test object' => [ | ||
'data' => (object) [], | ||
'expectedException' => 'InvalidArgumentException', | ||
], | ||
'test array' => [ | ||
'data' => [], | ||
'expectedException' => 'InvalidArgumentException', | ||
], | ||
'test object with __toString defined' => [ | ||
'data' => new class { public function __toString() {return '';} }, | ||
'expectedException' => false, | ||
], | ||
]; | ||
} | ||
} |