3 namespace Doctrine\Tests\ORM\Functional;
5 use Doctrine\Tests\Models\StockExchange\Stock;
6 use Doctrine\Tests\Models\StockExchange\Market;
7 use Doctrine\Tests\Models\StockExchange\Bond;
9 require_once __DIR__ . '/../../TestInit.php';
14 class IndexByAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
17 * @var Doctrine\Tests\Models\StockExchange\Market
23 public function setUp()
25 $this->useModelSet('stockexchange');
30 public function loadFixture()
32 $this->market = new Market("Some Exchange");
33 $stock1 = new Stock("AAPL", 10, $this->market);
34 $stock2 = new Stock("GOOG", 20, $this->market);
36 $this->bond = new Bond("MyBond");
37 $this->bond->addStock($stock1);
38 $this->bond->addStock($stock2);
40 $this->_em->persist($this->market);
41 $this->_em->persist($stock1);
42 $this->_em->persist($stock2);
43 $this->_em->persist($this->bond);
48 public function testManyToOneFinder()
50 /* @var $market Doctrine\Tests\Models\StockExchange\Market */
51 $market = $this->_em->find('Doctrine\Tests\Models\StockExchange\Market', $this->market->getId());
53 $this->assertEquals(2, count($market->stocks));
54 $this->assertTrue(isset($market->stocks['AAPL']), "AAPL symbol has to be key in indexed assocation.");
55 $this->assertTrue(isset($market->stocks['GOOG']), "GOOG symbol has to be key in indexed assocation.");
56 $this->assertEquals("AAPL", $market->stocks['AAPL']->getSymbol());
57 $this->assertEquals("GOOG", $market->stocks['GOOG']->getSymbol());
60 public function testManyToOneDQL()
62 $dql = "SELECT m, s FROM Doctrine\Tests\Models\StockExchange\Market m JOIN m.stocks s WHERE m.id = ?1";
63 $market = $this->_em->createQuery($dql)->setParameter(1, $this->market->getId())->getSingleResult();
65 $this->assertEquals(2, count($market->stocks));
66 $this->assertTrue(isset($market->stocks['AAPL']), "AAPL symbol has to be key in indexed assocation.");
67 $this->assertTrue(isset($market->stocks['GOOG']), "GOOG symbol has to be key in indexed assocation.");
68 $this->assertEquals("AAPL", $market->stocks['AAPL']->getSymbol());
69 $this->assertEquals("GOOG", $market->stocks['GOOG']->getSymbol());
72 public function testManyToMany()
74 $bond = $this->_em->find('Doctrine\Tests\Models\StockExchange\Bond', $this->bond->getId());
76 $this->assertEquals(2, count($bond->stocks));
77 $this->assertTrue(isset($bond->stocks['AAPL']), "AAPL symbol has to be key in indexed assocation.");
78 $this->assertTrue(isset($bond->stocks['GOOG']), "GOOG symbol has to be key in indexed assocation.");
79 $this->assertEquals("AAPL", $bond->stocks['AAPL']->getSymbol());
80 $this->assertEquals("GOOG", $bond->stocks['GOOG']->getSymbol());
83 public function testManytoManyDQL()
85 $dql = "SELECT b, s FROM Doctrine\Tests\Models\StockExchange\Bond b JOIN b.stocks s WHERE b.id = ?1";
86 $bond = $this->_em->createQuery($dql)->setParameter(1, $this->bond->getId())->getSingleResult();
88 $this->assertEquals(2, count($bond->stocks));
89 $this->assertTrue(isset($bond->stocks['AAPL']), "AAPL symbol has to be key in indexed assocation.");
90 $this->assertTrue(isset($bond->stocks['GOOG']), "GOOG symbol has to be key in indexed assocation.");
91 $this->assertEquals("AAPL", $bond->stocks['AAPL']->getSymbol());
92 $this->assertEquals("GOOG", $bond->stocks['GOOG']->getSymbol());
95 public function testDqlOverrideIndexBy()
97 $dql = "SELECT b, s FROM Doctrine\Tests\Models\StockExchange\Bond b JOIN b.stocks s INDEX BY s.id WHERE b.id = ?1";
98 $bond = $this->_em->createQuery($dql)->setParameter(1, $this->bond->getId())->getSingleResult();
100 $this->assertEquals(2, count($bond->stocks));
101 $this->assertFalse(isset($bond->stocks['AAPL']), "AAPL symbol not exists in re-indexed assocation.");
102 $this->assertFalse(isset($bond->stocks['GOOG']), "GOOG symbol not exists in re-indexed assocation.");