Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
52 / 52 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| phpbb_dbal_auto_increment_test | |
100.00% |
52 / 52 |
|
100.00% |
5 / 5 |
6 | |
100.00% |
1 / 1 |
| getDataSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setUp | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
| tearDown | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| get_default_values | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| test_auto_increment | |
100.00% |
29 / 29 |
|
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 | |
| 14 | class phpbb_dbal_auto_increment_test extends phpbb_database_test_case |
| 15 | { |
| 16 | protected $db; |
| 17 | protected $db_doctrine; |
| 18 | protected $tools; |
| 19 | protected $table_exists; |
| 20 | protected $table_data; |
| 21 | |
| 22 | public function getDataSet() |
| 23 | { |
| 24 | return $this->createXMLDataSet(__DIR__ . '/fixtures/config.xml'); |
| 25 | } |
| 26 | |
| 27 | protected function setUp(): void |
| 28 | { |
| 29 | parent::setUp(); |
| 30 | |
| 31 | $table_prefix = 'prefix_'; |
| 32 | $this->db = $this->new_dbal(); |
| 33 | $this->db_doctrine = $this->new_doctrine_dbal(); |
| 34 | $factory = new \phpbb\db\tools\factory(); |
| 35 | $this->tools = $factory->get($this->db_doctrine); |
| 36 | $this->tools->set_table_prefix($table_prefix); |
| 37 | |
| 38 | $this->table_data = array( |
| 39 | 'COLUMNS' => array( |
| 40 | 'c_id' => array('UINT', NULL, 'auto_increment'), |
| 41 | 'c_uint' => array('UINT', 4), |
| 42 | ), |
| 43 | 'PRIMARY_KEY' => 'c_id', |
| 44 | ); |
| 45 | $this->tools->sql_create_table('prefix_table_name', $this->table_data); |
| 46 | $this->table_exists = true; |
| 47 | } |
| 48 | |
| 49 | protected function tearDown(): void |
| 50 | { |
| 51 | if ($this->table_exists) |
| 52 | { |
| 53 | $this->tools->sql_table_drop('prefix_table_name'); |
| 54 | } |
| 55 | |
| 56 | parent::tearDown(); |
| 57 | } |
| 58 | |
| 59 | protected static function get_default_values() |
| 60 | { |
| 61 | return array( |
| 62 | 'c_uint' => 0, |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | public function test_auto_increment() |
| 67 | { |
| 68 | $sql = 'DELETE FROM prefix_table_name'; |
| 69 | $this->db->sql_query($sql); |
| 70 | |
| 71 | $row1 = array_merge(self::get_default_values(), array( |
| 72 | 'c_uint' => 1, |
| 73 | )); |
| 74 | $row2 = array_merge(self::get_default_values(), array( |
| 75 | 'c_uint' => 2, |
| 76 | )); |
| 77 | |
| 78 | $sql = 'INSERT INTO prefix_table_name ' . $this->db->sql_build_array('INSERT', $row1); |
| 79 | $this->db->sql_query($sql); |
| 80 | $id1 = $this->db->sql_nextid(); |
| 81 | |
| 82 | $sql = 'INSERT INTO prefix_table_name ' . $this->db->sql_build_array('INSERT', $row2); |
| 83 | $this->db->sql_query($sql); |
| 84 | $id2 = $this->db->sql_nextid(); |
| 85 | |
| 86 | $this->assertGreaterThan($id1, $id2, 'Auto increment should increase the id value'); |
| 87 | |
| 88 | $sql = "SELECT * |
| 89 | FROM prefix_table_name WHERE c_id = $id1"; |
| 90 | $result = $this->db->sql_query($sql); |
| 91 | $row_actual = $this->db->sql_fetchrow($result); |
| 92 | $this->db->sql_freeresult($result); |
| 93 | |
| 94 | $row1['c_id'] = $id1; |
| 95 | $this->assertEquals($row1, $row_actual); |
| 96 | |
| 97 | $sql = "SELECT * |
| 98 | FROM prefix_table_name WHERE c_id = $id2"; |
| 99 | $result = $this->db->sql_query($sql); |
| 100 | $row_actual = $this->db->sql_fetchrow($result); |
| 101 | $this->db->sql_freeresult($result); |
| 102 | |
| 103 | $row2['c_id'] = $id2; |
| 104 | $this->assertEquals($row2, $row_actual); |
| 105 | } |
| 106 | } |