1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Behat\Gherkin\Node\TableNode; |
4
|
|
|
use Behat\Behat\Context\Context; |
5
|
|
|
use Behat\Behat\Context\SnippetAcceptingContext; |
6
|
|
|
use Behat\Behat\Tester\Exception\PendingException; |
7
|
|
|
use Doctrine\DBAL\DriverManager; |
8
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
9
|
|
|
use Symfony\Component\Console\Helper\Table; |
10
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
11
|
|
|
use Extraload\Pipeline\DefaultPipeline; |
12
|
|
|
use Extraload\Extractor\CsvExtractor; |
13
|
|
|
use Extraload\Transformer\NoopTransformer; |
14
|
|
|
use Extraload\Transformer\CallbackTransformer; |
15
|
|
|
use Extraload\Transformer\TransformerChain; |
16
|
|
|
use Extraload\Loader\ConsoleLoader; |
17
|
|
|
use Extraload\Loader\Doctrine\DbalLoader; |
18
|
|
|
|
19
|
|
|
class DefaultPipelineContext extends BaseContext implements Context, SnippetAcceptingContext |
20
|
|
|
{ |
21
|
|
|
private $workingTable = 'book'; |
22
|
|
|
private $workingFile; |
23
|
|
|
private $pipeline; |
24
|
|
|
private $output; |
25
|
|
|
private $connection; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @Given a file named :name with: |
29
|
|
|
*/ |
30
|
|
|
public function aFileNamedWith($name, PyStringNode $content) |
31
|
|
|
{ |
32
|
|
|
$this->workingFile = $this->createFileFromStringNode($name, $content); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @Given I create csv to console pipeline using :transformer transformer |
37
|
|
|
*/ |
38
|
|
|
public function iCreateCsvToConsolePipelineUsingTransformer($transformer) |
39
|
|
|
{ |
40
|
|
|
return $this->pipeline = new DefaultPipeline( |
41
|
|
|
$this->createCsvExtractor(), |
42
|
|
|
$this->createTransformer($transformer), |
43
|
|
|
new ConsoleLoader( |
44
|
|
|
new Table($this->output = new BufferedOutput()) |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @Given I create csv to database pipeline |
51
|
|
|
*/ |
52
|
|
|
public function iCreateCsvToDatabasePipeline() |
53
|
|
|
{ |
54
|
|
|
return $this->pipeline = new DefaultPipeline( |
55
|
|
|
$this->createCsvExtractor(), |
56
|
|
|
$this->createTransformer('callable'), |
57
|
|
|
new DbalLoader( |
58
|
|
|
$this->getConnection(), |
59
|
|
|
$this->workingTable |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @Given I process it |
66
|
|
|
*/ |
67
|
|
|
public function iProcessIt() |
68
|
|
|
{ |
69
|
|
|
$this->pipeline->process(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @Then I should see in console: |
74
|
|
|
*/ |
75
|
|
|
public function iShouldSeeInConsole(PyStringNode $expected) |
76
|
|
|
{ |
77
|
|
|
$expected = $this->stringNodeToString($expected); |
78
|
|
|
$actual = trim($this->output->fetch()); |
79
|
|
|
|
80
|
|
|
PHPUnit_Framework_Assert::assertEquals($expected, $actual); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @Then I should see in database: |
85
|
|
|
*/ |
86
|
|
|
public function iShouldSeeInDatabase(TableNode $table) |
87
|
|
|
{ |
88
|
|
|
$actual = $this->getConnection() |
89
|
|
|
->createQueryBuilder() |
90
|
|
|
->select('*') |
91
|
|
|
->from($this->workingTable) |
92
|
|
|
->execute() |
93
|
|
|
->fetchAll() |
94
|
|
|
; |
95
|
|
|
|
96
|
|
|
foreach ($table->getHash() as $key => $expected) { |
97
|
|
|
PHPUnit_Framework_Assert::assertEquals($expected, $actual[$key]); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function createCsvExtractor() |
102
|
|
|
{ |
103
|
|
|
return new CsvExtractor(new \SplFileObject($this->workingFile)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function createTransformer($type) |
107
|
|
|
{ |
108
|
|
|
switch ($type) { |
109
|
|
|
case 'callable': |
110
|
|
|
return new CallbackTransformer(function ($data) { |
111
|
|
|
return [ |
112
|
|
|
'isbn' => $data[0], |
113
|
|
|
'title' => $data[1], |
114
|
|
|
'author' => $data[2], |
115
|
|
|
]; |
116
|
|
|
}); |
117
|
|
|
|
118
|
|
|
case 'chain': |
119
|
|
|
return new TransformerChain([ |
120
|
|
|
new CallbackTransformer(function ($data) { |
121
|
|
|
unset($data[0]); |
122
|
|
|
|
123
|
|
|
return $data; |
124
|
|
|
}), |
125
|
|
|
new CallbackTransformer(function ($data) { |
126
|
|
|
return [ |
127
|
|
|
'title' => $data[1], |
128
|
|
|
'author' => $data[2], |
129
|
|
|
]; |
130
|
|
|
}) |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
throw new PendingException(sprintf('Implement %s transformer creator.', $type)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function getConnection() |
138
|
|
|
{ |
139
|
|
|
if (null === $this->connection) { |
140
|
|
|
$this->connection = DriverManager::getConnection(['url' => 'sqlite:///:memory:']); |
141
|
|
|
$this->connection->exec(sprintf('CREATE TABLE %s(isbn, title, author)', $this->workingTable)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->connection; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|