Issues (2963)

tests/SchemaTest.php (1 issue)

1
<?php
2
/**
3
 * SchemaTest.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2018 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace LibreNMS\Tests;
27
28
use LibreNMS\DB\Schema;
29
30
class SchemaTest extends TestCase
31
{
32
    private $mock_schema = [
33
        'bills' => [
34
            'Columns' => [
35
                ['Field' => 'bill_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
36
            ],
37
            'Indexes' => ['bill_id' => ['Name' => 'bill_id', 'Columns' => ['bill_id'], 'Unique' => true, 'Type' => 'BTREE']],
38
        ],
39
        'bill_ports' => [
40
            'Columns' => [
41
                ['Field' => 'bill_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => ''],
42
                ['Field' => 'port_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => ''],
43
            ],
44
        ],
45
        'devices' => [
46
            'Columns' => [
47
                ['Field' => 'device_id', 'Type' => 'int(11) unsigned', 'Null' => false, 'Extra' => 'auto_increment'],
48
                ['Field' => 'location_id', 'Type' => 'int(11)', 'Null' => true, 'Extra' => ''],
49
            ],
50
            'Indexes' => [
51
                'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['device_id'], 'Unique' => true, 'Type' => 'BTREE'],
52
            ],
53
        ],
54
        'locations' => [
55
            'Columns' => [
56
                ['Field' => 'id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
57
            ],
58
            'Indexes' => [
59
                'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['id'], 'Unique' => true, 'Type' => 'BTREE'],
60
            ],
61
        ],
62
        'ports' => [
63
            'Columns' => [
64
                ['Field' => 'port_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
65
                ['Field' => 'device_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => '', 'Default' => '0'],
66
            ],
67
            'Indexes' => [
68
                'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['port_id'], 'Unique' => true, 'Type' => 'BTREE'],
69
            ],
70
        ],
71
        'sensors' => [
72
            'Columns' => [
73
                ['Field' => 'sensor_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
74
                ['Field' => 'device_id', 'Type' => 'int(11) unsigned', 'Null' => false, 'Extra' => '', 'Default' => '0'],
75
            ],
76
            'Indexes' => [
77
                'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['sensor_id'], 'Unique' => true, 'Type' => 'BTREE'],
78
            ],
79
        ],
80
        'sensors_to_state_indexes' => [
81
            'Columns' => [
82
                ['Field' => 'sensors_to_state_translations_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
83
                ['Field' => 'sensor_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => ''],
84
                ['Field' => 'state_index_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => ''],
85
            ],
86
            'Indexes' => [
87
                'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['sensors_to_state_translations_id'], 'Unique' => true, 'Type' => 'BTREE'],
88
            ],
89
        ],
90
        'state_indexes' => [
91
            'Columns' => [
92
                ['Field' => 'state_index_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
93
            ],
94
            'Indexes' => [
95
                'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['state_index_id'], 'Unique' => true, 'Type' => 'BTREE'],
96
            ],
97
        ],
98
        'state_translations' => [
99
            'Columns' => [
100
                ['Field' => 'state_translation_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
101
                ['Field' => 'state_index_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => ''],
102
            ],
103
            'Indexes' => [
104
                'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['state_translation_id'], 'Unique' => true, 'Type' => 'BTREE'],
105
            ],
106
        ],
107
    ];
108
109
    /**
110
     * @return Schema
111
     */
112
    private function getSchemaMock()
113
    {
114
        // use a Mock so we don't have to rely on the schema being stable.
115
116
        $schema = $this->getMockBuilder(Schema::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

116
        $schema = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(Schema::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
117
            ->setMethods(['getSchema'])
118
            ->getMock();
119
120
        $schema->method('getSchema')->willReturn($this->mock_schema);
121
122
        /** @var $schema Schema Mock of Schema */
123
        return $schema;
124
    }
125
126
    public function testTableRelationships()
127
    {
128
        // mock getSchema
129
        $schema = $this->getSchemaMock();
130
131
        $expected = [
132
            'bills' => [],
133
            'bill_ports' => ['bills', 'ports'],
134
            'devices' => ['locations'],
135
            'locations' => [],
136
            'ports' => ['devices'],
137
            'sensors' => ['devices'],
138
            'sensors_to_state_indexes' => ['sensors', 'state_indexes'],
139
            'state_indexes' => [],
140
            'state_translations' => ['state_indexes'],
141
        ];
142
143
        $this->assertEquals($expected, $schema->getTableRelationships());
144
    }
145
146
    public function testFindRelationshipPath()
147
    {
148
        $schema = $this->getSchemaMock();
149
150
        $this->assertEquals(['devices'], $schema->findRelationshipPath('devices'));
151
        $this->assertEquals(['locations', 'devices'], $schema->findRelationshipPath('locations'));
152
        $this->assertEquals(['devices', 'ports'], $schema->findRelationshipPath('ports'));
153
        $this->assertEquals(['devices', 'ports', 'bill_ports'], $schema->findRelationshipPath('bill_ports'));
154
        $this->assertEquals(['devices', 'ports', 'bill_ports', 'bills'], $schema->findRelationshipPath('bills'));
155
        $this->assertEquals(
156
            ['devices', 'sensors', 'sensors_to_state_indexes', 'state_indexes', 'state_translations'],
157
            $schema->findRelationshipPath('state_translations')
158
        );
159
    }
160
}
161