|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Database\Live\MySQLi; |
| 15 | + |
| 16 | +use CodeIgniter\Database\Live\AbstractGetFieldDataTest; |
| 17 | +use Config\Database; |
| 18 | + |
| 19 | +/** |
| 20 | + * @group DatabaseLive |
| 21 | + * |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +final class GetFieldDataTest extends AbstractGetFieldDataTest |
| 25 | +{ |
| 26 | + protected function createForge(): void |
| 27 | + { |
| 28 | + if ($this->db->DBDriver !== 'MySQLi') { |
| 29 | + $this->markTestSkipped('This test is only for MySQLi.'); |
| 30 | + } |
| 31 | + |
| 32 | + $this->forge = Database::forge($this->db); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * As of MySQL 8.0.17, the display width attribute for integer data types |
| 37 | + * is deprecated and is not reported back anymore. |
| 38 | + * |
| 39 | + * @see https://dev.mysql.com/doc/refman/8.0/en/numeric-type-attributes.html |
| 40 | + */ |
| 41 | + private function isOldMySQL(): bool |
| 42 | + { |
| 43 | + return ! ( |
| 44 | + version_compare($this->db->getVersion(), '8.0.17', '>=') |
| 45 | + && strpos($this->db->getVersion(), 'MariaDB') === false |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public function testGetFieldData(): void |
| 50 | + { |
| 51 | + $fields = $this->db->getFieldData('test1'); |
| 52 | + |
| 53 | + $this->assertJsonStringEqualsJsonString( |
| 54 | + json_encode([ |
| 55 | + (object) [ |
| 56 | + 'name' => 'id', |
| 57 | + 'type' => 'int', |
| 58 | + 'max_length' => $this->isOldMySQL() ? 11 : null, |
| 59 | + 'default' => null, // The default value is not defined. |
| 60 | + 'primary_key' => 1, |
| 61 | + 'nullable' => false, |
| 62 | + ], |
| 63 | + (object) [ |
| 64 | + 'name' => 'text_not_null', |
| 65 | + 'type' => 'varchar', |
| 66 | + 'max_length' => 64, |
| 67 | + 'default' => null, // The default value is not defined. |
| 68 | + 'primary_key' => 0, |
| 69 | + 'nullable' => false, |
| 70 | + ], |
| 71 | + (object) [ |
| 72 | + 'name' => 'text_null', |
| 73 | + 'type' => 'varchar', |
| 74 | + 'max_length' => 64, |
| 75 | + 'default' => null, // The default value is not defined. |
| 76 | + 'primary_key' => 0, |
| 77 | + 'nullable' => true, |
| 78 | + ], |
| 79 | + (object) [ |
| 80 | + 'name' => 'int_default_0', |
| 81 | + 'type' => 'int', |
| 82 | + 'max_length' => $this->isOldMySQL() ? 11 : null, |
| 83 | + 'default' => '0', // int 0 |
| 84 | + 'primary_key' => 0, |
| 85 | + 'nullable' => false, |
| 86 | + ], |
| 87 | + (object) [ |
| 88 | + 'name' => 'text_default_null', |
| 89 | + 'type' => 'varchar', |
| 90 | + 'max_length' => 64, |
| 91 | + 'default' => null, // NULL value |
| 92 | + 'primary_key' => 0, |
| 93 | + 'nullable' => true, |
| 94 | + ], |
| 95 | + (object) [ |
| 96 | + 'name' => 'text_default_text_null', |
| 97 | + 'type' => 'varchar', |
| 98 | + 'max_length' => 64, |
| 99 | + 'default' => 'null', // string "null" |
| 100 | + 'primary_key' => 0, |
| 101 | + 'nullable' => false, |
| 102 | + ], |
| 103 | + (object) [ |
| 104 | + 'name' => 'text_default_abc', |
| 105 | + 'type' => 'varchar', |
| 106 | + 'max_length' => 64, |
| 107 | + 'default' => 'abc', // string "abc" |
| 108 | + 'primary_key' => 0, |
| 109 | + 'nullable' => false, |
| 110 | + ], |
| 111 | + ]), |
| 112 | + json_encode($fields) |
| 113 | + ); |
| 114 | + } |
| 115 | +} |
0 commit comments