<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

/**
 * @group DDC-2214
 */
class DDC2214Test extends \Doctrine\Tests\OrmFunctionalTestCase
{

    protected $lineList = array();

    protected $relatedList = array();

    protected function setUp()
    {
        parent::setUp();

        $this->_schemaTool->createSchema(array(
            $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2214Line'),
            $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2214LineRelated'),
        ));

        $this->loadFixtures();
    }

    protected function loadFixtures()
    {
        $l1 = new DDC2214Line();
        $l2 = new DDC2214Line();
        $l3 = new DDC2214Line();

        $r1 = new DDC2214LineRelated();
        $r2 = new DDC2214LineRelated();
        $r3 = new DDC2214LineRelated();

        
        $r1->line_related = 1;
        $r2->line_related = 2;
        $r3->line_related = 3;

        $r1->count = 1;
        $r2->count = 2;
        $r3->count = 3;

        $r1->line = $l1;
        $r2->line = $l2;
        $r3->line = $l3;

        $this->_em->persist($l1);
        $this->_em->persist($l2);
        $this->_em->persist($l3);

        $this->_em->persist($r1);
        $this->_em->persist($r2);
        $this->_em->persist($r3);

        $this->_em->flush();
        $this->_em->clear();

        $this->lineList[] = $l1;
        $this->lineList[] = $l2;
        $this->lineList[] = $l3;
        
        $this->relatedList[] = $r1;
        $this->relatedList[] = $r2;
        $this->relatedList[] = $r3;
    }

    public function testIssue()
    {
        $lines   = $this->lineList;
        $logger  = $this->_em->getConnection()->getConfiguration()->getSQLLogger();
        $ids     = array_map(function($r){
            return $r->id;
        }, $this->relatedList);

        $related = $this->_em->getRepository(__NAMESPACE__ . '\DDC2214LineRelated')->findBy(array('line' => $lines), array('count' => 'DESC'), 20);
        $query   = end($logger->queries);

        $this->assertCount(3, $related);
        $this->assertEquals($ids, $query['params'][0]);
        $this->assertEquals(\Doctrine\DBAL\Connection::PARAM_INT_ARRAY, $query['types'][0]);
    }

}

/**
 * @Entity
 */
class DDC2214LineRelated
{

    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @ManyToOne(targetEntity="DDC2214Line", inversedBy="line_related")
     * @JoinColumn(name="line_id", referencedColumnName="id",nullable=false)
     */
    public $line;

    /**
     * @Column(type="smallint", nullable=false)
     */
    public $count = 0;

    /**
     * @Column(name="line_id_related", type="integer")
     */
    public $line_related;

}

/**
 * @Entity
 */
class DDC2214Line
{

    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @OneToMany(targetEntity="DDC2214LineRelated", mappedBy="line")
     */
    public $line_related;

}