1
0
mirror of synced 2024-12-14 15:16:04 +03:00
doctrine2/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1430Test.php

297 lines
8.0 KiB
PHP
Raw Normal View History

2011-11-15 20:28:57 +04:00
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
require_once __DIR__ . '/../../../TestInit.php';
/**
* @group DDC-1430
*/
class DDC1430Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
try {
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1430Order'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1430OrderProduct'),
));
$this->loadFixtures();
} catch (\Exception $exc) {
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
}
}
public function testOrderByFields()
{
$repository = $this->_em->getRepository(__NAMESPACE__ . '\DDC1430Order');
$builder = $repository->createQueryBuilder('o');
$query = $builder->select('o.id, o.date, COUNT(p.id) AS p_count')
->leftJoin('o.products', 'p')
->groupBy('o.id, o.date')
2011-11-16 15:48:11 +04:00
->orderBy('o.id')
2011-11-15 20:28:57 +04:00
->getQuery();
2011-12-20 01:56:19 +04:00
2011-11-16 15:48:11 +04:00
$this->assertEquals('SELECT o.id, o.date, COUNT(p.id) AS p_count FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1430Order o LEFT JOIN o.products p GROUP BY o.id, o.date ORDER BY o.id ASC', $query->getDQL());
$this->assertEquals('SELECT d0_.order_id AS order_id0, d0_.created_at AS created_at1, COUNT(d1_.id) AS sclr2 FROM DDC1430Order d0_ LEFT JOIN DDC1430OrderProduct d1_ ON d0_.order_id = d1_.order_id GROUP BY d0_.order_id, d0_.created_at ORDER BY d0_.order_id ASC', $query->getSQL());
2011-11-15 20:28:57 +04:00
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$result = $query->getResult();
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->assertEquals(2, sizeof($result));
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->assertArrayHasKey('id', $result[0]);
$this->assertArrayHasKey('id', $result[1]);
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->assertArrayHasKey('p_count', $result[0]);
$this->assertArrayHasKey('p_count', $result[1]);
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->assertEquals(1, $result[0]['id']);
$this->assertEquals(2, $result[1]['id']);
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->assertEquals(2, $result[0]['p_count']);
$this->assertEquals(3, $result[1]['p_count']);
}
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
public function testOrderByAllObjectFields()
{
$repository = $this->_em->getRepository(__NAMESPACE__ . '\DDC1430Order');
$builder = $repository->createQueryBuilder('o');
$query = $builder->select('o, COUNT(p.id) AS p_count')
->leftJoin('o.products', 'p')
2011-11-16 15:48:11 +04:00
->groupBy('o.id, o.date, o.status')
->orderBy('o.id')
2011-11-15 20:28:57 +04:00
->getQuery();
2011-12-20 01:56:19 +04:00
2011-11-16 15:48:11 +04:00
$this->assertEquals('SELECT o, COUNT(p.id) AS p_count FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1430Order o LEFT JOIN o.products p GROUP BY o.id, o.date, o.status ORDER BY o.id ASC', $query->getDQL());
$this->assertEquals('SELECT d0_.order_id AS order_id0, d0_.created_at AS created_at1, d0_.order_status AS order_status2, COUNT(d1_.id) AS sclr3 FROM DDC1430Order d0_ LEFT JOIN DDC1430OrderProduct d1_ ON d0_.order_id = d1_.order_id GROUP BY d0_.order_id, d0_.created_at, d0_.order_status ORDER BY d0_.order_id ASC', $query->getSQL());
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$result = $query->getResult();
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->assertEquals(2, sizeof($result));
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->assertTrue($result[0][0] instanceof DDC1430Order);
$this->assertTrue($result[1][0] instanceof DDC1430Order);
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->assertEquals($result[0][0]->getId(), 1);
$this->assertEquals($result[1][0]->getId(), 2);
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->assertEquals($result[0]['p_count'], 2);
$this->assertEquals($result[1]['p_count'], 3);
}
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
public function testTicket()
{
$repository = $this->_em->getRepository(__NAMESPACE__ . '\DDC1430Order');
$builder = $repository->createQueryBuilder('o');
$query = $builder->select('o, COUNT(p.id) AS p_count')
->leftJoin('o.products', 'p')
->groupBy('o')
2011-11-16 15:48:11 +04:00
->orderBy('o.id')
2011-11-15 20:28:57 +04:00
->getQuery();
2011-12-20 01:56:19 +04:00
2011-11-16 15:48:11 +04:00
$this->assertEquals('SELECT o, COUNT(p.id) AS p_count FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1430Order o LEFT JOIN o.products p GROUP BY o ORDER BY o.id ASC', $query->getDQL());
$this->assertEquals('SELECT d0_.order_id AS order_id0, d0_.created_at AS created_at1, d0_.order_status AS order_status2, COUNT(d1_.id) AS sclr3 FROM DDC1430Order d0_ LEFT JOIN DDC1430OrderProduct d1_ ON d0_.order_id = d1_.order_id GROUP BY d0_.order_id, d0_.created_at, d0_.order_status ORDER BY d0_.order_id ASC', $query->getSQL());
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$result = $query->getResult();
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->assertEquals(2, sizeof($result));
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->assertTrue($result[0][0] instanceof DDC1430Order);
$this->assertTrue($result[1][0] instanceof DDC1430Order);
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->assertEquals($result[0][0]->getId(), 1);
$this->assertEquals($result[1][0]->getId(), 2);
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->assertEquals($result[0]['p_count'], 2);
$this->assertEquals($result[1]['p_count'], 3);
}
public function loadFixtures()
{
$o1 = new DDC1430Order('NEW');
$o2 = new DDC1430Order('OK');
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$o1->addProduct(new DDC1430OrderProduct(1.1));
$o1->addProduct(new DDC1430OrderProduct(1.2));
$o2->addProduct(new DDC1430OrderProduct(2.1));
$o2->addProduct(new DDC1430OrderProduct(2.2));
$o2->addProduct(new DDC1430OrderProduct(2.3));
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
$this->_em->persist($o1);
$this->_em->persist($o2);
$this->_em->flush();
}
}
/**
* @Entity
*/
class DDC1430Order
{
/**
2011-12-20 01:56:19 +04:00
* @Id
2011-11-15 20:28:57 +04:00
* @Column(name="order_id", type="integer")
* @GeneratedValue()
*/
protected $id;
/**
2011-12-20 01:56:19 +04:00
* @Column(name="created_at", type="datetime")
2011-11-15 20:28:57 +04:00
*/
private $date;
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
/**
2011-12-20 01:56:19 +04:00
* @Column(name="order_status", type="string")
2011-11-15 20:28:57 +04:00
*/
private $status;
/**
* @OneToMany(targetEntity="DDC1430OrderProduct", mappedBy="order", cascade={"persist", "remove"})
2011-12-20 01:56:19 +04:00
*
2011-11-15 20:28:57 +04:00
* @var \Doctrine\Common\Collections\ArrayCollection $products
*/
private $products;
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
public function __construct($status)
{
$this->status = $status;
$this->date = new \DateTime();
$this->products = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
/**
2011-12-20 01:56:19 +04:00
* @return string
2011-11-15 20:28:57 +04:00
*/
public function getStatus()
{
return $this->status;
}
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
/**
2011-12-20 01:56:19 +04:00
* @param string $status
2011-11-15 20:28:57 +04:00
*/
public function setStatus($status)
{
$this->status = $status;
}
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
/**
* @return \Doctrine\Common\Collections\ArrayCollection
*/
public function getProducts()
{
return $this->products;
}
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
/**
2011-12-20 01:56:19 +04:00
* @param DDC1430OrderProduct $product
2011-11-15 20:28:57 +04:00
*/
public function addProduct(DDC1430OrderProduct $product)
{
$product->setOrder($this);
$this->products->add($product);
}
}
/**
* @Entity
*/
class DDC1430OrderProduct
{
/**
2011-12-20 01:56:19 +04:00
* @Id
2011-11-15 20:28:57 +04:00
* @Column(type="integer")
* @GeneratedValue()
*/
protected $id;
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
/**
* @var DDC1430Order $order
2011-12-20 01:56:19 +04:00
*
2011-11-15 20:28:57 +04:00
* @ManyToOne(targetEntity="DDC1430Order", inversedBy="products")
* @JoinColumn(name="order_id", referencedColumnName="order_id", nullable = false)
*/
private $order;
/**
2011-12-20 01:56:19 +04:00
* @column(type="float")
2011-11-15 20:28:57 +04:00
*/
private $value;
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
/**
2011-12-20 01:56:19 +04:00
* @param float $value
2011-11-15 20:28:57 +04:00
*/
public function __construct($value)
{
$this->value = $value;
}
2011-12-20 01:56:19 +04:00
2011-11-15 20:28:57 +04:00
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
2011-12-20 01:56:19 +04:00
* @return DDC1430Order
2011-11-15 20:28:57 +04:00
*/
public function getOrder()
{
return $this->order;
}
/**
2011-12-20 01:56:19 +04:00
* @param DDC1430Order $order
2011-11-15 20:28:57 +04:00
*/
public function setOrder(DDC1430Order $order)
{
$this->order = $order;
}
/**
2011-12-20 01:56:19 +04:00
* @return float
2011-11-15 20:28:57 +04:00
*/
public function getValue()
{
return $this->value;
}
/**
2011-12-20 01:56:19 +04:00
* @param float $value
2011-11-15 20:28:57 +04:00
*/
public function setValue($value)
{
$this->value = $value;
}
}