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

218 lines
6.9 KiB
PHP
Raw Normal View History

2011-09-24 01:10:58 +04:00
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use DateTime;
require_once __DIR__ . '/../../../TestInit.php';
2011-09-24 23:16:25 +04:00
/**
2011-10-30 01:58:09 +04:00
* @group DDC-1335
2011-09-24 23:16:25 +04:00
*/
2011-10-30 01:58:09 +04:00
class DDC1335Test extends \Doctrine\Tests\OrmFunctionalTestCase
2011-09-24 01:10:58 +04:00
{
protected function setUp()
{
parent::setUp();
try {
2011-09-25 19:20:48 +04:00
$this->_schemaTool->createSchema(array(
2011-10-30 01:58:09 +04:00
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1335User'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1335Phone'),
2011-09-25 19:20:48 +04:00
));
$this->loadFixture();
2011-09-24 01:10:58 +04:00
} catch(\Exception $e) {
}
}
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
public function testDql()
{
2011-10-30 01:58:09 +04:00
$dql = 'SELECT u FROM ' . __NAMESPACE__ . '\DDC1335User u INDEX BY u.id';
2011-09-24 23:10:10 +04:00
$query = $this->_em->createQuery($dql);
$result = $query->getResult();
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
$this->assertEquals(sizeof($result), 3);
$this->assertArrayHasKey(1, $result);
$this->assertArrayHasKey(2, $result);
$this->assertArrayHasKey(3, $result);
2011-10-30 01:58:09 +04:00
$dql = 'SELECT u, p FROM '.__NAMESPACE__ . '\DDC1335User u INDEX BY u.email INNER JOIN u.phones p INDEX BY p.id';
2011-09-24 23:10:10 +04:00
$query = $this->_em->createQuery($dql);
$result = $query->getResult();
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
$this->assertEquals(sizeof($result), 3);
$this->assertArrayHasKey('foo@foo.com', $result);
$this->assertArrayHasKey('bar@bar.com', $result);
$this->assertArrayHasKey('foobar@foobar.com', $result);
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
$this->assertEquals(sizeof($result['foo@foo.com']->phones), 3);
$this->assertEquals(sizeof($result['bar@bar.com']->phones), 3);
$this->assertEquals(sizeof($result['foobar@foobar.com']->phones), 3);
2011-10-30 01:58:09 +04:00
$foo = $result['foo@foo.com']->phones->toArray();
$bar = $result['bar@bar.com']->phones->toArray();
$foobar = $result['foobar@foobar.com']->phones->toArray();
2011-10-30 01:58:09 +04:00
$this->assertArrayHasKey(1, $foo);
$this->assertArrayHasKey(2, $foo);
$this->assertArrayHasKey(3, $foo);
2011-10-30 01:58:09 +04:00
$this->assertArrayHasKey(4, $bar);
$this->assertArrayHasKey(5, $bar);
$this->assertArrayHasKey(6, $bar);
$this->assertArrayHasKey(7, $foobar);
$this->assertArrayHasKey(8, $foobar);
$this->assertArrayHasKey(9, $foobar);
2011-09-24 23:10:10 +04:00
}
2011-09-24 01:10:58 +04:00
public function testTicket()
{
$builder = $this->_em->createQueryBuilder();
2011-10-30 01:58:09 +04:00
$builder->select('u')->from(__NAMESPACE__ . '\DDC1335User', 'u', 'u.id');
2011-09-24 01:10:58 +04:00
2011-09-24 23:10:10 +04:00
$dql = $builder->getQuery()->getDQL();
$result = $builder->getQuery()->getResult();
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
$this->assertEquals(sizeof($result), 3);
$this->assertArrayHasKey(1, $result);
$this->assertArrayHasKey(2, $result);
$this->assertArrayHasKey(3, $result);
2011-10-30 01:58:09 +04:00
$this->assertEquals('SELECT u FROM ' . __NAMESPACE__ . '\DDC1335User u INDEX BY u.id', $dql);
2011-09-24 23:10:10 +04:00
}
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
public function testIndexByUnique()
{
$builder = $this->_em->createQueryBuilder();
2011-10-30 01:58:09 +04:00
$builder->select('u')->from(__NAMESPACE__ . '\DDC1335User', 'u', 'u.email');
2011-09-24 01:10:58 +04:00
2011-09-24 23:10:10 +04:00
$dql = $builder->getQuery()->getDQL();
$result = $builder->getQuery()->getResult();
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
$this->assertEquals(sizeof($result), 3);
$this->assertArrayHasKey('foo@foo.com', $result);
$this->assertArrayHasKey('bar@bar.com', $result);
$this->assertArrayHasKey('foobar@foobar.com', $result);
2011-10-30 01:58:09 +04:00
$this->assertEquals('SELECT u FROM ' . __NAMESPACE__ . '\DDC1335User u INDEX BY u.email', $dql);
2011-09-24 23:10:10 +04:00
}
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
public function testIndexWithJoin()
{
$builder = $this->_em->createQueryBuilder();
$builder->select('u','p')
2011-10-30 01:58:09 +04:00
->from(__NAMESPACE__ . '\DDC1335User', 'u', 'u.email')
2011-09-24 23:10:10 +04:00
->join('u.phones', 'p', null, null, 'p.id');
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
$dql = $builder->getQuery()->getDQL();
$result = $builder->getQuery()->getResult();
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
$this->assertEquals(sizeof($result), 3);
$this->assertArrayHasKey('foo@foo.com', $result);
$this->assertArrayHasKey('bar@bar.com', $result);
$this->assertArrayHasKey('foobar@foobar.com', $result);
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
$this->assertEquals(sizeof($result['foo@foo.com']->phones), 3);
$this->assertEquals(sizeof($result['bar@bar.com']->phones), 3);
$this->assertEquals(sizeof($result['foobar@foobar.com']->phones), 3);
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
$this->assertArrayHasKey(1, $result['foo@foo.com']->phones->toArray());
$this->assertArrayHasKey(2, $result['foo@foo.com']->phones->toArray());
$this->assertArrayHasKey(3, $result['foo@foo.com']->phones->toArray());
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
$this->assertArrayHasKey(4, $result['bar@bar.com']->phones->toArray());
$this->assertArrayHasKey(5, $result['bar@bar.com']->phones->toArray());
$this->assertArrayHasKey(6, $result['bar@bar.com']->phones->toArray());
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
$this->assertArrayHasKey(7, $result['foobar@foobar.com']->phones->toArray());
$this->assertArrayHasKey(8, $result['foobar@foobar.com']->phones->toArray());
$this->assertArrayHasKey(9, $result['foobar@foobar.com']->phones->toArray());
2011-10-30 01:58:09 +04:00
$this->assertEquals('SELECT u, p FROM '.__NAMESPACE__ . '\DDC1335User u INDEX BY u.email INNER JOIN u.phones p INDEX BY p.id', $dql);
2011-09-24 23:10:10 +04:00
}
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
private function loadFixture()
{
$p1 = array('11 xxxx-xxxx','11 yyyy-yyyy','11 zzzz-zzzz');
$p2 = array('22 xxxx-xxxx','22 yyyy-yyyy','22 zzzz-zzzz');
$p3 = array('33 xxxx-xxxx','33 yyyy-yyyy','33 zzzz-zzzz');
2011-10-30 01:58:09 +04:00
$u1 = new DDC1335User("foo@foo.com", "Foo",$p1);
$u2 = new DDC1335User("bar@bar.com", "Bar",$p2);
$u3 = new DDC1335User("foobar@foobar.com", "Foo Bar",$p3);
2011-09-24 23:10:10 +04:00
$this->_em->persist($u1);
$this->_em->persist($u2);
$this->_em->persist($u3);
$this->_em->flush();
$this->_em->clear();
2011-09-24 01:10:58 +04:00
}
}
/**
* @Entity
*/
2011-10-30 01:58:09 +04:00
class DDC1335User
2011-09-24 01:10:58 +04:00
{
/**
* @Id @Column(type="integer")
* @GeneratedValue
*/
2011-09-24 23:10:10 +04:00
public $id;
/**
* @Column(type="string", unique=true)
*/
public $email;
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
/**
* @Column(type="string")
*/
public $name;
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
/**
2011-10-30 01:58:09 +04:00
* @OneToMany(targetEntity="DDC1335Phone", mappedBy="user", cascade={"persist", "remove"})
2011-09-24 23:10:10 +04:00
*/
public $phones;
2011-10-30 01:58:09 +04:00
2011-09-25 19:20:48 +04:00
public function __construct($email, $name, array $numbers = array())
2011-09-24 23:10:10 +04:00
{
$this->name = $name;
$this->email = $email;
$this->phones = new \Doctrine\Common\Collections\ArrayCollection();
2011-10-30 01:58:09 +04:00
2011-09-24 23:10:10 +04:00
foreach ($numbers as $number) {
2011-10-30 01:58:09 +04:00
$this->phones->add(new DDC1335Phone($this,$number));
2011-09-24 23:10:10 +04:00
}
}
}
/**
* @Entity
*/
2011-10-30 01:58:09 +04:00
class DDC1335Phone
2011-09-24 23:10:10 +04:00
{
/**
* @Id
* @Column(name="id", type="integer")
2011-10-30 01:58:09 +04:00
* @GeneratedValue
2011-09-24 23:10:10 +04:00
*/
public $id;
2011-09-24 01:10:58 +04:00
/**
2011-10-30 01:58:09 +04:00
* @Column(name="numericalValue", type="string", nullable = false)
2011-09-24 01:10:58 +04:00
*/
2011-10-30 01:58:09 +04:00
public $numericalValue;
2011-09-24 01:10:58 +04:00
2011-09-24 23:10:10 +04:00
/**
2011-10-30 01:58:09 +04:00
* @ManyToOne(targetEntity="DDC1335User", inversedBy="phones")
2011-09-24 23:10:10 +04:00
* @JoinColumn(name="user_id", referencedColumnName="id", nullable = false)
*/
public $user;
public function __construct($user, $number)
{
$this->user = $user;
2011-10-30 01:58:09 +04:00
$this->numericalValue = $number;
2011-09-24 23:10:10 +04:00
}
2011-09-24 01:10:58 +04:00
}