ActiveRelationTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace yiiunit\extensions\mongodb\file;
  3. use yiiunit\extensions\mongodb\data\ar\Customer;
  4. use yiiunit\extensions\mongodb\data\ar\file\CustomerFile;
  5. use yiiunit\extensions\mongodb\TestCase;
  6. /**
  7. * @group file
  8. */
  9. class ActiveRelationTest extends TestCase
  10. {
  11. protected function setUp()
  12. {
  13. parent::setUp();
  14. \yiiunit\extensions\mongodb\data\ar\ActiveRecord::$db = $this->getConnection();
  15. \yiiunit\extensions\mongodb\data\ar\file\ActiveRecord::$db = $this->getConnection();
  16. $this->setUpTestRows();
  17. }
  18. protected function tearDown()
  19. {
  20. $this->dropCollection(Customer::collectionName());
  21. $this->dropCollection(CustomerFile::collectionName());
  22. parent::tearDown();
  23. }
  24. /**
  25. * Sets up test rows.
  26. */
  27. protected function setUpTestRows()
  28. {
  29. $fileCollection = $this->getConnection()->getFileCollection(CustomerFile::collectionName());
  30. $customers = [];
  31. $files = [];
  32. for ($i = 1; $i <= 5; $i++) {
  33. $file = [
  34. 'tag' => 'tag' . $i,
  35. 'status' => $i,
  36. ];
  37. $content = 'content' . $i;
  38. $file['_id'] = $fileCollection->insertFileContent($content, $file);
  39. $file['content'] = $content;
  40. $files[] = $file;
  41. $customers[] = [
  42. 'name' => 'name' . $i,
  43. 'email' => 'email' . $i,
  44. 'address' => 'address' . $i,
  45. 'status' => $i,
  46. 'file_id' => $file['_id'],
  47. ];
  48. }
  49. $customerCollection = $this->getConnection()->getCollection(Customer::collectionName());
  50. $customers = $customerCollection->batchInsert($customers);
  51. }
  52. // Tests :
  53. public function testFindLazy()
  54. {
  55. /* @var $customer Customer */
  56. $customer = Customer::findOne(['status' => 2]);
  57. $this->assertFalse($customer->isRelationPopulated('file'));
  58. $file = $customer->file;
  59. $this->assertTrue($customer->isRelationPopulated('file'));
  60. $this->assertTrue($file instanceof CustomerFile);
  61. $this->assertEquals((string) $file->_id, (string) $customer->file_id);
  62. $this->assertEquals(1, count($customer->relatedRecords));
  63. }
  64. public function testFindEager()
  65. {
  66. /* @var $customers Customer[] */
  67. $customers = Customer::find()->with('file')->all();
  68. $this->assertEquals(5, count($customers));
  69. $this->assertTrue($customers[0]->isRelationPopulated('file'));
  70. $this->assertTrue($customers[1]->isRelationPopulated('file'));
  71. $this->assertTrue($customers[0]->file instanceof CustomerFile);
  72. $this->assertEquals((string) $customers[0]->file->_id, (string) $customers[0]->file_id);
  73. $this->assertTrue($customers[1]->file instanceof CustomerFile);
  74. $this->assertEquals((string) $customers[1]->file->_id, (string) $customers[1]->file_id);
  75. }
  76. }