Skip to content

Commit 0f53a86

Browse files
committed
Improved tests
1 parent d6fbe18 commit 0f53a86

File tree

7 files changed

+330
-37
lines changed

7 files changed

+330
-37
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ $export = new SpreadsheetExporter();
7575

7676
$modelBuilder->bind('companies', $companies);
7777

78-
$export->export($modelBuilder->build('mapping.yml'), 'myFile.xslx');
78+
$export->export($modelBuilder->build(new YamlMappingReader('mapping.yml')), 'myFile.xslx');
7979
```
8080

8181
Here is a more complete example:
@@ -112,7 +112,7 @@ $modelBuilder->bindFunction('up', function($str) {
112112
return strtoupper($str);
113113
});
114114

115-
$export->export($modelBuilder->build('mapping.yml'), 'myFile.xslx');
115+
$export->export($modelBuilder->build(new YamlMappingReader('mapping.yml')), 'myFile.xslx');
116116
```
117117

118118
### File format
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Xport\MappingReader;
4+
5+
use Xport\SpreadsheetModel\Parser\ParsingException;
6+
7+
/**
8+
* File reader
9+
*
10+
* @author Matthieu Napoli <matthieu@mnapoli.fr>
11+
*/
12+
abstract class MappingReader
13+
{
14+
/**
15+
* @throws ParsingException
16+
* @return array
17+
*/
18+
abstract public function getMapping();
19+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Xport\MappingReader;
4+
5+
use Symfony\Component\Yaml\Yaml;
6+
use Xport\SpreadsheetModel\Parser\ParsingException;
7+
8+
/**
9+
* YAML file reader
10+
*
11+
* @author Matthieu Napoli <matthieu@mnapoli.fr>
12+
*/
13+
class YamlMappingReader extends MappingReader
14+
{
15+
/**
16+
* @var string
17+
*/
18+
protected $fileName;
19+
20+
/**
21+
* @param string $fileName
22+
* @throws \InvalidArgumentException File not found or not readable
23+
*/
24+
public function __construct($fileName)
25+
{
26+
if (!file_exists($fileName) || !is_readable($fileName)) {
27+
throw new \InvalidArgumentException("The file '$fileName' has not been found or is not readable");
28+
}
29+
$this->fileName = $fileName;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function getMapping()
36+
{
37+
try {
38+
return Yaml::parse($this->fileName);
39+
} catch (\Exception $e) {
40+
throw new ParsingException($e->getMessage());
41+
}
42+
}
43+
}

src/Xport/SpreadsheetModelBuilder.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use Symfony\Component\PropertyAccess\PropertyAccess;
66
use Symfony\Component\PropertyAccess\PropertyAccessor;
7-
use Symfony\Component\Yaml\Parser;
7+
use Xport\MappingReader\MappingReader;
88
use Xport\SpreadsheetModel\Cell;
99
use Xport\SpreadsheetModel\Column;
1010
use Xport\SpreadsheetModel\Parser\ForEachParser;
11+
use Xport\SpreadsheetModel\Parser\ParsingException;
1112
use Xport\SpreadsheetModel\Parser\Scope;
1213
use Xport\SpreadsheetModel\Parser\TwigParser;
1314
use Xport\SpreadsheetModel\SpreadsheetModel;
@@ -61,18 +62,14 @@ public function bind($name, $value)
6162
/**
6263
* Build a model.
6364
*
64-
* @param $mappingFile
65+
* @param MappingReader $mappingReader
66+
* @throws ParsingException
6567
* @return SpreadsheetModel
6668
*/
67-
public function build($mappingFile)
69+
public function build(MappingReader $mappingReader)
6870
{
69-
$yaml = file_get_contents($mappingFile);
70-
71-
$yamlReader = new Parser();
72-
$yamlStructure = $yamlReader->parse($yaml);
73-
7471
$model = new SpreadsheetModel();
75-
$this->parseRoot($model, $yamlStructure, $this->scope);
72+
$this->parseRoot($model, $mappingReader->getMapping(), $this->scope);
7673

7774
return $model;
7875
}
@@ -153,10 +150,10 @@ private function parseTables(Sheet $sheet, $yamlSheet, Scope $sheetScope)
153150
private function parseTable(Sheet $sheet, $yamlTable, Scope $tableScope)
154151
{
155152
if (!isset($yamlTable) || !array_key_exists('columns', $yamlTable)) {
156-
throw new \Exception("'table' must contain 'columns'");
153+
throw new ParsingException("'table' must contain 'columns'");
157154
}
158155
if (!isset($yamlTable) || !array_key_exists('lines', $yamlTable)) {
159-
throw new \Exception("'table' must contain 'lines'");
156+
throw new ParsingException("'table' must contain 'lines'");
160157
}
161158

162159
$table = new Table();

tests/XportTest/FunctionalTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace XportTest;
4+
5+
use Xport\MappingReader\YamlMappingReader;
6+
use Xport\SpreadsheetModel\SpreadsheetModel;
7+
use Xport\SpreadsheetModel\Sheet;
8+
use Xport\SpreadsheetModelBuilder;
9+
10+
class FunctionalTest extends \PHPUnit_Framework_TestCase
11+
{
12+
public function testSpreadsheetModelBuilder()
13+
{
14+
$modelBuilder = new SpreadsheetModelBuilder();
15+
16+
$cells = [];
17+
18+
$cell1 = new \stdClass();
19+
$cell1->name = 'Foo';
20+
$inputSet11 = new \stdClass();
21+
$input111 = new \stdClass();
22+
$input111->value = 10;
23+
$input111->uncertainty = 0.15;
24+
$inputSet11->inputs = [$input111];
25+
$cell1->inputSets = [$inputSet11];
26+
$cells[] = $cell1;
27+
28+
$cell2 = new \stdClass();
29+
$cell2->name = 'Bar';
30+
$cell2->inputSets = [];
31+
$cells[] = $cell2;
32+
33+
$modelBuilder->bind('cells', $cells);
34+
35+
/** @var SpreadsheetModel $result */
36+
$result = $modelBuilder->build(new YamlMappingReader(__DIR__ . '/Fixtures/mapping.yml'));
37+
38+
$this->assertTrue($result instanceof SpreadsheetModel);
39+
$this->assertCount(2, $result->getSheets());
40+
41+
foreach ($result->getSheets() as $sheet) {
42+
$this->assertTrue($sheet instanceof Sheet);
43+
}
44+
45+
$sheet = $result->getSheets()[0];
46+
$this->assertEquals('1 - Foo', $sheet->getLabel());
47+
$this->assertCount(1, $sheet->getTables());
48+
49+
$table = $sheet->getTables()[0];
50+
$this->assertCount(1, $table->getLines());
51+
$this->assertCount(2, $table->getColumns());
52+
$this->assertCount(2, $table->getCells());
53+
}
54+
}

0 commit comments

Comments
 (0)