Skip to content
Permalink
Browse files

Wrote unit tests for the rest of ValueBinder class

  • Loading branch information...
coryjthompson committed Jan 26, 2017
1 parent dbd6e7a commit 572f8c79ae7891654c555f8271053085f2d02d80
Showing with 113 additions and 0 deletions.
  1. +113 −0 tests/TestCase/Database/ValueBinderTest.php
@@ -22,6 +22,40 @@
*/
class ValueBinderTest extends TestCase
{
/**
* test the bind method
*/
public function testBind()
{
$valueBinder = new ValueBinder();
$valueBinder->bind(':c0', 'value0');
$valueBinder->bind(':c1', 1, 'int');
$valueBinder->bind(':c2', 'value2');
$this->assertCount(3, $valueBinder->bindings());
$expected = [
':c0' => [
'value' => 'value0',
'type' => 'string',
'placeholder' => 'c0'
],
':c1' => [
'value' => 1,
'type' => 'int',
'placeholder' => 'c1'
],
':c2' => [
'value' => 'value2',
'type' => 'string',
'placeholder' => 'c2'
]
];
$bindings = $valueBinder->bindings();
$this->assertEquals($expected, $bindings);
}
/**
* test the placeholder method
*/
@@ -43,4 +77,83 @@ public function testPlaceholder()
$result = $valueBinder->placeholder('c');
$this->assertEquals(':c2', $result);
}
public function testGenerateManyNamed()
{
$valueBinder = new ValueBinder();
$values = [
'value0',
'value1'
];
$expected = [
':c0',
':c1'
];
$placeholders = $valueBinder->generateManyNamed($values);
$this->assertEquals($expected, $placeholders);
}
/**
* test the reset method
*/
public function testReset()
{
$valueBinder = new ValueBinder();
$valueBinder->bind(':c0', 'value0');
$valueBinder->bind(':c1', 'value1');
$this->assertCount(2, $valueBinder->bindings());
$valueBinder->reset();
$this->assertCount(0, $valueBinder->bindings());
$placeholder = $valueBinder->placeholder('c');
$this->assertEquals(':c0', $placeholder);
}
/**
* test the resetCount method
*/
public function testResetCount()
{
$valueBinder = new ValueBinder();
// Ensure the _bindings array IS NOT affected by resetCount
$valueBinder->bind(':c0', 'value0');
$valueBinder->bind(':c1', 'value1');
$this->assertCount(2, $valueBinder->bindings());
// Ensure the placeholder generation IS affected by resetCount
$valueBinder->placeholder('c');
$valueBinder->placeholder('c');
$result = $valueBinder->placeholder('c');
$this->assertEquals(':c2', $result);
$valueBinder->resetCount();
$placeholder = $valueBinder->placeholder('c');
$this->assertEquals(':c0', $placeholder);
$this->assertCount(2, $valueBinder->bindings());
}
/**
* tests the attachTo method
*/
public function testAttachTo()
{
$valueBinder = new ValueBinder();
$statementMock = $this->getMockBuilder('Cake\Database\Statement\StatementDecorator')
->disableOriginalConstructor()
->setMethods(['bindValue'])
->getMock();
$statementMock->expects($this->at(0))->method('bindValue')->with('c0', 'value0', 'string');
$statementMock->expects($this->at(1))->method('bindValue')->with('c1', 'value1', 'string');
$valueBinder->attachTo($statementMock); //empty array shouldn't call statement
$valueBinder->bind(':c0', 'value0', 'string');
$valueBinder->bind(':c1', 'value1', 'string');
$valueBinder->attachTo($statementMock);
}
}

0 comments on commit 572f8c7

Please sign in to comment.
You can’t perform that action at this time.