|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mathielen\ImportEngine; |
|
3
|
|
|
|
|
4
|
|
|
use Mathielen\ImportEngine\Import\Import; |
|
5
|
|
|
use Mathielen\ImportEngine\Import\Run\ImportRunner; |
|
6
|
|
|
use Mathielen\ImportEngine\Importer\Importer; |
|
7
|
|
|
use Mathielen\ImportEngine\Storage\Format\CompressedFormat; |
|
8
|
|
|
use Mathielen\ImportEngine\Storage\Format\CsvFormat; |
|
9
|
|
|
use Mathielen\ImportEngine\Storage\Format\ExcelFormat; |
|
10
|
|
|
use Mathielen\ImportEngine\Storage\Format\Format; |
|
11
|
|
|
use Mathielen\ImportEngine\Storage\Format\XmlFormat; |
|
12
|
|
|
use Mathielen\ImportEngine\Storage\LocalFileStorage; |
|
13
|
|
|
use Mathielen\ImportEngine\Storage\StorageInfo; |
|
14
|
|
|
use Mathielen\ImportEngine\ValueObject\ImportConfiguration; |
|
15
|
|
|
use Mathielen\ImportEngine\ValueObject\ImportRun; |
|
16
|
|
|
|
|
17
|
|
|
class LocalFileTest extends \PHPUnit_Framework_TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @dataProvider getStorages |
|
22
|
|
|
* @medium |
|
23
|
|
|
*/ |
|
24
|
|
|
public function testImport($sourceFile, Format $format, Format $targetFormat=null) |
|
25
|
|
|
{ |
|
26
|
|
|
if (!$targetFormat) { |
|
27
|
|
|
$targetFormat = $format; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$targetFile = tempnam('/tmp', 'test'); |
|
31
|
|
|
@unlink($targetFile); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
$sourceStorage = new LocalFileStorage(new \SplFileInfo($sourceFile), $format); |
|
34
|
|
|
$targetStorage = new LocalFileStorage(new \SplFileInfo($targetFile), $targetFormat); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertEquals(new StorageInfo(array( |
|
37
|
|
|
'name' => basename($targetFile), |
|
38
|
|
|
'hash' => null, |
|
39
|
|
|
'format' => $targetFormat, |
|
40
|
|
|
'size' => 0, |
|
41
|
|
|
'count' => 0 |
|
42
|
|
|
)), $targetStorage->info()); |
|
43
|
|
|
|
|
44
|
|
|
$importer = Importer::build($targetStorage); |
|
45
|
|
|
|
|
46
|
|
|
$importConfiguration = new ImportConfiguration(); |
|
47
|
|
|
$importRun = $importConfiguration->toRun(); |
|
48
|
|
|
|
|
49
|
|
|
$import = Import::build($importer, $sourceStorage, $importRun); |
|
50
|
|
|
|
|
51
|
|
|
$importRunner = new ImportRunner(); |
|
52
|
|
|
$importRunner->run($import); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertFileExists($targetFile); |
|
55
|
|
|
|
|
56
|
|
|
if ($format instanceof XmlFormat) { |
|
57
|
|
|
$this->assertXmlFileEqualsXmlFile($sourceFile, $targetFile); |
|
58
|
|
|
} elseif ($format instanceof CsvFormat) { |
|
59
|
|
|
$this->assertFileEquals($sourceFile, $targetFile); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getStorages() |
|
64
|
|
|
{ |
|
65
|
|
|
return array( |
|
66
|
|
|
array(__DIR__ . '/../../../metadata/testfiles/flatdata.csv', new CsvFormat()), |
|
67
|
|
|
array(__DIR__ . '/../../../metadata/testfiles/flatdata-excel.xls', new ExcelFormat(false)), |
|
68
|
|
|
array(__DIR__ . '/../../../metadata/testfiles/flatdata-excel-xml.xlsx', new ExcelFormat(false)), |
|
69
|
|
|
array(__DIR__ . '/../../../metadata/testfiles/flatdata-csv-zip.zip', new CompressedFormat('testmapping.csv', 'zip', new CsvFormat()), new CsvFormat()), |
|
70
|
|
|
array(__DIR__ . '/../../../metadata/testfiles/hierarchicaldata-xml.xml', new XmlFormat()), |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: