BatchQueryResultTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace yiiunit\extensions\mongodb;
  3. use yii\mongodb\BatchQueryResult;
  4. use yii\mongodb\Query;
  5. use yiiunit\extensions\mongodb\data\ar\ActiveRecord;
  6. use yiiunit\extensions\mongodb\data\ar\Customer;
  7. use yiiunit\extensions\mongodb\data\ar\CustomerOrder;
  8. class BatchQueryResultTest extends TestCase
  9. {
  10. protected function setUp()
  11. {
  12. parent::setUp();
  13. ActiveRecord::$db = $this->getConnection();
  14. $this->setUpTestRows();
  15. }
  16. protected function tearDown()
  17. {
  18. $this->dropCollection(Customer::collectionName());
  19. $this->dropCollection(CustomerOrder::collectionName());
  20. parent::tearDown();
  21. }
  22. /**
  23. * Sets up test rows.
  24. */
  25. protected function setUpTestRows()
  26. {
  27. $customers = [];
  28. for ($i = 1; $i <= 9; $i++) {
  29. $customers[] = [
  30. 'name' => 'name' . $i,
  31. 'email' => 'email' . $i,
  32. 'address' => 'address' . $i,
  33. 'status' => $i,
  34. ];
  35. }
  36. $customerCollection = $this->getConnection()->getCollection('customer');
  37. $customers = $customerCollection->batchInsert($customers);
  38. $customerOrders = [];
  39. foreach ($customers as $i => $customer) {
  40. $customerOrders[] = [
  41. 'customer_id' => $customer['_id'],
  42. 'number' => $customer['status'],
  43. ];
  44. $customerOrders[] = [
  45. 'customer_id' => $customer['_id'],
  46. 'number' => $customer['status'] + 100,
  47. ];
  48. }
  49. $customerOrderCollection = $this->getConnection()->getCollection('customer_order');
  50. $customerOrderCollection->batchInsert($customerOrders);
  51. }
  52. // Tests :
  53. public function testQuery()
  54. {
  55. $db = $this->getConnection();
  56. // initialize property test
  57. $query = new Query();
  58. $query->from('customer')->orderBy('id');
  59. $result = $query->batch(2, $db);
  60. $this->assertTrue($result instanceof BatchQueryResult);
  61. $this->assertEquals(2, $result->batchSize);
  62. $this->assertTrue($result->query === $query);
  63. // normal query
  64. $query = new Query();
  65. $query->from('customer');
  66. $allRows = [];
  67. $batch = $query->batch(2, $db);
  68. foreach ($batch as $rows) {
  69. $allRows = array_merge($allRows, $rows);
  70. }
  71. $this->assertEquals(9, count($allRows));
  72. // sorted query
  73. $query = new Query();
  74. $query->from('customer')->orderBy('name');
  75. $allRows = [];
  76. $batch = $query->batch(2, $db);
  77. foreach ($batch as $rows) {
  78. $allRows = array_merge($allRows, $rows);
  79. }
  80. $this->assertEquals(9, count($allRows));
  81. $this->assertEquals('name1', $allRows[0]['name']);
  82. $this->assertEquals('name2', $allRows[1]['name']);
  83. $this->assertEquals('name3', $allRows[2]['name']);
  84. // rewind
  85. $allRows = [];
  86. foreach ($batch as $rows) {
  87. $allRows = array_merge($allRows, $rows);
  88. }
  89. $this->assertEquals(9, count($allRows));
  90. // reset
  91. $batch->reset();
  92. // empty query
  93. $query = new Query();
  94. $query->from('customer')->where(['name' => 'unexistingName']);
  95. $allRows = [];
  96. $batch = $query->batch(2, $db);
  97. foreach ($batch as $rows) {
  98. $allRows = array_merge($allRows, $rows);
  99. }
  100. $this->assertEquals(0, count($allRows));
  101. // query with index
  102. $query = new Query();
  103. $query->from('customer')->indexBy('name');
  104. $allRows = [];
  105. foreach ($query->batch(2, $db) as $rows) {
  106. $allRows = array_merge($allRows, $rows);
  107. }
  108. $this->assertEquals(9, count($allRows));
  109. $this->assertEquals('address1', $allRows['name1']['address']);
  110. $this->assertEquals('address2', $allRows['name2']['address']);
  111. $this->assertEquals('address3', $allRows['name3']['address']);
  112. // each
  113. $query = new Query();
  114. $query->from('customer')->orderBy('name');
  115. $allRows = [];
  116. foreach ($query->each(100, $db) as $rows) {
  117. $allRows[] = $rows;
  118. }
  119. $this->assertEquals(9, count($allRows));
  120. $this->assertEquals('name1', $allRows[0]['name']);
  121. $this->assertEquals('name2', $allRows[1]['name']);
  122. $this->assertEquals('name3', $allRows[2]['name']);
  123. // each with key
  124. $query = new Query();
  125. $query->from('customer')->orderBy('name')->indexBy('name');
  126. $allRows = [];
  127. foreach ($query->each(100, $db) as $key => $row) {
  128. $allRows[$key] = $row;
  129. }
  130. $this->assertEquals(9, count($allRows));
  131. $this->assertEquals('address1', $allRows['name1']['address']);
  132. $this->assertEquals('address2', $allRows['name2']['address']);
  133. $this->assertEquals('address3', $allRows['name3']['address']);
  134. }
  135. public function testActiveQuery()
  136. {
  137. $db = $this->getConnection();
  138. $query = Customer::find()->orderBy('id');
  139. $customers = [];
  140. foreach ($query->batch(2, $db) as $models) {
  141. $customers = array_merge($customers, $models);
  142. }
  143. $this->assertEquals(9, count($customers));
  144. $this->assertEquals('name1', $customers[0]->name);
  145. $this->assertEquals('name2', $customers[1]->name);
  146. $this->assertEquals('name3', $customers[2]->name);
  147. // batch with eager loading
  148. $query = Customer::find()->with('orders')->orderBy('id');
  149. $customers = [];
  150. foreach ($query->batch(2, $db) as $models) {
  151. $customers = array_merge($customers, $models);
  152. foreach ($models as $model) {
  153. $this->assertTrue($model->isRelationPopulated('orders'));
  154. }
  155. }
  156. $this->assertEquals(9, count($customers));
  157. $this->assertEquals(2, count($customers[0]->orders));
  158. $this->assertEquals(2, count($customers[1]->orders));
  159. $this->assertEquals(2, count($customers[2]->orders));
  160. }
  161. }