|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Database\Live\MySQLi; |
| 13 | + |
| 14 | +use CodeIgniter\Database\MySQLi\Connection; |
| 15 | +use CodeIgniter\Database\MySQLi\Forge; |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use CodeIgniter\Test\DatabaseTestTrait; |
| 18 | +use Config\Database; |
| 19 | + |
| 20 | +/** |
| 21 | + * @group DatabaseLive |
| 22 | + * |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +final class GetFieldDataTest extends CIUnitTestCase |
| 26 | +{ |
| 27 | + use DatabaseTestTrait; |
| 28 | + |
| 29 | + protected $migrate = false; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var Connection |
| 33 | + */ |
| 34 | + protected $db; |
| 35 | + |
| 36 | + private Forge $forge; |
| 37 | + |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + parent::setUp(); |
| 41 | + |
| 42 | + if ($this->db->DBDriver !== 'MySQLi') { |
| 43 | + $this->markTestSkipped('This test is only for MySQLi.'); |
| 44 | + } |
| 45 | + |
| 46 | + $this->forge = Database::forge($this->db); |
| 47 | + } |
| 48 | + |
| 49 | + public function testGetFieldData(): void |
| 50 | + { |
| 51 | + $this->forge->dropTable('test1', true); |
| 52 | + |
| 53 | + $this->forge->addField([ |
| 54 | + 'id' => [ |
| 55 | + 'type' => 'INT', |
| 56 | + 'auto_increment' => true, |
| 57 | + ], |
| 58 | + 'text_not_null' => [ |
| 59 | + 'type' => 'VARCHAR', |
| 60 | + 'constraint' => 64, |
| 61 | + ], |
| 62 | + 'text_null' => [ |
| 63 | + 'type' => 'VARCHAR', |
| 64 | + 'constraint' => 64, |
| 65 | + 'null' => true, |
| 66 | + ], |
| 67 | + 'int_default_0' => [ |
| 68 | + 'type' => 'INT', |
| 69 | + 'default' => 0, |
| 70 | + ], |
| 71 | + 'text_default_null' => [ |
| 72 | + 'type' => 'VARCHAR', |
| 73 | + 'constraint' => 64, |
| 74 | + 'default' => null, |
| 75 | + ], |
| 76 | + 'text_default_text_null' => [ |
| 77 | + 'type' => 'VARCHAR', |
| 78 | + 'constraint' => 64, |
| 79 | + 'default' => 'null', |
| 80 | + ], |
| 81 | + 'text_default_abc' => [ |
| 82 | + 'type' => 'VARCHAR', |
| 83 | + 'constraint' => 64, |
| 84 | + 'default' => 'abc', |
| 85 | + ], |
| 86 | + ]); |
| 87 | + $this->forge->addKey('id', true); |
| 88 | + $this->forge->createTable('test1'); |
| 89 | + |
| 90 | + $fields = $this->db->getFieldData('test1'); |
| 91 | + |
| 92 | + $this->assertJsonStringEqualsJsonString( |
| 93 | + json_encode([ |
| 94 | + (object) [ |
| 95 | + 'name' => 'id', |
| 96 | + 'type' => 'int', |
| 97 | + 'max_length' => null, |
| 98 | + 'default' => null, // The default value is not defined. |
| 99 | + 'primary_key' => 1, |
| 100 | + 'nullable' => false, |
| 101 | + ], |
| 102 | + (object) [ |
| 103 | + 'name' => 'text_not_null', |
| 104 | + 'type' => 'varchar', |
| 105 | + 'max_length' => 64, |
| 106 | + 'default' => null, // The default value is not defined. |
| 107 | + 'primary_key' => 0, |
| 108 | + 'nullable' => false, |
| 109 | + ], |
| 110 | + (object) [ |
| 111 | + 'name' => 'text_null', |
| 112 | + 'type' => 'varchar', |
| 113 | + 'max_length' => 64, |
| 114 | + 'default' => null, // The default value is not defined. |
| 115 | + 'primary_key' => 0, |
| 116 | + 'nullable' => true, |
| 117 | + ], |
| 118 | + (object) [ |
| 119 | + 'name' => 'int_default_0', |
| 120 | + 'type' => 'int', |
| 121 | + 'max_length' => null, |
| 122 | + 'default' => '0', // int 0 |
| 123 | + 'primary_key' => 0, |
| 124 | + 'nullable' => false, |
| 125 | + ], |
| 126 | + (object) [ |
| 127 | + 'name' => 'text_default_null', |
| 128 | + 'type' => 'varchar', |
| 129 | + 'max_length' => 64, |
| 130 | + 'default' => null, // NULL value |
| 131 | + 'primary_key' => 0, |
| 132 | + 'nullable' => true, |
| 133 | + ], |
| 134 | + (object) [ |
| 135 | + 'name' => 'text_default_text_null', |
| 136 | + 'type' => 'varchar', |
| 137 | + 'max_length' => 64, |
| 138 | + 'default' => 'null', // string "null" |
| 139 | + 'primary_key' => 0, |
| 140 | + 'nullable' => false, |
| 141 | + ], |
| 142 | + (object) [ |
| 143 | + 'name' => 'text_default_abc', |
| 144 | + 'type' => 'varchar', |
| 145 | + 'max_length' => 64, |
| 146 | + 'default' => 'abc', // string "abc" |
| 147 | + 'primary_key' => 0, |
| 148 | + 'nullable' => false, |
| 149 | + ], |
| 150 | + ]), |
| 151 | + json_encode($fields) |
| 152 | + ); |
| 153 | + |
| 154 | + $this->forge->dropTable('test1', true); |
| 155 | + } |
| 156 | +} |
0 commit comments