AbstractTestCase.php 3.59 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

namespace Doctrine\Tests\DBAL\Sharding\SQLAzure;

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Sharding\SQLAzure\SQLAzureShardManager;

Luís Cobucci's avatar
Luís Cobucci committed
9
abstract class AbstractTestCase extends \PHPUnit\Framework\TestCase
10
{
Gabriel Caruso's avatar
Gabriel Caruso committed
11 12 13
    /**
     * @var \Doctrine\DBAL\Connection
     */
14
    protected $conn;
Gabriel Caruso's avatar
Gabriel Caruso committed
15 16 17 18

    /**
     * @var SQLAzureShardManager
     */
19 20
    protected $sm;

21
    protected function setUp()
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    {
        if (!isset($GLOBALS['db_type']) || strpos($GLOBALS['db_type'], "sqlsrv") === false) {
            $this->markTestSkipped('No driver or sqlserver driver specified.');
        }

        $params = array(
            'driver' => $GLOBALS['db_type'],
            'dbname' => $GLOBALS['db_name'],
            'user' => $GLOBALS['db_username'],
            'password' => $GLOBALS['db_password'],
            'host' => $GLOBALS['db_host'],
            'sharding' => array(
                'federationName' => 'Orders_Federation',
                'distributionKey' => 'CustID',
                'distributionType' => 'integer',
                'filteringEnabled' => false,
            ),
            'driverOptions' => array('MultipleActiveResultSets' => false)
        );
        $this->conn = DriverManager::getConnection($params);
42 43 44 45 46 47 48

        $serverEdition = $this->conn->fetchColumn("SELECT CONVERT(NVARCHAR(128), SERVERPROPERTY('Edition'))");

        if (0 !== strpos($serverEdition, 'SQL Azure')) {
            $this->markTestSkipped('SQL Azure only test.');
        }

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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
        // assume database is created and schema is:
        // Global products table
        // Customers, Orders, OrderItems federation tables.
        // See http://cloud.dzone.com/articles/using-sql-azure-federations
        $this->sm = new SQLAzureShardManager($this->conn);
    }

    public function createShopSchema()
    {
        $schema = new Schema();

        $products = $schema->createTable('Products');
        $products->addColumn('ProductID', 'integer');
        $products->addColumn('SupplierID', 'integer');
        $products->addColumn('ProductName', 'string');
        $products->addColumn('Price', 'decimal', array('scale' => 2, 'precision' => 12));
        $products->setPrimaryKey(array('ProductID'));
        $products->addOption('azure.federated', true);

        $customers = $schema->createTable('Customers');
        $customers->addColumn('CustomerID', 'integer');
        $customers->addColumn('CompanyName', 'string');
        $customers->addColumn('FirstName', 'string');
        $customers->addColumn('LastName', 'string');
        $customers->setPrimaryKey(array('CustomerID'));
        $customers->addOption('azure.federated', true);
        $customers->addOption('azure.federatedOnColumnName', 'CustomerID');

        $orders = $schema->createTable('Orders');
        $orders->addColumn('CustomerID', 'integer');
        $orders->addColumn('OrderID', 'integer');
        $orders->addColumn('OrderDate', 'datetime');
        $orders->setPrimaryKey(array('CustomerID', 'OrderID'));
        $orders->addOption('azure.federated', true);
        $orders->addOption('azure.federatedOnColumnName', 'CustomerID');

        $orderItems = $schema->createTable('OrderItems');
        $orderItems->addColumn('CustomerID', 'integer');
        $orderItems->addColumn('OrderID', 'integer');
        $orderItems->addColumn('ProductID', 'integer');
        $orderItems->addColumn('Quantity', 'integer');
        $orderItems->setPrimaryKey(array('CustomerID', 'OrderID', 'ProductID'));
        $orderItems->addOption('azure.federated', true);
        $orderItems->addOption('azure.federatedOnColumnName', 'CustomerID');

        return $schema;
    }
}