/bluebox/libraries/doctrine/tests/Import/SchemaTestCase.php

https://github.com/robertleeplummerjr/bluebox · PHP · 113 lines · 65 code · 17 blank · 31 comment · 14 complexity · 67e8792b0f7c4dac324f98587dc742a1 MD5 · raw file

  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.phpdoctrine.org>.
  20. */
  21. /**
  22. * Doctrine_Import_Schema_TestCase
  23. *
  24. * @package Doctrine
  25. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @category Object Relational Mapping
  28. * @link www.phpdoctrine.org
  29. * @since 1.0
  30. * @version $Revision$
  31. */
  32. class Doctrine_Import_Schema_TestCase extends Doctrine_UnitTestCase
  33. {
  34. public $buildSchema;
  35. public $schema;
  36. public function testYmlImport()
  37. {
  38. $path = dirname(__FILE__) . '/import_builder_test';
  39. $import = new Doctrine_Import_Schema();
  40. $import->importSchema('schema.yml', 'yml', $path);
  41. if ( ! file_exists($path . '/SchemaTestUser.php')) {
  42. $this->fail();
  43. }
  44. if ( ! file_exists($path . '/SchemaTestProfile.php')) {
  45. $this->fail();
  46. }
  47. $this->assertEqual(Doctrine::getTable('AliasTest')->getFieldName('test_col'), 'test_col_alias');
  48. Doctrine_Lib::removeDirectories($path);
  49. }
  50. public function testBuildSchema()
  51. {
  52. $schema = new Doctrine_Import_Schema();
  53. $array = $schema->buildSchema('schema.yml', 'yml');
  54. $model = $array['SchemaTestUser'];
  55. $this->assertTrue(array_key_exists('connection', $model));
  56. $this->assertTrue(array_key_exists('className', $model));
  57. $this->assertTrue(array_key_exists('tableName', $model));
  58. $this->assertTrue(array_key_exists('columns', $model) && is_array($model['columns']));
  59. $this->assertTrue(array_key_exists('relations', $model) && is_array($model['relations']));
  60. $this->assertTrue(array_key_exists('indexes', $model) && is_array($model['indexes']));
  61. $this->assertTrue(array_key_exists('attributes', $model) && is_array($model['attributes']));
  62. $this->assertTrue(array_key_exists('templates', $model) && is_array($model['columns']));
  63. $this->assertTrue(array_key_exists('actAs', $model) && is_array($model['actAs']));
  64. $this->assertTrue(array_key_exists('options', $model) && is_array($model['options']));
  65. $this->assertTrue(array_key_exists('package', $model));
  66. $this->assertTrue(array_key_exists('inheritance', $model) && is_array($model['inheritance']));
  67. $this->assertTrue(array_key_exists('detect_relations', $model) && is_bool($model['detect_relations']));
  68. $this->assertEqual($array['AliasTest']['columns']['test_col']['name'], 'test_col as test_col_alias');
  69. }
  70. public function testSchemaRelationshipCompletion()
  71. {
  72. $this->buildSchema = new Doctrine_Import_Schema();
  73. $this->schema = $this->buildSchema->buildSchema('schema.yml', 'yml');
  74. foreach ($this->schema as $name => $properties) {
  75. foreach ($properties['relations'] as $alias => $relation) {
  76. if ( ! $this->_verifyMultiDirectionalRelationship($name, $alias, $relation)) {
  77. $this->fail();
  78. return false;
  79. }
  80. }
  81. }
  82. $this->pass();
  83. }
  84. protected function _verifyMultiDirectionalRelationship($class, $relationAlias, $relation)
  85. {
  86. $foreignClass = $relation['class'];
  87. $foreignAlias = isset($relation['foreignAlias']) ? $relation['foreignAlias']:$class;
  88. $foreignClassRelations = $this->schema[$foreignClass]['relations'];
  89. // Check to see if the foreign class has the opposite end defined for the class/foreignAlias
  90. if (isset($foreignClassRelations[$foreignAlias])) {
  91. return true;
  92. } else {
  93. return false;
  94. }
  95. }
  96. }