RegistryTest.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /*
  3. * This file is part of the Doctrine Bundle
  4. *
  5. * The code was originally distributed inside the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien@symfony.com>
  8. * (c) Doctrine Project, Benjamin Eberlei <kontakt@beberlei.de>
  9. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. namespace Doctrine\Bundle\DoctrineBundle\Tests;
  14. use Doctrine\Bundle\DoctrineBundle\Registry;
  15. class RegistryTest extends TestCase
  16. {
  17. public function testGetDefaultConnectionName()
  18. {
  19. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  20. $registry = new Registry($container, array(), array(), 'default', 'default');
  21. $this->assertEquals('default', $registry->getDefaultConnectionName());
  22. }
  23. public function testGetDefaultEntityManagerName()
  24. {
  25. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  26. $registry = new Registry($container, array(), array(), 'default', 'default');
  27. $this->assertEquals('default', $registry->getDefaultEntityManagerName());
  28. }
  29. public function testGetDefaultConnection()
  30. {
  31. $conn = $this->getMock('Doctrine\DBAL\Connection', array(), array(), '', false);
  32. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  33. $container->expects($this->once())
  34. ->method('get')
  35. ->with($this->equalTo('doctrine.dbal.default_connection'))
  36. ->will($this->returnValue($conn));
  37. $registry = new Registry($container, array('default' => 'doctrine.dbal.default_connection'), array(), 'default', 'default');
  38. $this->assertSame($conn, $registry->getConnection());
  39. }
  40. public function testGetConnection()
  41. {
  42. $conn = $this->getMock('Doctrine\DBAL\Connection', array(), array(), '', false);
  43. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  44. $container->expects($this->once())
  45. ->method('get')
  46. ->with($this->equalTo('doctrine.dbal.default_connection'))
  47. ->will($this->returnValue($conn));
  48. $registry = new Registry($container, array('default' => 'doctrine.dbal.default_connection'), array(), 'default', 'default');
  49. $this->assertSame($conn, $registry->getConnection('default'));
  50. }
  51. public function testGetUnknownConnection()
  52. {
  53. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  54. $registry = new Registry($container, array(), array(), 'default', 'default');
  55. $this->setExpectedException('InvalidArgumentException', 'Doctrine ORM Connection named "default" does not exist.');
  56. $registry->getConnection('default');
  57. }
  58. public function testGetConnectionNames()
  59. {
  60. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  61. $registry = new Registry($container, array('default' => 'doctrine.dbal.default_connection'), array(), 'default', 'default');
  62. $this->assertEquals(array('default' => 'doctrine.dbal.default_connection'), $registry->getConnectionNames());
  63. }
  64. public function testGetDefaultEntityManager()
  65. {
  66. $em = new \stdClass();
  67. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  68. $container->expects($this->once())
  69. ->method('get')
  70. ->with($this->equalTo('doctrine.orm.default_entity_manager'))
  71. ->will($this->returnValue($em));
  72. $registry = new Registry($container, array(), array('default' => 'doctrine.orm.default_entity_manager'), 'default', 'default');
  73. $this->assertSame($em, $registry->getEntityManager());
  74. }
  75. public function testGetEntityManager()
  76. {
  77. $em = new \stdClass();
  78. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  79. $container->expects($this->once())
  80. ->method('get')
  81. ->with($this->equalTo('doctrine.orm.default_entity_manager'))
  82. ->will($this->returnValue($em));
  83. $registry = new Registry($container, array(), array('default' => 'doctrine.orm.default_entity_manager'), 'default', 'default');
  84. $this->assertSame($em, $registry->getEntityManager('default'));
  85. }
  86. public function testGetUnknownEntityManager()
  87. {
  88. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  89. $registry = new Registry($container, array(), array(), 'default', 'default');
  90. $this->setExpectedException('InvalidArgumentException', 'Doctrine ORM Manager named "default" does not exist.');
  91. $registry->getEntityManager('default');
  92. }
  93. public function testResetDefaultEntityManager()
  94. {
  95. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  96. $container->expects($this->once())
  97. ->method('set')
  98. ->with($this->equalTo('doctrine.orm.default_entity_manager'), $this->equalTo(null));
  99. $registry = new Registry($container, array(), array('default' => 'doctrine.orm.default_entity_manager'), 'default', 'default');
  100. $registry->resetEntityManager();
  101. }
  102. public function testResetEntityManager()
  103. {
  104. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  105. $container->expects($this->once())
  106. ->method('set')
  107. ->with($this->equalTo('doctrine.orm.default_entity_manager'), $this->equalTo(null));
  108. $registry = new Registry($container, array(), array('default' => 'doctrine.orm.default_entity_manager'), 'default', 'default');
  109. $registry->resetEntityManager('default');
  110. }
  111. public function testResetUnknownEntityManager()
  112. {
  113. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  114. $registry = new Registry($container, array(), array(), 'default', 'default');
  115. $this->setExpectedException('InvalidArgumentException', 'Doctrine ORM Manager named "default" does not exist.');
  116. $registry->resetEntityManager('default');
  117. }
  118. }