DBALMysqlResourceTest::testCreateTmpTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.7
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Maketok\DataMigration\IntegrationTest\Storage\Db;
4
5
use Maketok\DataMigration\Action\ConfigInterface;
6
use Maketok\DataMigration\Storage\Db\DBALMysqlResource;
7
use PHPUnit_Extensions_Database_DataSet_IDataSet;
8
use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
9
10
class DBALMysqlResourceTest extends \PHPUnit_Extensions_Database_TestCase
11
{
12
    /**
13
     * @var ConfigInterface
14
     */
15
    private $config;
16
    /**
17
     * @var DBALMysqlResource
18
     */
19
    private $resource;
20
    /**
21
     * @var \PDO
22
     */
23
    private $pdo;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function getTearDownOperation()
29
    {
30
        return \PHPUnit_Extensions_Database_Operation_Factory::TRUNCATE();
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function setUp()
37
    {
38
        $config = include __DIR__ . '/assets/config.php';
39
        if (isset($config) && $config instanceof ConfigInterface) {
40
            $this->config = $config;
41
            $this->resource = new DBALMysqlResource($this->config);
42
            $ref1 = new \ReflectionProperty(get_class($this->resource->getConnection()), '_conn');
43
            $ref1->setAccessible(true);
44
            $this->pdo = $ref1->getValue($this->resource->getConnection());
45
        } else {
46
            throw new \Exception("Can't find config file.");
47
        }
48
49
        parent::setUp();
50
51
        // assert that 2 pdo's are same
52
        $pdo1 = $this->getConnection()->getConnection();
53
        $pdo2 = $ref1->getValue($this->resource->getConnection());
54
55
        $this->assertSame($pdo1, $pdo2);
56
    }
57
58
    /**
59
     * Returns the test database connection.
60
     * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
61
     * @throws \Exception
62
     */
63
    protected function getConnection()
64
    {
65
        if (isset($this->pdo)) {
66
            return $this->createDefaultDBConnection($this->pdo);
67
        }
68
        throw new \Exception("Can't find pdo in config.");
69
    }
70
71
    /**
72
     * Returns the test dataset.
73
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
74
     */
75
    protected function getDataSet()
76
    {
77
        return $this->createXMLDataSet(__DIR__ . '/assets/testStructure.xml');
78
    }
79
80
    public function testGetRowCount()
81
    {
82
        $this->assertEquals(2, $this->getConnection()->getRowCount('customers'));
83
    }
84
85
    public function testCreateTmpTable()
86
    {
87
        $this->resource->createTmpTable('tmp_123', [
88
            'id' => 'integer',
89
            'testnull' => ['text', ['notnull' => false]]
90
        ]);
91
92
        $builder = $this->resource->getConnection()->createQueryBuilder();
93
        $builder->insert('tmp_123')->values(['id' => 1]);
94
        $this->resource->getConnection()->executeUpdate($builder->getSQL());
95
96
        $this->assertEquals(1, $this->getConnection()->getRowCount('tmp_123'));
97
        // assert table
98
        $expected = $this->createXMLDataSet(__DIR__ . '/assets/tmp_123.xml');
99
        $actual = $this->getConnection()->createQueryTable("tmp_123", "SELECT * FROM `tmp_123`");
100
        $this->assertTablesEqual($expected->getTable('tmp_123'), $actual);
101
    }
102
103
    /**
104
     * @depends testCreateTmpTable
105
     */
106
    public function testDelete()
107
    {
108
        $this->resource->createTmpTable('tmp_123', ['id' => 'integer']);
109
110
        $builder = $this->resource->getConnection()->createQueryBuilder();
111
        $builder->insert('tmp_123')->values(['id' => 1]);
112
        $this->resource->getConnection()->executeUpdate($builder->getSQL());
113
114
        // now delete
115
        $res = $this->resource->deleteUsingTempPK('customers', 'tmp_123');
116
117
        $this->assertEquals(1, $res);
118
119
        // 1 customer left
120
        $this->assertEquals(1, $this->getConnection()->getRowCount('customers'));
121
        // assert table
122
        $expected = $this->createXMLDataSet(__DIR__ . '/assets/expectedCustomersAfterDelete.xml');
123
        $actual = $this->getConnection()->createQueryTable("customers", "SELECT * FROM `customers`");
124
        $this->assertTablesEqual($expected->getTable('customers'), $actual);
125
    }
126
127
    public function testLoad()
128
    {
129
        $file = __DIR__ . '/assets/toLoad.csv';
130
        $this->resource->loadData('customers', $file, true);
131
132
        $this->assertEquals(4, $this->getConnection()->getRowCount('customers'));
133
        // assert table
134
        $expected = $this->createXMLDataSet(__DIR__ . '/assets/expectedCustomersAfterLoad.xml');
135
        $actual = $this->getConnection()->createQueryTable("customers", "SELECT * FROM `customers`");
136
        $this->assertTablesEqual($expected->getTable('customers'), $actual);
137
    }
138
139
    /**
140
     * @depends testCreateTmpTable
141
     */
142
    public function testMove1()
143
    {
144
        $this->resource->createTmpTable('tmp_123', ['id' => 'integer', 'firstname' => 'string']);
145
146
        $this->resource->move('customers', 'tmp_123', ['id', 'firstname'], [], ['id'], 'DESC');
147
148
        $this->assertEquals(2, $this->getConnection()->getRowCount('tmp_123'));
149
        // assert table
150
        $expected = $this->createXMLDataSet(__DIR__ . '/assets/expectedCustomersAfterMove1.xml');
151
        $actual = $this->getConnection()->createQueryTable("tmp_123", "SELECT * FROM `tmp_123`");
152
        $this->assertTablesEqual($expected->getTable('tmp_123'), $actual);
153
    }
154
155
    /**
156
     * @depends testCreateTmpTable
157
     */
158
    public function testMove2()
159
    {
160
        $this->resource->createTmpTable('tmp_123', ['id' => 'integer', 'firstname' => 'string']);
161
162
        $this->resource->move(
163
            'customers',
164
            'tmp_123',
165
            ['id', 'firstname'],
166
            ['firstname' => [
167
                'nin' => ['Bob']
168
            ]]
169
        );
170
171
        $this->assertEquals(1, $this->getConnection()->getRowCount('tmp_123'));
172
        // assert table
173
        $expected = $this->createXMLDataSet(__DIR__ . '/assets/expectedCustomersAfterMove2.xml');
174
        $actual = $this->getConnection()->createQueryTable("tmp_123", "SELECT * FROM `tmp_123`");
175
        $this->assertTablesEqual($expected->getTable('tmp_123'), $actual);
176
    }
177
178
    public function testDump()
179
    {
180
        $expected1 = array(
181
            array('id' => '1', 'firstname' => 'Oleg'),
182
        );
183
        $this->assertSame($expected1, $this->resource->dumpData('customers', ['id', 'firstname'], 1));
184
        $expected2 = array(
185
            array('id' => '2', 'firstname' => 'Bob', 'lastname' => 'Bobbington',
186
                'age' => '35', 'email' => '[email protected]')
187
        );
188
        $this->assertSame($expected2, $this->resource->dumpData('customers', [], 1, 1));
189
    }
190
}
191