Passed
Pull Request — master (#3233)
by Sergey
12:25
created

DropSchemaSqlCollectorTest::createViewMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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