Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
use Piwik\Db;
use Piwik\Sequence;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
/**
* Class Core_SequenceTest
*
* @group Core
* @group Sequence
*/
class Core_SequenceTest extends IntegrationTestCase
{
/**
* @var Sequence
*/
private $sequence;
public function setUp()
{
parent::setUp();
$this->sequence = new Sequence('mySequence0815');
$this->sequence->create();
}
public function test_create_shouldAddNewSequenceWithInitalId1()
{
$sequence = $this->getEmptySequence();
$id = $sequence->create();
$this->assertSame(0, $id);
// verify
$id = $sequence->getCurrentId();
$this->assertSame(0, $id);
}
public function test_create_WithCustomInitialValue()
{
$sequence = $this->getEmptySequence();
$id = $sequence->create(11);
$this->assertSame(11, $id);
// verify
$id = $sequence->getCurrentId();
$this->assertSame(11, $id);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Duplicate entry
*/
public function test_create_shouldFailIfSequenceAlreadyExists()
{
$this->sequence->create();
}
public function test_getNextId_shouldGenerateNextId()
{
$this->assertNextIdGenerated(1);
$this->assertNextIdGenerated(2);
$this->assertNextIdGenerated(3);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Sequence 'notCreatedSequence' not found
*/
public function test_getNextId_shouldFailIfThereIsNoSequenceHavingThisName()
{
$sequence = $this->getEmptySequence();
$sequence->getNextId();
}
private function assertNextIdGenerated($expectedId)
{
$id = $this->sequence->getNextId();
$this->assertSame($expectedId, $id);
// verify
$id = $this->sequence->getCurrentId();
$this->assertSame($expectedId, $id);
}
public function test_getCurrentId_shouldReturnTheCurrentIdAsInt()
{
$id = $this->sequence->getCurrentId();
$this->assertSame(0, $id);
}
public function test_getCurrentId_shouldReturnNullIfSequenceDoesNotExist()
{
$sequence = $this->getEmptySequence();
$id = $sequence->getCurrentId();
$this->assertNull($id);
}
private function getEmptySequence()
{
return new Sequence('notCreatedSequence');
}
}