public function testCommon()
 {
     $url = 'https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=1K5pS0Rz6KrM0n-ju_CBm0DZxFkKyoyJd3Orhhk3MYz4&exportFormat=xlsx';
     $path = Yii::getAlias('@tests/_output/AdvancedImporter.xlsx');
     file_put_contents($path, file_get_contents($url));
     $importer = new Importer(['filePath' => $path, 'sheetNames' => ['Data'], 'standardModelsConfig' => [['className' => Test::className(), 'labels' => ['Test', 'Tests'], 'standardAttributesConfig' => [['name' => 'type', 'valueReplacement' => Test::getTypesList()], ['name' => 'description', 'valueReplacement' => function ($value) {
         return $value ? Html::tag('p', $value) : '';
     }], ['name' => 'author_id', 'valueReplacement' => function ($value) {
         return Author::find()->select('id')->where(['name' => $value]);
     }]]], ['className' => Question::className(), 'labels' => ['Question', 'Questions']], ['className' => Answer::className(), 'labels' => ['Answer', 'Answers']]]]);
     $result = $importer->run();
     $this->assertEquals($importer->error, null);
     $this->assertEquals($result, true);
     $this->assertEquals(Test::find()->count(), 5);
     $this->assertEquals(Test::findOne(1)->attributes, ['id' => 1, 'name' => 'Basic test', 'type' => 2, 'description' => '<p>This is the basic test</p>', 'author_id' => 2]);
     $this->assertEquals(Test::findOne(2)->attributes, ['id' => 2, 'name' => 'Common test', 'type' => 1, 'description' => '', 'author_id' => 1]);
     $this->assertEquals(Test::findOne(3)->attributes, ['id' => 3, 'name' => 'Programming test', 'type' => 2, 'description' => '', 'author_id' => 2]);
     $this->assertEquals(Test::findOne(4)->attributes, ['id' => 4, 'name' => 'Language test', 'type' => 1, 'description' => '', 'author_id' => 1]);
     $this->assertEquals(Test::findOne(5)->attributes, ['id' => 5, 'name' => 'Science test', 'type' => 1, 'description' => '', 'author_id' => 1]);
     $this->assertEquals(Question::find()->count(), 5);
     $this->assertEquals(Question::findOne(1)->attributes, ['id' => 1, 'test_id' => 1, 'content' => "What's your name?", 'sort' => 1]);
     $this->assertEquals(Question::findOne(2)->attributes, ['id' => 2, 'test_id' => 1, 'content' => 'How old are you?', 'sort' => 2]);
     $this->assertEquals(Question::findOne(3)->attributes, ['id' => 3, 'test_id' => 5, 'content' => "What's your name?", 'sort' => 1]);
     $this->assertEquals(Question::findOne(4)->attributes, ['id' => 4, 'test_id' => 5, 'content' => 'How old are you?', 'sort' => 2]);
     $this->assertEquals(Question::findOne(5)->attributes, ['id' => 5, 'test_id' => 1, 'content' => 'Yes or no?', 'sort' => 1]);
     $this->assertEquals(Answer::find()->count(), 2);
     $this->assertEquals(Answer::findOne(1)->attributes, ['id' => 1, 'question_id' => 5, 'content' => 'Yes', 'sort' => 1]);
     $this->assertEquals(Answer::findOne(2)->attributes, ['id' => 2, 'question_id' => 5, 'content' => 'No', 'sort' => 2]);
 }
Example #2
0
 public function testCommon()
 {
     $url = 'https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=18EybqxPRLadRcXn9_xN1aTGzQkJb6B3fvgCDTYW__gU&exportFormat=xlsx';
     $path = Yii::getAlias('@tests/_output/BasicImporter.xlsx');
     file_put_contents($path, file_get_contents($url));
     $importer = new Importer(['filePath' => $path, 'standardModelsConfig' => [['className' => Test::className(), 'standardAttributesConfig' => [['name' => 'type', 'valueReplacement' => Test::getTypesList()], ['name' => 'description', 'valueReplacement' => function ($value) {
         return $value ? Html::tag('p', $value) : '';
     }], ['name' => 'author_id', 'valueReplacement' => function ($value) {
         return Author::find()->select('id')->where(['name' => $value]);
     }]]]]]);
     $result = $importer->run();
     $this->assertEquals($importer->error, null);
     $this->assertEquals($result, true);
     $this->assertEquals(Test::find()->count(), 3);
     $this->assertEquals(Test::findOne(1)->attributes, ['id' => 1, 'name' => 'Basic test', 'type' => 2, 'description' => '<p>This is the basic test</p>', 'author_id' => 2]);
     $this->assertEquals(Test::findOne(2)->attributes, ['id' => 2, 'name' => 'Common test', 'type' => 1, 'description' => '', 'author_id' => 1]);
     $this->assertEquals(Test::findOne(3)->attributes, ['id' => 3, 'name' => 'Programming test', 'type' => 2, 'description' => '', 'author_id' => 2]);
 }
 public function getAuthors()
 {
     return $this->hasMany(Author::className(), ['id' => 'book_id'])->viaTable('book_has_author', ['author_id' => 'id']);
 }
Example #4
0
 /**
  * @inheritdoc
  */
 public function rules()
 {
     return [[['name', 'type', 'author_id'], 'required'], [['name', 'description'], 'string'], ['type', 'in', 'range' => array_keys(static::getTypesList())], ['author_id', 'exist', 'targetClass' => Author::className(), 'targetAttribute' => 'id']];
 }
Example #5
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getAuthor()
 {
     return $this->hasOne(Author::className(), ['id' => 'author_id']);
 }