QueryTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace yiiunit\extensions\mongodb\file;
  3. use yii\mongodb\file\Download;
  4. use yii\mongodb\file\Query;
  5. use yiiunit\extensions\mongodb\TestCase;
  6. /**
  7. * @group file
  8. */
  9. class QueryTest extends TestCase
  10. {
  11. protected function setUp()
  12. {
  13. parent::setUp();
  14. $this->setUpTestRows();
  15. }
  16. protected function tearDown()
  17. {
  18. $this->dropFileCollection();
  19. parent::tearDown();
  20. }
  21. /**
  22. * Sets up test rows.
  23. */
  24. protected function setUpTestRows()
  25. {
  26. $collection = $this->getConnection()->getFileCollection();
  27. for ($i = 1; $i <= 10; $i++) {
  28. $collection->insertFileContent('content' . $i, [
  29. 'filename' => 'name' . $i,
  30. 'file_index' => $i,
  31. ]);
  32. }
  33. }
  34. // Tests :
  35. public function testAll()
  36. {
  37. $connection = $this->getConnection();
  38. $query = new Query();
  39. $rows = $query->from('fs')->all($connection);
  40. $this->assertEquals(10, count($rows));
  41. }
  42. public function testOne()
  43. {
  44. $connection = $this->getConnection();
  45. $query = new Query();
  46. $row = $query->from('fs')->one($connection);
  47. $this->assertTrue(is_array($row));
  48. $this->assertTrue($row['file'] instanceof Download);
  49. }
  50. public function testDirectMatch()
  51. {
  52. $connection = $this->getConnection();
  53. $query = new Query();
  54. $rows = $query->from('fs')
  55. ->where(['file_index' => 5])
  56. ->all($connection);
  57. $this->assertEquals(1, count($rows));
  58. $file = $rows[0];
  59. $this->assertEquals('name5', $file['filename']);
  60. }
  61. }