ActiveRecordTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace yiiunit\extensions\mongodb\file;
  3. use MongoDB\BSON\ObjectID;
  4. use Yii;
  5. use yii\helpers\FileHelper;
  6. use yiiunit\extensions\mongodb\TestCase;
  7. use yii\mongodb\file\ActiveQuery;
  8. use yiiunit\extensions\mongodb\data\ar\file\ActiveRecord;
  9. use yiiunit\extensions\mongodb\data\ar\file\CustomerFile;
  10. /**
  11. * @group file
  12. */
  13. class ActiveRecordTest extends TestCase
  14. {
  15. /**
  16. * @var array[] list of test rows.
  17. */
  18. protected $testRows = [];
  19. protected function setUp()
  20. {
  21. parent::setUp();
  22. ActiveRecord::$db = $this->getConnection();
  23. $this->dropFileCollection(CustomerFile::collectionName());
  24. $this->setUpTestRows();
  25. $filePath = $this->getTestFilePath();
  26. if (!file_exists($filePath)) {
  27. FileHelper::createDirectory($filePath);
  28. }
  29. }
  30. protected function tearDown()
  31. {
  32. $filePath = $this->getTestFilePath();
  33. if (file_exists($filePath)) {
  34. FileHelper::removeDirectory($filePath);
  35. }
  36. $this->dropFileCollection(CustomerFile::collectionName());
  37. parent::tearDown();
  38. }
  39. /**
  40. * @return string test file path.
  41. */
  42. protected function getTestFilePath()
  43. {
  44. return Yii::getAlias('@yiiunit/extensions/mongodb/runtime') . DIRECTORY_SEPARATOR . basename(get_class($this)) . '_' . getmypid();
  45. }
  46. /**
  47. * Sets up test rows.
  48. */
  49. protected function setUpTestRows()
  50. {
  51. $collection = $this->getConnection()->getFileCollection(CustomerFile::collectionName());
  52. $rows = [];
  53. for ($i = 1; $i <= 10; $i++) {
  54. $record = [
  55. 'tag' => 'tag' . $i,
  56. 'status' => $i,
  57. ];
  58. $content = 'content' . $i;
  59. $record['_id'] = $collection->insertFileContent($content, $record);
  60. $record['content'] = $content;
  61. $rows[] = $record;
  62. }
  63. $this->testRows = $rows;
  64. }
  65. // Tests :
  66. public function testFind()
  67. {
  68. // find one
  69. $result = CustomerFile::find();
  70. $this->assertTrue($result instanceof ActiveQuery);
  71. $customer = $result->one();
  72. $this->assertTrue($customer instanceof CustomerFile);
  73. // find all
  74. $customers = CustomerFile::find()->all();
  75. $this->assertCount(10, $customers);
  76. $this->assertTrue($customers[0] instanceof CustomerFile);
  77. $this->assertTrue($customers[1] instanceof CustomerFile);
  78. // find by _id
  79. $testId = $this->testRows[0]['_id'];
  80. $customer = CustomerFile::findOne($testId);
  81. $this->assertTrue($customer instanceof CustomerFile);
  82. $this->assertEquals($testId, $customer->_id);
  83. // find by column values
  84. $customer = CustomerFile::findOne(['tag' => 'tag5']);
  85. $this->assertTrue($customer instanceof CustomerFile);
  86. $this->assertEquals($this->testRows[4]['_id'], $customer->_id);
  87. $this->assertEquals('tag5', $customer->tag);
  88. $customer = CustomerFile::findOne(['tag' => 'unexisting tag']);
  89. $this->assertNull($customer);
  90. // find by attributes
  91. $customer = CustomerFile::find()->where(['status' => 4])->one();
  92. $this->assertTrue($customer instanceof CustomerFile);
  93. $this->assertEquals(4, $customer->status);
  94. // find count, sum, average, min, max, distinct
  95. $this->assertEquals(10, CustomerFile::find()->count());
  96. $this->assertEquals(1, CustomerFile::find()->where(['status' => 2])->count());
  97. $this->assertEquals((1+10)/2*10, CustomerFile::find()->sum('status'));
  98. $this->assertEquals((1+10)/2, CustomerFile::find()->average('status'));
  99. $this->assertEquals(1, CustomerFile::find()->min('status'));
  100. $this->assertEquals(10, CustomerFile::find()->max('status'));
  101. $this->assertEquals(range(1, 10), CustomerFile::find()->distinct('status'));
  102. // scope
  103. $this->assertEquals(1, CustomerFile::find()->activeOnly()->count());
  104. // asArray
  105. $testRow = $this->testRows[2];
  106. $customer = CustomerFile::find()->where(['_id' => $testRow['_id']])->asArray()->one();
  107. $this->assertEquals($testRow['_id'], $customer['_id']);
  108. $this->assertEquals($testRow['tag'], $customer['tag']);
  109. $this->assertEquals($testRow['status'], $customer['status']);
  110. // indexBy
  111. $customers = CustomerFile::find()->indexBy('tag')->all();
  112. $this->assertTrue($customers['tag1'] instanceof CustomerFile);
  113. $this->assertTrue($customers['tag2'] instanceof CustomerFile);
  114. // indexBy callable
  115. $customers = CustomerFile::find()->indexBy(function ($customer) {
  116. return $customer->status . '-' . $customer->status;
  117. })->all();
  118. $this->assertTrue($customers['1-1'] instanceof CustomerFile);
  119. $this->assertTrue($customers['2-2'] instanceof CustomerFile);
  120. }
  121. public function testInsert()
  122. {
  123. $record = new CustomerFile();
  124. $record->tag = 'new new';
  125. $record->status = 7;
  126. $this->assertTrue($record->isNewRecord);
  127. $record->save();
  128. $this->assertTrue($record->_id instanceof ObjectID);
  129. $this->assertFalse($record->isNewRecord);
  130. $fileContent = $record->getFileContent();
  131. $this->assertEmpty($fileContent);
  132. }
  133. /**
  134. * @depends testInsert
  135. */
  136. public function testInsertFile()
  137. {
  138. $record = new CustomerFile();
  139. $record->tag = 'new new';
  140. $record->status = 7;
  141. $fileName = __FILE__;
  142. $record->setAttribute('file', $fileName);
  143. $record->save();
  144. $this->assertTrue($record->_id instanceof ObjectID);
  145. $this->assertFalse($record->isNewRecord);
  146. $fileContent = $record->getFileContent();
  147. $this->assertEquals(file_get_contents($fileName), $fileContent);
  148. }
  149. /**
  150. * @depends testInsert
  151. */
  152. public function testInsertFileContent()
  153. {
  154. $record = new CustomerFile();
  155. $record->tag = 'new new';
  156. $record->status = 7;
  157. $newFileContent = 'Test new file content';
  158. $record->setAttribute('newFileContent', $newFileContent);
  159. $record->save();
  160. $this->assertTrue($record->_id instanceof ObjectID);
  161. $this->assertFalse($record->isNewRecord);
  162. $fileContent = $record->getFileContent();
  163. $this->assertEquals($newFileContent, $fileContent);
  164. }
  165. /**
  166. * @depends testInsert
  167. */
  168. public function testUpdate()
  169. {
  170. $record = new CustomerFile();
  171. $record->tag = 'new new';
  172. $record->status = 7;
  173. $record->save();
  174. // save
  175. $record = CustomerFile::findOne($record->_id);
  176. $this->assertTrue($record instanceof CustomerFile);
  177. $this->assertEquals(7, $record->status);
  178. $this->assertFalse($record->isNewRecord);
  179. $record->status = 9;
  180. $record->save();
  181. $this->assertEquals(9, $record->status);
  182. $this->assertFalse($record->isNewRecord);
  183. $record2 = CustomerFile::findOne($record->_id);
  184. $this->assertEquals(9, $record2->status);
  185. // updateAll
  186. $pk = ['_id' => $record->_id];
  187. $ret = CustomerFile::updateAll(['status' => 55], $pk);
  188. $this->assertEquals(1, $ret);
  189. $record = CustomerFile::findOne($pk);
  190. $this->assertEquals(55, $record->status);
  191. }
  192. /**
  193. * @depends testUpdate
  194. * @depends testInsertFileContent
  195. */
  196. public function testUpdateFile()
  197. {
  198. $record = new CustomerFile();
  199. $record->tag = 'new new';
  200. $record->status = 7;
  201. $newFileContent = 'Test new file content';
  202. $record->setAttribute('newFileContent', $newFileContent);
  203. $record->save();
  204. $updateFileName = __FILE__;
  205. $record = CustomerFile::findOne($record->_id);
  206. $record->setAttribute('file', $updateFileName);
  207. $record->status = 55;
  208. $record->save();
  209. $this->assertEquals(file_get_contents($updateFileName), $record->getFileContent());
  210. $record2 = CustomerFile::findOne($record->_id);
  211. $this->assertEquals($record->status, $record2->status);
  212. $this->assertEquals(file_get_contents($updateFileName), $record2->getFileContent());
  213. $this->assertEquals($record->tag, $record2->tag);
  214. }
  215. /**
  216. * @depends testUpdate
  217. * @depends testInsertFileContent
  218. */
  219. public function testUpdateFileContent()
  220. {
  221. $record = new CustomerFile();
  222. $record->tag = 'new new';
  223. $record->status = 7;
  224. $newFileContent = 'Test new file content';
  225. $record->setAttribute('newFileContent', $newFileContent);
  226. $record->save();
  227. $updateFileContent = 'New updated file content';
  228. $record = CustomerFile::findOne($record->_id);
  229. $record->setAttribute('newFileContent', $updateFileContent);
  230. $record->status = 55;
  231. $record->save();
  232. $this->assertEquals($updateFileContent, $record->getFileContent());
  233. $record2 = CustomerFile::findOne($record->_id);
  234. $this->assertEquals($record->status, $record2->status);
  235. $this->assertEquals($updateFileContent, $record2->getFileContent());
  236. }
  237. /**
  238. * @depends testInsertFileContent
  239. */
  240. public function testWriteFile()
  241. {
  242. $record = new CustomerFile();
  243. $record->tag = 'new new';
  244. $record->status = 7;
  245. $newFileContent = 'Test new file content';
  246. $record->setAttribute('newFileContent', $newFileContent);
  247. $record->save();
  248. $outputFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . 'out.txt';
  249. $this->assertTrue($record->writeFile($outputFileName));
  250. $this->assertEquals($newFileContent, file_get_contents($outputFileName));
  251. $record2 = CustomerFile::findOne($record->_id);
  252. $outputFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . 'out_refreshed.txt';
  253. $this->assertTrue($record2->writeFile($outputFileName));
  254. $this->assertEquals($newFileContent, file_get_contents($outputFileName));
  255. }
  256. /**
  257. * @depends testInsertFileContent
  258. */
  259. public function testGetFileResource()
  260. {
  261. $record = new CustomerFile();
  262. $record->tag = 'new new';
  263. $record->status = 7;
  264. $newFileContent = 'Test new file content';
  265. $record->setAttribute('newFileContent', $newFileContent);
  266. $record->save();
  267. $fileResource = $record->getFileResource();
  268. $contents = stream_get_contents($fileResource);
  269. fclose($fileResource);
  270. $this->assertEquals($newFileContent, $contents);
  271. $record2 = CustomerFile::findOne($record->_id);
  272. $fileResource = $record2->getFileResource();
  273. $contents = stream_get_contents($fileResource);
  274. fclose($fileResource);
  275. $this->assertEquals($newFileContent, $contents);
  276. }
  277. /**
  278. * @depends testInsert
  279. *
  280. * @see https://github.com/yiisoft/yii2-mongodb/pull/146
  281. */
  282. public function testInsertCustomId()
  283. {
  284. $record = new CustomerFile();
  285. $record->_id = 'custom';
  286. $record->tag = 'new new';
  287. $record->status = 7;
  288. $record->save(false);
  289. $this->assertEquals('custom', $record->_id);
  290. }
  291. }