Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
39.53% covered (danger)
39.53%
34 / 86
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
schema_generator_test
39.53% covered (danger)
39.53%
34 / 86
66.67% covered (warning)
66.67%
4 / 6
17.83
0.00% covered (danger)
0.00%
0 / 1
 setUp
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
2.00
 get_schema_generator
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 test_check_dependencies_fail
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 test_get_schema_success
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 column_add_after_data
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 1
2
 test_column_add_after
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14require_once __DIR__ . '/../dbal/migration/dummy_order.php';
15require_once __DIR__ . '/../dbal/migration/dummy_order_0.php';
16require_once __DIR__ . '/../dbal/migration/dummy_order_1.php';
17require_once __DIR__ . '/../dbal/migration/dummy_order_2.php';
18require_once __DIR__ . '/../dbal/migration/dummy_order_3.php';
19require_once __DIR__ . '/../dbal/migration/dummy_order_4.php';
20require_once __DIR__ . '/../dbal/migration/dummy_order_5.php';
21
22class schema_generator_test extends phpbb_test_case
23{
24    /** @var \phpbb\config\config */
25    protected $config;
26
27    /** @var \phpbb\db\driver\driver_interface */
28    protected $db;
29
30    /** @var \phpbb\db\tools\doctrine */
31    protected $db_tools;
32
33    /** @var \Doctrine\DBAL\Connection */
34    protected $doctrine_db;
35
36    /** @var \phpbb\db\migration\schema_generator */
37    protected $generator;
38
39    /** @var string */
40    protected $table_prefix;
41
42    /** @var string */
43    protected $phpbb_root_path;
44
45    /** @var string */
46    protected $php_ext;
47
48    protected function setUp(): void
49    {
50        global $phpbb_root_path, $phpEx;
51
52        parent::setUp();
53
54        $this->table_prefix = 'phpbb_';
55        $this->phpbb_root_path = $phpbb_root_path;
56        $this->php_ext = $phpEx;
57
58        $this->config = new \phpbb\config\config(array());
59        $this->db = new \phpbb\db\driver\sqlite3();
60        // Some local setups may not configure a DB driver. If the configured
61        // 'dbms' is null, skip this test instead of failing with a TypeError in
62        // the connection factory which expects a non-null driver name.
63        $mock_config = new phpbb_mock_config_php_file();
64        if ($mock_config->get('dbms') === null)
65        {
66            self::markTestSkipped('Skipping schema generator tests: dbms is not configured (null) in local environment.');
67        }
68        $this->doctrine_db = \phpbb\db\doctrine\connection_factory::get_connection($mock_config);
69        $factory = new \phpbb\db\tools\factory();
70        $this->db_tools = $factory->get($this->doctrine_db);
71        $this->db_tools->set_table_prefix($this->table_prefix);
72    }
73
74    protected function get_schema_generator(array $class_names)
75    {
76        $this->generator = new \phpbb\db\migration\schema_generator($class_names, $this->config, $this->db, $this->db_tools, $this->phpbb_root_path, $this->php_ext, $this->table_prefix, phpbb_database_test_case::get_core_tables());
77
78        return $this->generator;
79    }
80
81    public function test_check_dependencies_fail()
82    {
83        $this->expectException(\UnexpectedValueException::class);
84        $this->get_schema_generator(array('\phpbb\db\migration\data\v310\forgot_password'));
85
86        $this->generator->get_schema();
87    }
88
89    public function test_get_schema_success()
90    {
91        $this->get_schema_generator(array(
92            '\phpbb\db\migration\data\v30x\release_3_0_1_rc1',
93            '\phpbb\db\migration\data\v30x\release_3_0_0',
94            '\phpbb\db\migration\data\v310\boardindex'
95        ));
96
97        $this->assertArrayHasKey('phpbb_users', $this->generator->get_schema());
98    }
99
100    public static function column_add_after_data()
101    {
102        return array(
103            array(
104                'phpbb_dbal_migration_dummy_order_0',
105                array(
106                    'foobar1',
107                    'foobar2',
108                    'foobar3',
109                ),
110            ),
111            array(
112                'phpbb_dbal_migration_dummy_order_1',
113                array(
114                    'foobar1',
115                    'foobar3',
116                    'foobar4',
117                ),
118            ),
119            array(
120                'phpbb_dbal_migration_dummy_order_2',
121                array(
122                    'foobar1',
123                    'foobar3',
124                    'foobar5',
125                ),
126            ),
127            array(
128                'phpbb_dbal_migration_dummy_order_3',
129                array(
130                    'foobar1',
131                    'foobar3',
132                    'foobar6',
133                ),
134            ),
135            array(
136                'phpbb_dbal_migration_dummy_order_4',
137                array(
138                    'foobar1',
139                    'foobar3',
140                    'foobar7',
141                ),
142            ),
143            array(
144                'phpbb_dbal_migration_dummy_order_5',
145                array(
146                    'foobar1',
147                    'foobar3',
148                    'foobar9',
149                    'foobar8',
150                ),
151            ),
152        );
153    }
154
155    /**
156    * @dataProvider column_add_after_data
157    */
158    public function test_column_add_after($migration, $expected)
159    {
160        $this->get_schema_generator(array(
161            'phpbb_dbal_migration_dummy_order',
162            $migration,
163        ));
164
165        $tables = $this->generator->get_schema();
166
167        $this->assertEquals(
168            $expected,
169            array_keys($tables[$this->table_prefix . 'column_order_test1']['COLUMNS']),
170            'The schema generator could not position the column correctly, using the "after" option in the migration script.'
171        );
172    }
173}