TestCaseTrait::setConnection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace Pumpkin\Database;
3
4
/**
5
 * Class TestCaseTrait
6
 * @package Pumpkin\Database
7
 * @author Raphaël Lefebvre <[email protected]>
8
 */
9
trait TestCaseTrait
10
{
11
    use \Pumpkin\Test\TestCaseTrait;
12
13
    /**
14
     * main db connection.
15
     *
16
     * @var \PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
17
     */
18
    private static $connection;
19
20
    /**
21
     * @var \PHPUnit_Extensions_Database_DataSet_IDataSet
22
     */
23
    private $dataSet;
24
25
    /**
26
     * @param \PDO $connection
27
     * @param string $schema
28
     * @return mixed
29
     */
30
    abstract protected function createDefaultDBConnection(\PDO $connection, $schema = '');
31
32
    /**
33
     * {@inheritDoc}
34
     *
35
     * @return \PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
36
     */
37 1
    protected function getConnection()
38
    {
39 1
        if (null === self::$connection) {
40 1
            $this->setConnection($this->buildConnection());
41 1
        }
42 1
        return self::$connection;
43
    }
44
45
    /**
46
     * Setter of $connection.
47
     *
48
     * @param \PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection $connection
49
     */
50 1
    private function setConnection(\PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection $connection)
51
    {
52 1
        self::$connection = $connection;
53 1
    }
54
55
    /**
56
     * Builder of connection.
57
     *
58
     * @return \PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
59
     */
60 1
    private function buildConnection()
0 ignored issues
show
Coding Style Naming introduced by
The variable $GLOBALS is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
buildConnection uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
61
    {
62 1
        return $this->createDefaultDBConnection(
63 1
            new \PDO(
64 1
                $GLOBALS['db_dsn'],
65 1
                $GLOBALS['db_username'],
66 1
                $GLOBALS['db_password']
67 1
            )
68 1
        );
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     *
74
     * @return \PHPUnit_Extensions_Database_DataSet_IDataSet
75
     */
76 2
    protected function getDataSet()
77
    {
78 2
        if (null === $this->dataSet) {
79 2
            $this->setDataSet($this->buildDataSet());
80 2
        }
81 2
        return $this->dataSet;
82
    }
83
84
    /**
85
     * @param \PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
86
     */
87 2
    private function setDataSet(\PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
88
    {
89 2
        $this->dataSet = $dataSet;
90 2
    }
91
92
    /**
93
     * @return \PHPUnit_Extensions_Database_DataSet_DefaultDataSet
94
     */
95 2
    private function buildDataSet()
96
    {
97 2
        $result = new \PHPUnit_Extensions_Database_DataSet_DefaultDataSet();
98 2
        foreach ($this->getTest()->getTables() as $table) {
99 2
            $result->addTable($table->toPhpUnitTable());
100 2
        }
101 2
        return $result;
102
    }
103
}
104