DoctrineExtractorTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo;
  11. use Doctrine\DBAL\Types\Type as DBALType;
  12. use Doctrine\ORM\EntityManager;
  13. use Doctrine\ORM\Tools\Setup;
  14. use PHPUnit\Framework\TestCase;
  15. use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
  16. use Symfony\Component\PropertyInfo\Type;
  17. /**
  18. * @author Kévin Dunglas <dunglas@gmail.com>
  19. */
  20. class DoctrineExtractorTest extends TestCase
  21. {
  22. /**
  23. * @var DoctrineExtractor
  24. */
  25. private $extractor;
  26. protected function setUp()
  27. {
  28. $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'), true);
  29. $entityManager = EntityManager::create(array('driver' => 'pdo_sqlite'), $config);
  30. if (!DBALType::hasType('foo')) {
  31. DBALType::addType('foo', 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineFooType');
  32. $entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('custom_foo', 'foo');
  33. }
  34. $this->extractor = new DoctrineExtractor($entityManager->getMetadataFactory());
  35. }
  36. public function testGetProperties()
  37. {
  38. $this->assertEquals(
  39. array(
  40. 'id',
  41. 'guid',
  42. 'time',
  43. 'json',
  44. 'simpleArray',
  45. 'float',
  46. 'decimal',
  47. 'bool',
  48. 'binary',
  49. 'customFoo',
  50. 'bigint',
  51. 'foo',
  52. 'bar',
  53. 'indexedBar',
  54. 'indexedFoo',
  55. ),
  56. $this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy')
  57. );
  58. }
  59. public function testGetPropertiesWithEmbedded()
  60. {
  61. if (!class_exists('Doctrine\ORM\Mapping\Embedded')) {
  62. $this->markTestSkipped('@Embedded is not available in Doctrine ORM lower than 2.5.');
  63. }
  64. $this->assertEquals(
  65. array(
  66. 'id',
  67. 'embedded',
  68. ),
  69. $this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded')
  70. );
  71. }
  72. /**
  73. * @dataProvider typesProvider
  74. */
  75. public function testExtract($property, array $type = null)
  76. {
  77. $this->assertEquals($type, $this->extractor->getTypes('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy', $property, array()));
  78. }
  79. public function testExtractWithEmbedded()
  80. {
  81. if (!class_exists('Doctrine\ORM\Mapping\Embedded')) {
  82. $this->markTestSkipped('@Embedded is not available in Doctrine ORM lower than 2.5.');
  83. }
  84. $expectedTypes = array(new Type(
  85. Type::BUILTIN_TYPE_OBJECT,
  86. false,
  87. 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineEmbeddable'
  88. ));
  89. $actualTypes = $this->extractor->getTypes(
  90. 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded',
  91. 'embedded',
  92. array()
  93. );
  94. $this->assertEquals($expectedTypes, $actualTypes);
  95. }
  96. public function typesProvider()
  97. {
  98. return array(
  99. array('id', array(new Type(Type::BUILTIN_TYPE_INT))),
  100. array('guid', array(new Type(Type::BUILTIN_TYPE_STRING))),
  101. array('bigint', array(new Type(Type::BUILTIN_TYPE_STRING))),
  102. array('float', array(new Type(Type::BUILTIN_TYPE_FLOAT))),
  103. array('decimal', array(new Type(Type::BUILTIN_TYPE_STRING))),
  104. array('bool', array(new Type(Type::BUILTIN_TYPE_BOOL))),
  105. array('binary', array(new Type(Type::BUILTIN_TYPE_RESOURCE))),
  106. array('json', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true))),
  107. array('foo', array(new Type(Type::BUILTIN_TYPE_OBJECT, true, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation'))),
  108. array('bar', array(new Type(
  109. Type::BUILTIN_TYPE_OBJECT,
  110. false,
  111. 'Doctrine\Common\Collections\Collection',
  112. true,
  113. new Type(Type::BUILTIN_TYPE_INT),
  114. new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
  115. ))),
  116. array('indexedBar', array(new Type(
  117. Type::BUILTIN_TYPE_OBJECT,
  118. false,
  119. 'Doctrine\Common\Collections\Collection',
  120. true,
  121. new Type(Type::BUILTIN_TYPE_STRING),
  122. new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
  123. ))),
  124. array('indexedFoo', array(new Type(
  125. Type::BUILTIN_TYPE_OBJECT,
  126. false,
  127. 'Doctrine\Common\Collections\Collection',
  128. true,
  129. new Type(Type::BUILTIN_TYPE_STRING),
  130. new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
  131. ))),
  132. array('simpleArray', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)))),
  133. array('customFoo', null),
  134. array('notMapped', null),
  135. );
  136. }
  137. public function testGetPropertiesCatchException()
  138. {
  139. $this->assertNull($this->extractor->getProperties('Not\Exist'));
  140. }
  141. public function testGetTypesCatchException()
  142. {
  143. $this->assertNull($this->extractor->getTypes('Not\Exist', 'baz'));
  144. }
  145. }