l/DbalMigrationTest.php$1   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
wmc 1
lcom 0
cbo 0
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\Operation\Factory;
11
use PHPUnit\DbUnit\TestCase;
12
use SlayerBirden\DataFlow\DataBagInterface;
13
use SlayerBirden\DataFlow\Emitter\BlackHole;
14
use SlayerBirden\DataFlow\Pipe\FilterCallbackInterface;
15
use SlayerBirden\DataFlow\Pipe\MapperCallbackInterface;
16
use SlayerBirden\DataFlow\PipelineBuilder;
17
use SlayerBirden\DataFlow\Plumber;
18
use SlayerBirden\DataFlow\Provider\Dbal;
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 DbalMigrationTest extends TestCase
25
{
26
    /**
27
     * @var Connection
28
     */
29
    private $connection;
30
    private $emitter;
31
    private $pipeline;
32
33
    protected function setUp(): void
34
    {
35
        $params = require __DIR__ . '/config/db-config.php';
36
        $this->connection = DriverManager::getConnection($params);
37
38
        $schema = new Schema();
39
        // create Heroes Table
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
        // create Users Table
50
        $table = $schema->createTable('users');
51
        $table->addColumn('id', 'integer', [
52
            'autoincrement' => true,
53
        ]);
54
        $table->addColumn('first', 'string');
55
        $table->addColumn('last', 'string');
56
        $table->addColumn('code', 'string');
57
        $table->setPrimaryKey(['id']);
58
        $table->addUniqueIndex(['code']);
59
60
        $currentSchema = $this->connection->getSchemaManager()->createSchema();
61
        $sql = $currentSchema->getMigrateToSql($schema, $this->connection->getDatabasePlatform());
62
        array_walk($sql, function ($script) {
63
            $this->connection->executeUpdate($script);
64
        });
65
66
        parent::setUp();
67
        $this->emitter = new BlackHole();
68
        $utility = new WriterUtility($this->connection);
69
        $this->pipeline = (new PipelineBuilder($this->emitter))
70
            ->delete(['id'])
71
            ->filter(new class implements FilterCallbackInterface
72
            {
73
                public function __invoke(DataBagInterface $dataBag): bool
74
                {
75
                    return !empty($dataBag['code']);
76
                }
77
            })
78
            ->map('name', new class implements MapperCallbackInterface
79
            {
80
                public function __invoke($value, DataBagInterface $dataBag)
81
                {
82
                    return $dataBag['first'] . ' ' . $dataBag['last'];
83
                }
84
            })
85
            ->addSection(new Write(
86
                'heroes_write',
87
                $this->connection,
88
                'heroes',
89
                $utility,
90
                new UniqueIndexStrategy('heroes', $utility),
91
                $this->emitter
92
            ))
93
            ->build();
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    protected function getConnection()
100
    {
101
        $con = $this->connection->getWrappedConnection();
102
        if ($con instanceof PDOConnection) {
103
            return $this->createDefaultDBConnection($con);
104
        }
105
106
        throw new ConnectionException('Could not get PDO object.');
107
    }
108
109
    protected function getTearDownOperation()
110
    {
111
        return Factory::TRUNCATE();
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117
    protected function getDataSet()
118
    {
119
        return $this->createArrayDataSet([
120
            'users' => [
121
                [
122
                    'id' => 1,
123
                    'first' => 'John',
124
                    'last' => 'Doe',
125
                    'code' => ''
126
                ],
127
                [
128
                    'id' => 2,
129
                    'first' => 'Peter',
130
                    'last' => 'Parker',
131
                    'code' => 'spiderman'
132
                ],
133
            ],
134
            'heroes' => [],
135
        ]);
136
    }
137
138
    public function testDbSimplePipeFlow()
139
    {
140
        $provider = new Dbal('db_users', $this->connection, 'users');
141
142
        (new Plumber($provider, $this->pipeline, $this->emitter))->pour();
143
144
        $actual = $this->getConnection()->createQueryTable('heroes', 'SELECT * FROM `heroes`');
145
        $expected = $this->createArrayDataSet([
146
            'heroes' => [
147
                [
148
                    'id' => 1,
149
                    'name' => 'Peter Parker',
150
                    'code' => 'spiderman',
151
                ],
152
            ]
153
        ]);
154
155
        $this->assertTablesEqual($expected->getTable('heroes'), $actual);
156
    }
157
158
    public function testGetSize()
159
    {
160
        $provider = new Dbal('db_users', $this->connection, 'users');
161
162
        $this->assertSame(2, $provider->getEstimatedSize());
163
    }
164
}
165