Completed
Pull Request — master (#2)
by Саша
36:16 queued 06:11
created

DefaultPipelineContext::getConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

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