Failed Conditions
Pull Request — master (#3162)
by Sergei
65:26
created

testGuidShouldBeUniqueAndAscending()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional\Platform;
4
5
use Doctrine\DBAL\Platforms\SqlitePlatform;
6
use Doctrine\DBAL\Statement;
7
use Doctrine\Tests\DbalFunctionalTestCase;
8
use function end;
9
10
class GuidExpressionTest extends DbalFunctionalTestCase
11
{
12
    public function testGuidShouldMatchPattern()
13
    {
14
        $value = $this->_conn->query($this->getSelectGuidSql())->fetchColumn();
15
16
        self::assertRegExp(
17
            '/[0-9A-F]{8}\-[0-9A-F]{4}\-[0-9A-F]{4}\-[8-9A-B][0-9A-F]{3}\-[0-9A-F]{12}/i',
18
            $value
19
        );
20
    }
21
22
    /**
23
     * This test does (of course) not proof that all generated GUIDs are
24
     * random, it should however provide some basic confidence.
25
     */
26
    public function testGuidShouldBeUniqueAndAscending()
27
    {
28
        $platform = $this->_conn->getDatabasePlatform();
29
30
        if ($platform instanceof SqlitePlatform) {
31
            $this->markTestSkipped('Currently incorrectly implemented in SQLite platform');
32
        }
33
34
        $statement = $this->_conn->prepare($this->getSelectGuidSql());
35
        $values    = [$this->generateGuid($statement)];
36
37
        for ($i = 0; $i < 99; $i++) {
38
            $statement->execute();
39
            $value = $statement->fetchColumn();
40
41
            self::assertNotContains($value, $values, 'Duplicate value detected');
42
            self::assertGreaterThan(end($values), $value);
43
44
            $values[] = $value;
45
        }
46
47
        $statement->closeCursor();
48
    }
49
50
    private function generateGuid(Statement $statement) : string
51
    {
52
        $statement->execute();
53
54
        return $statement->fetchColumn();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $statement->fetchColumn() could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
55
    }
56
57
    private function getSelectGuidSql()
58
    {
59
        $platform = $this->_conn->getDatabasePlatform();
60
61
        return $platform->getDummySelectSQL(
62
            $platform->getGuidExpression()
63
        );
64
    }
65
}
66