DBAL421Test.php 1.46 KB
Newer Older
Steve Müller's avatar
Steve Müller committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php

namespace Doctrine\Tests\DBAL\Functional\Ticket;

/**
 * @group DBAL-421
 */
class DBAL421Test extends \Doctrine\Tests\DbalFunctionalTestCase
{
    protected function setUp()
    {
        parent::setUp();

        $platform = $this->_conn->getDatabasePlatform()->getName();
        if (!in_array($platform, array('mysql', 'sqlite'))) {
            $this->markTestSkipped('Currently restricted to MySQL and SQLite.');
        }
    }

    public function testGuidShouldMatchPattern()
    {
        $guid = $this->_conn->query($this->getSelectGuidSql())->fetchColumn();
        $pattern = '/[0-9A-F]{8}\-[0-9A-F]{4}\-[0-9A-F]{4}\-[8-9A-B][0-9A-F]{3}\-[0-9A-F]{12}/i';
24
        self::assertEquals(1, preg_match($pattern, $guid), "GUID does not match pattern");
Steve Müller's avatar
Steve Müller committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38
    }

    /**
     * This test does (of course) not proof that all generated GUIDs are
     * random, it should however provide some basic confidence.
     */
    public function testGuidShouldBeRandom()
    {
        $statement = $this->_conn->prepare($this->getSelectGuidSql());
        $guids = array();

        for ($i = 0; $i < 99; $i++) {
            $statement->execute();
            $guid = $statement->fetchColumn();
39
            self::assertNotContains($guid, $guids, "Duplicate GUID detected");
Steve Müller's avatar
Steve Müller committed
40 41 42 43 44 45 46 47 48 49 50
            $guids[] = $guid;
        }

        $statement->closeCursor();
    }

    private function getSelectGuidSql()
    {
        return "SELECT " . $this->_conn->getDatabasePlatform()->getGuidExpression();
    }
}