ActiveFixtureTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace yiiunit\extensions\mongodb;
  3. use Yii;
  4. use yii\helpers\FileHelper;
  5. use yii\helpers\VarDumper;
  6. use yii\mongodb\ActiveFixture;
  7. use yiiunit\extensions\mongodb\data\ar\Customer;
  8. class ActiveFixtureTest extends TestCase
  9. {
  10. protected function setUp()
  11. {
  12. parent::setUp();
  13. $this->mockApplication();
  14. }
  15. protected function tearDown()
  16. {
  17. $this->dropCollection(Customer::collectionName());
  18. FileHelper::removeDirectory(Yii::getAlias('@runtime/fixtures'));
  19. parent::tearDown();
  20. }
  21. // Tests :
  22. public function testLoadCollection()
  23. {
  24. /* @var $fixture ActiveFixture|\PHPUnit_Framework_MockObject_MockObject */
  25. $fixture = $this->getMockBuilder(ActiveFixture::className())
  26. ->setConstructorArgs([
  27. [
  28. 'db' => $this->getConnection(),
  29. 'collectionName' => Customer::collectionName()
  30. ]
  31. ])
  32. ->setMethods(['getData'])
  33. ->getMock();
  34. $fixture->expects($this->any())->method('getData')->will($this->returnValue([
  35. ['name' => 'name1'],
  36. ['name' => 'name2'],
  37. ]));
  38. $fixture->load();
  39. $rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
  40. $this->assertCount(2, $rows);
  41. }
  42. public function testLoadClass()
  43. {
  44. /* @var $fixture ActiveFixture|\PHPUnit_Framework_MockObject_MockObject */
  45. $fixture = $this->getMockBuilder(ActiveFixture::className())
  46. ->setConstructorArgs([
  47. [
  48. 'db' => $this->getConnection(),
  49. 'collectionName' => Customer::collectionName()
  50. ]
  51. ])
  52. ->setMethods(['getData'])
  53. ->getMock();
  54. $fixture->expects($this->any())->method('getData')->will($this->returnValue([
  55. ['name' => 'name1'],
  56. ['name' => 'name2'],
  57. ]));
  58. $fixture->load();
  59. $rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
  60. $this->assertCount(2, $rows);
  61. }
  62. /**
  63. * @depends testLoadCollection
  64. *
  65. * @see https://github.com/yiisoft/yii2-mongodb/pull/40
  66. */
  67. public function testLoadEmptyData()
  68. {
  69. /* @var $fixture ActiveFixture|\PHPUnit_Framework_MockObject_MockObject */
  70. $fixture = $this->getMockBuilder(ActiveFixture::className())
  71. ->setConstructorArgs([
  72. [
  73. 'db' => $this->getConnection(),
  74. 'collectionName' => Customer::collectionName()
  75. ]
  76. ])
  77. ->setMethods(['getData'])
  78. ->getMock();
  79. $fixture->expects($this->any())->method('getData')->will($this->returnValue([
  80. // empty
  81. ]));
  82. $fixture->load(); // should be no error
  83. $rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
  84. $this->assertEmpty($rows);
  85. }
  86. /**
  87. * @depends testLoadCollection
  88. *
  89. * @see https://github.com/yiisoft/yii2-mongodb/issues/145
  90. */
  91. public function testDefaultDataFile()
  92. {
  93. $db = $this->getConnection();
  94. $fixturePath = Yii::getAlias('@runtime/fixtures');
  95. $fixtureDataPath = $fixturePath . DIRECTORY_SEPARATOR . 'data';
  96. FileHelper::createDirectory($fixtureDataPath);
  97. $className = 'TestFixture_' . sha1(get_class($this));
  98. $classDefinition = <<<PHP
  99. <?php
  100. class {$className} extends \yii\mongodb\ActiveFixture
  101. {
  102. }
  103. PHP;
  104. $classFile = $fixturePath . DIRECTORY_SEPARATOR . $className . '.php';
  105. file_put_contents($classFile, $classDefinition);
  106. require_once($classFile);
  107. $fixtureData = [
  108. ['name' => 'name1'],
  109. ['name' => 'name2'],
  110. ];
  111. $fixtureDataFile = $fixtureDataPath . DIRECTORY_SEPARATOR . Customer::collectionName() . '.php';
  112. $fixtureDataContent = '<?php return ' . VarDumper::export($fixtureData) . ';';
  113. file_put_contents($fixtureDataFile, $fixtureDataContent);
  114. $fixtureData = [
  115. ['name' => 'name1'],
  116. ['name' => 'name2'],
  117. ['name' => 'name3'],
  118. ];
  119. $fixtureDataFile = $fixtureDataPath . DIRECTORY_SEPARATOR . $db->getDefaultDatabaseName() . '.' . Customer::collectionName() . '.php';
  120. $fixtureDataContent = '<?php return ' . VarDumper::export($fixtureData) . ';';
  121. file_put_contents($fixtureDataFile, $fixtureDataContent);
  122. /* @var $fixture ActiveFixture */
  123. $fixture = new $className([
  124. 'db' => $db,
  125. 'collectionName' => Customer::collectionName(),
  126. ]);
  127. $fixture->load();
  128. $rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
  129. $this->assertCount(2, $rows);
  130. $fixture = new $className([
  131. 'db' => $db,
  132. 'collectionName' => [
  133. $db->getDefaultDatabaseName(),
  134. Customer::collectionName()
  135. ],
  136. ]);
  137. $fixture->load();
  138. $rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
  139. $this->assertCount(3, $rows);
  140. }
  141. }