Skip to content
Permalink
Browse files

Updated unit tests to reflect fix to ValueBinder->placeholder

  • Loading branch information...
coryjthompson committed Jan 25, 2017
1 parent 2d852a6 commit dbd6e7a8d1f0327bbb983a227027ae9dbd1e7ddf
@@ -195,9 +195,6 @@ public function sql(ValueBinder $generator)
$this->_processExpressions();
}
$i = 0;
$columns = [];
$columns = $this->_columnNames();
$defaults = array_fill_keys($columns, null);
$placeholders = [];
@@ -220,7 +217,7 @@ public function sql(ValueBinder $generator)
continue;
}
$placeholder = $generator->placeholder($i);
$placeholder = $generator->placeholder('c');
$rowPlaceholders[] = $placeholder;
$generator->bind($placeholder, $value, $types[$column]);
}
@@ -37,19 +37,19 @@ public function testSqlOutput()
$expr2->eq('test2', 'false');
$caseExpression = new CaseExpression($expr, 'foobar');
$expected = 'CASE WHEN test = :c0 THEN :c1 END';
$expected = 'CASE WHEN test = :c0 THEN :param1 END';
$this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
$caseExpression->add($expr2);
$expected = 'CASE WHEN test = :c0 THEN :c1 WHEN test2 = :c2 THEN :c3 END';
$expected = 'CASE WHEN test = :c0 THEN :param1 WHEN test2 = :c2 THEN :param3 END';
$this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
$caseExpression = new CaseExpression([$expr], ['foobar', 'else']);
$expected = 'CASE WHEN test = :c0 THEN :c1 ELSE :c2 END';
$expected = 'CASE WHEN test = :c0 THEN :param1 ELSE :param2 END';
$this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
$caseExpression = new CaseExpression([$expr], ['foobar' => 'literal', 'else']);
$expected = 'CASE WHEN test = :c0 THEN foobar ELSE :c1 END';
$expected = 'CASE WHEN test = :c0 THEN foobar ELSE :param1 END';
$this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
}
@@ -63,12 +63,12 @@ public function testSqlOutputZero()
$expression = new QueryExpression();
$expression->add(['id' => 'test']);
$caseExpression = new CaseExpression([$expression], [0], ['integer']);
$expected = 'CASE WHEN id = :c0 THEN :c1 END';
$expected = 'CASE WHEN id = :c0 THEN :param1 END';
$binder = new ValueBinder();
$this->assertSame($expected, $caseExpression->sql($binder));
$expected = [
':c0' => ['value' => 'test', 'type' => null, 'placeholder' => 'c0'],
':c1' => ['value' => 0, 'type' => 'integer', 'placeholder' => 'c1'],
':param1' => ['value' => 0, 'type' => 'integer', 'placeholder' => 'param1'],
];
$this->assertEquals($expected, $binder->bindings());
}
@@ -44,15 +44,15 @@ public function testArityMultiplePlainValues()
{
$f = new FunctionExpression('MyFunction', ['foo', 'bar']);
$binder = new ValueBinder;
$this->assertEquals("MyFunction(:c0, :c1)", $f->sql($binder));
$this->assertEquals("MyFunction(:param0, :param1)", $f->sql($binder));
$this->assertEquals('foo', $binder->bindings()[':c0']['value']);
$this->assertEquals('bar', $binder->bindings()[':c1']['value']);
$this->assertEquals('foo', $binder->bindings()[':param0']['value']);
$this->assertEquals('bar', $binder->bindings()[':param1']['value']);
$binder = new ValueBinder;
$f = new FunctionExpression('MyFunction', ['bar']);
$this->assertEquals("MyFunction(:c0)", $f->sql($binder));
$this->assertEquals('bar', $binder->bindings()[':c0']['value']);
$this->assertEquals("MyFunction(:param0)", $f->sql($binder));
$this->assertEquals('bar', $binder->bindings()[':param0']['value']);
}
/**
@@ -64,7 +64,7 @@ public function testLiteralParams()
{
$binder = new ValueBinder;
$f = new FunctionExpression('MyFunction', ['foo' => 'literal', 'bar']);
$this->assertEquals("MyFunction(foo, :c0)", $f->sql($binder));
$this->assertEquals("MyFunction(foo, :param0)", $f->sql($binder));
}
/**
@@ -78,7 +78,7 @@ public function testFunctionNesting()
$binder = new ValueBinder;
$f = new FunctionExpression('MyFunction', ['foo', 'bar']);
$g = new FunctionExpression('Wrapper', ['bar' => 'literal', $f]);
$this->assertEquals("Wrapper(bar, (MyFunction(:c0, :c1)))", $g->sql($binder));
$this->assertEquals("Wrapper(bar, (MyFunction(:param0, :param1)))", $g->sql($binder));
}
/**
@@ -34,11 +34,11 @@ public function testsSimpleTuple()
{
$f = new TupleComparison(['field1', 'field2'], [1, 2], ['integer', 'integer'], '=');
$binder = new ValueBinder;
$this->assertEquals('(field1, field2) = (:c0, :c1)', $f->sql($binder));
$this->assertSame(1, $binder->bindings()[':c0']['value']);
$this->assertSame(2, $binder->bindings()[':c1']['value']);
$this->assertSame('integer', $binder->bindings()[':c0']['type']);
$this->assertSame('integer', $binder->bindings()[':c1']['type']);
$this->assertEquals('(field1, field2) = (:tuple0, :tuple1)', $f->sql($binder));
$this->assertSame(1, $binder->bindings()[':tuple0']['value']);
$this->assertSame(2, $binder->bindings()[':tuple1']['value']);
$this->assertSame('integer', $binder->bindings()[':tuple0']['type']);
$this->assertSame('integer', $binder->bindings()[':tuple1']['type']);
}
/**
@@ -51,10 +51,10 @@ public function testTupleWithExpressionFields()
$field1 = new QueryExpression(['a' => 1]);
$f = new TupleComparison([$field1, 'field2'], [4, 5], ['integer', 'integer'], '>');
$binder = new ValueBinder;
$this->assertEquals('(a = :c0, field2) > (:c1, :c2)', $f->sql($binder));
$this->assertEquals('(a = :c0, field2) > (:tuple1, :tuple2)', $f->sql($binder));
$this->assertSame(1, $binder->bindings()[':c0']['value']);
$this->assertSame(4, $binder->bindings()[':c1']['value']);
$this->assertSame(5, $binder->bindings()[':c2']['value']);
$this->assertSame(4, $binder->bindings()[':tuple1']['value']);
$this->assertSame(5, $binder->bindings()[':tuple2']['value']);
}
/**
@@ -67,9 +67,9 @@ public function testTupleWithExpressionValues()
$value1 = new QueryExpression(['a' => 1]);
$f = new TupleComparison(['field1', 'field2'], [$value1, 2], ['integer', 'integer'], '=');
$binder = new ValueBinder;
$this->assertEquals('(field1, field2) = (a = :c0, :c1)', $f->sql($binder));
$this->assertEquals('(field1, field2) = (a = :c0, :tuple1)', $f->sql($binder));
$this->assertSame(1, $binder->bindings()[':c0']['value']);
$this->assertSame(2, $binder->bindings()[':c1']['value']);
$this->assertSame(2, $binder->bindings()[':tuple1']['value']);
}
/**
@@ -86,11 +86,11 @@ public function testTupleWithInComparison()
'IN'
);
$binder = new ValueBinder;
$this->assertEquals('(field1, field2) IN ((:c0,:c1), (:c2,:c3))', $f->sql($binder));
$this->assertSame(1, $binder->bindings()[':c0']['value']);
$this->assertSame(2, $binder->bindings()[':c1']['value']);
$this->assertSame(3, $binder->bindings()[':c2']['value']);
$this->assertSame(4, $binder->bindings()[':c3']['value']);
$this->assertEquals('(field1, field2) IN ((:tuple0,:tuple1), (:tuple2,:tuple3))', $f->sql($binder));
$this->assertSame(1, $binder->bindings()[':tuple0']['value']);
$this->assertSame(2, $binder->bindings()[':tuple1']['value']);
$this->assertSame(3, $binder->bindings()[':tuple2']['value']);
$this->assertSame(4, $binder->bindings()[':tuple3']['value']);
}
/**
@@ -153,6 +153,6 @@ public function testFieldAsSingleExpression()
$value = [1, 1];
$f = new TupleComparison(new QueryExpression('a, b'), $value);
$binder = new ValueBinder;
$this->assertEquals('(a, b) = (:c0, :c1)', $f->sql($binder));
$this->assertEquals('(a, b) = (:tuple0, :tuple1)', $f->sql($binder));
}
}
@@ -63,8 +63,8 @@ public function testComparisonSimple()
$comparison = new Comparison('field', 'the thing', 'test', '=');
$binder = new ValueBinder;
$sql = $comparison->sql($binder);
$this->assertEquals('field = (CONCAT(:c0, :c1))', $sql);
$this->assertEquals('the thing', $binder->bindings()[':c0']['value']);
$this->assertEquals('field = (CONCAT(:param0, :param1))', $sql);
$this->assertEquals('the thing', $binder->bindings()[':param0']['value']);
$found = false;
$comparison->traverse(function ($exp) use (&$found) {
@@ -84,9 +84,9 @@ public function testComparisonMultiple()
$comparison = new Comparison('field', ['2', '3'], 'test[]', 'IN');
$binder = new ValueBinder;
$sql = $comparison->sql($binder);
$this->assertEquals('field IN (CONCAT(:c0, :c1),CONCAT(:c2, :c3))', $sql);
$this->assertEquals('2', $binder->bindings()[':c0']['value']);
$this->assertEquals('3', $binder->bindings()[':c2']['value']);
$this->assertEquals('field IN (CONCAT(:param0, :param1),CONCAT(:param2, :param3))', $sql);
$this->assertEquals('2', $binder->bindings()[':param0']['value']);
$this->assertEquals('3', $binder->bindings()[':param2']['value']);
$found = false;
$comparison->traverse(function ($exp) use (&$found) {
@@ -105,9 +105,9 @@ public function testBetween()
$between = new BetweenExpression('field', 'from', 'to', 'test');
$binder = new ValueBinder;
$sql = $between->sql($binder);
$this->assertEquals('field BETWEEN CONCAT(:c0, :c1) AND CONCAT(:c2, :c3)', $sql);
$this->assertEquals('from', $binder->bindings()[':c0']['value']);
$this->assertEquals('to', $binder->bindings()[':c2']['value']);
$this->assertEquals('field BETWEEN CONCAT(:param0, :param1) AND CONCAT(:param2, :param3)', $sql);
$this->assertEquals('from', $binder->bindings()[':param0']['value']);
$this->assertEquals('to', $binder->bindings()[':param2']['value']);
$expressions = [];
$between->traverse(function ($exp) use (&$expressions) {
@@ -133,11 +133,11 @@ public function testCaseExpression()
$binder = new ValueBinder;
$sql = $case->sql($binder);
$this->assertEquals('CASE WHEN foo = :c0 THEN CONCAT(:c1, :c2) ELSE CONCAT(:c3, :c4) END', $sql);
$this->assertEquals('CASE WHEN foo = :c0 THEN CONCAT(:param1, :param2) ELSE CONCAT(:param3, :param4) END', $sql);
$this->assertEquals('1', $binder->bindings()[':c0']['value']);
$this->assertEquals('value1', $binder->bindings()[':c1']['value']);
$this->assertEquals('value2', $binder->bindings()[':c3']['value']);
$this->assertEquals('value1', $binder->bindings()[':param1']['value']);
$this->assertEquals('value2', $binder->bindings()[':param3']['value']);
$expressions = [];
$case->traverse(function ($exp) use (&$expressions) {
@@ -158,8 +158,8 @@ public function testFunctionExpression()
$function = new FunctionExpression('DATE', ['2016-01'], ['test']);
$binder = new ValueBinder;
$sql = $function->sql($binder);
$this->assertEquals('DATE((CONCAT(:c0, :c1)))', $sql);
$this->assertEquals('2016-01', $binder->bindings()[':c0']['value']);
$this->assertEquals('DATE((CONCAT(:param0, :param1)))', $sql);
$this->assertEquals('2016-01', $binder->bindings()[':param0']['value']);
$expressions = [];
$function->traverse(function ($exp) use (&$expressions) {
@@ -184,11 +184,11 @@ public function testValuesExpression()
$binder = new ValueBinder;
$sql = $values->sql($binder);
$this->assertEquals(
' VALUES ((CONCAT(:c0, :c1))), ((CONCAT(:c2, :c3)))',
' VALUES ((CONCAT(:param0, :param1))), ((CONCAT(:param2, :param3)))',
$sql
);
$this->assertEquals('foo', $binder->bindings()[':c0']['value']);
$this->assertEquals('bar', $binder->bindings()[':c2']['value']);
$this->assertEquals('foo', $binder->bindings()[':param0']['value']);
$this->assertEquals('bar', $binder->bindings()[':param2']['value']);
$expressions = [];
$values->traverse(function ($exp) use (&$expressions) {
@@ -129,7 +129,7 @@ public function testConcat()
{
$function = $this->functions->concat(['title' => 'literal', ' is a string']);
$this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
$this->assertEquals("CONCAT(title, :c0)", $function->sql(new ValueBinder));
$this->assertEquals("CONCAT(title, :param0)", $function->sql(new ValueBinder));
$this->assertEquals('string', $function->returnType());
}
@@ -142,7 +142,7 @@ public function testCoalesce()
{
$function = $this->functions->coalesce(['NULL' => 'literal', '1', 'a'], ['a' => 'date']);
$this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
$this->assertEquals("COALESCE(NULL, :c0, :c1)", $function->sql(new ValueBinder));
$this->assertEquals("COALESCE(NULL, :param0, :param1)", $function->sql(new ValueBinder));
$this->assertEquals('date', $function->returnType());
}

0 comments on commit dbd6e7a

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