<commit_before><?php

namespace Oro\Bundle\BatchBundle\Tests\Unit\ORM\Query;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\Query;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Statement;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\UnitOfWork;

use Oro\Bundle\BatchBundle\ORM\Query\QueryCountCalculator;
use Oro\Bundle\EntityBundle\ORM\SqlQuery;

class QueryCountCalculatorTest extends \PHPUnit_Framework_TestCase
{
    const TEST_COUNT = 42;

    /**
     * @param string $dql
     * @param string $expectedCountQuery
     * @param array  $sqlParameters
     * @param array  $types
     * @param array  $queryParameters
     *
     * @dataProvider getCountDataProvider
     */
    public function testCalculateCount(
        $dql,
        $expectedCountQuery,
        array $sqlParameters = [],
        array $types = [],
        array $queryParameters = []
    ) {
        /** @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);

        $connection->expects($this->once())
            ->method('executeQuery')
            ->with($expectedCountQuery, $sqlParameters, $types)
            ->will($this->returnValue($statement));

        $statement->expects($this->any())
            ->method('fetch')
            ->will($this->onConsecutiveCalls(['sclr_0' => self::TEST_COUNT], false));
        $statement->expects($this->any())
            ->method('fetchColumn')
            ->will($this->returnValue(self::TEST_COUNT));

        $this->assertEquals(self::TEST_COUNT, QueryCountCalculator::calculateCount($query));
    }

    /**
     * @return array
     */
    public function getCountDataProvider()
    {
        return [
/*<<<<<<< HEAD
                'expectedCountQuery' => 'SELECT count(t0_.a) AS sclr_0 FROM  t0_',
            ),
            'empty with group by' => array(
                'dql'                => 'SELECT e FROM Stub:Entity e GROUP BY e.b',
                'expectedCountQuery' => 'SELECT COUNT(*) FROM ' .
                    '(SELECT t0_.a AS a_0, t0_.b AS b_1 FROM  t0_ GROUP BY t0_.b) AS e',
            ),
            'single parameters'   => array(
                'dql'                => 'SELECT e FROM Stub:Entity e WHERE e.a = :a AND e.b = :b',
                'expectedCountQuery' => 'SELECT count(t0_.a) AS sclr_0 FROM  t0_ WHERE t0_.a = ? AND t0_.b = ?',
                'sqlParameters'      => array(1, 2),
                'types'              => array(Type::INTEGER, Type::INTEGER),
                'queryParameters'    => array('a' => 1, 'b' => 2),
            ),
            'multiple parameters' => array(
                'dql'
                    => 'SELECT DISTINCT e.a FROM Stub:Entity e WHERE e.a = :value AND e.b = :value',
                'expectedCountQuery'
                    => 'SELECT DISTINCT count(DISTINCT t0_.a) AS sclr_0 FROM  t0_ WHERE t0_.a = ? AND t0_.b = ?',
                'sqlParameters'      => array(3, 3),
                'types'              => array(Type::INTEGER, Type::INTEGER),
                'queryParameters'    => array('value' => 3),
            ),
=======*/
            'empty'               => [
                'dql'                => 'SELECT e FROM Stub:Entity e',
                'expectedCountQuery' => 'SELECT count(t0_.a) AS sclr_0 FROM  t0_',
            ],
            'empty with group by' => [
                'dql'                => 'SELECT e FROM Stub:Entity e GROUP BY e.b',
                'expectedCountQuery' => 'SELECT COUNT(*) FROM ' .
                    '(SELECT t0_.a AS a_0, t0_.b AS b_1 FROM  t0_ GROUP BY t0_.b) AS e',
            ],
            'single parameters'   => [
                'dql'                => 'SELECT e FROM Stub:Entity e WHERE e.a = :a AND e.b = :b',
                'expectedCountQuery' => 'SELECT count(t0_.a) AS sclr_0 FROM  t0_ WHERE t0_.a = ? AND t0_.b = ?',
                'sqlParameters'      => [1, 2],
                'types'              => [Type::INTEGER, Type::INTEGER],
                'queryParameters'    => ['a' => 1, 'b' => 2],
            ],
            'multiple parameters' => [
                'dql'
                    => 'SELECT DISTINCT e.a FROM Stub:Entity e WHERE e.a = :value AND e.b = :value',
                'expectedCountQuery'
                    => 'SELECT DISTINCT count(DISTINCT t0_.a) AS sclr_0 FROM  t0_ WHERE t0_.a = ? AND t0_.b = ?',
                'sqlParameters'      => [3, 3],
                'types'              => [Type::INTEGER, Type::INTEGER],
                'queryParameters'    => ['value' => 3],
            ],
        ];
    }

    /**
     * @param string $sql<commit_msg>BAP-8267: Fix possible bugs. Fix invalid backmerge<commit_after><?php

namespace Oro\Bundle\BatchBundle\Tests\Unit\ORM\Query;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\Query;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Statement;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\UnitOfWork;

use Oro\Bundle\BatchBundle\ORM\Query\QueryCountCalculator;
use Oro\Bundle\EntityBundle\ORM\SqlQuery;

class QueryCountCalculatorTest extends \PHPUnit_Framework_TestCase
{
    const TEST_COUNT = 42;

    /**
     * @param string $dql
     * @param string $expectedCountQuery
     * @param array  $sqlParameters
     * @param array  $types
     * @param array  $queryParameters
     *
     * @dataProvider getCountDataProvider
     */
    public function testCalculateCount(
        $dql,
        $expectedCountQuery,
        array $sqlParameters = [],
        array $types = [],
        array $queryParameters = []
    ) {
        /** @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);

        $connection->expects($this->once())
            ->method('executeQuery')
            ->with($expectedCountQuery, $sqlParameters, $types)
            ->will($this->returnValue($statement));

        $statement->expects($this->any())
            ->method('fetch')
            ->will($this->onConsecutiveCalls(['sclr_0' => self::TEST_COUNT], false));
        $statement->expects($this->any())
            ->method('fetchColumn')
            ->will($this->returnValue(self::TEST_COUNT));

        $this->assertEquals(self::TEST_COUNT, QueryCountCalculator::calculateCount($query));
    }

    /**
     * @return array
     */
    public function getCountDataProvider()
    {
        return [
            'empty'               => [
                'dql'                => 'SELECT e FROM Stub:Entity e',
                'expectedCountQuery' => 'SELECT count(t0_.a) AS sclr_0 FROM  t0_',
            ],
            'empty with group by' => [
                'dql'                => 'SELECT e FROM Stub:Entity e GROUP BY e.b',
                'expectedCountQuery' => 'SELECT COUNT(*) FROM ' .
                    '(SELECT t0_.a AS a_0, t0_.b AS b_1 FROM  t0_ GROUP BY t0_.b) AS e',
            ],
            'single parameters'   => [
                'dql'                => 'SELECT e FROM Stub:Entity e WHERE e.a = :a AND e.b = :b',
                'expectedCountQuery' => 'SELECT count(t0_.a) AS sclr_0 FROM  t0_ WHERE t0_.a = ? AND t0_.b = ?',
                'sqlParameters'      => [1, 2],
                'types'              => [Type::INTEGER, Type::INTEGER],
                'queryParameters'    => ['a' => 1, 'b' => 2],
            ],
            'multiple parameters' => [
                'dql'
                    => 'SELECT DISTINCT e.a FROM Stub:Entity e WHERE e.a = :value AND e.b = :value',
                'expectedCountQuery'
                    => 'SELECT DISTINCT count(DISTINCT t0_.a) AS sclr_0 FROM  t0_ WHERE t0_.a = ? AND t0_.b = ?',
                'sqlParameters'      => [3, 3],
                'types'              => [Type::INTEGER, Type::INTEGER],
                'queryParameters'    => ['value' => 3],
            ],
        ];
    }

    /**
     * @param string $sql
{'fraction_non_alphanumeric': 0.13789134438305709, 'fraction_numerical': 0.010128913443830571, 'mean_word_length': 1.869550858652576, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 106, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '985025', 'n_tokens_mistral': 2643, 'n_tokens_neox': 2513, 'n_words': 558}