DoctrineDbalTestCase   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 70
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 2
A isDoctrineDbalNotAvailable() 0 4 1
A getConnection() 0 9 1
A createSchema() 0 22 2
A insertData() 0 9 3
1
<?php
2
3
namespace Pagerfanta\Tests\Adapter;
4
5
use Doctrine\DBAL\DriverManager;
6
use Doctrine\DBAL\Query\QueryBuilder;
7
use Doctrine\DBAL\Schema\Schema;
8
use PHPUnit\Framework\TestCase;
9
10
abstract class DoctrineDbalTestCase extends TestCase
11
{
12
    /**
13
     * @var QueryBuilder
14
     */
15
    protected $qb;
16
17
    protected function setUp()
18
    {
19
        if ($this->isDoctrineDbalNotAvailable()) {
20
            $this->markTestSkipped('Doctrine DBAL is not available');
21
        }
22
23
        $conn = $this->getConnection();
24
25
        $this->createSchema($conn);
26
        $this->insertData($conn);
27
28
        $this->qb = new QueryBuilder($conn);
29
        $this->qb->select('p.*')->from('posts', 'p');
30
    }
31
32
    private function isDoctrineDbalNotAvailable()
33
    {
34
        return !class_exists('Doctrine\DBAL\DriverManager');
35
    }
36
37
    private function getConnection()
38
    {
39
        $params = $conn = array(
0 ignored issues
show
Unused Code introduced by
$conn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
            'driver' => 'pdo_sqlite',
41
            'memory' => true,
42
        );
43
44
        return DriverManager::getConnection($params);
45
    }
46
47
    private function createSchema($conn)
48
    {
49
        $schema = new Schema();
50
        $posts = $schema->createTable('posts');
51
        $posts->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
52
        $posts->addColumn('username', 'string', array('length' => 32));
53
        $posts->addColumn('post_content', 'text');
54
        $posts->setPrimaryKey(array('id'));
55
56
        $comments = $schema->createTable('comments');
57
        $comments->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
58
        $comments->addColumn('post_id', 'integer', array('unsigned' => true));
59
        $comments->addColumn('username', 'string', array('length' => 32));
60
        $comments->addColumn('content', 'text');
61
        $comments->setPrimaryKey(array('id'));
62
63
        $queries = $schema->toSql($conn->getDatabasePlatform()); // get queries to create this schema.
64
65
        foreach ($queries as $sql) {
66
            $conn->executeQuery($sql);
67
        }
68
    }
69
70
    private function insertData($conn)
71
    {
72
        for ($i = 1; $i <= 50; $i++) {
73
            $conn->insert('posts', array('username' => 'Jon Doe', 'post_content' => 'Post #'.$i));
74
            for ($j = 1; $j <= 5; $j++) {
75
                $conn->insert('comments', array('post_id' => $i, 'username' => 'Jon Doe', 'content' => 'Comment #'.$j));
76
            }
77
        }
78
    }
79
}
80