From 24efeb812c2850aedeb7b37c789ecfb55c815f66 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 4 Oct 2017 14:34:43 +0200 Subject: [PATCH] Added tests for Query Builder insert, update and delete (#14911) --- tests/framework/db/QueryBuilderTest.php | 133 ++++++++++++++++++++++++++++---- 1 file changed, 118 insertions(+), 15 deletions(-) diff --git a/tests/framework/db/QueryBuilderTest.php b/tests/framework/db/QueryBuilderTest.php index ca327b9..5a3d992 100644 --- a/tests/framework/db/QueryBuilderTest.php +++ b/tests/framework/db/QueryBuilderTest.php @@ -1765,11 +1765,55 @@ abstract class QueryBuilderTest extends DatabaseTestCase $this->assertEquals([':to' => 4], $params); } -// public function testInsert() -// { -// // TODO implement -// } -// + public function insertProvider() + { + return [ + 'regular-values' => [ + 'customer', + [ + 'email' => 'test@example.com', + 'name' => 'silverfire', + 'address' => 'Kyiv {{city}}, Ukraine', + 'is_active' => false, + 'related_id' => null, + ], + $this->replaceQuotes("INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]]) VALUES (:qp0, :qp1, :qp2, :qp3, :qp4)"), + [ + ':qp0' => 'test@example.com', + ':qp1' => 'silverfire', + ':qp2' => 'Kyiv {{city}}, Ukraine', + ':qp3' => false, + ':qp4' => null, + ], + ], + 'params-and-expressions' => [ + '{{%type}}', + [ + '{{%type}}.[[related_id]]' => null, + '[[time]]' => new Expression('now()'), + ], + 'INSERT INTO {{%type}} ({{%type}}.[[related_id]], [[time]]) VALUES (:qp0, now())', + [ + ':qp0' => null, + ] + ], + ]; + } + + /** + * @dataProvider insertProvider + * @param string $table + * @param array $columns + * @param string $expectedSQL + * @param array $expectedParams + */ + public function testInsert($table, $columns, $expectedSQL, $expectedParams) + { + $actualParams = []; + $actualSQL = $this->getQueryBuilder()->insert($table, $columns, $actualParams); + $this->assertSame($expectedSQL, $actualSQL); + $this->assertSame($expectedParams, $actualParams); + } public function batchInsertProvider() { @@ -1833,16 +1877,75 @@ abstract class QueryBuilderTest extends DatabaseTestCase $sql = $queryBuilder->batchInsert($table, $columns, $value); $this->assertEquals($expected, $sql); } -// -// public function testUpdate() -// { -// // TODO implement -// } -// -// public function testDelete() -// { -// // TODO implement -// } + + public function updateProvider() + { + return [ + [ + 'customer', + [ + 'status' => 1, + 'updated_at' => new Expression('now()'), + ], + [ + 'id' => 100, + ], + $this->replaceQuotes('UPDATE [[customer]] SET [[status]]=:qp0, [[updated_at]]=now() WHERE [[id]]=:qp1'), + [ + ':qp0' => 1, + ':qp1' => 100, + ], + ], + ]; + } + + /** + * @dataProvider updateProvider + * @param string $table + * @param array $columns + * @param array|string $condition + * @param string $expectedSQL + * @param array $expectedParams + */ + public function testUpdate($table, $columns, $condition, $expectedSQL, $expectedParams) + { + $actualParams = []; + $actualSQL = $this->getQueryBuilder()->update($table, $columns, $condition, $actualParams); + $this->assertSame($expectedSQL, $actualSQL); + $this->assertSame($expectedParams, $actualParams); + } + + public function deleteProvider() + { + return [ + [ + 'user', + [ + 'is_enabled' => false, + 'power' => new Expression('WRONG_POWER()') + ], + $this->replaceQuotes('DELETE FROM [[user]] WHERE ([[is_enabled]]=:qp0) AND ([[power]]=WRONG_POWER())'), + [ + ':qp0' => false, + ] + ], + ]; + } + + /** + * @dataProvider deleteProvider + * @param string $table + * @param array|string $condition + * @param string $expectedSQL + * @param array $expectedParams + */ + public function testDelete($table, $condition, $expectedSQL, $expectedParams) + { + $actualParams = []; + $actualSQL = $this->getQueryBuilder()->delete($table, $condition, $actualParams); + $this->assertSame($expectedSQL, $actualSQL); + $this->assertSame($expectedParams, $actualParams); + } public function testCommentColumn()