MigrationTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace yiiunit\extensions\mongodb;
  3. use MongoDB\BSON\ObjectID;
  4. class MigrationTest extends TestCase
  5. {
  6. protected function tearDown()
  7. {
  8. $this->dropCollection('customer');
  9. parent::tearDown();
  10. }
  11. /**
  12. * @return Migration migration instance.
  13. */
  14. protected function createMigration()
  15. {
  16. return new Migration(['db' => $this->getConnection()]);
  17. }
  18. // Tests :
  19. public function testCollectionOperations()
  20. {
  21. $migration = $this->createMigration();
  22. $migration->createCollection('customer');
  23. $this->assertNotEmpty($migration->db->getDatabase()->listCollections(['name' => 'customer']));
  24. $migration->dropCollection('customer');
  25. $this->assertEmpty($migration->db->getDatabase()->listCollections(['name' => 'customer']));
  26. }
  27. public function testIndexOperations()
  28. {
  29. $migration = $this->createMigration();
  30. $migration->createIndexes('customer', [
  31. ['key' => 'name']
  32. ]);
  33. $this->assertCount(2, $migration->db->getCollection('customer')->listIndexes());
  34. $migration->dropIndexes('customer', '*');
  35. $this->assertCount(1, $migration->db->getCollection('customer')->listIndexes());
  36. $migration->createIndex('customer', ['name']);
  37. $this->assertCount(2, $migration->db->getCollection('customer')->listIndexes());
  38. $migration->dropIndex('customer', ['name']);
  39. $this->assertCount(1, $migration->db->getCollection('customer')->listIndexes());
  40. $migration->createIndex('customer', ['name']);
  41. $migration->dropAllIndexes('customer');
  42. $this->assertCount(1, $migration->db->getCollection('customer')->listIndexes());
  43. }
  44. public function testDataOperations()
  45. {
  46. $migration = $this->createMigration();
  47. $id = $migration->insert('customer', ['name' => 'John Doe']);
  48. $this->assertTrue($id instanceof ObjectID);
  49. $migration->update('customer', ['_id' => $id], ['name' => 'new name']);
  50. list($row) = $this->findAll($migration->db->getCollection('customer'));
  51. $this->assertEquals('new name', $row['name']);
  52. $migration->save('customer', ['_id' => $id, 'name' => 'save']);
  53. list($row) = $this->findAll($migration->db->getCollection('customer'));
  54. $this->assertEquals('save', $row['name']);
  55. $rows = $migration->batchInsert('customer', [
  56. ['name' => 'customer 1'],
  57. ['name' => 'customer 2'],
  58. ]);
  59. $this->assertCount(2, $rows);
  60. $this->assertEquals(3, $migration->remove('customer', []));
  61. }
  62. /**
  63. * @depends testCollectionOperations
  64. */
  65. public function testCommandOutput()
  66. {
  67. $migration = $this->createMigration();
  68. $migration->compact = false;
  69. $migration->createCollection('customer');
  70. $this->assertCount(2, $migration->logs);
  71. $migration->compact = true;
  72. $migration->logs = [];
  73. $migration->dropCollection('customer');
  74. $this->assertEmpty($migration->logs);
  75. }
  76. }
  77. /**
  78. * Migration is mock of [[\yii\mongodb\Migration]] used for the unit tests.
  79. */
  80. class Migration extends \yii\mongodb\Migration
  81. {
  82. /**
  83. * @var array list of log messages
  84. */
  85. public $logs = [];
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function up()
  90. {
  91. // blank
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function down()
  97. {
  98. // blank
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. protected function log($string)
  104. {
  105. $this->logs[] = $string;
  106. }
  107. }