IndexByAssociationTest.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\StockExchange\Stock;
  4. use Doctrine\Tests\Models\StockExchange\Market;
  5. use Doctrine\Tests\Models\StockExchange\Bond;
  6. require_once __DIR__ . '/../../TestInit.php';
  7. /**
  8. * @group DDC-250
  9. */
  10. class IndexByAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
  11. {
  12. /**
  13. * @var Doctrine\Tests\Models\StockExchange\Market
  14. */
  15. private $market;
  16. private $bond;
  17. public function setUp()
  18. {
  19. $this->useModelSet('stockexchange');
  20. parent::setUp();
  21. $this->loadFixture();
  22. }
  23. public function loadFixture()
  24. {
  25. $this->market = new Market("Some Exchange");
  26. $stock1 = new Stock("AAPL", 10, $this->market);
  27. $stock2 = new Stock("GOOG", 20, $this->market);
  28. $this->bond = new Bond("MyBond");
  29. $this->bond->addStock($stock1);
  30. $this->bond->addStock($stock2);
  31. $this->_em->persist($this->market);
  32. $this->_em->persist($stock1);
  33. $this->_em->persist($stock2);
  34. $this->_em->persist($this->bond);
  35. $this->_em->flush();
  36. $this->_em->clear();
  37. }
  38. public function testManyToOneFinder()
  39. {
  40. /* @var $market Doctrine\Tests\Models\StockExchange\Market */
  41. $market = $this->_em->find('Doctrine\Tests\Models\StockExchange\Market', $this->market->getId());
  42. $this->assertEquals(2, count($market->stocks));
  43. $this->assertTrue(isset($market->stocks['AAPL']), "AAPL symbol has to be key in indexed assocation.");
  44. $this->assertTrue(isset($market->stocks['GOOG']), "GOOG symbol has to be key in indexed assocation.");
  45. $this->assertEquals("AAPL", $market->stocks['AAPL']->getSymbol());
  46. $this->assertEquals("GOOG", $market->stocks['GOOG']->getSymbol());
  47. }
  48. public function testManyToOneDQL()
  49. {
  50. $dql = "SELECT m, s FROM Doctrine\Tests\Models\StockExchange\Market m JOIN m.stocks s WHERE m.id = ?1";
  51. $market = $this->_em->createQuery($dql)->setParameter(1, $this->market->getId())->getSingleResult();
  52. $this->assertEquals(2, count($market->stocks));
  53. $this->assertTrue(isset($market->stocks['AAPL']), "AAPL symbol has to be key in indexed assocation.");
  54. $this->assertTrue(isset($market->stocks['GOOG']), "GOOG symbol has to be key in indexed assocation.");
  55. $this->assertEquals("AAPL", $market->stocks['AAPL']->getSymbol());
  56. $this->assertEquals("GOOG", $market->stocks['GOOG']->getSymbol());
  57. }
  58. public function testManyToMany()
  59. {
  60. $bond = $this->_em->find('Doctrine\Tests\Models\StockExchange\Bond', $this->bond->getId());
  61. $this->assertEquals(2, count($bond->stocks));
  62. $this->assertTrue(isset($bond->stocks['AAPL']), "AAPL symbol has to be key in indexed assocation.");
  63. $this->assertTrue(isset($bond->stocks['GOOG']), "GOOG symbol has to be key in indexed assocation.");
  64. $this->assertEquals("AAPL", $bond->stocks['AAPL']->getSymbol());
  65. $this->assertEquals("GOOG", $bond->stocks['GOOG']->getSymbol());
  66. }
  67. public function testManytoManyDQL()
  68. {
  69. $dql = "SELECT b, s FROM Doctrine\Tests\Models\StockExchange\Bond b JOIN b.stocks s WHERE b.id = ?1";
  70. $bond = $this->_em->createQuery($dql)->setParameter(1, $this->bond->getId())->getSingleResult();
  71. $this->assertEquals(2, count($bond->stocks));
  72. $this->assertTrue(isset($bond->stocks['AAPL']), "AAPL symbol has to be key in indexed assocation.");
  73. $this->assertTrue(isset($bond->stocks['GOOG']), "GOOG symbol has to be key in indexed assocation.");
  74. $this->assertEquals("AAPL", $bond->stocks['AAPL']->getSymbol());
  75. $this->assertEquals("GOOG", $bond->stocks['GOOG']->getSymbol());
  76. }
  77. public function testDqlOverrideIndexBy()
  78. {
  79. $dql = "SELECT b, s FROM Doctrine\Tests\Models\StockExchange\Bond b JOIN b.stocks s INDEX BY s.id WHERE b.id = ?1";
  80. $bond = $this->_em->createQuery($dql)->setParameter(1, $this->bond->getId())->getSingleResult();
  81. $this->assertEquals(2, count($bond->stocks));
  82. $this->assertFalse(isset($bond->stocks['AAPL']), "AAPL symbol not exists in re-indexed assocation.");
  83. $this->assertFalse(isset($bond->stocks['GOOG']), "GOOG symbol not exists in re-indexed assocation.");
  84. }
  85. }