/**
  * Testing method loadData() for one-to-many relation
  * - attribute relationalData must be correct
  * - expecting exception if model does not exists
  * @see RelationBehavior::loadData
  */
 public function testLoadDataManyToMany()
 {
     /** @var FakeNewsModel|\PHPUnit_Framework_MockObject_MockObject $mockModel */
     $mockModel = $this->getMock(FakeNewsModel::className(), ['getNewsFiles', 'getNews_files', 'getFiles']);
     $mockModel->id = 1;
     $mockModel->expects($this->any())->method('getNewsFiles')->willReturn($mockModel->hasMany(FakeNewsFilesModel::className(), ['news_id' => 'id']));
     $activeQuery = $mockModel->hasMany(FakeFilesModel::className(), ['id' => 'file_id'])->via('newsFiles');
     $mockModel->expects($this->any())->method('getNews_files')->willReturn($activeQuery);
     $mockModel->expects($this->any())->method('getFiles')->willReturn($mockModel->hasMany(FakeFilesModel::className(), ['id' => 'file_id'])->via('newsFiles'));
     $behavior = new RelationBehavior(['relationalFields' => ['news_files', 'files']]);
     $behavior->owner = $mockModel;
     $file_ids = [];
     $files = [['src' => '/images/new.image1.png'], ['src' => '/images/new.image2.png']];
     foreach ($files as $data) {
         $file_ids[] = $this->createFile($data);
     }
     $oldModels = $behavior->owner->getNewsFiles()->all();
     $behavior->news_files = $file_ids;
     $method = new \ReflectionMethod($behavior, 'loadData');
     $method->setAccessible(true);
     $method->invoke($behavior);
     $expected = ['news_files' => ['activeQuery' => $activeQuery, 'junctionModelClass' => FakeNewsFilesModel::className(), 'junctionTable' => FakeNewsFilesModel::tableName(), 'junctionColumn' => 'news_id', 'relatedColumn' => 'file_id']];
     foreach ($file_ids as $file) {
         $expected['news_files']['newModels'][] = new FakeNewsFilesModel(['file_id' => $file]);
     }
     $expected['news_files']['oldModels'] = $oldModels;
     $this->assertAttributeEquals($expected, 'relationalData', $behavior);
     $this->expectException(RelationException::class);
     $this->expectExceptionMessage('Related records for attribute files not found');
     $behavior->files = [100, 101];
     $method = new \ReflectionMethod($behavior, 'loadData');
     $method->setAccessible(true);
     $method->invoke($behavior);
 }
示例#2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getNewsFiles()
 {
     return $this->hasMany(FakeNewsFilesModel::className(), ['news_id' => 'id']);
 }