IndexTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Schema;
  3. require_once __DIR__ . '/../../TestInit.php';
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\DBAL\Schema\Table;
  6. use Doctrine\DBAL\Schema\Column;
  7. use Doctrine\DBAL\Schema\Index;
  8. class IndexTest extends \PHPUnit_Framework_TestCase
  9. {
  10. public function createIndex($unique=false, $primary=false)
  11. {
  12. return new Index("foo", array("bar", "baz"), $unique, $primary);
  13. }
  14. public function testCreateIndex()
  15. {
  16. $idx = $this->createIndex();
  17. $this->assertEquals("foo", $idx->getName());
  18. $columns = $idx->getColumns();
  19. $this->assertEquals(2, count($columns));
  20. $this->assertEquals(array("bar", "baz"), $columns);
  21. $this->assertFalse($idx->isUnique());
  22. $this->assertFalse($idx->isPrimary());
  23. }
  24. public function testCreatePrimary()
  25. {
  26. $idx = $this->createIndex(false, true);
  27. $this->assertTrue($idx->isUnique());
  28. $this->assertTrue($idx->isPrimary());
  29. }
  30. public function testCreateUnique()
  31. {
  32. $idx = $this->createIndex(true, false);
  33. $this->assertTrue($idx->isUnique());
  34. $this->assertFalse($idx->isPrimary());
  35. }
  36. /**
  37. * @group DBAL-50
  38. */
  39. public function testFullfilledByUnique()
  40. {
  41. $idx1 = $this->createIndex(true, false);
  42. $idx2 = $this->createIndex(true, false);
  43. $idx3 = $this->createIndex();
  44. $this->assertTrue($idx1->isFullfilledBy($idx2));
  45. $this->assertFalse($idx1->isFullfilledBy($idx3));
  46. }
  47. /**
  48. * @group DBAL-50
  49. */
  50. public function testFullfilledByPrimary()
  51. {
  52. $idx1 = $this->createIndex(true, true);
  53. $idx2 = $this->createIndex(true, true);
  54. $idx3 = $this->createIndex(true, false);
  55. $this->assertTrue($idx1->isFullfilledBy($idx2));
  56. $this->assertFalse($idx1->isFullfilledBy($idx3));
  57. }
  58. /**
  59. * @group DBAL-50
  60. */
  61. public function testFullfilledByIndex()
  62. {
  63. $idx1 = $this->createIndex();
  64. $idx2 = $this->createIndex();
  65. $pri = $this->createIndex(true, true);
  66. $uniq = $this->createIndex(true);
  67. $this->assertTrue($idx1->isFullfilledBy($idx2));
  68. $this->assertTrue($idx1->isFullfilledBy($pri));
  69. $this->assertTrue($idx1->isFullfilledBy($uniq));
  70. }
  71. /**
  72. * @group DBAL-220
  73. */
  74. public function testFlags()
  75. {
  76. $idx1 = $this->createIndex();
  77. $this->assertFalse($idx1->hasFlag('clustered'));
  78. $idx1->addFlag('clustered');
  79. $this->assertTrue($idx1->hasFlag('clustered'));
  80. $this->assertTrue($idx1->hasFlag('CLUSTERED'));
  81. $idx1->removeFlag('clustered');
  82. $this->assertFalse($idx1->hasFlag('clustered'));
  83. }
  84. /**
  85. * @group DBAL-285
  86. */
  87. public function testIndexQuotes()
  88. {
  89. $index = new Index("foo", array("`bar`", "`baz`"));
  90. $this->assertTrue($index->spansColumns(array("bar", "baz")));
  91. $this->assertTrue($index->hasColumnAtPosition("bar", 0));
  92. $this->assertTrue($index->hasColumnAtPosition("baz", 1));
  93. $this->assertFalse($index->hasColumnAtPosition("bar", 1));
  94. $this->assertFalse($index->hasColumnAtPosition("baz", 0));
  95. }
  96. }