ActiveRelationTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace yiiunit\extensions\mongodb;
  3. use yiiunit\extensions\mongodb\data\ar\ActiveRecord;
  4. use yiiunit\extensions\mongodb\data\ar\Customer;
  5. use yiiunit\extensions\mongodb\data\ar\CustomerOrder;
  6. use yiiunit\extensions\mongodb\data\ar\Item;
  7. class ActiveRelationTest extends TestCase
  8. {
  9. protected function setUp()
  10. {
  11. parent::setUp();
  12. ActiveRecord::$db = $this->getConnection();
  13. $this->setUpTestRows();
  14. }
  15. protected function tearDown()
  16. {
  17. $this->dropCollection(Customer::collectionName());
  18. $this->dropCollection(CustomerOrder::collectionName());
  19. $this->dropCollection(Item::collectionName());
  20. parent::tearDown();
  21. }
  22. /**
  23. * Sets up test rows.
  24. */
  25. protected function setUpTestRows()
  26. {
  27. $customers = [];
  28. for ($i = 1; $i <= 5; $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. $items = [];
  39. for ($i = 1; $i <= 10; $i++) {
  40. $items[] = [
  41. 'name' => 'name' . $i,
  42. 'price' => $i,
  43. ];
  44. }
  45. $itemCollection = $this->getConnection()->getCollection('item');
  46. $items = $itemCollection->batchInsert($items);
  47. $customerOrders = [];
  48. foreach ($customers as $i => $customer) {
  49. $customerOrders[] = [
  50. 'customer_id' => $customer['_id'],
  51. 'number' => $customer['status'],
  52. 'item_ids' => [
  53. $items[$i]['_id'],
  54. $items[$i+5]['_id'],
  55. ],
  56. ];
  57. $customerOrders[] = [
  58. 'customer_id' => $customer['_id'],
  59. 'number' => $customer['status'] + 100,
  60. 'item_ids' => [
  61. $items[$i]['_id'],
  62. $items[$i+5]['_id'],
  63. ],
  64. ];
  65. }
  66. $customerOrderCollection = $this->getConnection()->getCollection('customer_order');
  67. $customerOrderCollection->batchInsert($customerOrders);
  68. }
  69. // Tests :
  70. public function testFindLazy()
  71. {
  72. /* @var $order CustomerOrder */
  73. $order = CustomerOrder::findOne(['number' => 2]);
  74. $this->assertFalse($order->isRelationPopulated('customer'));
  75. $customer = $order->customer;
  76. $this->assertTrue($order->isRelationPopulated('customer'));
  77. $this->assertTrue($customer instanceof Customer);
  78. $this->assertEquals((string) $customer->_id, (string) $order->customer_id);
  79. $this->assertEquals(1, count($order->relatedRecords));
  80. /* @var $customer Customer */
  81. $customer = Customer::findOne(['status' => 2]);
  82. $this->assertFalse($customer->isRelationPopulated('orders'));
  83. $orders = $customer->orders;
  84. $this->assertTrue($customer->isRelationPopulated('orders'));
  85. $this->assertTrue($orders[0] instanceof CustomerOrder);
  86. $this->assertEquals((string) $customer->_id, (string) $orders[0]->customer_id);
  87. }
  88. public function testFindEager()
  89. {
  90. /* @var $orders CustomerOrder[] */
  91. $orders = CustomerOrder::find()->with('customer')->all();
  92. $this->assertCount(10, $orders);
  93. $this->assertTrue($orders[0]->isRelationPopulated('customer'));
  94. $this->assertTrue($orders[1]->isRelationPopulated('customer'));
  95. $this->assertTrue($orders[0]->customer instanceof Customer);
  96. $this->assertEquals((string) $orders[0]->customer->_id, (string) $orders[0]->customer_id);
  97. $this->assertTrue($orders[1]->customer instanceof Customer);
  98. $this->assertEquals((string) $orders[1]->customer->_id, (string) $orders[1]->customer_id);
  99. /* @var $customers Customer[] */
  100. $customers = Customer::find()->with('orders')->all();
  101. $this->assertCount(5, $customers);
  102. $this->assertTrue($customers[0]->isRelationPopulated('orders'));
  103. $this->assertTrue($customers[1]->isRelationPopulated('orders'));
  104. $this->assertNotEmpty($customers[0]->orders);
  105. $this->assertTrue($customers[0]->orders[0] instanceof CustomerOrder);
  106. $this->assertEquals((string) $customers[0]->_id, (string) $customers[0]->orders[0]->customer_id);
  107. }
  108. /**
  109. * @see https://github.com/yiisoft/yii2/issues/5411
  110. *
  111. * @depends testFindEager
  112. */
  113. public function testFindEagerHasManyByArrayKey()
  114. {
  115. $order = CustomerOrder::find()->where(['number' => 1])->with('items')->one();
  116. $this->assertNotEmpty($order->items);
  117. }
  118. /**
  119. * @see https://github.com/yiisoft/yii2-mongodb/issues/173
  120. */
  121. public function testApplyLink()
  122. {
  123. /* @var $customer Customer */
  124. $customer = Customer::find()->orderBy(['_id' => SORT_DESC])->limit(1)->one();
  125. $this->assertCount(2, $customer->getOrders()->all());
  126. $this->assertEquals(2, $customer->getOrders()->count());
  127. $this->assertEquals(110, $customer->getOrders()->sum('number'));
  128. $this->assertEquals(55, $customer->getOrders()->average('number'));
  129. $this->assertEquals([5, 105], $customer->getOrders()->distinct('number'));
  130. $this->assertEquals((string)$customer->_id, (string)$customer->getOrders()->modify(['$set' => ['number' => 5]])->customer_id);
  131. }
  132. }