Failed Conditions
Pull Request — master (#3233)
by Sergey
10:50
created

DropSchemaSqlCollectorTest::testAcceptsView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 19
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Schema\Visitor;
4
5
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
6
7
/**
8
 * @covers Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector
9
 */
10
class DropSchemaSqlCollectorTest extends \PHPUnit\Framework\TestCase
11
{
12
    public function testGetQueriesUsesAcceptedForeignKeys()
13
    {
14
        $tableOne = $this->getTableMock();
15
        $tableTwo = $this->getTableMock();
16
17
        $keyConstraintOne = $this->getStubKeyConstraint('first');
18
        $keyConstraintTwo = $this->getStubKeyConstraint('second');
19
20
        $platform = $this->getMockBuilder('Doctrine\DBAL\Platforms\AbstractPlatform')
21
            ->setMethods(array('getDropForeignKeySQL'))
22
            ->getMockForAbstractClass();
23
24
        $collector = new DropSchemaSqlCollector($platform);
25
26
        $platform->expects($this->exactly(2))
27
             ->method('getDropForeignKeySQL');
28
29
        $platform->expects($this->at(0))
30
            ->method('getDropForeignKeySQL')
31
            ->with($keyConstraintOne, $tableOne);
32
33
        $platform->expects($this->at(1))
34
            ->method('getDropForeignKeySQL')
35
            ->with($keyConstraintTwo, $tableTwo);
36
37
        $collector->acceptForeignKey($tableOne, $keyConstraintOne);
38
        $collector->acceptForeignKey($tableTwo, $keyConstraintTwo);
39
40
        $collector->getQueries();
41
    }
42
43
    private function getTableMock()
44
    {
45
        return $this->getMockWithoutArguments('Doctrine\DBAL\Schema\Table');
46
    }
47
48
    private function getMockWithoutArguments($className)
49
    {
50
        return $this->getMockBuilder($className)->disableOriginalConstructor()->getMock();
51
    }
52
53
    private function getStubKeyConstraint($name)
54
    {
55
        $constraint = $this->getMockWithoutArguments('Doctrine\DBAL\Schema\ForeignKeyConstraint');
56
57
        $constraint->expects($this->any())
58
            ->method('getName')
59
            ->will($this->returnValue($name));
60
61
        $constraint->expects($this->any())
62
            ->method('getForeignColumns')
63
            ->will($this->returnValue(array()));
64
65
        $constraint->expects($this->any())
66
            ->method('getColumns')
67
            ->will($this->returnValue(array()));
68
69
        return $constraint;
70
    }
71
72
    public function testGivenForeignKeyWithZeroLength_acceptForeignKeyThrowsException()
73
    {
74
        $collector = new DropSchemaSqlCollector(
75
            $this->getMockForAbstractClass('Doctrine\DBAL\Platforms\AbstractPlatform')
76
        );
77
78
        $this->expectException( 'Doctrine\DBAL\Schema\SchemaException' );
79
        $collector->acceptForeignKey($this->getTableMock(), $this->getStubKeyConstraint(''));
80
    }
81
82
    public function testAcceptsView()
83
    {
84
85
        $platform = $this->getMockBuilder('Doctrine\DBAL\Platforms\AbstractPlatform')
86
            ->setMethods(array('getDropViewSQL'))
87
            ->getMockForAbstractClass();
88
89
        $collector = new DropSchemaSqlCollector($platform);
90
91
        $view = $this->createViewMock();
92
93
        $platform->expects($this->exactly(1))
94
            ->method('getDropViewSQL');
95
96
        $platform->expects($this->at(0))
97
            ->method('getDropViewSQL');
98
99
        $collector->acceptView($view);
100
        $collector->getQueries();
101
    }
102
103
    /**
104
     * @return \Doctrine\DBAL\Schema\View|\PHPUnit_Framework_MockObject_MockObject
105
     */
106
    private function createViewMock()
107
    {
108
        return $this->getMockBuilder('Doctrine\DBAL\Schema\View')
109
            ->disableOriginalConstructor()
110
            ->getMock();
111
    }
112
}
113