Test Failed
Push — master ( 87bd16...66bd2c )
by Webnet
04:04
created

SystemTestTrait::getConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 16
rs 9.9666
1
<?php
2
3
namespace WebnetFr\DatabaseAnonymizerBundle\Tests\System;
4
5
use Doctrine\DBAL\Configuration;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\DriverManager;
8
use Doctrine\DBAL\Exception\DriverException;
9
use Doctrine\DBAL\Schema\Schema;
10
11
/**
12
 * @author Vlad Riabchenko <[email protected]>
13
 */
14
trait SystemTestTrait
15
{
16
    /**
17
     * @param bool $toDatabase
18
     *
19
     * @return Connection
20
     *
21
     * @throws \Doctrine\DBAL\DBALException
22
     */
23
    private function getConnection(bool $toDatabase = true): Connection
24
    {
25
        $params = [
26
            'driver' => $GLOBALS['db_type'],
27
            'host' => $GLOBALS['db_host'],
28
            'user' => $GLOBALS['db_username'],
29
            'password' => $GLOBALS['db_password'],
30
        ];
31
32
        if ($toDatabase) {
33
            $params += ['dbname' => $GLOBALS['db_name']];
34
        }
35
36
        $config = new Configuration();
37
38
        return DriverManager::getConnection($params, $config);
39
    }
40
41
    /**
42
     * @param string $url
43
     * @param string $name
44
     *
45
     * @throws \Doctrine\DBAL\DBALException
46
     */
47
    public function regenerateUsersOrders(): void
48
    {
49
        $connection = $this->getConnection(false);
50
        $schemaManager = $connection->getSchemaManager();
51
52
        try {
53
            $schemaManager->dropDatabase($GLOBALS['db_name']);
54
        } catch (\Exception $e) {
55
            // If tardet database doesn't exist.
56
        }
57
58
        $schemaManager->createDatabase($GLOBALS['db_name']);
59
        $connection->close();
60
61
        $connection = $this->getConnection();
62
        $schemaManager = $connection->getSchemaManager();
63
        $schema = $schemaManager->createSchema();
64
65
        $user = $schema->createTable('users');
66
        $user->addColumn('id', 'integer', ['id' => true, 'unsigned' => true, 'unique']);
67
        $user->addColumn('email', 'string', ['length' => 256, 'notnull' => false]);
68
        $user->addColumn('firstname', 'string', ['length' => 256, 'notnull' => false]);
69
        $user->addColumn('lastname', 'string', ['length' => 256, 'notnull' => false]);
70
        $user->addColumn('birthdate', 'date', ['notnull' => false]);
71
        $user->addColumn('phone', 'string', ['length' => 20, 'notnull' => false]);
72
        $user->addColumn('password', 'string', ['length' => 64, 'notnull' => false]);
73
        $user->setPrimaryKey(['id']);
74
        $schemaManager->createTable($user);
75
76
        $order = $schema->createTable('orders');
77
        $order->addColumn('id', 'integer', ['unsigned' => true]);
78
        $order->addColumn('address', 'string', ['length' => 256, 'notnull' => false]);
79
        $order->addColumn('street_address', 'string', ['length' => 64, 'notnull' => false]);
80
        $order->addColumn('zip_code', 'string', ['length' => 10, 'notnull' => false]);
81
        $order->addColumn('city', 'string', ['length' => 64, 'notnull' => false]);
82
        $order->addColumn('country', 'string', ['length' => 64, 'notnull' => false]);
83
        $order->addColumn('comment', 'text', ['notnull' => false]);
84
        $order->addColumn('created_at', 'datetime', ['notnull' => false]);
85
        $order->addColumn('user_id', 'integer', ['unsigned' => true, 'notnull' => false]);
86
        $order->setPrimaryKey(['id']);
87
        $order->addForeignKeyConstraint($user, ['user_id'], ['id']);
88
        $schemaManager->createTable($order);
89
90
        $productivity = $schema->createTable('productivity');
91
        $productivity->addColumn('day', 'date', ['notnull' => false]);
92
        $productivity->addColumn('user_id', 'integer', ['unsigned' => true, 'notnull' => false]);
93
        $productivity->addColumn('feedback', 'text', ['notnull' => false]);
94
        $productivity->setPrimaryKey(['day', 'user_id']);
95
        $productivity->addForeignKeyConstraint($user, ['user_id'], ['id']);
96
        $schemaManager->createTable($productivity);
97
98
        foreach (range(1, 10) as $i) {
99
            $connection->createQueryBuilder()
100
                ->insert('users')
101
                ->values(['id' => $i])
102
                ->execute();
103
        }
104
105
        foreach (range(1, 20) as $i) {
0 ignored issues
show
Comprehensibility Bug introduced by
$i is overwriting a variable from outer foreach loop.
Loading history...
106
            $connection->createQueryBuilder()
107
                ->insert('orders')
108
                ->values(['id' => $i, 'user_id' => mt_rand(1, 10)])
109
                ->execute();
110
        }
111
112
        foreach (range(1, 30) as $i) {
0 ignored issues
show
Comprehensibility Bug introduced by
$i is overwriting a variable from outer foreach loop.
Loading history...
113
            $connection->createQueryBuilder()
114
                ->insert('productivity')
115
                ->values([
116
                    'day' => $connection->quote(new \DateTime("+$i days"), 'date'),
117
                    'user_id' => mt_rand(1, 10)
118
                ])
119
                ->execute();
120
        }
121
122
        $connection->close();
123
    }
124
}
125