ConvertDoctrine1SchemaTest.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\Tests\ORM\Tools;
  22. use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
  23. use Doctrine\ORM\Tools\ConvertDoctrine1Schema;
  24. use Doctrine\Tests\Mocks\MetadataDriverMock;
  25. use Doctrine\Tests\Mocks\DatabasePlatformMock;
  26. use Doctrine\Tests\Mocks\EntityManagerMock;
  27. use Doctrine\Tests\Mocks\ConnectionMock;
  28. use Doctrine\Tests\Mocks\DriverMock;
  29. use Doctrine\Common\EventManager;
  30. use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
  31. require_once __DIR__ . '/../../TestInit.php';
  32. /**
  33. * Test case for converting a Doctrine 1 style schema to Doctrine 2 mapping files
  34. *
  35. * @author Jonathan H. Wage <jonwage@gmail.com>
  36. * @author Roman Borschel <roman@code-factory.org
  37. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  38. * @link http://www.phpdoctrine.org
  39. * @since 2.0
  40. * @version $Revision$
  41. */
  42. class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase
  43. {
  44. protected function _createEntityManager($metadataDriver)
  45. {
  46. $driverMock = new DriverMock();
  47. $config = new \Doctrine\ORM\Configuration();
  48. $config->setProxyDir(__DIR__ . '/../../Proxies');
  49. $config->setProxyNamespace('Doctrine\Tests\Proxies');
  50. $eventManager = new EventManager();
  51. $conn = new ConnectionMock(array(), $driverMock, $config, $eventManager);
  52. $mockDriver = new MetadataDriverMock();
  53. $config->setMetadataDriverImpl($metadataDriver);
  54. return EntityManagerMock::create($conn, $config, $eventManager);
  55. }
  56. public function testTest()
  57. {
  58. if ( ! class_exists('Symfony\Component\Yaml\Yaml', true)) {
  59. $this->markTestSkipped('Please install Symfony YAML Component into the include path of your PHP installation.');
  60. }
  61. $cme = new ClassMetadataExporter();
  62. $converter = new ConvertDoctrine1Schema(__DIR__ . '/doctrine1schema');
  63. $exporter = $cme->getExporter('yml', __DIR__ . '/convert');
  64. $exporter->setOverwriteExistingFiles(true);
  65. $exporter->setMetadata($converter->getMetadata());
  66. $exporter->export();
  67. $this->assertTrue(file_exists(__DIR__ . '/convert/User.dcm.yml'));
  68. $this->assertTrue(file_exists(__DIR__ . '/convert/Profile.dcm.yml'));
  69. $metadataDriver = new \Doctrine\ORM\Mapping\Driver\YamlDriver(__DIR__ . '/convert');
  70. $em = $this->_createEntityManager($metadataDriver);
  71. $cmf = new DisconnectedClassMetadataFactory();
  72. $cmf->setEntityManager($em);
  73. $metadata = $cmf->getAllMetadata();
  74. $profileClass = $cmf->getMetadataFor('Profile');
  75. $userClass = $cmf->getMetadataFor('User');
  76. $this->assertEquals(2, count($metadata));
  77. $this->assertEquals('Profile', $profileClass->name);
  78. $this->assertEquals('User', $userClass->name);
  79. $this->assertEquals(4, count($profileClass->fieldMappings));
  80. $this->assertEquals(5, count($userClass->fieldMappings));
  81. $this->assertEquals('text', $userClass->fieldMappings['clob']['type']);
  82. $this->assertEquals('test_alias', $userClass->fieldMappings['theAlias']['columnName']);
  83. $this->assertEquals('theAlias', $userClass->fieldMappings['theAlias']['fieldName']);
  84. $this->assertEquals('Profile', $profileClass->associationMappings['User']['sourceEntity']);
  85. $this->assertEquals('User', $profileClass->associationMappings['User']['targetEntity']);
  86. $this->assertEquals('username', $userClass->table['uniqueConstraints']['username']['columns'][0]);
  87. }
  88. public function tearDown()
  89. {
  90. @unlink(__DIR__ . '/convert/User.dcm.yml');
  91. @unlink(__DIR__ . '/convert/Profile.dcm.yml');
  92. @rmdir(__DIR__ . '/convert');
  93. }
  94. }