DropSchemaSqlCollectorTest.php 2.56 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL\Schema\Visitor;

5
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
6

7 8 9
/**
 * @covers Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector
 */
10 11 12 13
class DropSchemaSqlCollectorTest extends \PHPUnit_Framework_TestCase
{
    public function testGetQueriesUsesAcceptedForeignKeys()
    {
14 15
        $tableOne = $this->getTableMock();
        $tableTwo = $this->getTableMock();
16 17 18 19

        $keyConstraintOne = $this->getStubKeyConstraint('first');
        $keyConstraintTwo = $this->getStubKeyConstraint('second');

Jeroen De Dauw's avatar
Jeroen De Dauw committed
20
        $platform = $this->getMockBuilder('Doctrine\DBAL\Platforms\AbstractPlatform')
Jeroen De Dauw's avatar
Jeroen De Dauw committed
21 22
            ->setMethods(array('getDropForeignKeySQL'))
            ->getMockForAbstractClass();
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

        $collector = new DropSchemaSqlCollector($platform);

        $platform->expects($this->exactly(2))
            ->method('getDropForeignKeySQL');

        $platform->expects($this->at(0))
            ->method('getDropForeignKeySQL')
            ->with($keyConstraintOne, $tableOne);

        $platform->expects($this->at(1))
            ->method('getDropForeignKeySQL')
            ->with($keyConstraintTwo, $tableTwo);

        $collector->acceptForeignKey($tableOne, $keyConstraintOne);
        $collector->acceptForeignKey($tableTwo, $keyConstraintTwo);

        $collector->getQueries();
    }

Jeroen De Dauw's avatar
Jeroen De Dauw committed
43 44 45 46
    private function getTableMock()
    {
        return $this->getMockWithoutArguments('Doctrine\DBAL\Schema\Table');
    }
47

48 49 50 51 52 53 54 55 56
    private function getMockWithoutArguments($className)
    {
        return $this->getMockBuilder($className)->disableOriginalConstructor()->getMock();
    }

    private function getStubKeyConstraint($name)
    {
        $constraint = $this->getMockWithoutArguments('Doctrine\DBAL\Schema\ForeignKeyConstraint');

Jeroen De Dauw's avatar
Jeroen De Dauw committed
57 58 59
        $constraint->expects($this->any())
            ->method('getName')
            ->will($this->returnValue($name));
60

Jeroen De Dauw's avatar
Jeroen De Dauw committed
61 62 63
        $constraint->expects($this->any())
            ->method('getForeignColumns')
            ->will($this->returnValue(array()));
64

Jeroen De Dauw's avatar
Jeroen De Dauw committed
65 66 67
        $constraint->expects($this->any())
            ->method('getColumns')
            ->will($this->returnValue(array()));
68 69 70

        return $constraint;
    }
71

Jeroen De Dauw's avatar
Jeroen De Dauw committed
72 73 74 75 76
    public function testGivenForeignKeyWithZeroLength_acceptForeignKeyThrowsException()
    {
        $collector = new DropSchemaSqlCollector(
            $this->getMockForAbstractClass('Doctrine\DBAL\Platforms\AbstractPlatform')
        );
77

Jeroen De Dauw's avatar
Jeroen De Dauw committed
78 79 80
        $this->setExpectedException( 'Doctrine\DBAL\Schema\SchemaException' );
        $collector->acceptForeignKey($this->getTableMock(), $this->getStubKeyConstraint(''));
    }
81
}