/** * @param Query|QueryBuilder $criteria * * @return string (sql) */ protected function getSqlCriteria($criteria) { if ($criteria instanceof QueryBuilder) { return $criteria->getQuery()->getSQL(); } elseif ($criteria instanceof Query) { return $criteria->getSQL(); } else { throw new \Exception(sprintf('Criteria must be instance of Query or QueryBuilder, instance of %s given', get_class($criteria))); } }
/** * @dataProvider functionsDataProvider * @param array $functions * @param string $dql * @param string $sql * @param string $expectedResult */ public function testDqlFunction(array $functions, $dql, $sql, $expectedResult) { $configuration = $this->entityManager->getConfiguration(); foreach ($functions as $function) { $this->registerDqlFunction($function['type'], $function['name'], $function['className'], $configuration); } $query = new Query($this->entityManager); $query->setDQL($dql); $this->assertEquals($sql, $query->getSQL(), sprintf('Unexpected SQL for "%s"', $dql)); $result = $query->getArrayResult(); $this->assertNotEmpty($result); $this->assertEquals($expectedResult, array_values(array_shift($result)), sprintf('Unexpected result for "%s"', $dql)); }
/** * @param string $dql * @param array $sqlParameters * @param array $types * @param array $queryParameters * * @dataProvider getCountDataProvider */ public function testCalculateCount($dql, array $sqlParameters, array $types, array $queryParameters = array()) { /** @var $entityManager EntityManager|\PHPUnit_Framework_MockObject_MockObject */ /** @var $connection Connection|\PHPUnit_Framework_MockObject_MockObject */ /** @var $statement Statement|\PHPUnit_Framework_MockObject_MockObject */ list($entityManager, $connection, $statement) = $this->prepareMocks(); $query = new Query($entityManager); $query->setDQL($dql); $query->setParameters($queryParameters); $expectedSql = 'SELECT COUNT(*) FROM (' . $query->getSQL() . ') AS e'; $connection->expects($this->once())->method('executeQuery')->with($expectedSql, $sqlParameters, $types)->will($this->returnValue($statement)); $statement->expects($this->once())->method('fetchColumn')->with()->will($this->returnValue(self::TEST_COUNT)); $this->assertEquals(self::TEST_COUNT, QueryCountCalculator::calculateCount($query)); }
/** * @dataProvider functionsDataProvider * @param array $functions * @param string $dql * @param string $sql * @param string $expectedResult */ public function testDqlFunction(array $functions, $dql, $sql, $expectedResult) { $configuration = $this->entityManager->getConfiguration(); foreach ($functions as $function) { $this->registerDqlFunction($function['type'], $function['name'], $function['className'], $configuration); } $query = new Query($this->entityManager); $query->setDQL($dql); if (is_array($sql)) { $constraints = array(); foreach ($sql as $sqlVariant) { $constraints[] = $this->equalTo($sqlVariant); } $constraint = new \PHPUnit_Framework_Constraint_Or(); $constraint->setConstraints($constraints); $this->assertThat($query->getSQL(), $constraint); } else { $this->assertEquals($sql, $query->getSQL(), sprintf('Unexpected SQL for "%s"', $dql)); } $result = $query->getArrayResult(); $this->assertNotEmpty($result); $this->assertEquals($expectedResult, array_values(array_shift($result)), sprintf('Unexpected result for "%s"', $dql)); }
/** * @dataProvider functionsDataProvider * @param string $type * @param string $functionName * @param string $functionClass * @param string $dql * @param string $sql * @param string $expectedResult */ public function testDateFunction($type, $functionName, $functionClass, $dql, $sql, $expectedResult) { $configuration = $this->entityManager->getConfiguration(); switch ($type) { case 'datetime': $configuration->addCustomDatetimeFunction($functionName, $functionClass); break; case 'numeric': $configuration->addCustomNumericFunction($functionName, $functionClass); break; case 'string': default: $configuration->addCustomStringFunction($functionName, $functionClass); } $query = new Query($this->entityManager); $query->setDQL($dql); $this->assertEquals($sql, $query->getSQL(), sprintf('Unexpected SQL for "%s"', $dql)); $result = $query->getArrayResult(); $this->assertNotEmpty($result); $this->assertEquals($expectedResult, array_values(array_shift($result)), sprintf('Unexpected result for "%s"', $dql)); }
/** * Prints debugging information about the given query * * @param Query $query */ protected function debugQuery(Query $query) { if (!$this->options['verbose']) { return; } $sql = $query->getSQL(); if (is_array($sql)) { $sql = implode('; ', $sql); } $this->logger->info($sql); }
/** * @depends testIssue */ public function testCacheMissWhenTypeChanges(Query $query) { $query->setParameter('field', 'test', 'string'); $this->assertStringEndsWith('.field = ?', $query->getSQL()); }
/** * return query in cache * * @param Query $query * @param integer $time * @param string $MODE [MODE_GET, MODE_PUT , MODE_NORMAL , MODE_REFRESH] * @param boolean $setCacheable * @param string $namespace * @param string $input_hash * * @return Query * @access public * @author Etienne de Longeaux <*****@*****.**> */ public function cacheQuery(Query $query, $time = 3600, $MODE = 3, $setCacheable = true, $namespace = '', $input_hash = '') { if (!$query) { throw new \Gedmo\Exception\InvalidArgumentException('Invalide query instance'); } // create single file from all input if (empty($input_hash)) { $input_hash = $namespace . sha1(serialize($query->getParameters()) . $query->getSQL()); } $query->useResultCache(true, $time, (string) $input_hash); $query->useQueryCache(true); if (method_exists($query, 'setCacheMode')) { $query->setCacheMode($MODE); } if (method_exists($query, 'setCacheable')) { $query->setCacheable($setCacheable); } return $query; }
protected function getHash(Query $query) : string { $sql = $query->getSQL(); $params = $query->getParameters(); return sha1($sql . serialize($params)); }