Completed
Pull Request — master (#294)
by
unknown
03:26
created

PdoWriterTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Ddeboer\DataImport\Tests\Writer;
4
5
use Ddeboer\DataImport\Writer\PdoWriter;
6
7
class PdoWriterTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @var \PDO
11
     */
12
    private $pdo;
13
14
    public function setUp()
15
    {
16
        $this->pdo = new \PDO('sqlite::memory:');
17
        $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); //important
18
        $this->pdo->exec('DROP TABLE IF EXISTS `example`');
19
        $this->pdo->exec('CREATE TABLE `example` (a TEXT, b TEXT)');
20
    }
21
22
    public function testValidWriteItem()
23
    {
24
        $writer = new PdoWriter($this->pdo, 'example');
25
        $writer->prepare();
26
        $writer->writeItem(array('a' => 'foo', 'b' => 'bar'));
27
        $writer->finish();
28
29
        $stmnt = $this->pdo->query('SELECT * FROM `example`');
30
        $this->assertEquals(
31
            array(array('a'=>'foo', 'b'=>'bar')),
32
            $stmnt->fetchAll(\PDO::FETCH_ASSOC),
33
            'database does not contain expected row'
34
        );
35
    }
36
37
    public function testValidWriteMultiple()
38
    {
39
        $writer = new PdoWriter($this->pdo, 'example');
40
        $writer->prepare();
41
        $writer->writeItem(array('a' => 'foo', 'b' => 'bar'));
42
        $writer->writeItem(array('a' => 'cat', 'b' => 'dog'));
43
        $writer->writeItem(array('a' => 'ac', 'b' => 'dc'));
44
        $writer->finish();
45
46
        $stmnt = $this->pdo->query('SELECT * FROM `example`');
47
        $this->assertEquals(
48
            array(array('a'=>'foo', 'b'=>'bar'), array('a'=>'cat', 'b'=>'dog'), array('a'=>'ac', 'b'=>'dc')),
49
            $stmnt->fetchAll(\PDO::FETCH_ASSOC),
50
            'database does not contain all expected rows'
51
        );
52
    }
53
54
    /**
55
     * @expectedException \Ddeboer\DataImport\Exception\WriterException
56
     */
57
    public function testWriteTooManyValues()
58
    {
59
        $writer = new PdoWriter($this->pdo, 'example');
60
        $writer->prepare();
61
        $writer->writeItem(array('foo', 'bar', 'baz')); //expects two
62
        $writer->finish();
63
    }
64
65
    /**
66
     * @expectedException \Ddeboer\DataImport\Exception\WriterException
67
     */
68
    public function testWriteToNonexistentTable()
69
    {
70
        $writer = new PdoWriter($this->pdo, 'foobar');
71
        $writer->prepare();
72
        $writer->writeItem(array('foo', 'bar'));
73
        $writer->finish();
74
    }
75
76
    /**
77
     * Tests PDO instance with silent errors.
78
     *
79
     * @expectedException \Ddeboer\DataImport\Exception\WriterException
80
     */
81
    public function testStatementCreateFailureWithNoException()
82
    {
83
        $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
84
85
        $writer = new PdoWriter($this->pdo, 'foob`ar');
86
        $writer->prepare();
87
        $writer->writeItem(array('foo', 'bar'));
88
        $writer->finish();
89
    }
90
91
    /**
92
     * Tests PDO instance with silent errors. First inert prepares the statement, second creates an exception.
93
     *
94
     * @expectedException \Ddeboer\DataImport\Exception\WriterException
95
     */
96
    public function testWriteFailureWithNoException()
97
    {
98
        $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
99
100
        $writer = new PdoWriter($this->pdo, 'example');
101
        $writer->prepare();
102
        $writer->writeItem(array('foo', 'bar'));
103
        $writer->writeItem(array('foo', 'bar', 'baz'));
104
        $writer->finish();
105
    }
106
}
107