DBALMysqlResourceInsertNoLoadTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 73
c 0
b 0
f 0
wmc 2
lcom 1
cbo 6
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 30 1
A testLoad() 0 26 1
1
<?php
2
3
namespace Maketok\DataMigration\Storage\Db;
4
5
use Doctrine\DBAL\Platforms\MySqlPlatform;
6
use org\bovigo\vfs\vfsStream;
7
use org\bovigo\vfs\vfsStreamDirectory;
8
9
class DBALMysqlResourceInsertNoLoadTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @var DBALMysqlResourceInsertNoLoad
13
     */
14
    private $resource;
15
    /**
16
     * @var vfsStreamDirectory
17
     */
18
    private $root;
19
    /**
20
     * @var \PHPUnit_Framework_MockObject_MockObject
21
     */
22
    protected $connection;
23
24
    public function setUp()
25
    {
26
        $driverMock = $this->getMockBuilder('\Doctrine\DBAL\Driver\PDOMySql\Driver')->getMock();
27
        $platform = new MySqlPlatform();
28
        $driverMock->expects($this->any())->method('getDatabasePlatform')->willReturn($platform);
29
        $con = $this->getMockBuilder('\Doctrine\DBAL\Driver\Connection')->getMock();
30
        $con->expects($this->any())->method('quote')->willReturnCallback(function ($var) {
31
            return '\'' . $var . '\'';
32
        });
33
        $this->connection = $this->getMockBuilder('\Doctrine\DBAL\Connection')
34
            ->setConstructorArgs([
35
                ['pdo' => $con],
36
                $driverMock
37
            ])
38
            ->setMethods(['executeUpdate'])
39
            ->getMock();
40
        $driverMock = $this->getMockBuilder('\Maketok\DataMigration\Storage\Db\DBALMysqlResourceInsertNoLoad')
41
            ->setMethods(null)
42
            ->disableOriginalConstructor()
43
            ->getMock();
44
        $refProperty = new \ReflectionProperty(
45
            '\Maketok\DataMigration\Storage\Db\DBALMysqlResourceInsertNoLoad',
46
            'connection'
47
        );
48
        $refProperty->setAccessible(true);
49
        $refProperty->setValue($driverMock, $this->connection);
50
51
        $this->resource = $driverMock;
52
        $this->root = vfsStream::setup('root');
53
    }
54
55
    public function testLoad()
56
    {
57
        $rows = <<<CSV
58
data1,2,"buzz bar"
59
kk2,3,"curly \nhair"
60
61
CSV;
62
        $sql = <<<MYSQL
63
INSERT INTO `table1` 
64
VALUES (?,?,?)
65
MYSQL;
66
67
        $this->connection->expects($this->exactly(2))->method('executeUpdate')->withConsecutive(
68
            [
69
                $this->equalTo($sql), $this->equalTo(['data1', '2', "buzz bar"])
70
            ],
71
            [
72
                $this->equalTo($sql), $this->equalTo(['kk2', '3', "curly \nhair"])
73
            ]
74
        );
75
        $file = vfsStream::newFile('test.csv');
76
        $file->setContent($rows);
77
        $file->at($this->root);
78
79
        $this->resource->loadData('table1', vfsStream::url('root/test.csv'));
80
    }
81
}
82