From 46de16e00f9789cf6d93c0be73e8e5c69e18cdc8 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 27 Oct 2017 21:33:23 -0400 Subject: [PATCH] Add deprecation warnings to the Database package. Add deprecation warnings and update tests in the database package. --- src/Database/Connection.php | 13 + src/Database/Driver.php | 4 + .../Expression/FunctionExpression.php | 4 + src/Database/Expression/QueryExpression.php | 10 +- src/Database/Expression/ValuesExpression.php | 12 + src/Database/IdentifierQuoter.php | 4 +- src/Database/Log/LoggingStatement.php | 4 + src/Database/Query.php | 34 +- src/Database/Schema/CachedCollection.php | 4 + src/Database/Schema/TableSchema.php | 4 + src/Database/Type.php | 3 + src/Database/TypeMap.php | 8 + src/Database/TypeMapTrait.php | 8 + src/Database/TypedResultTrait.php | 4 + src/TestSuite/Fixture/FixtureManager.php | 4 +- tests/TestCase/Database/ConnectionTest.php | 71 ++-- tests/TestCase/Database/DriverTest.php | 28 +- .../Expression/QueryExpressionTest.php | 30 +- .../ExpressionTypeCastingIntegrationTest.php | 6 +- .../Database/FunctionsBuilderTest.php | 36 +- .../Database/Log/LoggingStatementTest.php | 27 +- tests/TestCase/Database/QueryTest.php | 321 +++++++++--------- .../Database/Schema/CollectionTest.php | 4 +- .../Database/Schema/MysqlSchemaTest.php | 2 +- .../Database/Schema/PostgresSchemaTest.php | 2 +- .../Database/Schema/SqliteSchemaTest.php | 2 +- tests/TestCase/Database/Schema/TableTest.php | 28 +- tests/TestCase/Database/SchemaCacheTest.php | 4 +- 28 files changed, 441 insertions(+), 240 deletions(-) diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 94ea84f6656..5f5b530a17e 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -206,6 +206,7 @@ public function getDriver() */ public function driver($driver = null, $config = []) { + deprecationWarning('Connection::driver() is deprecated. Use Connection::setDriver()/getDriver() instead.'); if ($driver !== null) { $this->setDriver($driver, $config); } @@ -380,6 +381,10 @@ public function getSchemaCollection() */ public function schemaCollection(SchemaCollection $collection = null) { + deprecationWarning( + 'Connection::schemaCollection() is deprecated. ' . + 'Use Connection::setSchemaCollection()/getSchemaCollection() instead.' + ); if ($collection !== null) { $this->setSchemaCollection($collection); } @@ -591,6 +596,10 @@ public function isSavePointsEnabled() */ public function useSavePoints($enable = null) { + deprecationWarning( + 'Connection::useSavePoints() is deprecated. ' . + 'Use Connection::enableSavePoints()/isSavePointsEnabled() instead.' + ); if ($enable !== null) { $this->enableSavePoints($enable); } @@ -816,6 +825,10 @@ public function logQueries($enable = null) */ public function logger($instance = null) { + deprecationWarning( + 'Connection::logger() is deprecated. ' . + 'Use Connection::setLogger()/getLogger() instead.' + ); if ($instance === null) { return $this->getLogger(); } diff --git a/src/Database/Driver.php b/src/Database/Driver.php index cb9b72459e3..1db63eb7421 100644 --- a/src/Database/Driver.php +++ b/src/Database/Driver.php @@ -336,6 +336,10 @@ public function isAutoQuotingEnabled() */ public function autoQuoting($enable = null) { + deprecationWarning( + 'Driver::autoQuoting() is deprecated. ' . + 'Use Driver::enableAutoQuoting()/isAutoQuotingEnabled() instead.' + ); if ($enable !== null) { $this->enableAutoQuoting($enable); } diff --git a/src/Database/Expression/FunctionExpression.php b/src/Database/Expression/FunctionExpression.php index e18fb97f2cf..550df63a46a 100644 --- a/src/Database/Expression/FunctionExpression.php +++ b/src/Database/Expression/FunctionExpression.php @@ -104,6 +104,10 @@ public function getName() */ public function name($name = null) { + deprecationWarning( + 'FunctionExpression::name() is deprecated. ' . + 'Use FunctionExpression::setName()/getName() instead.' + ); if ($name !== null) { return $this->setName($name); } diff --git a/src/Database/Expression/QueryExpression.php b/src/Database/Expression/QueryExpression.php index cf8606edfc2..0a4f529e47d 100644 --- a/src/Database/Expression/QueryExpression.php +++ b/src/Database/Expression/QueryExpression.php @@ -105,6 +105,10 @@ public function getConjunction() */ public function tieWith($conjunction = null) { + deprecationWarning( + 'QueryExpression::tieWith() is deprecated. ' . + 'Use QueryExpression::setConjunction()/getConjunction() instead.' + ); if ($conjunction !== null) { return $this->setConjunction($conjunction); } @@ -118,10 +122,14 @@ public function tieWith($conjunction = null) * @param string|null $conjunction value to be used for joining conditions. If null it * will not set any value, but return the currently stored one * @return string|$this - * @deprecated 3.2.0 Use tieWith() instead + * @deprecated 3.2.0 Use setConjunction()/getConjunction() instead */ public function type($conjunction = null) { + deprecationWarning( + 'QueryExpression::type() is deprecated. ' . + 'Use QueryExpression::setConjunction()/getConjunction() instead.' + ); return $this->tieWith($conjunction); } diff --git a/src/Database/Expression/ValuesExpression.php b/src/Database/Expression/ValuesExpression.php index b505f73fdeb..c7e1c39b807 100644 --- a/src/Database/Expression/ValuesExpression.php +++ b/src/Database/Expression/ValuesExpression.php @@ -134,6 +134,10 @@ public function getColumns() */ public function columns($cols = null) { + deprecationWarning( + 'ValuesExpression::columns() is deprecated. ' . + 'Use ValuesExpression::setColumns()/getColumns() instead.' + ); if ($cols !== null) { return $this->setColumns($cols); } @@ -200,6 +204,10 @@ public function getValues() */ public function values($values = null) { + deprecationWarning( + 'ValuesExpression::values() is deprecated. ' . + 'Use ValuesExpression::setValues()/getValues() instead.' + ); if ($values !== null) { return $this->setValues($values); } @@ -243,6 +251,10 @@ public function getQuery() */ public function query(Query $query = null) { + deprecationWarning( + 'ValuesExpression::query() is deprecated. ' . + 'Use ValuesExpression::setQuery()/getQuery() instead.' + ); if ($query !== null) { return $this->setQuery($query); } diff --git a/src/Database/IdentifierQuoter.php b/src/Database/IdentifierQuoter.php index 348e646df07..f5866e85c64 100644 --- a/src/Database/IdentifierQuoter.php +++ b/src/Database/IdentifierQuoter.php @@ -53,7 +53,7 @@ public function __construct(Driver $driver) public function quote(Query $query) { $binder = $query->getValueBinder(); - $query->valueBinder(false); + $query->setValueBinder(false); if ($query->type() === 'insert') { $this->_quoteInsert($query); @@ -64,7 +64,7 @@ public function quote(Query $query) } $query->traverseExpressions([$this, 'quoteExpression']); - $query->valueBinder($binder); + $query->setValueBinder($binder); return $query; } diff --git a/src/Database/Log/LoggingStatement.php b/src/Database/Log/LoggingStatement.php index 372c0d170ce..44d3b413c08 100644 --- a/src/Database/Log/LoggingStatement.php +++ b/src/Database/Log/LoggingStatement.php @@ -115,6 +115,10 @@ public function bindValue($column, $value, $type = 'string') */ public function logger($instance = null) { + deprecationWarning( + 'LoggingStatement::logger() is deprecated. ' . + 'Use LoggingStatement::setLogger()/getLogger() instead.' + ); if ($instance === null) { return $this->getLogger(); } diff --git a/src/Database/Query.php b/src/Database/Query.php index 4bef3aed6b1..b1c6f701667 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -182,6 +182,10 @@ public function getConnection() */ public function connection($connection = null) { + deprecationWarning( + 'Query::connection() is deprecated. ' . + 'Use Query::setConnection()/getConnection() instead.' + ); if ($connection !== null) { return $this->setConnection($connection); } @@ -1003,6 +1007,7 @@ public function andWhere($conditions, $types = []) */ public function orWhere($conditions, $types = []) { + deprecationWarning('Query::orWhere() is deprecated. Use Query::where() instead.'); $this->_conjugate('where', $conditions, 'OR', $types); return $this; @@ -1242,6 +1247,7 @@ public function andHaving($conditions, $types = []) */ public function orHaving($conditions, $types = []) { + deprecationWarning('Query::orHaving() is deprecated. Use Query::having() instead.'); $this->_conjugate('having', $conditions, 'OR', $types); return $this; @@ -1865,6 +1871,23 @@ public function getValueBinder() return $this->_valueBinder; } + /** + * Overwrite the current value binder + * + * A ValueBinder is responsible for generating query placeholders and temporarily + * associate values to those placeholders so that they can be passed correctly + * to the statement object. + * + * @param \Cake\Database\ValueBinder|bool $binder The binder or false to disable binding. + * @return $this + */ + public function setValueBinder($binder) + { + $this->_valueBinder = $binder; + + return $this; + } + /** * Returns the currently used ValueBinder instance. If a value is passed, * it will be set as the new instance to be used. @@ -1873,13 +1896,14 @@ public function getValueBinder() * associate values to those placeholders so that they can be passed correctly * to the statement object. * - * @deprecated 3.5.0 Use getValueBinder() for the getter part instead. + * @deprecated 3.5.0 Use setValueBinder()/getValueBinder() instead. * @param \Cake\Database\ValueBinder|null $binder new instance to be set. If no value is passed the * default one will be returned * @return $this|\Cake\Database\ValueBinder */ public function valueBinder($binder = null) { + deprecationWarning('Query::valueBinder() is deprecated. Use Query::getValueBinder()/setValueBinder() instead.'); if ($binder === null) { if ($this->_valueBinder === null) { $this->_valueBinder = new ValueBinder(); @@ -1949,6 +1973,10 @@ public function isBufferedResultsEnabled() */ public function bufferResults($enable = null) { + deprecationWarning( + 'Query::bufferResults() is deprecated. ' . + 'Use Query::enableBufferedResults()/isBufferedResultsEnabled() instead.' + ); if ($enable !== null) { return $this->enableBufferedResults($enable); } @@ -1996,6 +2024,10 @@ public function getSelectTypeMap() */ public function selectTypeMap(TypeMap $typeMap = null) { + deprecationWarning( + 'Query::selectTypeMap() is deprecated. ' . + 'Use Query::setSelectTypeMap()/getSelectTypeMap() instead.' + ); if ($typeMap !== null) { return $this->setSelectTypeMap($typeMap); } diff --git a/src/Database/Schema/CachedCollection.php b/src/Database/Schema/CachedCollection.php index 0c999c6d59f..8ec484f0293 100644 --- a/src/Database/Schema/CachedCollection.php +++ b/src/Database/Schema/CachedCollection.php @@ -119,6 +119,10 @@ public function getCacheMetadata() */ public function cacheMetadata($enable = null) { + deprecationWarning( + 'CachedCollection::cacheMetadata() is deprecated. ' . + 'Use CachedCollection::setCacheMetadata()/getCacheMetadata() instead.' + ); if ($enable !== null) { $this->setCacheMetadata($enable); } diff --git a/src/Database/Schema/TableSchema.php b/src/Database/Schema/TableSchema.php index 906682b5893..b7063a89b30 100644 --- a/src/Database/Schema/TableSchema.php +++ b/src/Database/Schema/TableSchema.php @@ -778,6 +778,10 @@ public function isTemporary() */ public function temporary($temporary = null) { + deprecationWarning( + 'TableSchema::temporary() is deprecated. ' . + 'Use TableSchema::setTemporary()/getTemporary() instead.' + ); if ($temporary !== null) { return $this->setTemporary($temporary); } diff --git a/src/Database/Type.php b/src/Database/Type.php index 92d74835225..8ee722b98d2 100644 --- a/src/Database/Type.php +++ b/src/Database/Type.php @@ -223,6 +223,7 @@ public function toPHP($value, Driver $driver) */ protected function _basicTypeCast($value) { + deprecationWarning('Type::_basicTypeCast() is deprecated.'); if ($value === null) { return null; } @@ -259,6 +260,7 @@ public function toStatement($value, Driver $driver) */ public static function boolval($value) { + deprecationWarning('Type::boolval() is deprecated.'); if (is_string($value) && !is_numeric($value)) { return strtolower($value) === 'true'; } @@ -277,6 +279,7 @@ public static function boolval($value) */ public static function strval($value) { + deprecationWarning('Type::strval() is deprecated.'); if (is_array($value)) { $value = ''; } diff --git a/src/Database/TypeMap.php b/src/Database/TypeMap.php index e57886c97d1..e3005d611ea 100644 --- a/src/Database/TypeMap.php +++ b/src/Database/TypeMap.php @@ -108,6 +108,10 @@ public function getDefaults() */ public function defaults(array $defaults = null) { + deprecationWarning( + 'TypeMap::defaults() is deprecated. ' . + 'Use TypeMap::setDefaults()/getDefaults() instead.' + ); if ($defaults !== null) { return $this->setDefaults($defaults); } @@ -180,6 +184,10 @@ public function getTypes() */ public function types(array $types = null) { + deprecationWarning( + 'TypeMap::types() is deprecated. ' . + 'Use TypeMap::setTypes()/getTypes() instead.' + ); if ($types !== null) { return $this->setTypes($types); } diff --git a/src/Database/TypeMapTrait.php b/src/Database/TypeMapTrait.php index 990308197a4..573259dd2b0 100644 --- a/src/Database/TypeMapTrait.php +++ b/src/Database/TypeMapTrait.php @@ -62,6 +62,10 @@ public function getTypeMap() */ public function typeMap($typeMap = null) { + deprecationWarning( + 'TypeMapTrait::typeMap() is deprecated. ' . + 'Use TypeMapTrait::setTypeMap()/getTypeMap() instead.' + ); if ($typeMap !== null) { return $this->setTypeMap($typeMap); } @@ -101,6 +105,10 @@ public function getDefaultTypes() */ public function defaultTypes(array $types = null) { + deprecationWarning( + 'TypeMapTrait::defaultTypes() is deprecated. ' . + 'Use TypeMapTrait::setDefaultTypes()/getDefaultTypes() instead.' + ); if ($types !== null) { return $this->setDefaultTypes($types); } diff --git a/src/Database/TypedResultTrait.php b/src/Database/TypedResultTrait.php index aaf7a31cdfa..33779806294 100644 --- a/src/Database/TypedResultTrait.php +++ b/src/Database/TypedResultTrait.php @@ -60,6 +60,10 @@ public function setReturnType($type) */ public function returnType($type = null) { + deprecationWarning( + 'TypedResultTrait::returnType() is deprecated. ' . + 'Use TypedResultTrait::setReturnType()/getReturnType() instead.' + ); if ($type !== null) { $this->_returnType = $type; diff --git a/src/TestSuite/Fixture/FixtureManager.php b/src/TestSuite/Fixture/FixtureManager.php index 88a786ad8e8..be8495f83c7 100644 --- a/src/TestSuite/Fixture/FixtureManager.php +++ b/src/TestSuite/Fixture/FixtureManager.php @@ -286,7 +286,7 @@ public function load($test) try { $createTables = function ($db, $fixtures) use ($test) { - $tables = $db->schemaCollection()->listTables(); + $tables = $db->getSchemaCollection()->listTables(); $configName = $db->configName(); if (!isset($this->_insertionMap[$configName])) { $this->_insertionMap[$configName] = []; @@ -455,7 +455,7 @@ public function loadSingle($name, $db = null, $dropTables = true) } if (!$this->isFixtureSetup($db->configName(), $fixture)) { - $sources = $db->schemaCollection()->listTables(); + $sources = $db->getSchemaCollection()->listTables(); $this->_setupTable($fixture, $db, $sources, $dropTables); } diff --git a/tests/TestCase/Database/ConnectionTest.php b/tests/TestCase/Database/ConnectionTest.php index 43ac47811fe..2da670c3b72 100644 --- a/tests/TestCase/Database/ConnectionTest.php +++ b/tests/TestCase/Database/ConnectionTest.php @@ -57,7 +57,7 @@ public function setUp() public function tearDown() { Log::reset(); - $this->connection->useSavePoints(false); + $this->connection->enableSavePoints(false); unset($this->connection); parent::tearDown(); } @@ -602,7 +602,7 @@ public function testVirtualNestedTransaction3() */ public function testSavePoints() { - $this->skipIf(!$this->connection->useSavePoints(true)); + $this->skipIf(!$this->connection->enableSavePoints(true)); $this->connection->begin(); $this->connection->delete('things', ['id' => 1]); @@ -632,7 +632,7 @@ public function testSavePoints() public function testSavePoints2() { - $this->skipIf(!$this->connection->useSavePoints(true)); + $this->skipIf(!$this->connection->enableSavePoints(true)); $this->connection->begin(); $this->connection->delete('things', ['id' => 1]); @@ -688,10 +688,10 @@ public function testInTransaction() public function testInTransactionWithSavePoints() { $this->skipIf( - $this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver, + $this->connection->getDriver() instanceof \Cake\Database\Driver\Sqlserver, 'SQLServer fails when this test is included.' ); - $this->skipIf(!$this->connection->useSavePoints(true)); + $this->skipIf(!$this->connection->enableSavePoints(true)); $this->connection->begin(); $this->assertTrue($this->connection->inTransaction()); @@ -816,23 +816,26 @@ public function testQuoteIdentifier() * * @return void */ - public function testLoggerDefault() + public function testGetLoggerDefault() { - $logger = $this->connection->logger(); + $logger = $this->connection->getLogger(); $this->assertInstanceOf('Cake\Database\Log\QueryLogger', $logger); - $this->assertSame($logger, $this->connection->logger()); + $this->assertSame($logger, $this->connection->getLogger()); } /** * Tests that a custom logger object can be set * + * @group deprecated * @return void */ public function testSetLogger() { - $logger = new QueryLogger; - $this->connection->logger($logger); - $this->assertSame($logger, $this->connection->logger()); + $this->deprecated(function () { + $logger = new QueryLogger; + $this->connection->logger($logger); + $this->assertSame($logger, $this->connection->logger()); + }); } /** @@ -856,10 +859,10 @@ public function testLoggerDecorator() { $logger = new QueryLogger; $this->connection->logQueries(true); - $this->connection->logger($logger); + $this->connection->setLogger($logger); $st = $this->connection->prepare('SELECT 1'); $this->assertInstanceOf(LoggingStatement::class, $st); - $this->assertSame($logger, $st->logger()); + $this->assertSame($logger, $st->getLogger()); $this->connection->logQueries(false); $st = $this->connection->prepare('SELECT 1'); @@ -888,7 +891,7 @@ public function testLogQueries() public function testLogFunction() { $logger = $this->getMockBuilder(QueryLogger::class)->getMock(); - $this->connection->logger($logger); + $this->connection->setLogger($logger); $logger->expects($this->once())->method('log') ->with($this->logicalAnd( $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'), @@ -912,10 +915,10 @@ public function testLogBeginRollbackTransaction() $connection->logQueries(true); $driver = $this->getMockFormDriver(); - $connection->driver($driver); + $connection->setDriver($driver); $logger = $this->getMockBuilder(QueryLogger::class)->getMock(); - $connection->logger($logger); + $connection->setLogger($logger); $logger->expects($this->at(0))->method('log') ->with($this->logicalAnd( $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'), @@ -946,7 +949,7 @@ public function testLogCommitTransaction() ->getMock(); $logger = $this->getMockBuilder(QueryLogger::class)->getMock(); - $connection->logger($logger); + $connection->setLogger($logger); $logger->expects($this->at(1))->method('log') ->with($this->logicalAnd( @@ -1034,7 +1037,7 @@ public function testTransactionalWithException() * * @return void */ - public function testSchemaCollection() + public function testSetSchemaCollection() { $driver = $this->getMockFormDriver(); $connection = $this->getMockBuilder(Connection::class) @@ -1042,14 +1045,40 @@ public function testSchemaCollection() ->setConstructorArgs([['driver' => $driver]]) ->getMock(); - $schema = $connection->schemaCollection(); + $schema = $connection->getSchemaCollection(); $this->assertInstanceOf('Cake\Database\Schema\Collection', $schema); $schema = $this->getMockBuilder('Cake\Database\Schema\Collection') ->setConstructorArgs([$connection]) ->getMock(); - $connection->schemaCollection($schema); - $this->assertSame($schema, $connection->schemaCollection()); + $connection->setSchemaCollection($schema); + $this->assertSame($schema, $connection->getSchemaCollection()); + } + + /** + * Tests it is possible to set a schema collection object + * + * @group deprecated + * @return void + */ + public function testSchemaCollection() + { + $this->deprecated(function () { + $driver = $this->getMockFormDriver(); + $connection = $this->getMockBuilder(Connection::class) + ->setMethods(['connect']) + ->setConstructorArgs([['driver' => $driver]]) + ->getMock(); + + $schema = $connection->schemaCollection(); + $this->assertInstanceOf('Cake\Database\Schema\Collection', $schema); + + $schema = $this->getMockBuilder('Cake\Database\Schema\Collection') + ->setConstructorArgs([$connection]) + ->getMock(); + $connection->schemaCollection($schema); + $this->assertSame($schema, $connection->schemaCollection()); + }); } /** diff --git a/tests/TestCase/Database/DriverTest.php b/tests/TestCase/Database/DriverTest.php index 9f08d75c710..0c266336f1b 100644 --- a/tests/TestCase/Database/DriverTest.php +++ b/tests/TestCase/Database/DriverTest.php @@ -61,12 +61,12 @@ public function testConstructor() $arg = ['quoteIdentifiers' => true]; $driver = $this->getMockForAbstractClass(Driver::class, [$arg]); - $this->assertTrue($driver->autoQuoting()); + $this->assertTrue($driver->isAutoQuotingEnabled()); $arg = ['username' => 'GummyBear']; $driver = $this->getMockForAbstractClass(Driver::class, [$arg]); - $this->assertFalse($driver->autoQuoting()); + $this->assertFalse($driver->isAutoQuotingEnabled()); } /** @@ -169,19 +169,25 @@ public function testIsConnected() */ public function testAutoQuoting() { - $this->assertFalse($this->driver->autoQuoting()); + $this->assertFalse($this->driver->isAutoQuotingEnabled()); - $this->driver->autoQuoting(true); - $this->assertTrue($this->driver->autoQuoting()); + $this->assertSame($this->driver, $this->driver->enableAutoQuoting(true)); + $this->assertTrue($this->driver->isAutoQuotingEnabled()); - $this->assertTrue($this->driver->autoQuoting(true)); - $this->assertFalse($this->driver->autoQuoting(false)); + $this->driver->enableAutoQuoting(false); + $this->assertFalse($this->driver->isAutoQuotingEnabled()); - $this->assertTrue($this->driver->autoQuoting('string')); - $this->assertFalse($this->driver->autoQuoting('0')); + $this->driver->enableAutoQuoting('string'); + $this->assertTrue($this->driver->isAutoQuotingEnabled()); - $this->assertTrue($this->driver->autoQuoting(1)); - $this->assertFalse($this->driver->autoQuoting(0)); + $this->driver->enableAutoQuoting('0'); + $this->assertFalse($this->driver->isAutoQuotingEnabled()); + + $this->driver->enableAutoQuoting(1); + $this->assertTrue($this->driver->isAutoQuotingEnabled()); + + $this->driver->enableAutoQuoting(0); + $this->assertFalse($this->driver->isAutoQuotingEnabled()); } /** diff --git a/tests/TestCase/Database/Expression/QueryExpressionTest.php b/tests/TestCase/Database/Expression/QueryExpressionTest.php index d6bd88f3754..46546548342 100644 --- a/tests/TestCase/Database/Expression/QueryExpressionTest.php +++ b/tests/TestCase/Database/Expression/QueryExpressionTest.php @@ -43,35 +43,41 @@ public function testConjunction() /** * Test tieWith() works. * + * @group deprecated * @return */ public function testTieWith() { - $expr = new QueryExpression(['1', '2']); - $binder = new ValueBinder(); + $this->deprecated(function () { + $expr = new QueryExpression(['1', '2']); + $binder = new ValueBinder(); - $this->assertSame($expr, $expr->tieWith('+')); - $this->assertSame('+', $expr->tieWith()); + $this->assertSame($expr, $expr->tieWith('+')); + $this->assertSame('+', $expr->tieWith()); - $result = $expr->sql($binder); - $this->assertEquals('(1 + 2)', $result); + $result = $expr->sql($binder); + $this->assertEquals('(1 + 2)', $result); + }); } /** * Test type() works. * + * @group deprecated * @return */ public function testType() { - $expr = new QueryExpression(['1', '2']); - $binder = new ValueBinder(); + $this->deprecated(function () { + $expr = new QueryExpression(['1', '2']); + $binder = new ValueBinder(); - $this->assertSame($expr, $expr->type('+')); - $this->assertSame('+', $expr->type()); + $this->assertSame($expr, $expr->type('+')); + $this->assertSame('+', $expr->type()); - $result = $expr->sql($binder); - $this->assertEquals('(1 + 2)', $result); + $result = $expr->sql($binder); + $this->assertEquals('(1 + 2)', $result); + }); } /** diff --git a/tests/TestCase/Database/ExpressionTypeCastingIntegrationTest.php b/tests/TestCase/Database/ExpressionTypeCastingIntegrationTest.php index 76bbe53b9cd..27b62e621d0 100644 --- a/tests/TestCase/Database/ExpressionTypeCastingIntegrationTest.php +++ b/tests/TestCase/Database/ExpressionTypeCastingIntegrationTest.php @@ -79,7 +79,7 @@ public function setUp() { parent::setUp(); $this->connection = ConnectionManager::get('test'); - $this->skipIf($this->connection->driver() instanceof Sqlserver, 'This tests uses functions specific to other drivers'); + $this->skipIf($this->connection->getDriver() instanceof Sqlserver, 'This tests uses functions specific to other drivers'); Type::map('ordered_uuid', OrderedUuidType::class); } @@ -109,9 +109,9 @@ public function testInsert() ->select('id') ->from('ordered_uuid_items') ->order('id') - ->defaultTypes(['id' => 'ordered_uuid']); + ->setDefaultTypes(['id' => 'ordered_uuid']); - $query->selectTypeMap($query->typeMap()); + $query->setSelectTypeMap($query->getTypeMap()); $results = $query->execute()->fetchAll('assoc'); $this->assertEquals(new UuidValue('419a8da0482b7756b21f27da40cf8569'), $results[0]['id']); diff --git a/tests/TestCase/Database/FunctionsBuilderTest.php b/tests/TestCase/Database/FunctionsBuilderTest.php index 88016fe6f4e..64fd1fab98f 100644 --- a/tests/TestCase/Database/FunctionsBuilderTest.php +++ b/tests/TestCase/Database/FunctionsBuilderTest.php @@ -43,11 +43,11 @@ public function testArbitrary() { $function = $this->functions->MyFunc(['b' => 'literal']); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); - $this->assertEquals('MyFunc', $function->name()); + $this->assertEquals('MyFunc', $function->getName()); $this->assertEquals('MyFunc(b)', $function->sql(new ValueBinder)); $function = $this->functions->MyFunc(['b'], ['string'], 'integer'); - $this->assertEquals('integer', $function->returnType()); + $this->assertEquals('integer', $function->getReturnType()); } /** @@ -60,12 +60,12 @@ public function testSum() $function = $this->functions->sum('total'); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('SUM(total)', $function->sql(new ValueBinder)); - $this->assertEquals('float', $function->returnType()); + $this->assertEquals('float', $function->getReturnType()); $function = $this->functions->sum('total', ['integer']); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('SUM(total)', $function->sql(new ValueBinder)); - $this->assertEquals('integer', $function->returnType()); + $this->assertEquals('integer', $function->getReturnType()); } /** @@ -78,7 +78,7 @@ public function testAvg() $function = $this->functions->avg('salary'); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('AVG(salary)', $function->sql(new ValueBinder)); - $this->assertEquals('float', $function->returnType()); + $this->assertEquals('float', $function->getReturnType()); } /** @@ -91,7 +91,7 @@ public function testMAX() $function = $this->functions->max('created', ['datetime']); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('MAX(created)', $function->sql(new ValueBinder)); - $this->assertEquals('datetime', $function->returnType()); + $this->assertEquals('datetime', $function->getReturnType()); } /** @@ -104,7 +104,7 @@ public function testMin() $function = $this->functions->min('created', ['date']); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('MIN(created)', $function->sql(new ValueBinder)); - $this->assertEquals('date', $function->returnType()); + $this->assertEquals('date', $function->getReturnType()); } /** @@ -117,7 +117,7 @@ public function testCount() $function = $this->functions->count('*'); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('COUNT(*)', $function->sql(new ValueBinder)); - $this->assertEquals('integer', $function->returnType()); + $this->assertEquals('integer', $function->getReturnType()); } /** @@ -130,7 +130,7 @@ public function testConcat() $function = $this->functions->concat(['title' => 'literal', ' is a string']); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('CONCAT(title, :param0)', $function->sql(new ValueBinder)); - $this->assertEquals('string', $function->returnType()); + $this->assertEquals('string', $function->getReturnType()); } /** @@ -143,7 +143,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, :param0, :param1)', $function->sql(new ValueBinder)); - $this->assertEquals('date', $function->returnType()); + $this->assertEquals('date', $function->getReturnType()); } /** @@ -156,17 +156,17 @@ public function testNow() $function = $this->functions->now(); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('NOW()', $function->sql(new ValueBinder)); - $this->assertEquals('datetime', $function->returnType()); + $this->assertEquals('datetime', $function->getReturnType()); $function = $this->functions->now('date'); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('CURRENT_DATE()', $function->sql(new ValueBinder)); - $this->assertEquals('date', $function->returnType()); + $this->assertEquals('date', $function->getReturnType()); $function = $this->functions->now('time'); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('CURRENT_TIME()', $function->sql(new ValueBinder)); - $this->assertEquals('time', $function->returnType()); + $this->assertEquals('time', $function->getReturnType()); } /** @@ -179,12 +179,12 @@ public function testExtract() $function = $this->functions->extract('day', 'created'); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('EXTRACT(day FROM created)', $function->sql(new ValueBinder)); - $this->assertEquals('integer', $function->returnType()); + $this->assertEquals('integer', $function->getReturnType()); $function = $this->functions->datePart('year', 'modified'); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('EXTRACT(year FROM modified)', $function->sql(new ValueBinder)); - $this->assertEquals('integer', $function->returnType()); + $this->assertEquals('integer', $function->getReturnType()); } /** @@ -197,7 +197,7 @@ public function testDateAdd() $function = $this->functions->dateAdd('created', -3, 'day'); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('DATE_ADD(created, INTERVAL -3 day)', $function->sql(new ValueBinder)); - $this->assertEquals('datetime', $function->returnType()); + $this->assertEquals('datetime', $function->getReturnType()); } /** @@ -210,11 +210,11 @@ public function testDayOfWeek() $function = $this->functions->dayOfWeek('created'); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('DAYOFWEEK(created)', $function->sql(new ValueBinder)); - $this->assertEquals('integer', $function->returnType()); + $this->assertEquals('integer', $function->getReturnType()); $function = $this->functions->weekday('created'); $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function); $this->assertEquals('DAYOFWEEK(created)', $function->sql(new ValueBinder)); - $this->assertEquals('integer', $function->returnType()); + $this->assertEquals('integer', $function->getReturnType()); } } diff --git a/tests/TestCase/Database/Log/LoggingStatementTest.php b/tests/TestCase/Database/Log/LoggingStatementTest.php index 73930ef3347..abe341100e8 100644 --- a/tests/TestCase/Database/Log/LoggingStatementTest.php +++ b/tests/TestCase/Database/Log/LoggingStatementTest.php @@ -44,7 +44,7 @@ public function testExecuteNoParams() )); $st = new LoggingStatement($inner); $st->queryString = 'SELECT bar FROM foo'; - $st->logger($logger); + $st->setLogger($logger); $st->execute(); } @@ -69,7 +69,7 @@ public function testExecuteWithParams() )); $st = new LoggingStatement($inner); $st->queryString = 'SELECT bar FROM foo'; - $st->logger($logger); + $st->setLogger($logger); $st->execute(['a' => 1, 'b' => 2]); } @@ -107,7 +107,7 @@ public function testExecuteWithBinding() $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock(); $st = new LoggingStatement($inner, $driver); $st->queryString = 'SELECT bar FROM foo'; - $st->logger($logger); + $st->setLogger($logger); $st->bindValue('a', 1); $st->bindValue('b', $date, 'date'); $st->execute(); @@ -140,10 +140,29 @@ public function testExecuteWithError() )); $st = new LoggingStatement($inner); $st->queryString = 'SELECT bar FROM foo'; - $st->logger($logger); + $st->setLogger($logger); $st->execute(); } + /** + * Tests setting and getting the logger + * + * @group deprecated + * @return void + */ + public function testLoggerCompat() + { + $this->deprecated(function () { + $logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')->getMock(); + $st = new LoggingStatement(); + + $this->assertNull($st->logger()); + + $st->logger($logger); + $this->assertSame($logger, $st->logger()); + }); + } + /** * Tests setting and getting the logger * diff --git a/tests/TestCase/Database/QueryTest.php b/tests/TestCase/Database/QueryTest.php index 32377d4aab2..5df3941055d 100644 --- a/tests/TestCase/Database/QueryTest.php +++ b/tests/TestCase/Database/QueryTest.php @@ -39,13 +39,13 @@ public function setUp() { parent::setUp(); $this->connection = ConnectionManager::get('test'); - $this->autoQuote = $this->connection->driver()->autoQuoting(); + $this->autoQuote = $this->connection->getDriver()->isAutoQuotingEnabled(); } public function tearDown() { parent::tearDown(); - $this->connection->driver()->autoQuoting($this->autoQuote); + $this->connection->getDriver()->enableAutoQuoting($this->autoQuote); unset($this->connection); } @@ -69,7 +69,7 @@ public function testDefaultType() */ public function testSelectFieldsOnly() { - $this->connection->driver()->autoQuoting(false); + $this->connection->getDriver()->enableAutoQuoting(false); $query = new Query($this->connection); $result = $query->select('1 + 1')->execute(); $this->assertInstanceOf('Cake\Database\StatementInterface', $result); @@ -95,7 +95,7 @@ public function testSelectFieldsOnly() */ public function testSelectClosure() { - $this->connection->driver()->autoQuoting(false); + $this->connection->getDriver()->enableAutoQuoting(false); $query = new Query($this->connection); $result = $query->select(function ($q) use ($query) { $this->assertSame($query, $q); @@ -384,7 +384,7 @@ public function testSelectRightJoin() { $this->loadFixtures('Articles', 'Comments'); $this->skipIf( - $this->connection->driver() instanceof \Cake\Database\Driver\Sqlite, + $this->connection->getDriver() instanceof \Cake\Database\Driver\Sqlite, 'SQLite does not support RIGHT joins' ); $query = new Query($this->connection); @@ -793,22 +793,25 @@ public function testSelectWhereArrayTypeEmptyWithExpression() /** * Tests that Query::orWhere() can be used to concatenate conditions with OR * + * @group deprecated * @return void */ public function testSelectOrWhere() { - $this->loadFixtures('Comments'); - $query = new Query($this->connection); - $result = $query - ->select(['id']) - ->from('comments') - ->where(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime']) - ->orWhere(['created' => new \DateTime('2007-03-18 10:47:23')], ['created' => 'datetime']) - ->execute(); - $this->assertCount(2, $result); - $this->assertEquals(['id' => 1], $result->fetch('assoc')); - $this->assertEquals(['id' => 2], $result->fetch('assoc')); - $result->closeCursor(); + $this->deprecated(function () { + $this->loadFixtures('Comments'); + $query = new Query($this->connection); + $result = $query + ->select(['id']) + ->from('comments') + ->where(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime']) + ->orWhere(['created' => new \DateTime('2007-03-18 10:47:23')], ['created' => 'datetime']) + ->execute(); + $this->assertCount(2, $result); + $this->assertEquals(['id' => 1], $result->fetch('assoc')); + $this->assertEquals(['id' => 2], $result->fetch('assoc')); + $result->closeCursor(); + }); } /** @@ -845,59 +848,65 @@ public function testSelectAndWhere() * Tests that combining Query::andWhere() and Query::orWhere() produces * correct conditions nesting * + * @group deprecated * @return void */ public function testSelectExpressionNesting() { - $this->loadFixtures('Comments'); - $query = new Query($this->connection); - $result = $query - ->select(['id']) - ->from('comments') - ->where(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime']) - ->orWhere(['id' => 2]) - ->andWhere(['created >=' => new \DateTime('2007-03-18 10:40:00')], ['created' => 'datetime']) - ->execute(); - $this->assertCount(2, $result); - $this->assertEquals(['id' => 1], $result->fetch('assoc')); - $this->assertEquals(['id' => 2], $result->fetch('assoc')); - $result->closeCursor(); + $this->deprecated(function () { + $this->loadFixtures('Comments'); + $query = new Query($this->connection); + $result = $query + ->select(['id']) + ->from('comments') + ->where(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime']) + ->orWhere(['id' => 2]) + ->andWhere(['created >=' => new \DateTime('2007-03-18 10:40:00')], ['created' => 'datetime']) + ->execute(); + $this->assertCount(2, $result); + $this->assertEquals(['id' => 1], $result->fetch('assoc')); + $this->assertEquals(['id' => 2], $result->fetch('assoc')); + $result->closeCursor(); - $query = new Query($this->connection); - $result = $query - ->select(['id']) - ->from('comments') - ->where(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime']) - ->orWhere(['id' => 2]) - ->andWhere(['created >=' => new \DateTime('2007-03-18 10:40:00')], ['created' => 'datetime']) - ->orWhere(['created' => new \DateTime('2007-03-18 10:49:23')], ['created' => 'datetime']) - ->execute(); - $this->assertCount(3, $result); - $this->assertEquals(['id' => 1], $result->fetch('assoc')); - $this->assertEquals(['id' => 2], $result->fetch('assoc')); - $this->assertEquals(['id' => 3], $result->fetch('assoc')); - $result->closeCursor(); + $query = new Query($this->connection); + $result = $query + ->select(['id']) + ->from('comments') + ->where(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime']) + ->orWhere(['id' => 2]) + ->andWhere(['created >=' => new \DateTime('2007-03-18 10:40:00')], ['created' => 'datetime']) + ->orWhere(['created' => new \DateTime('2007-03-18 10:49:23')], ['created' => 'datetime']) + ->execute(); + $this->assertCount(3, $result); + $this->assertEquals(['id' => 1], $result->fetch('assoc')); + $this->assertEquals(['id' => 2], $result->fetch('assoc')); + $this->assertEquals(['id' => 3], $result->fetch('assoc')); + $result->closeCursor(); + }); } /** * Tests that Query::orWhere() can be used without calling where() before * + * @group deprecated * @return void */ public function testSelectOrWhereNoPreviousCondition() { - $this->loadFixtures('Comments'); - $query = new Query($this->connection); - $result = $query - ->select(['id']) - ->from('comments') - ->orWhere(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime']) - ->orWhere(['created' => new \DateTime('2007-03-18 10:47:23')], ['created' => 'datetime']) - ->execute(); - $this->assertCount(2, $result); - $this->assertEquals(['id' => 1], $result->fetch('assoc')); - $this->assertEquals(['id' => 2], $result->fetch('assoc')); - $result->closeCursor(); + $this->deprecated(function () { + $this->loadFixtures('Comments'); + $query = new Query($this->connection); + $result = $query + ->select(['id']) + ->from('comments') + ->orWhere(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime']) + ->orWhere(['created' => new \DateTime('2007-03-18 10:47:23')], ['created' => 'datetime']) + ->execute(); + $this->assertCount(2, $result); + $this->assertEquals(['id' => 1], $result->fetch('assoc')); + $this->assertEquals(['id' => 2], $result->fetch('assoc')); + $result->closeCursor(); + }); } /** @@ -1036,39 +1045,42 @@ public function testSelectAndWhereUsingClosure() * Tests that it is possible to pass a closure to orWhere() to build a set of * conditions and return the expression to be used * + * @group deprecated * @return void */ public function testSelectOrWhereUsingClosure() { - $this->loadFixtures('Comments'); - $query = new Query($this->connection); - $result = $query - ->select(['id']) - ->from('comments') - ->where(['id' => '1']) - ->orWhere(function ($exp) { - return $exp->eq('created', new \DateTime('2007-03-18 10:47:23'), 'datetime'); - }) - ->execute(); - $this->assertCount(2, $result); - $this->assertEquals(['id' => 1], $result->fetch('assoc')); - $this->assertEquals(['id' => 2], $result->fetch('assoc')); - $result->closeCursor(); + $this->deprecated(function () { + $this->loadFixtures('Comments'); + $query = new Query($this->connection); + $result = $query + ->select(['id']) + ->from('comments') + ->where(['id' => '1']) + ->orWhere(function ($exp) { + return $exp->eq('created', new \DateTime('2007-03-18 10:47:23'), 'datetime'); + }) + ->execute(); + $this->assertCount(2, $result); + $this->assertEquals(['id' => 1], $result->fetch('assoc')); + $this->assertEquals(['id' => 2], $result->fetch('assoc')); + $result->closeCursor(); - $query = new Query($this->connection); - $result = $query - ->select(['id']) - ->from('comments') - ->where(['id' => '1']) - ->orWhere(function ($exp) { - return $exp - ->eq('created', new \DateTime('2012-12-22 12:00'), 'datetime') - ->eq('id', 3); - }) - ->execute(); - $this->assertCount(1, $result); - $this->assertEquals(['id' => 1], $result->fetch('assoc')); - $result->closeCursor(); + $query = new Query($this->connection); + $result = $query + ->select(['id']) + ->from('comments') + ->where(['id' => '1']) + ->orWhere(function ($exp) { + return $exp + ->eq('created', new \DateTime('2012-12-22 12:00'), 'datetime') + ->eq('id', 3); + }) + ->execute(); + $this->assertCount(1, $result); + $this->assertEquals(['id' => 1], $result->fetch('assoc')); + $result->closeCursor(); + }); } /** @@ -1380,7 +1392,7 @@ public function testInClausePlaceholderGeneration() ->from('comments') ->where(['id IN' => [1, 2]]) ->sql(); - $bindings = $query->valueBinder()->bindings(); + $bindings = $query->getValueBinder()->bindings(); $this->assertArrayHasKey(':c0', $bindings); $this->assertEquals('c0', $bindings[':c0']['placeholder']); $this->assertArrayHasKey(':c1', $bindings); @@ -2040,48 +2052,51 @@ public function testSelectHaving() * Tests that Query::orHaving() can be used to concatenate conditions with OR * in the having clause * + * @group deprecated * @return void */ public function testSelectOrHaving() { - $this->loadFixtures('Authors', 'Articles'); - $query = new Query($this->connection); - $result = $query - ->select(['total' => 'count(author_id)', 'author_id']) - ->from('articles') - ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')]) - ->group('author_id') - ->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer']) - ->orHaving(['count(author_id) <' => 2], ['count(author_id)' => 'integer']) - ->execute(); - $expected = [['total' => 1, 'author_id' => 3]]; - $this->assertEquals($expected, $result->fetchAll('assoc')); - - $query = new Query($this->connection); - $result = $query - ->select(['total' => 'count(author_id)', 'author_id']) - ->from('articles') - ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')]) - ->group('author_id') - ->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer']) - ->orHaving(['count(author_id) <=' => 2], ['count(author_id)' => 'integer']) - ->execute(); - $expected = [['total' => 2, 'author_id' => 1], ['total' => 1, 'author_id' => 3]]; - $this->assertEquals($expected, $result->fetchAll('assoc')); - - $query = new Query($this->connection); - $result = $query - ->select(['total' => 'count(author_id)', 'author_id']) - ->from('articles') - ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')]) - ->group('author_id') - ->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer']) - ->orHaving(function ($e) { - return $e->add('count(author_id) = 1 + 1'); - }) - ->execute(); - $expected = [['total' => 2, 'author_id' => 1]]; - $this->assertEquals($expected, $result->fetchAll('assoc')); + $this->deprecated(function () { + $this->loadFixtures('Authors', 'Articles'); + $query = new Query($this->connection); + $result = $query + ->select(['total' => 'count(author_id)', 'author_id']) + ->from('articles') + ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')]) + ->group('author_id') + ->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer']) + ->orHaving(['count(author_id) <' => 2], ['count(author_id)' => 'integer']) + ->execute(); + $expected = [['total' => 1, 'author_id' => 3]]; + $this->assertEquals($expected, $result->fetchAll('assoc')); + + $query = new Query($this->connection); + $result = $query + ->select(['total' => 'count(author_id)', 'author_id']) + ->from('articles') + ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')]) + ->group('author_id') + ->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer']) + ->orHaving(['count(author_id) <=' => 2], ['count(author_id)' => 'integer']) + ->execute(); + $expected = [['total' => 2, 'author_id' => 1], ['total' => 1, 'author_id' => 3]]; + $this->assertEquals($expected, $result->fetchAll('assoc')); + + $query = new Query($this->connection); + $result = $query + ->select(['total' => 'count(author_id)', 'author_id']) + ->from('articles') + ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')]) + ->group('author_id') + ->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer']) + ->orHaving(function ($e) { + return $e->add('count(author_id) = 1 + 1'); + }) + ->execute(); + $expected = [['total' => 2, 'author_id' => 1]]; + $this->assertEquals($expected, $result->fetchAll('assoc')); + }); } /** @@ -2537,8 +2552,8 @@ public function testUnionOrderBy() { $this->loadFixtures('Articles', 'Comments'); $this->skipIf( - ($this->connection->driver() instanceof \Cake\Database\Driver\Sqlite || - $this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver), + ($this->connection->getDriver() instanceof \Cake\Database\Driver\Sqlite || + $this->connection->getDriver() instanceof \Cake\Database\Driver\Sqlserver), 'Driver does not support ORDER BY in UNIONed queries.' ); $union = (new Query($this->connection)) @@ -3077,7 +3092,7 @@ public function testInsertSimple() $result->closeCursor(); //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT - if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) { + if (!$this->connection->getDriver() instanceof \Cake\Database\Driver\Sqlserver) { $this->assertCount(1, $result, '1 row should be inserted'); } @@ -3143,7 +3158,7 @@ public function testInsertSparseRow() $result->closeCursor(); //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT - if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) { + if (!$this->connection->getDriver() instanceof \Cake\Database\Driver\Sqlserver) { $this->assertCount(1, $result, '1 row should be inserted'); } @@ -3181,7 +3196,7 @@ public function testInsertMultipleRowsSparse() $result->closeCursor(); //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT - if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) { + if (!$this->connection->getDriver() instanceof \Cake\Database\Driver\Sqlserver) { $this->assertCount(2, $result, '2 rows should be inserted'); } @@ -3239,7 +3254,7 @@ public function testInsertFromSelect() $result->closeCursor(); //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT - if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) { + if (!$this->connection->getDriver() instanceof \Cake\Database\Driver\Sqlserver) { $this->assertCount(1, $result); } @@ -3305,7 +3320,7 @@ public function testInsertExpressionValues() $result->closeCursor(); //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT - if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) { + if (!$this->connection->getDriver() instanceof \Cake\Database\Driver\Sqlserver) { $this->assertCount(1, $result); } @@ -3335,7 +3350,7 @@ public function testInsertExpressionValues() $result = $query->execute(); $result->closeCursor(); //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT - if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) { + if (!$this->connection->getDriver() instanceof \Cake\Database\Driver\Sqlserver) { $this->assertCount(1, $result); } @@ -3467,10 +3482,10 @@ public function testDefaultTypes() { $this->loadFixtures('Comments'); $query = new Query($this->connection); - $this->assertEquals([], $query->defaultTypes()); + $this->assertEquals([], $query->getDefaultTypes()); $types = ['id' => 'integer', 'created' => 'datetime']; - $this->assertSame($query, $query->defaultTypes($types)); - $this->assertSame($types, $query->defaultTypes()); + $this->assertSame($query, $query->setDefaultTypes($types)); + $this->assertSame($types, $query->getDefaultTypes()); $results = $query->select(['id', 'comment']) ->from('comments') @@ -3601,7 +3616,7 @@ public function testAppendDelete() */ public function testQuotingSelectFieldsAndAlias() { - $this->connection->driver()->autoQuoting(true); + $this->connection->getDriver()->enableAutoQuoting(true); $query = new Query($this->connection); $sql = $query->select(['something'])->sql(); $this->assertQuotedQuery('SELECT $', $sql); @@ -3634,7 +3649,7 @@ public function testQuotingSelectFieldsAndAlias() */ public function testQuotingFromAndAlias() { - $this->connection->driver()->autoQuoting(true); + $this->connection->getDriver()->enableAutoQuoting(true); $query = new Query($this->connection); $sql = $query->select('*')->from(['something'])->sql(); $this->assertQuotedQuery('FROM ', $sql); @@ -3655,7 +3670,7 @@ public function testQuotingFromAndAlias() */ public function testQuotingDistinctOn() { - $this->connection->driver()->autoQuoting(true); + $this->connection->getDriver()->enableAutoQuoting(true); $query = new Query($this->connection); $sql = $query->select('*')->distinct(['something'])->sql(); $this->assertQuotedQuery('', $sql); @@ -3668,7 +3683,7 @@ public function testQuotingDistinctOn() */ public function testQuotingJoinsAndAlias() { - $this->connection->driver()->autoQuoting(true); + $this->connection->getDriver()->enableAutoQuoting(true); $query = new Query($this->connection); $sql = $query->select('*')->join(['something'])->sql(); $this->assertQuotedQuery('JOIN ', $sql); @@ -3689,7 +3704,7 @@ public function testQuotingJoinsAndAlias() */ public function testQuotingGroupBy() { - $this->connection->driver()->autoQuoting(true); + $this->connection->getDriver()->enableAutoQuoting(true); $query = new Query($this->connection); $sql = $query->select('*')->group(['something'])->sql(); $this->assertQuotedQuery('GROUP BY ', $sql); @@ -3710,7 +3725,7 @@ public function testQuotingGroupBy() */ public function testQuotingExpressions() { - $this->connection->driver()->autoQuoting(true); + $this->connection->getDriver()->enableAutoQuoting(true); $query = new Query($this->connection); $sql = $query->select('*') ->where(['something' => 'value']) @@ -3735,7 +3750,7 @@ public function testQuotingExpressions() */ public function testQuotingInsert() { - $this->connection->driver()->autoQuoting(true); + $this->connection->getDriver()->enableAutoQuoting(true); $query = new Query($this->connection); $sql = $query->insert(['bar', 'baz']) ->into('foo') @@ -3775,7 +3790,7 @@ public function testDebugInfo() { $query = (new Query($this->connection))->select('*') ->from('articles') - ->defaultTypes(['id' => 'integer']) + ->setDefaultTypes(['id' => 'integer']) ->where(['id' => '1']); $expected = [ @@ -3861,7 +3876,7 @@ public function testIsNullWithExpressions() */ public function testIsNullAutoQuoting() { - $this->connection->driver()->autoQuoting(true); + $this->connection->getDriver()->enableAutoQuoting(true); $query = new Query($this->connection); $query->select('*')->from('things')->where(function ($exp) { return $exp->isNull('field'); @@ -3992,7 +4007,7 @@ public function testSqlCaseStatement() ); //Postgres requires the case statement to be cast to a integer - if ($this->connection->driver() instanceof \Cake\Database\Driver\Postgres) { + if ($this->connection->getDriver() instanceof \Cake\Database\Driver\Postgres) { $publishedCase = $query->func()->cast([$publishedCase, 'integer' => 'literal'])->type(' AS '); $notPublishedCase = $query->func()->cast([$notPublishedCase, 'integer' => 'literal'])->type(' AS '); } @@ -4062,7 +4077,7 @@ public function testUnbufferedQuery() $query = new Query($this->connection); $result = $query->select(['body', 'author_id']) ->from('articles') - ->bufferResults(false) + ->enableBufferedResults(false) ->execute(); if (!method_exists($result, 'bufferResults')) { @@ -4120,8 +4135,8 @@ public function testDeepClone() $this->assertNotEquals($query->clause('order'), $dupe->clause('order')); $this->assertNotSame( - $query->selectTypeMap(), - $dupe->selectTypeMap() + $query->getSelectTypeMap(), + $dupe->getSelectTypeMap() ); } @@ -4133,11 +4148,11 @@ public function testDeepClone() public function testSelectTypeMap() { $query = new Query($this->connection); - $typeMap = $query->selectTypeMap(); + $typeMap = $query->getSelectTypeMap(); $this->assertInstanceOf(TypeMap::class, $typeMap); $another = clone $typeMap; - $query->selectTypeMap($another); - $this->assertSame($another, $query->selectTypeMap()); + $query->setSelectTypeMap($another); + $this->assertSame($another, $query->getSelectTypeMap()); } /** @@ -4153,7 +4168,7 @@ public function testSelectTypeConversion() ->select(['id', 'comment', 'the_date' => 'created']) ->from('comments') ->limit(1) - ->selectTypeMap()->types(['id' => 'integer', 'the_date' => 'datetime']); + ->getSelectTypeMap()->setTypes(['id' => 'integer', 'the_date' => 'datetime']); $result = $query->execute()->fetchAll('assoc'); $this->assertInternalType('integer', $result[0]['id']); $this->assertInstanceOf('DateTime', $result[0]['the_date']); @@ -4185,7 +4200,7 @@ public function testSymmetricJsonType() ->select(['comment']) ->from('comments') ->where(['id' => $id]) - ->selectTypeMap()->types(['comment' => 'json']); + ->getSelectTypeMap()->setTypes(['comment' => 'json']); $result = $query->execute(); $comment = $result->fetchAll('assoc')[0]['comment']; @@ -4226,7 +4241,7 @@ public function testBetweenExpressionAndTypeMap() $query = new Query($this->connection); $query->select('id') ->from('comments') - ->defaultTypes(['created' => 'datetime']) + ->setDefaultTypes(['created' => 'datetime']) ->where(function ($expr) { $from = new \DateTime('2007-03-18 10:45:00'); $to = new \DateTime('2007-03-18 10:48:00'); diff --git a/tests/TestCase/Database/Schema/CollectionTest.php b/tests/TestCase/Database/Schema/CollectionTest.php index c648b4e059a..32fd3ada04e 100644 --- a/tests/TestCase/Database/Schema/CollectionTest.php +++ b/tests/TestCase/Database/Schema/CollectionTest.php @@ -82,12 +82,12 @@ public function testDescribeIncorrectTable() */ public function testDescribeCache() { - $schema = $this->connection->schemaCollection(); + $schema = $this->connection->getSchemaCollection(); $table = $schema->describe('users'); Cache::delete('test_users', '_cake_model_'); $this->connection->cacheMetadata(true); - $schema = $this->connection->schemaCollection(); + $schema = $this->connection->getSchemaCollection(); $result = $schema->describe('users'); $this->assertEquals($table, $result); diff --git a/tests/TestCase/Database/Schema/MysqlSchemaTest.php b/tests/TestCase/Database/Schema/MysqlSchemaTest.php index c3d61c3155e..8c8bbdd4a02 100644 --- a/tests/TestCase/Database/Schema/MysqlSchemaTest.php +++ b/tests/TestCase/Database/Schema/MysqlSchemaTest.php @@ -1154,7 +1154,7 @@ public function testCreateTemporary() 'type' => 'integer', 'null' => false ]); - $table->temporary(true); + $table->setTemporary(true); $sql = $table->createSql($connection); $this->assertContains('CREATE TEMPORARY TABLE', $sql[0]); } diff --git a/tests/TestCase/Database/Schema/PostgresSchemaTest.php b/tests/TestCase/Database/Schema/PostgresSchemaTest.php index 30b9dcb3bfa..b91f82d72ae 100644 --- a/tests/TestCase/Database/Schema/PostgresSchemaTest.php +++ b/tests/TestCase/Database/Schema/PostgresSchemaTest.php @@ -1131,7 +1131,7 @@ public function testCreateTemporary() 'type' => 'integer', 'null' => false ]); - $table->temporary(true); + $table->setTemporary(true); $sql = $table->createSql($connection); $this->assertContains('CREATE TEMPORARY TABLE', $sql[0]); } diff --git a/tests/TestCase/Database/Schema/SqliteSchemaTest.php b/tests/TestCase/Database/Schema/SqliteSchemaTest.php index 7739b0fe9df..1888da55429 100644 --- a/tests/TestCase/Database/Schema/SqliteSchemaTest.php +++ b/tests/TestCase/Database/Schema/SqliteSchemaTest.php @@ -916,7 +916,7 @@ public function testCreateTemporary() 'type' => 'integer', 'null' => false ]); - $table->temporary(true); + $table->setTemporary(true); $sql = $table->createSql($connection); $this->assertContains('CREATE TEMPORARY TABLE', $sql[0]); } diff --git a/tests/TestCase/Database/Schema/TableTest.php b/tests/TestCase/Database/Schema/TableTest.php index dfa7fe8dfae..b09221c51d5 100644 --- a/tests/TestCase/Database/Schema/TableTest.php +++ b/tests/TestCase/Database/Schema/TableTest.php @@ -672,13 +672,31 @@ public function testAddConstraintForeignKeyBadData($data) * @return void */ public function testTemporary() + { + $this->deprecated(function () { + $table = new Table('articles'); + $this->assertFalse($table->temporary()); + $this->assertSame($table, $table->temporary(true)); + $this->assertTrue($table->temporary()); + $table->temporary(false); + $this->assertFalse($table->temporary()); + }); + } + + /** + * Tests the setTemporary() & isTemporary() method + * + * @return void + */ + public function testSetTemporary() { $table = new Table('articles'); - $this->assertFalse($table->temporary()); - $this->assertSame($table, $table->temporary(true)); - $this->assertTrue($table->temporary()); - $table->temporary(false); - $this->assertFalse($table->temporary()); + $this->assertFalse($table->isTemporary()); + $this->assertSame($table, $table->setTemporary(true)); + $this->assertTrue($table->isTemporary()); + + $table->setTemporary(false); + $this->assertFalse($table->isTemporary()); } /** diff --git a/tests/TestCase/Database/SchemaCacheTest.php b/tests/TestCase/Database/SchemaCacheTest.php index 2c855c34415..575aae1e763 100644 --- a/tests/TestCase/Database/SchemaCacheTest.php +++ b/tests/TestCase/Database/SchemaCacheTest.php @@ -88,7 +88,7 @@ public function testClearEnablesMetadataCache() $ormCache = new SchemaCache($ds); $ormCache->clear(); - $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $ds->schemaCollection()); + $this->assertInstanceOf(CachedCollection::class, $ds->getSchemaCollection()); } /** @@ -104,7 +104,7 @@ public function testBuildEnablesMetadataCache() $ormCache = new SchemaCache($ds); $ormCache->build(); - $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $ds->schemaCollection()); + $this->assertInstanceOf(CachedCollection::class, $ds->getSchemaCollection()); } /**