PoolingShardConnectionTest.php 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\Tests\DBAL\Sharding;

use Doctrine\DBAL\DriverManager;
23
use Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

class PoolingShardConnectionTest extends \PHPUnit_Framework_TestCase
{
    public function testConnect()
    {
        $conn = DriverManager::getConnection(array(
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'global' => array('memory' => true),
            'shards' => array(
                array('id' => 1, 'memory' => true),
                array('id' => 2, 'memory' => true),
            ),
            'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
        ));

        $this->assertFalse($conn->isConnected(0));
        $conn->connect(0);
        $this->assertEquals(1, $conn->fetchColumn('SELECT 1'));
        $this->assertTrue($conn->isConnected(0));

        $this->assertFalse($conn->isConnected(1));
        $conn->connect(1);
        $this->assertEquals(1, $conn->fetchColumn('SELECT 1'));
        $this->assertTrue($conn->isConnected(1));

        $this->assertFalse($conn->isConnected(2));
        $conn->connect(2);
        $this->assertEquals(1, $conn->fetchColumn('SELECT 1'));
        $this->assertTrue($conn->isConnected(2));

        $conn->close();
        $this->assertFalse($conn->isConnected(0));
        $this->assertFalse($conn->isConnected(1));
        $this->assertFalse($conn->isConnected(2));
    }

    public function testNoGlobalServerException()
    {
        $this->setExpectedException('InvalidArgumentException', "Connection Parameters require 'global' and 'shards' configurations.");

65
        DriverManager::getConnection(array(
66 67 68 69 70 71 72 73 74 75 76 77 78 79
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'shards' => array(
                array('id' => 1, 'memory' => true),
                array('id' => 2, 'memory' => true),
            ),
            'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
        ));
    }

    public function testNoShardsServersExecption()
    {
        $this->setExpectedException('InvalidArgumentException', "Connection Parameters require 'global' and 'shards' configurations.");

80
        DriverManager::getConnection(array(
81 82 83 84 85 86 87 88 89 90 91
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'global' => array('memory' => true),
            'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
        ));
    }

    public function testNoShardsChoserExecption()
    {
        $this->setExpectedException('InvalidArgumentException', "Missing Shard Choser configuration 'shardChoser'");

92
        DriverManager::getConnection(array(
93 94 95 96 97 98 99 100 101 102 103 104 105 106
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'global' => array('memory' => true),
            'shards' => array(
                array('id' => 1, 'memory' => true),
                array('id' => 2, 'memory' => true),
            ),
        ));
    }

    public function testShardChoserWrongInstance()
    {
        $this->setExpectedException('InvalidArgumentException', "The 'shardChoser' configuration is not a valid instance of Doctrine\DBAL\Sharding\ShardChoser\ShardChoser");

107
        DriverManager::getConnection(array(
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'global' => array('memory' => true),
            'shards' => array(
                array('id' => 1, 'memory' => true),
                array('id' => 2, 'memory' => true),
            ),
            'shardChoser' => new \stdClass,
        ));
    }

    public function testShardNonNumericId()
    {
        $this->setExpectedException('InvalidArgumentException', "Shard Id has to be a non-negative number.");

123
        DriverManager::getConnection(array(
124 125 126 127 128 129 130 131 132 133 134 135
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'global' => array('memory' => true),
            'shards' => array(
                array('id' => 'foo', 'memory' => true),
            ),
            'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
        ));
    }

    public function testShardMissingId()
    {
136
        $this->setExpectedException('InvalidArgumentException', "Missing 'id' for one configured shard. Please specify a unique shard-id.");
137

138
        DriverManager::getConnection(array(
139 140 141 142 143 144 145 146 147 148 149 150 151 152
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'global' => array('memory' => true),
            'shards' => array(
                array('memory' => true),
            ),
            'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
        ));
    }

    public function testDuplicateShardId()
    {
        $this->setExpectedException('InvalidArgumentException', "Shard 1 is duplicated in the configuration.");

153
        DriverManager::getConnection(array(
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
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'global' => array('memory' => true),
            'shards' => array(
                array('id' => 1, 'memory' => true),
                array('id' => 1, 'memory' => true),
            ),
            'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
        ));
    }

    public function testSwitchShardWithOpenTransactionException()
    {
        $conn = DriverManager::getConnection(array(
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'global' => array('memory' => true),
            'shards' => array(
                array('id' => 1, 'memory' => true),
            ),
            'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
        ));

        $conn->beginTransaction();

        $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'Cannot switch shard when transaction is active.');
        $conn->connect(1);
    }

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    public function testGetActiveShardId()
    {
        $conn = DriverManager::getConnection(array(
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'global' => array('memory' => true),
            'shards' => array(
                array('id' => 1, 'memory' => true),
            ),
            'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
        ));

        $this->assertNull($conn->getActiveShardId());

        $conn->connect(0);
        $this->assertEquals(0, $conn->getActiveShardId());

        $conn->connect(1);
        $this->assertEquals(1, $conn->getActiveShardId());

        $conn->close();
        $this->assertNull($conn->getActiveShardId());
    }

    public function testGetParamsOverride()
    {
        $conn = DriverManager::getConnection(array(
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
212
            'global' => array('memory' => true, 'host' => 'localhost'),
213
            'shards' => array(
214
                array('id' => 1, 'memory' => true, 'host' => 'foo'),
215 216 217 218 219 220 221
            ),
            'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
        ));

        $this->assertEquals(array(
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
222
            'global' => array('memory' => true, 'host' => 'localhost'),
223
            'shards' => array(
224
                array('id' => 1, 'memory' => true, 'host' => 'foo'),
225 226
            ),
            'shardChoser' => new MultiTenantShardChoser(),
227 228
            'memory' => true,
            'host' => 'localhost',
229 230 231 232 233 234
        ), $conn->getParams());

        $conn->connect(1);
        $this->assertEquals(array(
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
235
            'global' => array('memory' => true, 'host' => 'localhost'),
236
            'shards' => array(
237
                array('id' => 1, 'memory' => true, 'host' => 'foo'),
238 239 240 241
            ),
            'shardChoser' => new MultiTenantShardChoser(),
            'id' => 1,
            'memory' => true,
242
            'host' => 'foo',
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
        ), $conn->getParams());
    }

    public function testGetHostOverride()
    {
        $conn = DriverManager::getConnection(array(
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'host' => 'localhost',
            'global' => array('memory' => true),
            'shards' => array(
                array('id' => 1, 'memory' => true, 'host' => 'foo'),
            ),
            'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
        ));

        $this->assertEquals('localhost', $conn->getHost());

        $conn->connect(1);
        $this->assertEquals('foo', $conn->getHost());
    }

    public function testGetPortOverride()
    {
        $conn = DriverManager::getConnection(array(
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'port' => 3306,
            'global' => array('memory' => true),
            'shards' => array(
                array('id' => 1, 'memory' => true, 'port' => 3307),
            ),
            'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
        ));

        $this->assertEquals(3306, $conn->getPort());

        $conn->connect(1);
        $this->assertEquals(3307, $conn->getPort());
    }

    public function testGetUsernameOverride()
    {
        $conn = DriverManager::getConnection(array(
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'user' => 'foo',
            'global' => array('memory' => true),
            'shards' => array(
                array('id' => 1, 'memory' => true, 'user' => 'bar'),
            ),
            'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
        ));

        $this->assertEquals('foo', $conn->getUsername());

        $conn->connect(1);
        $this->assertEquals('bar', $conn->getUsername());
    }

    public function testGetPasswordOverride()
    {
        $conn = DriverManager::getConnection(array(
            'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
            'driver' => 'pdo_sqlite',
            'password' => 'foo',
            'global' => array('memory' => true),
            'shards' => array(
                array('id' => 1, 'memory' => true, 'password' => 'bar'),
            ),
            'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
        ));

        $this->assertEquals('foo', $conn->getPassword());

        $conn->connect(1);
        $this->assertEquals('bar', $conn->getPassword());
    }
}