CollectionTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace yiiunit\extensions\mongodb\file;
  3. use MongoDB\BSON\ObjectID;
  4. use yii\mongodb\file\Cursor;
  5. use yii\mongodb\file\Download;
  6. use yiiunit\extensions\mongodb\TestCase;
  7. /**
  8. * @group file
  9. */
  10. class CollectionTest extends TestCase
  11. {
  12. protected function tearDown()
  13. {
  14. $this->dropFileCollection('fs');
  15. parent::tearDown();
  16. }
  17. // Tests :
  18. public function testGetChunkCollection()
  19. {
  20. $collection = $this->getConnection()->getFileCollection();
  21. $chunkCollection = $collection->getChunkCollection();
  22. $this->assertTrue($chunkCollection instanceof \yii\mongodb\Collection);
  23. $this->assertTrue($chunkCollection->database instanceof \yii\mongodb\Database);
  24. }
  25. public function testGetFileCollection()
  26. {
  27. $collection = $this->getConnection()->getFileCollection();
  28. $fileCollection = $collection->getFileCollection();
  29. $this->assertTrue($fileCollection instanceof \yii\mongodb\Collection);
  30. $this->assertTrue($fileCollection->database instanceof \yii\mongodb\Database);
  31. }
  32. public function testEnsureIndexes()
  33. {
  34. $collection = $this->getConnection()->getFileCollection();
  35. $collection->ensureIndexes();
  36. $this->assertCount(2, $collection->listIndexes());
  37. $this->assertCount(2, $collection->getChunkCollection()->listIndexes());
  38. $collection->dropAllIndexes();
  39. $collection->ensureIndexes();
  40. $this->assertCount(1, $collection->listIndexes());
  41. $collection->ensureIndexes(true);
  42. $this->assertCount(2, $collection->listIndexes());
  43. }
  44. public function testFind()
  45. {
  46. $collection = $this->getConnection()->getFileCollection();
  47. $cursor = $collection->find();
  48. $this->assertTrue($cursor instanceof Cursor);
  49. }
  50. public function testInsertFile()
  51. {
  52. $collection = $this->getConnection()->getFileCollection();
  53. $filename = __FILE__;
  54. $id = $collection->insertFile($filename);
  55. $this->assertTrue($id instanceof ObjectID);
  56. $files = $this->findAll($collection);
  57. $this->assertEquals(1, count($files));
  58. $file = $files[0];
  59. $this->assertEquals(basename($filename), $file['filename']);
  60. $this->assertEquals(filesize($filename), $file['length']);
  61. }
  62. public function testInsertFileContent()
  63. {
  64. $collection = $this->getConnection()->getFileCollection();
  65. $bytes = 'Test file content';
  66. $id = $collection->insertFileContent($bytes);
  67. $this->assertTrue($id instanceof ObjectID);
  68. $files = $this->findAll($collection);
  69. $this->assertEquals(1, count($files));
  70. /* @var $file Download */
  71. $file = $files[0];
  72. $this->assertTrue($file['file'] instanceof Download);
  73. $this->assertEquals($bytes, $file['file']->getBytes());
  74. }
  75. /**
  76. * @depends testInsertFileContent
  77. */
  78. public function testGet()
  79. {
  80. $collection = $this->getConnection()->getFileCollection();
  81. $bytes = 'Test file content';
  82. $id = $collection->insertFileContent($bytes);
  83. $file = $collection->get($id);
  84. $this->assertTrue($file instanceof Download);
  85. $this->assertEquals($bytes, $file->getBytes());
  86. }
  87. /**
  88. * @depends testGet
  89. */
  90. public function testDeleteFile()
  91. {
  92. $collection = $this->getConnection()->getFileCollection();
  93. $bytes = 'Test file content';
  94. $id = $collection->insertFileContent($bytes);
  95. $this->assertTrue($collection->delete($id));
  96. $file = $collection->get($id);
  97. $this->assertNull($file);
  98. }
  99. /**
  100. * @depends testInsertFileContent
  101. */
  102. public function testRemove()
  103. {
  104. $collection = $this->getConnection()->getFileCollection();
  105. for ($i = 1; $i <=10; $i++) {
  106. $bytes = 'Test file content ' . $i;
  107. $collection->insertFileContent($bytes, [
  108. 'index' => $i
  109. ], ['chunkSize' => 15]);
  110. }
  111. $this->assertEquals(1, $collection->remove(['index' => ['$in' =>[1, 2, 3]]], ['limit' => 1]));
  112. $this->assertEquals(9, $collection->count());
  113. $this->assertEquals(18, $collection->getChunkCollection()->count());
  114. $this->assertEquals(3, $collection->remove(['index' => ['$in' =>[5, 7, 9]]]));
  115. $this->assertEquals(6, $collection->count());
  116. $this->assertEquals(12, $collection->getChunkCollection()->count());
  117. $this->assertEquals(6, $collection->remove());
  118. $this->assertEquals(0, $collection->count());
  119. $this->assertEquals(0, $collection->getChunkCollection()->count());
  120. }
  121. }