DB2SchemaManagerTest.php 5.76 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;
Sergei Morozov's avatar
Sergei Morozov committed
11 12
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
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
{
Sergei Morozov's avatar
Sergei Morozov committed
20
    /** @var Connection|PHPUnit_Framework_MockObject_MockObject */
21 22
    private $conn;

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

    protected function setUp()
    {
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
            ->setMethods(['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
     * @return void
Sergei Morozov's avatar
Sergei Morozov committed
43 44
     *
     * @group DBAL-2701
45
     */
46 47 48 49
    public function testListTableNamesFiltersAssetNamesCorrectly()
    {
        $this->conn->getConfiguration()->setFilterSchemaAssetsExpression('/^(?!T_)/');
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue([
Sergei Morozov's avatar
Sergei Morozov committed
50 51 52 53
            ['name' => 'FOO'],
            ['name' => 'T_FOO'],
            ['name' => 'BAR'],
            ['name' => 'T_BAR'],
54 55
        ]));

56
        self::assertSame(
57 58 59 60 61 62 63
            [
                'FOO',
                'BAR',
            ],
            $this->manager->listTableNames()
        );
    }
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

    /**
     * @return void
     *
     * @group DBAL-2701
     */
    public function testAssetFilteringSetsACallable()
    {
        $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();
        $this->assertInternalType('callable', $callable);

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

    /**
     * @return void
     */
    public function testListTableNamesFiltersAssetNamesCorrectlyWithCallable()
    {
        $accepted = ['T_FOO', 'T_BAR'];
        $this->conn->getConfiguration()->setSchemaAssetsFilter(static function ($assetName) use ($accepted) {
            return in_array($assetName, $accepted);
        });
105
        $this->conn->expects($this->any())->method('quote');
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        $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());
    }

    /**
     * @return void
     */
    public function testSettingNullExpressionWillResetCallable()
    {
        $accepted = ['T_FOO', 'T_BAR'];
        $this->conn->getConfiguration()->setSchemaAssetsFilter(static function ($assetName) use ($accepted) {
            return in_array($assetName, $accepted);
        });
133
        $this->conn->expects($this->any())->method('quote');
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 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 188 189 190 191 192 193 194 195 196 197 198 199 200
        $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());
    }

    /**
     * @return void
     */
    public function testSettingNullAsCallableClearsExpression()
    {
        $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());
    }
201
}