1
|
|
|
<?php |
2
|
|
|
namespace Mathielen\ImportEngine\Importer; |
3
|
|
|
|
4
|
|
|
use Mathielen\ImportEngine\Storage\Format\CsvFormat; |
5
|
|
|
|
6
|
|
|
class ImporterPreconditionTest extends \PHPUnit_Framework_TestCase |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
private $storage; |
10
|
|
|
|
11
|
|
|
protected function setUp() |
12
|
|
|
{ |
13
|
|
|
$info = array( |
14
|
|
|
'name' => 'file.csv', |
15
|
|
|
'format' => new CsvFormat() |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
$this->storage = $this->createMock('Mathielen\ImportEngine\Storage\StorageFormatInterface'); |
19
|
|
|
$this->storage |
20
|
|
|
->expects($this->any()) |
21
|
|
|
->method('info') |
22
|
|
|
->will($this->returnValue($info)); |
23
|
|
|
$this->storage |
24
|
|
|
->expects($this->any()) |
25
|
|
|
->method('getFields') |
26
|
|
|
->will($this->returnValue(array('A', 'B'))); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @dataProvider getFilenameData |
31
|
|
|
*/ |
32
|
|
|
public function testFilename($filename, $expectedResult) |
33
|
|
|
{ |
34
|
|
|
$preCondition = new ImporterPrecondition(); |
35
|
|
|
$this->assertEquals($expectedResult, $preCondition |
36
|
|
|
->filename($filename) |
37
|
|
|
->isSatisfiedBy($this->storage)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getFilenameData() |
41
|
|
|
{ |
42
|
|
|
return array( |
43
|
|
|
array('file.csv', true), |
44
|
|
|
array('^.*\.csv$', true), |
45
|
|
|
array('f1le.csv', false) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @dataProvider getFormatData |
51
|
|
|
*/ |
52
|
|
|
public function testFormat($format, $expectedResult) |
53
|
|
|
{ |
54
|
|
|
$preCondition = new ImporterPrecondition(); |
55
|
|
|
$this->assertEquals($expectedResult, $preCondition |
56
|
|
|
->format($format) |
57
|
|
|
->isSatisfiedBy($this->storage)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getFormatData() |
61
|
|
|
{ |
62
|
|
|
return array( |
63
|
|
|
array('csv', true), |
64
|
|
|
array('excel', false) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @expectedException \Mathielen\ImportEngine\Exception\InvalidConfigurationException |
70
|
|
|
*/ |
71
|
|
|
public function testFormatException() |
72
|
|
|
{ |
73
|
|
|
$storage = $this->createMock('Mathielen\ImportEngine\Storage\StorageInterface'); |
74
|
|
|
|
75
|
|
|
$preCondition = new ImporterPrecondition(); |
76
|
|
|
$preCondition |
77
|
|
|
->format('csv') |
78
|
|
|
->isSatisfiedBy($storage); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testFieldcount() |
82
|
|
|
{ |
83
|
|
|
$preCondition = new ImporterPrecondition(); |
84
|
|
|
$this->assertTrue($preCondition |
85
|
|
|
->fieldcount(2) |
86
|
|
|
->isSatisfiedBy($this->storage)); |
87
|
|
|
|
88
|
|
|
$preCondition = new ImporterPrecondition(); |
89
|
|
|
$this->assertFalse($preCondition |
90
|
|
|
->fieldcount(1) |
91
|
|
|
->isSatisfiedBy($this->storage)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @dataProvider getAnyFieldData |
96
|
|
|
*/ |
97
|
|
|
public function testAnyField($field, $expectedResult) |
98
|
|
|
{ |
99
|
|
|
$preCondition = new ImporterPrecondition(); |
100
|
|
|
$this->assertEquals($expectedResult, $preCondition |
101
|
|
|
->field($field) |
102
|
|
|
->isSatisfiedBy($this->storage)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getAnyFieldData() |
106
|
|
|
{ |
107
|
|
|
return array( |
108
|
|
|
array('a', true), |
109
|
|
|
array('z', false) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @dataProvider getFieldsetData |
115
|
|
|
*/ |
116
|
|
|
public function testFieldset($fieldset, $expectedResult) |
117
|
|
|
{ |
118
|
|
|
$preCondition = new ImporterPrecondition(); |
119
|
|
|
$this->assertEquals($expectedResult, $preCondition |
120
|
|
|
->fieldset($fieldset) |
121
|
|
|
->isSatisfiedBy($this->storage)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getFieldsetData() |
125
|
|
|
{ |
126
|
|
|
return array( |
127
|
|
|
array(array('a', 'b'), true), |
128
|
|
|
array(array('b', 'a'), false) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|