Completed
Push — master ( f7e2e6...df6590 )
by Tomasz
03:07
created

DoctrineConfigTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 165
wmc 15
lcom 1
cbo 7
rs 10
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Gendoria\CruftFlake\Config;
10
11
use Doctrine\DBAL\Connection;
12
use Doctrine\DBAL\DriverManager;
13
use Doctrine\DBAL\Schema\Schema;
14
use Doctrine\DBAL\Types\Type;
15
use PDO;
16
use PHPUnit_Extensions_Database_TestCase;
17
18
/**
19
 * Description of DoctrineConfigTest
20
 *
21
 * @author Tomasz Struczyński <[email protected]>
22
 */
23
class DoctrineConfigTest extends PHPUnit_Extensions_Database_TestCase
24
{
25
    /**
26
     * Doctrine connection.
27
     * 
28
     * @var Connection
29
     */
30
    private $connection;
31
    
32
    protected function setUp()
33
    {
34
        parent::setUp();
35
    }
36
    
37
    protected function getConnection()
38
    {
39
        $pdo = new PDO('sqlite::memory:');
40
        
41
        $this->connection = DriverManager::getConnection(array('pdo' => $pdo));
42
        DoctrineConfig::createTable($this->connection);
43
        
44
        return $this->createDefaultDBConnection($pdo, ':memory:');
45
    }
46
47
    protected function getDataSet()
48
    {
49
        return $this->createArrayDataSet(array());
50
    }
51
    
52
    public function testGetSingleMachineId()
53
    {
54
        $conn = $this->getDatabaseTester()->getConnection();
55
        $this->assertEquals(0, $conn->getRowCount(DoctrineConfig::DEFAULT_TABLE_NAME), "Pre-Condition");
56
        
57
        $config = new DoctrineConfig($this->connection);
58
        $this->assertEquals(0, $config->getMachine());
59
        $this->assertEquals(1, $conn->getRowCount(DoctrineConfig::DEFAULT_TABLE_NAME), "Inserting failed");
60
    }
61
    
62
    public function testGetSingleMachineIdException()
63
    {
64
        $this->setExpectedException('\RuntimeException', 'Cannot acquire machine ID');
65
        $conn = $this->getDatabaseTester()->getConnection();
66
        $this->assertEquals(0, $conn->getRowCount(DoctrineConfig::DEFAULT_TABLE_NAME), "Pre-Condition");
67
        
68
        $config = new DoctrineConfig($this->connection);
69
        $this->connection->close();
70
        $config->getMachine();
71
    }    
72
    
73
    public function testGetMultipleMachineIds()
74
    {
75
        $conn = $this->getDatabaseTester()->getConnection();
76
        
77
        $config1 = new DoctrineConfig($this->connection);
78
        $config2 = new DoctrineConfig($this->connection);
79
        $this->assertEquals(0, $config1->getMachine());
80
        $this->assertEquals(1, $config2->getMachine());
81
        $this->assertEquals(2, $conn->getRowCount(DoctrineConfig::DEFAULT_TABLE_NAME), "Inserting failed");
82
    }
83
    
84
    public function testDestroy()
85
    {
86
        $conn = $this->getDatabaseTester()->getConnection();
87
        
88
        $config1 = new DoctrineConfig($this->connection);
89
        $this->assertEquals(0, $config1->getMachine());
90
        $this->assertEquals(1, $conn->getRowCount(DoctrineConfig::DEFAULT_TABLE_NAME), "Inserting failed");
91
        $config1 = null;
92
        $this->assertEquals(0, $conn->getRowCount(DoctrineConfig::DEFAULT_TABLE_NAME), "Destroy failed");
93
        //When we create new config, it should have ID equals 1.
94
        $config2 = new DoctrineConfig($this->connection);
95
        $this->assertEquals(0, $config2->getMachine());
96
        $this->assertEquals(1, $conn->getRowCount(DoctrineConfig::DEFAULT_TABLE_NAME), "Inserting failed");
97
    }    
98
    
99
    public function testGetMultipleMachineIdsWithDestroy()
100
    {
101
        $conn = $this->getDatabaseTester()->getConnection();
102
        
103
        $config1 = new DoctrineConfig($this->connection);
104
        $config2 = new DoctrineConfig($this->connection);
105
        $config3 = new DoctrineConfig($this->connection);
106
        $config4 = new DoctrineConfig($this->connection);
107
        $this->assertEquals(0, $config1->getMachine());
108
        $this->assertEquals(1, $config2->getMachine());
109
        $this->assertEquals(2, $config3->getMachine());
110
        $this->assertEquals(3, $conn->getRowCount(DoctrineConfig::DEFAULT_TABLE_NAME), "Inserting failed");
111
        $config2 = null;
112
        $this->assertEquals(1, $config4->getMachine());
113
        $this->assertEquals(3, $conn->getRowCount(DoctrineConfig::DEFAULT_TABLE_NAME), "Inserting failed");
114
    }
115
    
116
    public function testGetMultipleMachineIdsWithDestroyFirst()
117
    {
118
        $conn = $this->getDatabaseTester()->getConnection();
119
        
120
        $config1 = new DoctrineConfig($this->connection);
121
        $config2 = new DoctrineConfig($this->connection);
122
        $config3 = new DoctrineConfig($this->connection);
123
        $config4 = new DoctrineConfig($this->connection);
124
        $this->assertEquals(0, $config1->getMachine());
125
        $this->assertEquals(1, $config2->getMachine());
126
        $this->assertEquals(2, $config3->getMachine());
127
        $this->assertEquals(3, $conn->getRowCount(DoctrineConfig::DEFAULT_TABLE_NAME), "Inserting failed");
128
        $config1 = null;
129
        $this->assertEquals(0, $config4->getMachine());
130
        $this->assertEquals(3, $conn->getRowCount(DoctrineConfig::DEFAULT_TABLE_NAME), "Inserting failed");
131
    }
132
    
133
    public function testGetMultipleMachineIdsTooManyMachines()
134
    {
135
        $this->setExpectedException('\RuntimeException', 'Cannot acquire machine ID - too many machines present');
136
        $conn = $this->getDatabaseTester()->getConnection();
137
        
138
        $machines = array();
139
        for ($k=0; $k<1024; $k++) {
140
            $machines[$k] = new DoctrineConfig($this->connection);
141
            $this->assertEquals($k, $machines[$k]->getMachine());
142
        }
143
        $this->assertEquals(1024, $conn->getRowCount(DoctrineConfig::DEFAULT_TABLE_NAME), "Inserting failed");
144
        $connEx = new DoctrineConfig($this->connection);
145
        $connEx->getMachine();
146
    }
147
    
148
    
149
    public function testHeartBeatNoMachineId()
150
    {
151
        $this->getDatabaseTester()->getConnection();
152
        $config1 = new DoctrineConfig($this->connection);
153
        $this->assertFalse($config1->heartbeat());
154
    }
155
    
156
    public function testHeartBeatMultiple()
157
    {
158
        $this->getDatabaseTester()->getConnection();
159
        $config1 = new DoctrineConfig($this->connection);
160
        $this->assertEquals(0, $config1->getMachine());
161
        $this->assertFalse($config1->heartbeat());
162
        $this->assertFalse($config1->heartbeat());
163
    }
164
    
165
    public function testHeartBeatFetchNeeded()
166
    {
167
        $this->getDatabaseTester()->getConnection();
168
        $config1 = new DoctrineConfig($this->connection);
169
        $this->assertEquals(0, $config1->getMachine());
170
        $this->connection->createQueryBuilder()
171
            ->delete(DoctrineConfig::DEFAULT_TABLE_NAME)
172
            ->where('machine_id=0')
173
            ->execute();
174
        $this->assertTrue($config1->heartbeat());
175
    }
176
    
177
    public function testHeartBeatException()
178
    {
179
        $this->setExpectedException('\RuntimeException', 'Counld not connect to database');
180
        $this->getDatabaseTester()->getConnection();
181
        $config1 = new DoctrineConfig($this->connection);
182
        $this->assertEquals(0, $config1->getMachine());
183
        $this->connection->close();
184
        $config1->heartbeat();
185
    }    
186
    
187
}
188