DbalPipeTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 136
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ __invoke() 0 4 1
A setUp() 0 43 1
A getTearDownOperation() 0 4 1
A getConnection() 0 9 2
A getDataSet() 0 12 1
A testDbSimplePipeFlow() 0 44 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlow\Test\Functional;
5
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\Driver\PDOConnection;
8
use Doctrine\DBAL\DriverManager;
9
use Doctrine\DBAL\Schema\Schema;
10
use PHPUnit\DbUnit\DataSet\IDataSet;
11
use PHPUnit\DbUnit\Operation\Factory;
12
use PHPUnit\DbUnit\TestCase;
13
use SlayerBirden\DataFlow\DataBagInterface;
14
use SlayerBirden\DataFlow\Emitter\BlackHole;
15
use SlayerBirden\DataFlow\Pipe\MapperCallbackInterface;
16
use SlayerBirden\DataFlow\PipelineBuilder;
17
use SlayerBirden\DataFlow\Plumber;
18
use SlayerBirden\DataFlow\Provider\ArrayProvider;
19
use SlayerBirden\DataFlow\Test\Functional\Exception\ConnectionException;
20
use SlayerBirden\DataFlow\Writer\Dbal\UpdateStrategy\UniqueIndexStrategy;
21
use SlayerBirden\DataFlow\Writer\Dbal\Write;
22
use SlayerBirden\DataFlow\Writer\Dbal\WriterUtility;
23
24
class DbalPipeTest extends TestCase
25
{
26
    /**
27
     * @var Connection
28
     */
29
    private $connection;
30
    private $pipeline;
31
    private $emitter;
32
33
    protected function setUp(): void
34
    {
35
        $params = require __DIR__ . '/config/db-config.php';
36
        $this->connection = DriverManager::getConnection($params);
37
38
        // create Table
39
        $schema = new Schema();
40
        $table = $schema->createTable('heroes');
41
        $table->addColumn('id', 'integer', [
42
            'autoincrement' => true,
43
        ]);
44
        $table->addColumn('name', 'string');
45
        $table->addColumn('code', 'string');
46
        $table->setPrimaryKey(['id']);
47
        $table->addUniqueIndex(['code']);
48
49
        $currentSchema = $this->connection->getSchemaManager()->createSchema();
50
        $sql = $currentSchema->getMigrateToSql($schema, $this->connection->getDatabasePlatform());
51
        array_walk($sql, function ($script) {
52
            $this->connection->executeUpdate($script);
53
        });
54
55
        parent::setUp();
56
        $this->emitter = new BlackHole();
57
        $utility = new WriterUtility($this->connection);
58
        $this->pipeline = (new PipelineBuilder($this->emitter))
59
            ->map('name', new class implements MapperCallbackInterface
60
            {
61
                public function __invoke($value, ?DataBagInterface $dataBag = null)
62
                {
63
                    return $dataBag['firstname'] . ' ' . $dataBag['lastname'];
64
                }
65
            })
66
            ->addSection(new Write(
67
                'heroes_write',
68
                $this->connection,
69
                'heroes',
70
                $utility,
71
                new UniqueIndexStrategy('heroes', $utility),
72
                $this->emitter
73
            ))
74
            ->build();
75
    }
76
77
    protected function getTearDownOperation()
78
    {
79
        return Factory::TRUNCATE();
80
    }
81
82
    /**
83
     * Returns the test database connection.
84
     *
85
     * @return \PHPUnit\DbUnit\Database\Connection
86
     */
87
    protected function getConnection()
88
    {
89
        $con = $this->connection->getWrappedConnection();
90
        if ($con instanceof PDOConnection) {
91
            return $this->createDefaultDBConnection($con);
92
        }
93
94
        throw new ConnectionException('Could not get PDO object.');
95
    }
96
97
    /**
98
     * Returns the test dataset.
99
     *
100
     * @return IDataSet
101
     */
102
    protected function getDataSet()
103
    {
104
        return $this->createArrayDataSet([
105
            'heroes' => [
106
                [
107
                    'id' => 1,
108
                    'name' => 'Spiderman',
109
                    'code' => 'spider-man',
110
                ],
111
            ],
112
        ]);
113
    }
114
115
    public function testDbSimplePipeFlow()
116
    {
117
        $provider = new ArrayProvider('heroes', [
118
            [
119
                'code' => 'super-man',
120
                'firstname' => 'Clark',
121
                'lastname' => 'Kent',
122
            ],
123
            [
124
                'code' => 'spider-man',
125
                'firstname' => 'Peter',
126
                'lastname' => 'Parker',
127
            ],
128
            [
129
                'code' => 'clockwerk',
130
                'firstname' => 'Clock',
131
                'lastname' => 'Werk',
132
            ],
133
        ]);
134
        (new Plumber($provider, $this->pipeline, $this->emitter))->pour();
135
136
        $actual = $this->getConnection()->createQueryTable('heroes', 'SELECT * FROM `heroes`');
137
        $expected = $this->createArrayDataSet([
138
            'heroes' => [
139
                [
140
                    'id' => 1,
141
                    'name' => 'Peter Parker',
142
                    'code' => 'spider-man',
143
                ],
144
                [
145
                    'id' => 2,
146
                    'name' => 'Clark Kent',
147
                    'code' => 'super-man',
148
                ],
149
                [
150
                    'id' => 3,
151
                    'name' => 'Clock Werk',
152
                    'code' => 'clockwerk',
153
                ],
154
            ]
155
        ]);
156
157
        $this->assertTablesEqual($expected->getTable('heroes'), $actual);
158
    }
159
}
160