Passed
Pull Request — master (#311)
by William
12:43
created

CreateDefinitionTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 61
c 3
b 0
f 0
dl 0
loc 102
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testParseErr2() 0 11 1
A testParse() 0 10 1
A testBuild2() 0 13 1
A testParseErr1() 0 12 1
A testParse2() 0 10 1
A testBuild() 0 12 1
A testBuildWithInvisibleKeyword() 0 16 1
1
<?php
2
3
namespace PhpMyAdmin\SqlParser\Tests\Components;
4
5
use PhpMyAdmin\SqlParser\Components\CreateDefinition;
6
use PhpMyAdmin\SqlParser\Parser;
7
use PhpMyAdmin\SqlParser\Tests\TestCase;
8
9
class CreateDefinitionTest extends TestCase
10
{
11
    public function testParse()
12
    {
13
        $component = CreateDefinition::parse(
14
            new Parser(),
15
            $this->getTokensList('(str TEXT, FULLTEXT INDEX indx (str))')
16
        );
17
        $this->assertEquals('str', $component[0]->name);
18
        $this->assertEquals('FULLTEXT INDEX', $component[1]->key->type);
19
        $this->assertEquals('indx', $component[1]->key->name);
20
        $this->assertEquals('FULLTEXT INDEX `indx` (`str`)', (string) $component[1]);
21
    }
22
23
    public function testParse2()
24
    {
25
        $component = CreateDefinition::parse(
26
            new Parser(),
27
            $this->getTokensList('(str TEXT NOT NULL INVISIBLE)')
28
        );
29
        $this->assertEquals('str', $component[0]->name);
30
        $this->assertEquals('TEXT', $component[0]->type->name);
31
        $this->assertTrue($component[0]->options->has('INVISIBLE'));
32
        $this->assertTrue($component[0]->options->has('NOT NULL'));
33
    }
34
35
    public function testParseErr1()
36
    {
37
        $parser = new Parser();
38
        $component = CreateDefinition::parse(
39
            $parser,
40
            $this->getTokensList('(str TEXT, FULLTEXT INDEX indx (str)')
41
        );
42
        $this->assertCount(2, $component);
43
44
        $this->assertEquals(
45
            'A closing bracket was expected.',
46
            $parser->errors[0]->getMessage()
47
        );
48
    }
49
50
    public function testParseErr2()
51
    {
52
        $parser = new Parser();
53
        CreateDefinition::parse(
54
            $parser,
55
            $this->getTokensList(')')
56
        );
57
58
        $this->assertEquals(
59
            'An opening bracket was expected.',
60
            $parser->errors[0]->getMessage()
61
        );
62
    }
63
64
    public function testBuild()
65
    {
66
        $parser = new Parser(
67
            'CREATE TABLE `payment` (' .
68
            '-- snippet' . "\n" .
69
            '`customer_id` smallint(5) unsigned NOT NULL,' .
70
            'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE' .
71
            ') ENGINE=InnoDB"'
72
        );
73
        $this->assertEquals(
74
            'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE',
75
            CreateDefinition::build($parser->statements[0]->fields[1])
76
        );
77
    }
78
79
    public function testBuild2()
80
    {
81
        $parser = new Parser(
82
            'CREATE TABLE `payment` (' .
83
            '-- snippet' . "\n" .
84
            '`customer_id` smallint(5) unsigned NOT NULL,' .
85
            '`customer_data` longtext CHARACTER SET utf8mb4 CHARSET utf8mb4_bin NOT NULL CHECK (json_valid(customer_data)),' .
86
            'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE' .
87
            ') ENGINE=InnoDB"'
88
        );
89
        $this->assertEquals(
90
            'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE',
91
            CreateDefinition::build($parser->statements[0]->fields[2])
92
        );
93
    }
94
95
    public function testBuildWithInvisibleKeyword()
96
    {
97
        $parser = new Parser(
98
            'CREATE TABLE `payment` (' .
99
            '-- snippet' . "\n" .
100
            '`customer_id` smallint(5) unsigned NOT NULL INVISIBLE,' .
101
            '`customer_data` longtext CHARACTER SET utf8mb4 CHARSET utf8mb4_bin NOT NULL ' .
102
            'CHECK (json_valid(customer_data)),CONSTRAINT `fk_payment_customer` FOREIGN KEY ' .
103
            '(`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE' .
104
            ') ENGINE=InnoDB"'
105
        );
106
        // TODO: when not supporting PHP 5.3 anymore, replace this by CreateStatement::class.
107
        $this->assertInstanceOf('PhpMyAdmin\\SqlParser\\Statements\\CreateStatement', $parser->statements[0]);
108
        $this->assertEquals(
109
            '`customer_id` smallint(5) UNSIGNED NOT NULL INVISIBLE',
110
            CreateDefinition::build($parser->statements[0]->fields[0])
111
        );
112
    }
113
}
114