DBALMysqlResourceInsertNoLoadTest::getConnection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 2
nc 2
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 Maketok\DataMigration\Storage\Db\DBALMysqlResourceInsertNoLoad;
8
use PHPUnit_Extensions_Database_DataSet_IDataSet;
9
use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
10
11
class DBALMysqlResourceInsertNoLoadTest extends \PHPUnit_Extensions_Database_TestCase
12
{
13
    /**
14
     * @var ConfigInterface
15
     */
16
    private $config;
17
    /**
18
     * @var DBALMysqlResource
19
     */
20
    private $resource;
21
    /**
22
     * @var \PDO
23
     */
24
    private $pdo;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function getTearDownOperation()
30
    {
31
        return \PHPUnit_Extensions_Database_Operation_Factory::TRUNCATE();
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function setUp()
38
    {
39
        $config = include __DIR__ . '/assets/config.php';
40
        if (isset($config) && $config instanceof ConfigInterface) {
41
            $this->config = $config;
42
            $this->resource = new DBALMysqlResourceInsertNoLoad($this->config);
43
            $ref1 = new \ReflectionProperty(get_class($this->resource->getConnection()), '_conn');
44
            $ref1->setAccessible(true);
45
            $this->pdo = $ref1->getValue($this->resource->getConnection());
46
        } else {
47
            throw new \Exception("Can't find config file.");
48
        }
49
50
        parent::setUp();
51
52
        // assert that 2 pdo's are same
53
        $pdo1 = $this->getConnection()->getConnection();
54
        $pdo2 = $ref1->getValue($this->resource->getConnection());
55
56
        $this->assertSame($pdo1, $pdo2);
57
    }
58
59
    /**
60
     * Returns the test database connection.
61
     * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
62
     * @throws \Exception
63
     */
64
    protected function getConnection()
65
    {
66
        if (isset($this->pdo)) {
67
            return $this->createDefaultDBConnection($this->pdo);
68
        }
69
        throw new \Exception("Can't find pdo in config.");
70
    }
71
72
    /**
73
     * Returns the test dataset.
74
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
75
     */
76
    protected function getDataSet()
77
    {
78
        return $this->createXMLDataSet(__DIR__ . '/assets/testStructure.xml');
79
    }
80
81
    public function testLoad()
82
    {
83
        $file = __DIR__ . '/assets/toLoad.csv';
84
        $this->resource->loadData('customers', $file, true);
85
86
        $this->assertEquals(4, $this->getConnection()->getRowCount('customers'));
87
        // assert table
88
        $expected = $this->createXMLDataSet(__DIR__ . '/assets/expectedCustomersAfterLoadInserted.xml');
89
        $actual = $this->getConnection()->createQueryTable("customers", "SELECT * FROM `customers`");
90
        $this->assertTablesEqual($expected->getTable('customers'), $actual);
91
    }
92
}
93