blob: 75a42f4a6ce5280d1a07632f7d2118a36192955c (
plain)
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
113
114
115
116
117
118
119
120
121
122
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration;
use Piwik\Db;
use Piwik\Sequence;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
/**
* @group Core
* @group Sequence
*/
class SequenceTest extends IntegrationTestCase
{
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);
}
public function test_create_shouldFailIfSequenceAlreadyExists()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Duplicate entry');
$sequence = $this->getExistingSequence();
$sequence->create();
}
public function test_getNextId_shouldGenerateNextId()
{
$sequence = $this->getExistingSequence();
$this->assertNextIdGenerated($sequence, 1);
$this->assertNextIdGenerated($sequence, 2);
$this->assertNextIdGenerated($sequence, 3);
}
public function test_getNextId_shouldFailIfThereIsNoSequenceHavingThisName()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Sequence \'notCreatedSequence\' not found');
$sequence = $this->getEmptySequence();
$sequence->getNextId();
}
public function test_getCurrentId_shouldReturnTheCurrentIdAsInt()
{
$sequence = $this->getExistingSequence();
$id = $sequence->getCurrentId();
$this->assertSame(0, $id);
}
public function test_getCurrentId_shouldReturnNullIfSequenceDoesNotExist()
{
$sequence = $this->getEmptySequence();
$id = $sequence->getCurrentId();
$this->assertNull($id);
}
public function test_exists_shouldReturnTrueIfSequenceExist()
{
$sequence = $this->getExistingSequence();
$this->assertTrue($sequence->exists());
}
public function test_exists_shouldReturnFalseIfSequenceExist()
{
$sequence = $this->getEmptySequence();
$this->assertFalse($sequence->exists());
}
private function assertNextIdGenerated(Sequence $sequence, $expectedId)
{
$id = $sequence->getNextId();
$this->assertSame($expectedId, $id);
// verify
$id = $sequence->getCurrentId();
$this->assertSame($expectedId, $id);
}
private function getEmptySequence()
{
return new Sequence('notCreatedSequence');
}
private function getExistingSequence()
{
$sequence = new Sequence('mySequence0815');
$sequence->create();
return $sequence;
}
}
|