SchemaTest.php 2.82 KB
Newer Older
Carsten Brandt committed
1 2 3 4 5 6 7
<?php

namespace yiiunit\framework\db;

use yii\caching\FileCache;
use yii\db\Schema;

8 9 10 11
/**
 * @group db
 * @group mysql
 */
Carsten Brandt committed
12 13
class SchemaTest extends DatabaseTestCase
{
14 15 16 17
    public function testGetTableNames()
    {
        /** @var Schema $schema */
        $schema = $this->getConnection()->schema;
Carsten Brandt committed
18

19 20 21 22 23 24 25 26
        $tables = $schema->getTableNames();
        $this->assertTrue(in_array('tbl_customer', $tables));
        $this->assertTrue(in_array('tbl_category', $tables));
        $this->assertTrue(in_array('tbl_item', $tables));
        $this->assertTrue(in_array('tbl_order', $tables));
        $this->assertTrue(in_array('tbl_order_item', $tables));
        $this->assertTrue(in_array('tbl_type', $tables));
    }
Carsten Brandt committed
27

28 29 30 31
    public function testGetTableSchemas()
    {
        /** @var Schema $schema */
        $schema = $this->getConnection()->schema;
Carsten Brandt committed
32

33 34 35 36 37 38
        $tables = $schema->getTableSchemas();
        $this->assertEquals(count($schema->getTableNames()), count($tables));
        foreach ($tables as $table) {
            $this->assertInstanceOf('yii\db\TableSchema', $table);
        }
    }
Carsten Brandt committed
39

40 41 42 43
    public function testGetNonExistingTableSchema()
    {
        $this->assertNull($this->getConnection()->schema->getTableSchema('nonexisting_table'));
    }
44

45 46 47 48
    public function testSchemaCache()
    {
        /** @var Schema $schema */
        $schema = $this->getConnection()->schema;
Carsten Brandt committed
49

50 51 52 53 54 55
        $schema->db->enableSchemaCache = true;
        $schema->db->schemaCache = new FileCache();
        $noCacheTable = $schema->getTableSchema('tbl_type', true);
        $cachedTable = $schema->getTableSchema('tbl_type', true);
        $this->assertEquals($noCacheTable, $cachedTable);
    }
56

57 58 59 60
    public function testCompositeFk()
    {
        /** @var Schema $schema */
        $schema = $this->getConnection()->schema;
61

62
        $table = $schema->getTableSchema('tbl_composite_fk');
63

64 65 66 67 68 69
        $this->assertCount(1, $table->foreignKeys);
        $this->assertTrue(isset($table->foreignKeys[0]));
        $this->assertEquals('tbl_order_item', $table->foreignKeys[0][0]);
        $this->assertEquals('order_id', $table->foreignKeys[0]['order_id']);
        $this->assertEquals('item_id', $table->foreignKeys[0]['item_id']);
    }
70

71 72 73 74 75 76 77 78 79 80 81 82 83
    public function testGetPDOType()
    {
        $values = [
            [null, \PDO::PARAM_NULL],
            ['', \PDO::PARAM_STR],
            ['hello', \PDO::PARAM_STR],
            [0, \PDO::PARAM_INT],
            [1, \PDO::PARAM_INT],
            [1337, \PDO::PARAM_INT],
            [true, \PDO::PARAM_BOOL],
            [false, \PDO::PARAM_BOOL],
            [$fp = fopen(__FILE__, 'rb'), \PDO::PARAM_LOB],
        ];
84

85 86
        /** @var Schema $schema */
        $schema = $this->getConnection()->schema;
87

88 89 90 91 92
        foreach ($values as $value) {
            $this->assertEquals($value[1], $schema->getPdoType($value[0]));
        }
        fclose($fp);
    }
Carsten Brandt committed
93
}