TableGeneratorTest.php 1.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

namespace Doctrine\Tests\DBAL\Functional;

use Doctrine\DBAL\Id\TableGenerator;

/**
 * @group DDC-450
 */
class TableGeneratorTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
    private $generator;

14
    protected function setUp()
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
    {
        parent::setUp();

        $platform = $this->_conn->getDatabasePlatform();
        if ($platform->getName() == "sqlite") {
            $this->markTestSkipped('TableGenerator does not work with SQLite');
        }

        try {
            $schema = new \Doctrine\DBAL\Schema\Schema();
            $visitor = new \Doctrine\DBAL\Id\TableGeneratorSchemaVisitor();
            $schema->visit($visitor);

            foreach ($schema->toSql($platform) as $sql) {
                $this->_conn->exec($sql);
            }

        } catch(\Exception $e) {
        }
        $this->generator = new TableGenerator($this->_conn);
    }

    public function testNextVal()
    {
        $id1 = $this->generator->nextValue("tbl1");
        $id2 = $this->generator->nextValue("tbl1");
        $id3 = $this->generator->nextValue("tbl2");

        $this->assertTrue($id1 > 0, "First id has to be larger than 0");
        $this->assertEquals($id1 + 1, $id2, "Second id is one larger than first one.");
        $this->assertEquals($id1, $id3, "First ids from different tables are equal.");
    }

    public function testNextValNotAffectedByOuterTransactions()
    {
        $this->_conn->beginTransaction();
        $id1 = $this->generator->nextValue("tbl1");
        $this->_conn->rollBack();
        $id2 = $this->generator->nextValue("tbl1");

        $this->assertTrue($id1 > 0, "First id has to be larger than 0");
        $this->assertEquals($id1 + 1, $id2, "Second id is one larger than first one.");
    }
}