123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- <?php
- namespace yiiunit\extensions\mongodb\file;
- use MongoDB\BSON\ObjectID;
- use Yii;
- use yii\helpers\FileHelper;
- use yiiunit\extensions\mongodb\TestCase;
- use yii\mongodb\file\ActiveQuery;
- use yiiunit\extensions\mongodb\data\ar\file\ActiveRecord;
- use yiiunit\extensions\mongodb\data\ar\file\CustomerFile;
- /**
- * @group file
- */
- class ActiveRecordTest extends TestCase
- {
- /**
- * @var array[] list of test rows.
- */
- protected $testRows = [];
- protected function setUp()
- {
- parent::setUp();
- ActiveRecord::$db = $this->getConnection();
- $this->dropFileCollection(CustomerFile::collectionName());
- $this->setUpTestRows();
- $filePath = $this->getTestFilePath();
- if (!file_exists($filePath)) {
- FileHelper::createDirectory($filePath);
- }
- }
- protected function tearDown()
- {
- $filePath = $this->getTestFilePath();
- if (file_exists($filePath)) {
- FileHelper::removeDirectory($filePath);
- }
- $this->dropFileCollection(CustomerFile::collectionName());
- parent::tearDown();
- }
- /**
- * @return string test file path.
- */
- protected function getTestFilePath()
- {
- return Yii::getAlias('@yiiunit/extensions/mongodb/runtime') . DIRECTORY_SEPARATOR . basename(get_class($this)) . '_' . getmypid();
- }
- /**
- * Sets up test rows.
- */
- protected function setUpTestRows()
- {
- $collection = $this->getConnection()->getFileCollection(CustomerFile::collectionName());
- $rows = [];
- for ($i = 1; $i <= 10; $i++) {
- $record = [
- 'tag' => 'tag' . $i,
- 'status' => $i,
- ];
- $content = 'content' . $i;
- $record['_id'] = $collection->insertFileContent($content, $record);
- $record['content'] = $content;
- $rows[] = $record;
- }
- $this->testRows = $rows;
- }
- // Tests :
- public function testFind()
- {
- // find one
- $result = CustomerFile::find();
- $this->assertTrue($result instanceof ActiveQuery);
- $customer = $result->one();
- $this->assertTrue($customer instanceof CustomerFile);
- // find all
- $customers = CustomerFile::find()->all();
- $this->assertCount(10, $customers);
- $this->assertTrue($customers[0] instanceof CustomerFile);
- $this->assertTrue($customers[1] instanceof CustomerFile);
- // find by _id
- $testId = $this->testRows[0]['_id'];
- $customer = CustomerFile::findOne($testId);
- $this->assertTrue($customer instanceof CustomerFile);
- $this->assertEquals($testId, $customer->_id);
- // find by column values
- $customer = CustomerFile::findOne(['tag' => 'tag5']);
- $this->assertTrue($customer instanceof CustomerFile);
- $this->assertEquals($this->testRows[4]['_id'], $customer->_id);
- $this->assertEquals('tag5', $customer->tag);
- $customer = CustomerFile::findOne(['tag' => 'unexisting tag']);
- $this->assertNull($customer);
- // find by attributes
- $customer = CustomerFile::find()->where(['status' => 4])->one();
- $this->assertTrue($customer instanceof CustomerFile);
- $this->assertEquals(4, $customer->status);
- // find count, sum, average, min, max, distinct
- $this->assertEquals(10, CustomerFile::find()->count());
- $this->assertEquals(1, CustomerFile::find()->where(['status' => 2])->count());
- $this->assertEquals((1+10)/2*10, CustomerFile::find()->sum('status'));
- $this->assertEquals((1+10)/2, CustomerFile::find()->average('status'));
- $this->assertEquals(1, CustomerFile::find()->min('status'));
- $this->assertEquals(10, CustomerFile::find()->max('status'));
- $this->assertEquals(range(1, 10), CustomerFile::find()->distinct('status'));
- // scope
- $this->assertEquals(1, CustomerFile::find()->activeOnly()->count());
- // asArray
- $testRow = $this->testRows[2];
- $customer = CustomerFile::find()->where(['_id' => $testRow['_id']])->asArray()->one();
- $this->assertEquals($testRow['_id'], $customer['_id']);
- $this->assertEquals($testRow['tag'], $customer['tag']);
- $this->assertEquals($testRow['status'], $customer['status']);
- // indexBy
- $customers = CustomerFile::find()->indexBy('tag')->all();
- $this->assertTrue($customers['tag1'] instanceof CustomerFile);
- $this->assertTrue($customers['tag2'] instanceof CustomerFile);
- // indexBy callable
- $customers = CustomerFile::find()->indexBy(function ($customer) {
- return $customer->status . '-' . $customer->status;
- })->all();
- $this->assertTrue($customers['1-1'] instanceof CustomerFile);
- $this->assertTrue($customers['2-2'] instanceof CustomerFile);
- }
- public function testInsert()
- {
- $record = new CustomerFile();
- $record->tag = 'new new';
- $record->status = 7;
- $this->assertTrue($record->isNewRecord);
- $record->save();
- $this->assertTrue($record->_id instanceof ObjectID);
- $this->assertFalse($record->isNewRecord);
- $fileContent = $record->getFileContent();
- $this->assertEmpty($fileContent);
- }
- /**
- * @depends testInsert
- */
- public function testInsertFile()
- {
- $record = new CustomerFile();
- $record->tag = 'new new';
- $record->status = 7;
- $fileName = __FILE__;
- $record->setAttribute('file', $fileName);
- $record->save();
- $this->assertTrue($record->_id instanceof ObjectID);
- $this->assertFalse($record->isNewRecord);
- $fileContent = $record->getFileContent();
- $this->assertEquals(file_get_contents($fileName), $fileContent);
- }
- /**
- * @depends testInsert
- */
- public function testInsertFileContent()
- {
- $record = new CustomerFile();
- $record->tag = 'new new';
- $record->status = 7;
- $newFileContent = 'Test new file content';
- $record->setAttribute('newFileContent', $newFileContent);
- $record->save();
- $this->assertTrue($record->_id instanceof ObjectID);
- $this->assertFalse($record->isNewRecord);
- $fileContent = $record->getFileContent();
- $this->assertEquals($newFileContent, $fileContent);
- }
- /**
- * @depends testInsert
- */
- public function testUpdate()
- {
- $record = new CustomerFile();
- $record->tag = 'new new';
- $record->status = 7;
- $record->save();
- // save
- $record = CustomerFile::findOne($record->_id);
- $this->assertTrue($record instanceof CustomerFile);
- $this->assertEquals(7, $record->status);
- $this->assertFalse($record->isNewRecord);
- $record->status = 9;
- $record->save();
- $this->assertEquals(9, $record->status);
- $this->assertFalse($record->isNewRecord);
- $record2 = CustomerFile::findOne($record->_id);
- $this->assertEquals(9, $record2->status);
- // updateAll
- $pk = ['_id' => $record->_id];
- $ret = CustomerFile::updateAll(['status' => 55], $pk);
- $this->assertEquals(1, $ret);
- $record = CustomerFile::findOne($pk);
- $this->assertEquals(55, $record->status);
- }
- /**
- * @depends testUpdate
- * @depends testInsertFileContent
- */
- public function testUpdateFile()
- {
- $record = new CustomerFile();
- $record->tag = 'new new';
- $record->status = 7;
- $newFileContent = 'Test new file content';
- $record->setAttribute('newFileContent', $newFileContent);
- $record->save();
- $updateFileName = __FILE__;
- $record = CustomerFile::findOne($record->_id);
- $record->setAttribute('file', $updateFileName);
- $record->status = 55;
- $record->save();
- $this->assertEquals(file_get_contents($updateFileName), $record->getFileContent());
- $record2 = CustomerFile::findOne($record->_id);
- $this->assertEquals($record->status, $record2->status);
- $this->assertEquals(file_get_contents($updateFileName), $record2->getFileContent());
- $this->assertEquals($record->tag, $record2->tag);
- }
- /**
- * @depends testUpdate
- * @depends testInsertFileContent
- */
- public function testUpdateFileContent()
- {
- $record = new CustomerFile();
- $record->tag = 'new new';
- $record->status = 7;
- $newFileContent = 'Test new file content';
- $record->setAttribute('newFileContent', $newFileContent);
- $record->save();
- $updateFileContent = 'New updated file content';
- $record = CustomerFile::findOne($record->_id);
- $record->setAttribute('newFileContent', $updateFileContent);
- $record->status = 55;
- $record->save();
- $this->assertEquals($updateFileContent, $record->getFileContent());
- $record2 = CustomerFile::findOne($record->_id);
- $this->assertEquals($record->status, $record2->status);
- $this->assertEquals($updateFileContent, $record2->getFileContent());
- }
- /**
- * @depends testInsertFileContent
- */
- public function testWriteFile()
- {
- $record = new CustomerFile();
- $record->tag = 'new new';
- $record->status = 7;
- $newFileContent = 'Test new file content';
- $record->setAttribute('newFileContent', $newFileContent);
- $record->save();
- $outputFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . 'out.txt';
- $this->assertTrue($record->writeFile($outputFileName));
- $this->assertEquals($newFileContent, file_get_contents($outputFileName));
- $record2 = CustomerFile::findOne($record->_id);
- $outputFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . 'out_refreshed.txt';
- $this->assertTrue($record2->writeFile($outputFileName));
- $this->assertEquals($newFileContent, file_get_contents($outputFileName));
- }
- /**
- * @depends testInsertFileContent
- */
- public function testGetFileResource()
- {
- $record = new CustomerFile();
- $record->tag = 'new new';
- $record->status = 7;
- $newFileContent = 'Test new file content';
- $record->setAttribute('newFileContent', $newFileContent);
- $record->save();
- $fileResource = $record->getFileResource();
- $contents = stream_get_contents($fileResource);
- fclose($fileResource);
- $this->assertEquals($newFileContent, $contents);
- $record2 = CustomerFile::findOne($record->_id);
- $fileResource = $record2->getFileResource();
- $contents = stream_get_contents($fileResource);
- fclose($fileResource);
- $this->assertEquals($newFileContent, $contents);
- }
- /**
- * @depends testInsert
- *
- * @see https://github.com/yiisoft/yii2-mongodb/pull/146
- */
- public function testInsertCustomId()
- {
- $record = new CustomerFile();
- $record->_id = 'custom';
- $record->tag = 'new new';
- $record->status = 7;
- $record->save(false);
- $this->assertEquals('custom', $record->_id);
- }
- }
|