| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- <?php
- declare(strict_types=1);
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 5.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Database\Query;
- use Cake\Database\Driver\Sqlserver;
- use Cake\Database\Exception\DatabaseException;
- use Cake\Database\ExpressionInterface;
- use Cake\Database\Query\InsertQuery;
- use Cake\Database\Query\SelectQuery;
- use Cake\Datasource\ConnectionManager;
- use Cake\Test\TestCase\Database\QueryAssertsTrait;
- use Cake\TestSuite\TestCase;
- use InvalidArgumentException;
- /**
- * Tests InsertQuery class
- */
- class InsertQueryTest extends TestCase
- {
- use QueryAssertsTrait;
- protected array $fixtures = [
- 'core.Articles',
- 'core.Authors',
- ];
- /**
- * @var \Cake\Database\Connection
- */
- protected $connection;
- /**
- * @var bool
- */
- protected $autoQuote;
- public function setUp(): void
- {
- parent::setUp();
- $this->connection = ConnectionManager::get('test');
- $this->autoQuote = $this->connection->getDriver()->isAutoQuotingEnabled();
- }
- public function tearDown(): void
- {
- parent::tearDown();
- $this->connection->getDriver()->enableAutoQuoting($this->autoQuote);
- unset($this->connection);
- }
- /**
- * You cannot call values() before insert() it causes all sorts of pain.
- */
- public function testInsertValuesBeforeInsertFailure(): void
- {
- $this->expectException(DatabaseException::class);
- $query = new InsertQuery($this->connection);
- $query->values([
- 'id' => 1,
- 'title' => 'mark',
- 'body' => 'test insert',
- ]);
- }
- /**
- * Inserting nothing should not generate an error.
- */
- public function testInsertNothing(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('At least 1 column is required to perform an insert.');
- $query = new InsertQuery($this->connection);
- $query->insert([]);
- }
- /**
- * Test insert() with no into()
- */
- public function testInsertNoInto(): void
- {
- $this->expectException(DatabaseException::class);
- $this->expectExceptionMessage('Could not compile insert query. No table was specified');
- $query = new InsertQuery($this->connection);
- $query->insert(['title', 'body'])->sql();
- }
- /**
- * Test insert overwrites values
- */
- public function testInsertOverwritesValues(): void
- {
- $query = new InsertQuery($this->connection);
- $query->insert(['title', 'body'])
- ->insert(['title'])
- ->into('articles')
- ->values([
- 'title' => 'mark',
- ]);
- $result = $query->sql();
- $this->assertQuotedQuery(
- 'INSERT INTO <articles> \(<title>\) (OUTPUT INSERTED\.\* )?' .
- 'VALUES \(:c0\)',
- $result,
- !$this->autoQuote
- );
- }
- /**
- * Test inserting a single row.
- */
- public function testInsertSimple(): void
- {
- $query = new InsertQuery($this->connection);
- $query->insert(['title', 'body'])
- ->into('articles')
- ->values([
- 'title' => 'mark',
- 'body' => 'test insert',
- ]);
- $result = $query->sql();
- $this->assertQuotedQuery(
- 'INSERT INTO <articles> \(<title>, <body>\) (OUTPUT INSERTED\.\* )?' .
- 'VALUES \(:c0, :c1\)',
- $result,
- !$this->autoQuote
- );
- $result = $query->execute();
- $result->closeCursor();
- //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
- if (!$this->connection->getDriver() instanceof Sqlserver) {
- $this->assertSame(1, $result->rowCount(), '1 row should be inserted');
- }
- $expected = [
- [
- 'id' => 4,
- 'author_id' => null,
- 'title' => 'mark',
- 'body' => 'test insert',
- 'published' => 'N',
- ],
- ];
- $this->assertTable('articles', 1, $expected, ['id >=' => 4]);
- }
- /**
- * Test insert queries quote integer column names
- */
- public function testInsertQuoteColumns(): void
- {
- $query = new InsertQuery($this->connection);
- $query->insert([123])
- ->into('articles')
- ->values([
- '123' => 'mark',
- ]);
- $result = $query->sql();
- $this->assertQuotedQuery(
- 'INSERT INTO <articles> \(<123>\) (OUTPUT INSERTED\.\* )?' .
- 'VALUES \(:c0\)',
- $result,
- !$this->autoQuote
- );
- }
- /**
- * Test an insert when not all the listed fields are provided.
- * Columns should be matched up where possible.
- */
- public function testInsertSparseRow(): void
- {
- $query = new InsertQuery($this->connection);
- $query->insert(['title', 'body'])
- ->into('articles')
- ->values([
- 'title' => 'mark',
- ]);
- $result = $query->sql();
- $this->assertQuotedQuery(
- 'INSERT INTO <articles> \(<title>, <body>\) (OUTPUT INSERTED\.\* )?' .
- 'VALUES \(:c0, :c1\)',
- $result,
- !$this->autoQuote
- );
- $result = $query->execute();
- $result->closeCursor();
- //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
- if (!$this->connection->getDriver() instanceof Sqlserver) {
- $this->assertSame(1, $result->rowCount(), '1 row should be inserted');
- }
- $expected = [
- [
- 'id' => 4,
- 'author_id' => null,
- 'title' => 'mark',
- 'body' => null,
- 'published' => 'N',
- ],
- ];
- $this->assertTable('articles', 1, $expected, ['id >=' => 4]);
- }
- /**
- * Test inserting multiple rows with sparse data.
- */
- public function testInsertMultipleRowsSparse(): void
- {
- $query = new InsertQuery($this->connection);
- $query->insert(['title', 'body'])
- ->into('articles')
- ->values([
- 'body' => 'test insert',
- ])
- ->values([
- 'title' => 'jose',
- ]);
- $result = $query->execute();
- $result->closeCursor();
- //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
- if (!$this->connection->getDriver() instanceof Sqlserver) {
- $this->assertSame(2, $result->rowCount(), '2 rows should be inserted');
- }
- $expected = [
- [
- 'id' => 4,
- 'author_id' => null,
- 'title' => null,
- 'body' => 'test insert',
- 'published' => 'N',
- ],
- [
- 'id' => 5,
- 'author_id' => null,
- 'title' => 'jose',
- 'body' => null,
- 'published' => 'N',
- ],
- ];
- $this->assertTable('articles', 2, $expected, ['id >=' => 4]);
- }
- /**
- * Test that INSERT INTO ... SELECT works.
- */
- public function testInsertFromSelect(): void
- {
- $select = (new SelectQuery($this->connection))->select(['name', "'some text'", 99])
- ->from('authors')
- ->where(['id' => 1]);
- $query = new InsertQuery($this->connection);
- $query->insert(
- ['title', 'body', 'author_id'],
- ['title' => 'string', 'body' => 'string', 'author_id' => 'integer']
- )
- ->into('articles')
- ->values($select);
- $result = $query->sql();
- $this->assertQuotedQuery(
- 'INSERT INTO <articles> \(<title>, <body>, <author_id>\) (OUTPUT INSERTED\.\* )?SELECT',
- $result,
- !$this->autoQuote
- );
- $this->assertQuotedQuery(
- 'SELECT <name>, \'some text\', 99 FROM <authors>',
- $result,
- !$this->autoQuote
- );
- $result = $query->execute();
- $result->closeCursor();
- //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
- if (!$this->connection->getDriver() instanceof Sqlserver) {
- $this->assertSame(1, $result->rowCount());
- }
- $result = (new SelectQuery($this->connection))->select('*')
- ->from('articles')
- ->where(['author_id' => 99])
- ->execute();
- $rows = $result->fetchAll('assoc');
- $this->assertCount(1, $rows);
- $expected = [
- 'id' => 4,
- 'title' => 'mariano',
- 'body' => 'some text',
- 'author_id' => 99,
- 'published' => 'N',
- ];
- $this->assertEquals($expected, $rows[0]);
- }
- /**
- * Test that an exception is raised when mixing query + array types.
- */
- public function testInsertFailureMixingTypesArrayFirst(): void
- {
- $this->expectException(DatabaseException::class);
- $query = new InsertQuery($this->connection);
- $query->insert(['name'])
- ->into('articles')
- ->values(['name' => 'mark'])
- ->values(new InsertQuery($this->connection));
- }
- /**
- * Test that an exception is raised when mixing query + array types.
- */
- public function testInsertFailureMixingTypesQueryFirst(): void
- {
- $this->expectException(DatabaseException::class);
- $query = new InsertQuery($this->connection);
- $query->insert(['name'])
- ->into('articles')
- ->values(new InsertQuery($this->connection))
- ->values(['name' => 'mark']);
- }
- /**
- * Test that insert can use expression objects as values.
- */
- public function testInsertExpressionValues(): void
- {
- $query = new InsertQuery($this->connection);
- $query->insert(['title', 'author_id'])
- ->into('articles')
- ->values(['title' => $query->newExpr("SELECT 'jose'"), 'author_id' => 99]);
- $result = $query->execute();
- $result->closeCursor();
- //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
- if (!$this->connection->getDriver() instanceof Sqlserver) {
- $this->assertSame(1, $result->rowCount());
- }
- $result = (new SelectQuery($this->connection))->select('*')
- ->from('articles')
- ->where(['author_id' => 99])
- ->execute();
- $rows = $result->fetchAll('assoc');
- $this->assertCount(1, $rows);
- $expected = [
- 'id' => 4,
- 'title' => 'jose',
- 'body' => null,
- 'author_id' => '99',
- 'published' => 'N',
- ];
- $this->assertEquals($expected, $rows[0]);
- $subquery = new SelectQuery($this->connection);
- $subquery->select(['name'])
- ->from('authors')
- ->where(['id' => 1]);
- $query = new InsertQuery($this->connection);
- $query->insert(['title', 'author_id'])
- ->into('articles')
- ->values(['title' => $subquery, 'author_id' => 100]);
- $result = $query->execute();
- //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
- if (!$this->connection->getDriver() instanceof Sqlserver) {
- $this->assertSame(1, $result->rowCount());
- }
- $result->closeCursor();
- $result = (new SelectQuery($this->connection))->select('*')
- ->from('articles')
- ->where(['author_id' => 100])
- ->execute();
- $rows = $result->fetchAll('assoc');
- $this->assertCount(1, $rows);
- $expected = [
- 'id' => 5,
- 'title' => 'mariano',
- 'body' => null,
- 'author_id' => '100',
- 'published' => 'N',
- ];
- $this->assertEquals($expected, $rows[0]);
- }
- /**
- * Test use of modifiers in a INSERT query
- *
- * Testing the generated SQL since the modifiers are usually different per driver
- */
- public function testInsertModifiers(): void
- {
- $query = new InsertQuery($this->connection);
- $result = $query
- ->insert(['title'])
- ->into('articles')
- ->values(['title' => 'foo'])
- ->modifier('IGNORE');
- $this->assertQuotedQuery(
- 'INSERT IGNORE INTO <articles> \(<title>\) (OUTPUT INSERTED\.\* )?',
- $result->sql(),
- !$this->autoQuote
- );
- $query = new InsertQuery($this->connection);
- $result = $query
- ->insert(['title'])
- ->into('articles')
- ->values(['title' => 'foo'])
- ->modifier(['IGNORE', 'LOW_PRIORITY']);
- $this->assertQuotedQuery(
- 'INSERT IGNORE LOW_PRIORITY INTO <articles> \(<title>\) (OUTPUT INSERTED\.\* )?',
- $result->sql(),
- !$this->autoQuote
- );
- }
- public function testCloneValuesExpression(): void
- {
- $query = new InsertQuery($this->connection);
- $query
- ->insert(['column'])
- ->into('table')
- ->values(['column' => $query->newExpr('value')]);
- $clause = $query->clause('values');
- $clauseClone = (clone $query)->clause('values');
- $this->assertInstanceOf(ExpressionInterface::class, $clause);
- $this->assertEquals($clause, $clauseClone);
- $this->assertNotSame($clause, $clauseClone);
- }
- /**
- * Test that epilog() will actually append a string to an insert query
- */
- public function testAppendInsert(): void
- {
- $query = new InsertQuery($this->connection);
- $sql = $query
- ->insert(['id', 'title'])
- ->into('articles')
- ->values([1, 'a title'])
- ->epilog('RETURNING id')
- ->sql();
- $this->assertStringContainsString('INSERT', $sql);
- $this->assertStringContainsString('INTO', $sql);
- $this->assertStringContainsString('VALUES', $sql);
- $this->assertSame(' RETURNING id', substr($sql, -13));
- }
- /**
- * Tests that insert query parts get quoted automatically
- */
- public function testQuotingInsert(): void
- {
- $this->connection->getDriver()->enableAutoQuoting(true);
- $query = new InsertQuery($this->connection);
- $sql = $query->insert(['bar', 'baz'])
- ->into('foo')
- ->sql();
- $this->assertQuotedQuery('INSERT INTO <foo> \(<bar>, <baz>\)', $sql);
- $query = new InsertQuery($this->connection);
- $sql = $query->insert([$query->newExpr('bar')])
- ->into('foo')
- ->sql();
- $this->assertQuotedQuery('INSERT INTO <foo> \(\(bar\)\)', $sql);
- }
- }
|