DB2SchemaManagerTest.php 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

namespace Doctrine\Tests\DBAL\Schema;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Schema\DB2SchemaManager;
11
use PHPUnit\Framework\MockObject\MockObject;
Sergei Morozov's avatar
Sergei Morozov committed
12
use PHPUnit\Framework\TestCase;
13
use function in_array;
14 15 16 17

/**
 * @covers \Doctrine\DBAL\Schema\DB2SchemaManager
 */
Sergei Morozov's avatar
Sergei Morozov committed
18
final class DB2SchemaManagerTest extends TestCase
19
{
20
    /** @var Connection|MockObject */
21 22
    private $conn;

Sergei Morozov's avatar
Sergei Morozov committed
23
    /** @var DB2SchemaManager */
24 25
    private $manager;

26
    protected function setUp() : void
27
    {
Sergei Morozov's avatar
Sergei Morozov committed
28 29 30 31
        $eventManager  = new EventManager();
        $driverMock    = $this->createMock(Driver::class);
        $platform      = $this->createMock(DB2Platform::class);
        $this->conn    = $this
32
            ->getMockBuilder(Connection::class)
33
            ->onlyMethods(['fetchAll', 'quote'])
34 35
            ->setConstructorArgs([['platform' => $platform], $driverMock, new Configuration(), $eventManager])
            ->getMock();
36 37 38
        $this->manager = new DB2SchemaManager($this->conn);
    }

39 40
    /**
     * @see https://github.com/doctrine/dbal/issues/2701
Sergei Morozov's avatar
Sergei Morozov committed
41 42
     *
     * @group DBAL-2701
43
     */
44
    public function testListTableNamesFiltersAssetNamesCorrectly() : void
45 46 47
    {
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
Sergei Morozov's avatar
Sergei Morozov committed
48 49 50 51
            ['name' => 'FOO'],
            ['name' => 'T_FOO'],
            ['name' => 'BAR'],
            ['name' => 'T_BAR'],
52 53
        ]));

54
        self::assertSame(
55 56 57 58 59 60 61
            [
                'FOO',
                'BAR',
            ],
            $this->manager->listTableNames()
        );
    }
62 63 64 65

    /**
     * @group DBAL-2701
     */
66
    public function testAssetFilteringSetsACallable() : void
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    {
        $filterExpression = '/^(?!T_)/';
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
            ['name' => 'FOO'],
            ['name' => 'T_FOO'],
            ['name' => 'BAR'],
            ['name' => 'T_BAR'],
        ]));

        self::assertSame(
            [
                'FOO',
                'BAR',
            ],
            $this->manager->listTableNames()
        );

        $callable = $this->conn->getConfiguration()->getSchemaAssetsFilter();
86
        self::assertIsCallable($callable);
87 88 89 90 91

        // BC check: Test that regexp expression is still preserved & accessible.
        $this->assertEquals($filterExpression, $this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
    }

92
    public function testListTableNamesFiltersAssetNamesCorrectlyWithCallable() : void
93 94 95 96 97
    {
        $accepted = ['T_FOO', 'T_BAR'];
        $this->conn->getConfiguration()->setSchemaAssetsFilter(static function ($assetName) use ($accepted) {
            return in_array($assetName, $accepted);
        });
98
        $this->conn->expects($this->any())->method('quote');
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
            ['name' => 'FOO'],
            ['name' => 'T_FOO'],
            ['name' => 'BAR'],
            ['name' => 'T_BAR'],
        ]));

        self::assertSame(
            [
                'T_FOO',
                'T_BAR',
            ],
            $this->manager->listTableNames()
        );

        $this->assertNull($this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
    }

117
    public function testSettingNullExpressionWillResetCallable() : void
118 119 120 121 122
    {
        $accepted = ['T_FOO', 'T_BAR'];
        $this->conn->getConfiguration()->setSchemaAssetsFilter(static function ($assetName) use ($accepted) {
            return in_array($assetName, $accepted);
        });
123
        $this->conn->expects($this->any())->method('quote');
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        $this->conn->expects($this->atLeastOnce())->method('fetchAll')->will($this->returnValue([
            ['name' => 'FOO'],
            ['name' => 'T_FOO'],
            ['name' => 'BAR'],
            ['name' => 'T_BAR'],
        ]));

        self::assertSame(
            [
                'T_FOO',
                'T_BAR',
            ],
            $this->manager->listTableNames()
        );

        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression(null);

        self::assertSame(
            [
                'FOO',
                'T_FOO',
                'BAR',
                'T_BAR',
            ],
            $this->manager->listTableNames()
        );

        $this->assertNull($this->conn->getConfiguration()->getSchemaAssetsFilter());
    }

154
    public function testSettingNullAsCallableClearsExpression() : void
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    {
        $filterExpression = '/^(?!T_)/';
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);

        $this->conn->expects($this->exactly(2))->method('fetchAll')->will($this->returnValue([
            ['name' => 'FOO'],
            ['name' => 'T_FOO'],
            ['name' => 'BAR'],
            ['name' => 'T_BAR'],
        ]));

        self::assertSame(
            [
                'FOO',
                'BAR',
            ],
            $this->manager->listTableNames()
        );

        $this->conn->getConfiguration()->setSchemaAssetsFilter(null);

        self::assertSame(
            [
                'FOO',
                'T_FOO',
                'BAR',
                'T_BAR',
            ],
            $this->manager->listTableNames()
        );

        $this->assertNull($this->conn->getConfiguration()->getFilterSchemaAssetsExpression());
    }
188
}