/**
  * Create a new database connection mock object for every test.
  *
  * @return void
  */
 protected function setUp()
 {
     parent::setUp();
     $this->connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
     $this->connection->expects($this->any())->method('quoteIdentifier')->will($this->returnArgument(0));
     $this->connection->expects($this->any())->method('getDatabasePlatform')->will($this->returnValue(new MockPlatform()));
 }
 /**
  * Create a new database connection mock object for every test.
  *
  * @return void
  */
 protected function setUp()
 {
     parent::setUp();
     $this->connection = $this->prophesize(Connection::class);
     $this->connection->quoteIdentifier(Argument::cetera())->will(function ($args) {
         return '"' . join('"."', explode('.', $args[0])) . '"';
     });
     $this->connection->quote(Argument::cetera())->will(function ($args) {
         return "'" . $args[0] . "'";
     });
     $this->connection->getDatabasePlatform()->willReturn(new MockPlatform());
     $this->queryContext = GeneralUtility::makeInstance(QueryContext::class);
     $this->expressionBuilder = GeneralUtility::makeInstance(ExpressionBuilder::class, $this->connection->reveal());
 }
 /**
  * @test
  */
 public function getServerVersionReportsPlatformVersion()
 {
     /** @var MysqliConnection|ObjectProphecy $driverProphet */
     $driverProphet = $this->prophesize(\Doctrine\DBAL\Driver\Mysqli\Driver::class);
     $driverProphet->willImplement(\Doctrine\DBAL\VersionAwarePlatformDriver::class);
     /** @var MysqliConnection|ObjectProphecy $wrappedConnectionProphet */
     $wrappedConnectionProphet = $this->prophesize(\Doctrine\DBAL\Driver\Mysqli\MysqliConnection::class);
     $wrappedConnectionProphet->willImplement(\Doctrine\DBAL\Driver\ServerInfoAwareConnection::class);
     $wrappedConnectionProphet->requiresQueryForServerVersion()->willReturn(false);
     $wrappedConnectionProphet->getServerVersion()->willReturn('5.7.11');
     $this->connection->expects($this->any())->method('getDriver')->willReturn($driverProphet->reveal());
     $this->connection->expects($this->any())->method('getWrappedConnection')->willReturn($wrappedConnectionProphet->reveal());
     $this->assertSame('mock 5.7.11', $this->connection->getServerVersion());
 }
 /**
  * @test
  */
 public function queryRestrictionsAreReevaluatedOnSettingsChangeForExecute()
 {
     $GLOBALS['TCA']['pages']['ctrl'] = ['tstamp' => 'tstamp', 'versioningWS' => true, 'delete' => 'deleted', 'crdate' => 'crdate', 'enablecolumns' => ['disabled' => 'hidden']];
     $this->connection->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
     $this->connection->quoteIdentifiers(Argument::cetera())->willReturnArgument(0);
     $this->connection->getExpressionBuilder()->willReturn(GeneralUtility::makeInstance(ExpressionBuilder::class, $this->connection->reveal()));
     $concreteQueryBuilder = GeneralUtility::makeInstance(\Doctrine\DBAL\Query\QueryBuilder::class, $this->connection->reveal());
     $subject = GeneralUtility::makeInstance(QueryBuilder::class, $this->connection->reveal(), null, $concreteQueryBuilder);
     $subject->select('*')->from('pages')->where('uid=1');
     $subject->getQueryContext()->setIgnoreEnableFields(true)->setIgnoredEnableFields(['disabled']);
     $expectedSQL = 'SELECT * FROM pages WHERE (uid=1) AND (pages.deleted = 0)';
     $this->connection->executeQuery($expectedSQL, Argument::cetera())->shouldBeCalled();
     $subject->execute();
     $subject->getQueryContext()->setIgnoreEnableFields(false);
     $expectedSQL = 'SELECT * FROM pages WHERE (uid=1) AND ((pages.hidden = 0) AND (pages.deleted = 0))';
     $this->connection->executeQuery($expectedSQL, Argument::cetera())->shouldBeCalled();
     $subject->execute();
 }
示例#5
0
 /**
  * Get COLLATION, ROW_FORMAT, COMMENT and ENGINE table options on MySQL connections.
  *
  * @param string[] $tableNames
  * @return array[]
  * @throws \InvalidArgumentException
  */
 protected function getTableOptions(array $tableNames) : array
 {
     $tableOptions = [];
     if (strpos($this->connection->getServerVersion(), 'MySQL') !== 0) {
         foreach ($tableNames as $tableName) {
             $tableOptions[$tableName] = [];
         }
         return $tableOptions;
     }
     $queryBuilder = $this->connection->createQueryBuilder();
     $result = $queryBuilder->select('TABLE_NAME AS table', 'ENGINE AS engine', 'ROW_FORMAT AS row_format', 'TABLE_COLLATION AS collate', 'TABLE_COMMENT AS comment')->from('information_schema.TABLES')->where($queryBuilder->expr()->eq('TABLE_TYPE', $queryBuilder->createNamedParameter('BASE TABLE', \PDO::PARAM_STR)), $queryBuilder->expr()->eq('TABLE_SCHEMA', $queryBuilder->createNamedParameter($this->connection->getDatabase(), \PDO::PARAM_STR)))->execute();
     while ($row = $result->fetch()) {
         $index = $row['table'];
         unset($row['table']);
         $tableOptions[$index] = $row;
     }
     return $tableOptions;
 }
示例#6
0
 /**
  * Adds an ordering to the query results.
  *
  * @param string $fieldName The fieldName to order by. Will be quoted according to database platform automatically.
  * @param string $order The ordering direction.
  *
  * @return QueryBuilder This QueryBuilder instance.
  */
 public function addOrderBy(string $fieldName, string $order = null) : QueryBuilder
 {
     $this->concreteQueryBuilder->addOrderBy($this->connection->quoteIdentifier($fieldName), $order);
     return $this;
 }
 /**
  * @test
  */
 public function literalQuotesValue()
 {
     $this->connectionProphet->quote('aField', 'Doctrine\\DBAL\\Types\\StringType')->shouldBeCalled()->willReturn('"aField"');
     $result = $this->subject->literal('aField', 'Doctrine\\DBAL\\Types\\StringType');
     $this->assertSame('"aField"', $result);
 }
 /**
  * Quotes a given input parameter.
  *
  * @param mixed $input The parameter to be quoted.
  * @param string|null $type The type of the parameter.
  *
  * @return string
  */
 public function literal($input, string $type = null) : string
 {
     return $this->connection->quote($input, $type);
 }
示例#9
0
 /**
  * This database backend uses some optimized queries for mysql
  * to get maximum performance.
  *
  * @param Connection $connection
  * @return bool
  */
 protected function isConnectionMysql(Connection $connection) : bool
 {
     $serverVersion = $connection->getServerVersion();
     return (bool) (strpos($serverVersion, 'MySQL') === 0);
 }
示例#10
0
 /**
  * Checks the character set of the database and reports an error if it is not utf-8.
  *
  * @param Connection $connection to the database to be checked
  * @return Status\StatusInterface
  */
 protected function checkMysqlDatabaseUtf8Status(Connection $connection)
 {
     /** @var QueryBuilder $queryBuilder */
     $queryBuilder = $connection->createQueryBuilder();
     $defaultDatabaseCharset = (string) $queryBuilder->select('DEFAULT_CHARACTER_SET_NAME')->from('information_schema.SCHEMATA')->where($queryBuilder->expr()->eq('SCHEMA_NAME', $queryBuilder->createNamedParameter($connection->getDatabase(), \PDO::PARAM_STR)))->setMaxResults(1)->execute()->fetchColumn();
     // also allow utf8mb4
     if (strpos($defaultDatabaseCharset, 'utf8') !== 0) {
         $status = new Status\ErrorStatus();
         $status->setTitle('MySQL database character set check failed');
         $status->setMessage('Checking database character set failed, got key "' . $defaultDatabaseCharset . '" instead of "utf8" or "utf8mb4"');
     } else {
         $status = new Status\OkStatus();
         $status->setTitle('Your database uses utf-8. All good.');
     }
     return $status;
 }