Passed
Push — fix-9097 ( 9d3fd1...fafd35 )
by Sam
07:01
created

PDODatabaseTest::testPreparedStatements()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 77
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 44
c 1
b 1
f 0
nc 2
nop 0
dl 0
loc 77
rs 9.216

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\ORM\Tests;
4
5
use SilverStripe\ORM\Connect\PDOQuery;
6
use SilverStripe\ORM\DB;
7
use SilverStripe\ORM\Connect\PDOConnector;
8
use SilverStripe\ORM\Queries\SQLUpdate;
9
use SilverStripe\Dev\SapphireTest;
10
11
/**
12
 * @skipUpgrade
13
 */
14
class PDODatabaseTest extends SapphireTest
15
{
16
    protected static $fixture_file = 'MySQLDatabaseTest.yml';
17
18
    protected static $extra_dataobjects = array(
19
        MySQLDatabaseTest\Data::class
20
    );
21
22
    public function testPreparedStatements()
23
    {
24
        if (!(DB::get_connector() instanceof PDOConnector)) {
25
            $this->markTestSkipped('This test requires the current DB connector is PDO');
26
        }
27
28
        // Test preparation of equivalent statemetns
29
        $result1 = DB::get_connector()->preparedQuery(
30
            'SELECT "Sort", "Title" FROM "MySQLDatabaseTest_Data" WHERE "Sort" > ? ORDER BY "Sort"',
31
            array(0)
32
        );
33
34
        $result2 = DB::get_connector()->preparedQuery(
35
            'SELECT "Sort", "Title" FROM "MySQLDatabaseTest_Data" WHERE "Sort" > ? ORDER BY "Sort"',
36
            array(2)
37
        );
38
        $this->assertInstanceOf(PDOQuery::class, $result1);
39
        $this->assertInstanceOf(PDOQuery::class, $result2);
40
41
        // Also select non-prepared statement
42
        $result3 = DB::get_connector()->query('SELECT "Sort", "Title" FROM "MySQLDatabaseTest_Data" ORDER BY "Sort"');
43
        $this->assertInstanceOf(PDOQuery::class, $result3);
44
45
        // Iterating one level should not buffer, but return the right result
46
        $this->assertEquals(
47
            array(
48
                'Sort' => 1,
49
                'Title' => 'First Item'
50
            ),
51
            $result1->next()
52
        );
53
        $this->assertEquals(
54
            array(
55
                'Sort' => 2,
56
                'Title' => 'Second Item'
57
            ),
58
            $result1->next()
59
        );
60
61
        // Test first
62
        $this->assertEquals(
63
            array(
64
                'Sort' => 1,
65
                'Title' => 'First Item'
66
            ),
67
            $result1->first()
68
        );
69
70
        // Test seek
71
        $result1->seek(1);
72
        $this->assertEquals(
73
            array(
74
                'Sort' => 2,
75
                'Title' => 'Second Item'
76
            ),
77
            $result1->next()
78
        );
79
80
        // Test count
81
        $this->assertEquals(4, $result1->numRecords());
82
83
        // Test second statement
84
        $this->assertEquals(
85
            array(
86
                'Sort' => 3,
87
                'Title' => 'Third Item'
88
            ),
89
            $result2->next()
90
        );
91
92
        // Test non-prepared query
93
        $this->assertEquals(
94
            array(
95
                'Sort' => 1,
96
                'Title' => 'First Item'
97
            ),
98
            $result3->next()
99
        );
100
    }
101
102
    public function testAffectedRows()
103
    {
104
        if (!(DB::get_connector() instanceof PDOConnector)) {
105
            $this->markTestSkipped('This test requires the current DB connector is PDO');
106
        }
107
108
        $query = new SQLUpdate('"MySQLDatabaseTest_Data"');
109
        $query->setAssignments(array('"Title"' => 'New Title'));
110
111
        // Test update which affects no rows
112
        $query->setWhere(array('"Title"' => 'Bob'));
113
        $result = $query->execute();
114
        $this->assertInstanceOf(PDOQuery::class, $result);
115
        $this->assertEquals(0, DB::affected_rows());
116
117
        // Test update which affects some rows
118
        $query->setWhere(array('"Title"' => 'First Item'));
119
        $result = $query->execute();
120
        $this->assertInstanceOf(PDOQuery::class, $result);
121
        $this->assertEquals(1, DB::affected_rows());
122
    }
123
}
124