PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/test/testsuite/generator/behavior/nestedset/NestedSetBehaviorTest.php

http://github.com/propelorm/Propel
PHP | 125 lines | 90 code | 19 blank | 16 comment | 0 complexity | 6d74ce695d5b95859ef31729d47d7f43 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once dirname(__FILE__) . '/../../../../tools/helpers/bookstore/BookstoreTestBase.php';
  10. require_once dirname(__FILE__) . '/../../../../../generator/lib/util/PropelQuickBuilder.php';
  11. require_once dirname(__FILE__) . '/../../../../../generator/lib/behavior/nestedset/NestedSetBehavior.php';
  12. require_once dirname(__FILE__) . '/../../../../../generator/lib/platform/MysqlPlatform.php';
  13. /**
  14. * Tests for NestedSetBehavior class
  15. *
  16. * @author François Zaninotto
  17. * @package generator.behavior.nestedset
  18. */
  19. class NestedSetBehaviorTest extends BookstoreTestBase
  20. {
  21. public function testDefault()
  22. {
  23. $table9 = Table9Peer::getTableMap();
  24. $this->assertEquals(count($table9->getColumns()), 5, 'nested_set adds three column by default');
  25. $this->assertTrue(method_exists('Table9', 'getTreeLeft'), 'nested_set adds a tree_left column by default');
  26. $this->assertTrue(method_exists('Table9', 'getLeftValue'), 'nested_set maps the left_value getter with the tree_left column');
  27. $this->assertTrue(method_exists('Table9', 'getTreeRight'), 'nested_set adds a tree_right column by default');
  28. $this->assertTrue(method_exists('Table9', 'getRightValue'), 'nested_set maps the right_value getter with the tree_right column');
  29. $this->assertTrue(method_exists('Table9', 'getTreeLevel'), 'nested_set adds a tree_level column by default');
  30. $this->assertTrue(method_exists('Table9', 'getLevel'), 'nested_set maps the level getter with the tree_level column');
  31. $this->assertFalse(method_exists('Table9', 'getTreeScope'), 'nested_set does not add a tree_scope column by default');
  32. $this->assertFalse(method_exists('Table9', 'getScopeValue'), 'nested_set does not map the scope_value getter with the tree_scope column by default');
  33. }
  34. public function testParameters()
  35. {
  36. $table10 = Table10Peer::getTableMap();
  37. $this->assertEquals(count($table10->getColumns()), 6, 'nested_set does not add columns when they already exist');
  38. $this->assertTrue(method_exists('Table10', 'getLeftValue'), 'nested_set maps the left_value getter with the tree_left column');
  39. $this->assertTrue(method_exists('Table10', 'getRightValue'), 'nested_set maps the right_value getter with the tree_right column');
  40. $this->assertTrue(method_exists('Table10', 'getLevel'), 'nested_set maps the level getter with the tree_level column');
  41. $this->assertTrue(method_exists('Table10', 'getScopeValue'), 'nested_set maps the scope_value getter with the tree_scope column when the use_scope parameter is true');
  42. }
  43. public function testGeneratedSqlForMySQL()
  44. {
  45. $schema = <<<XML
  46. <database name="default">
  47. <table name="thread">
  48. <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
  49. </table>
  50. <table name="post">
  51. <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
  52. <column name="body" type="VARCHAR" required="true" primaryString="true" />
  53. <foreign-key foreignTable="thread" onDelete="cascade">
  54. <reference local="thread_id" foreign="id" />
  55. </foreign-key>
  56. <behavior name="nested_set">
  57. <parameter name="use_scope" value="true" />
  58. <parameter name="scope_column" value="thread_id" />
  59. </behavior>
  60. <vendor type="mysql">
  61. <parameter name="Engine" value="InnoDB"/>
  62. </vendor>
  63. </table>
  64. </database>
  65. XML;
  66. $expectedSql = <<<SQL
  67. # This is a fix for InnoDB in MySQL >= 4.1.x
  68. # It "suspends judgement" for fkey relationships until are tables are set.
  69. SET FOREIGN_KEY_CHECKS = 0;
  70. -- ---------------------------------------------------------------------
  71. -- thread
  72. -- ---------------------------------------------------------------------
  73. DROP TABLE IF EXISTS `thread`;
  74. CREATE TABLE `thread`
  75. (
  76. `id` INTEGER NOT NULL AUTO_INCREMENT,
  77. PRIMARY KEY (`id`)
  78. ) ENGINE=MyISAM;
  79. -- ---------------------------------------------------------------------
  80. -- post
  81. -- ---------------------------------------------------------------------
  82. DROP TABLE IF EXISTS `post`;
  83. CREATE TABLE `post`
  84. (
  85. `id` INTEGER NOT NULL AUTO_INCREMENT,
  86. `body` VARCHAR(255) NOT NULL,
  87. `tree_left` INTEGER,
  88. `tree_right` INTEGER,
  89. `tree_level` INTEGER,
  90. `thread_id` INTEGER,
  91. PRIMARY KEY (`id`),
  92. INDEX `post_FI_1` (`thread_id`),
  93. CONSTRAINT `post_FK_1`
  94. FOREIGN KEY (`thread_id`)
  95. REFERENCES `thread` (`id`)
  96. ON DELETE CASCADE
  97. ) ENGINE=InnoDB;
  98. # This restores the fkey checks, after having unset them earlier
  99. SET FOREIGN_KEY_CHECKS = 1;
  100. SQL;
  101. $builder = new PropelQuickBuilder();
  102. $builder->setPlatform(new MysqlPlatform());
  103. $builder->setSchema($schema);
  104. $this->assertEquals($expectedSql, $builder->getSQL());
  105. }
  106. }