|  |  | @ -2,6 +2,8 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | namespace yiiunit\extensions\mongo\file; |  |  |  | namespace yiiunit\extensions\mongo\file; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | use Yii; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | use yii\helpers\FileHelper; | 
			
		
	
		
		
			
				
					
					|  |  |  | use yiiunit\extensions\mongo\MongoTestCase; |  |  |  | use yiiunit\extensions\mongo\MongoTestCase; | 
			
		
	
		
		
			
				
					
					|  |  |  | use yii\mongo\file\ActiveQuery; |  |  |  | use yii\mongo\file\ActiveQuery; | 
			
		
	
		
		
			
				
					
					|  |  |  | use yiiunit\data\ar\mongo\file\ActiveRecord; |  |  |  | use yiiunit\data\ar\mongo\file\ActiveRecord; | 
			
		
	
	
		
		
			
				
					|  |  | @ -22,15 +24,31 @@ class ActiveRecordTest extends MongoTestCase | 
			
		
	
		
		
			
				
					
					|  |  |  | 		parent::setUp(); |  |  |  | 		parent::setUp(); | 
			
		
	
		
		
			
				
					
					|  |  |  | 		ActiveRecord::$db = $this->getConnection(); |  |  |  | 		ActiveRecord::$db = $this->getConnection(); | 
			
		
	
		
		
			
				
					
					|  |  |  | 		$this->setUpTestRows(); |  |  |  | 		$this->setUpTestRows(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		$filePath = $this->getTestFilePath(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		if (!file_exists($filePath)) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 			FileHelper::createDirectory($filePath); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		} | 
			
		
	
		
		
			
				
					
					|  |  |  | 	} |  |  |  | 	} | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	protected function tearDown() |  |  |  | 	protected function tearDown() | 
			
		
	
		
		
			
				
					
					|  |  |  | 	{ |  |  |  | 	{ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		$filePath = $this->getTestFilePath(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		if (file_exists($filePath)) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 			FileHelper::removeDirectory($filePath); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		} | 
			
		
	
		
		
			
				
					
					|  |  |  | 		$this->dropFileCollection(CustomerFile::collectionName()); |  |  |  | 		$this->dropFileCollection(CustomerFile::collectionName()); | 
			
		
	
		
		
			
				
					
					|  |  |  | 		parent::tearDown(); |  |  |  | 		parent::tearDown(); | 
			
		
	
		
		
			
				
					
					|  |  |  | 	} |  |  |  | 	} | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	/** |  |  |  | 	/** | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	 * @return string test file path. | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	 */ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	protected function getTestFilePath() | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	{ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		return Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . basename(get_class($this)) . '_' . getmypid(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	} | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	/** | 
			
		
	
		
		
			
				
					
					|  |  |  | 	 * Sets up test rows. |  |  |  | 	 * Sets up test rows. | 
			
		
	
		
		
			
				
					
					|  |  |  | 	 */ |  |  |  | 	 */ | 
			
		
	
		
		
			
				
					
					|  |  |  | 	protected function setUpTestRows() |  |  |  | 	protected function setUpTestRows() | 
			
		
	
	
		
		
			
				
					|  |  | @ -256,4 +274,50 @@ class ActiveRecordTest extends MongoTestCase | 
			
		
	
		
		
			
				
					
					|  |  |  | 		$this->assertEquals($record->status, $record2->status); |  |  |  | 		$this->assertEquals($record->status, $record2->status); | 
			
		
	
		
		
			
				
					
					|  |  |  | 		$this->assertEquals($updateFileContent, $record2->getFileContent()); |  |  |  | 		$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::find($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::find($record->_id); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		$fileResource = $record2->getFileResource(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		$contents = stream_get_contents($fileResource); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		fclose($fileResource); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		$this->assertEquals($newFileContent, $contents); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	} | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } |