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]);
 }
示例#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 testCommon()
 {
     $exporter = new Exporter(['query' => Test::find()->where(['id' => 1]), 'filePath' => Yii::getAlias('@tests/_output/BasicExporter.xlsx'), 'sheetTitle' => 'Tests', 'standardModelsConfig' => [['className' => Test::className(), 'attributesOrder' => ['ID', 'Name', 'Type', 'Description', 'Author Name', 'Author Rating'], 'standardAttributesConfig' => [['name' => 'type', 'valueReplacement' => function ($model) {
         /* @var $model Test */
         return $model->getTypeLabel();
     }], ['name' => 'description', 'valueReplacement' => function ($model) {
         /* @var $model Test */
         return HtmlPurifier::process($model->description, ['HTML.ForbiddenElements' => ['p']]);
     }], ['name' => 'author_name', 'label' => 'Author Name', 'valueReplacement' => function ($model) {
         /* @var $model Test */
         return $model->author->name;
     }], ['name' => 'author_rating', 'label' => 'Author Rating', 'valueReplacement' => function ($model) {
         /* @var $model Test */
         return $model->author->rating;
     }]]]]]);
     $exporter->run();
     $phpExcel = PHPExcel_IOFactory::load($exporter->filePath);
     $this->assertEquals($phpExcel->getActiveSheet()->getTitle(), 'Tests');
     $attributeNames = [];
     $attributeValues = [];
     $c = 1;
     foreach ($phpExcel->getActiveSheet()->getRowIterator() as $row) {
         if ($c > 2) {
             break;
         }
         foreach ($row->getCellIterator() as $cell) {
             $column = $cell->getColumn();
             $value = $cell->getValue();
             $c == 1 ? $attributeNames[$column] = $value : ($attributeValues[$column] = $value);
         }
         $c++;
     }
     $attributeValues['A'] = (int) $attributeValues['A'];
     $attributeValues['F'] = (int) $attributeValues['F'];
     $this->assertEquals($attributeNames, ['A' => 'ID', 'B' => 'Name', 'C' => 'Type', 'D' => 'Description', 'E' => 'Author Name', 'F' => 'Author Rating']);
     $this->assertEquals($attributeValues, ['A' => 1, 'B' => 'Common test', 'C' => 'Closed', 'D' => 'This is the common test', 'E' => 'Ivan Ivanov', 'F' => 7]);
 }
示例#4
0
 /**
  * @inheritdoc
  */
 public function rules()
 {
     return [[['test_id', 'content', 'sort'], 'required'], ['test_id', 'exist', 'targetClass' => Test::className(), 'targetAttribute' => 'id'], ['content', 'string'], ['sort', 'integer']];
 }