-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathShowTableInfoTest.php
More file actions
208 lines (162 loc) · 7.8 KB
/
ShowTableInfoTest.php
File metadata and controls
208 lines (162 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Commands\Database;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\StreamFilterTrait;
use Config\Database;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Database\Seeds\CITestSeeder;
/**
* @internal
*/
#[Group('DatabaseLive')]
final class ShowTableInfoTest extends CIUnitTestCase
{
use DatabaseTestTrait;
use StreamFilterTrait;
protected $migrateOnce = true;
protected function setUp(): void
{
parent::setUp();
putenv('NO_COLOR=1');
CLI::init();
}
protected function tearDown(): void
{
parent::tearDown();
putenv('NO_COLOR');
CLI::init();
}
private function getNormalizedResult(): string
{
return str_replace(PHP_EOL, "\n", $this->getStreamFilterBuffer());
}
public function testDbTable(): void
{
command('db:table db_migrations');
$result = $this->getNormalizedResult();
$expected = 'Data of Table "db_migrations":';
$this->assertStringContainsString($expected, $result);
$expectedPattern = '/\| id[[:blank:]]+\| version[[:blank:]]+\| class[[:blank:]]+\| group[[:blank:]]+\| namespace[[:blank:]]+\| time[[:blank:]]+\| batch \|/';
$this->assertMatchesRegularExpression($expectedPattern, $result);
}
public function testDbTableShowsWithInvalidDBGroup(): void
{
command('db:table --show --dbgroup invalid');
$result = $this->getNormalizedResult();
$expected = '"invalid" is not a valid database connection group.';
$this->assertStringContainsString($expected, $result);
}
public function testDbTableShowsDBConfig(): void
{
command('db:table --show');
$result = $this->getNormalizedResult();
$expectedPattern = '/\| hostname[[:blank:]]+\| database[[:blank:]]+\| username[[:blank:]]+\| DBDriver[[:blank:]]+\| DBPrefix[[:blank:]]+\| port[[:blank:]]+\|/';
$this->assertMatchesRegularExpression($expectedPattern, $result);
}
public function testDbTableShow(): void
{
command('db:table --show');
$result = $this->getNormalizedResult();
$expected = 'The following is a list of the names of all database tables:';
$this->assertStringContainsString($expected, $result);
$expected = <<<'EOL'
+----+---------------------------+-------------+---------------+
| ID | Table Name | Num of Rows | Num of Fields |
+----+---------------------------+-------------+---------------+
EOL;
$this->assertStringContainsString($expected, $result);
}
public function testDbTableMetadata(): void
{
command('db:table db_migrations --metadata');
$result = $this->getNormalizedResult();
$expected = 'List of Metadata Information in Table "db_migrations":';
$this->assertStringContainsString($expected, $result);
$result = preg_replace('/\s+/', ' ', $result);
$expected = <<<'EOL'
| Field Name | Type | Max Length | Nullable | Default | Primary Key |
EOL;
$this->assertStringContainsString($expected, (string) $result);
}
public function testDbTableDesc(): void
{
$seeder = Database::seeder();
$seeder->call(CITestSeeder::class);
command('db:table db_user --desc');
$result = $this->getNormalizedResult();
$expected = 'Data of Table "db_user":';
$this->assertStringContainsString($expected, $result);
$expected = <<<'EOL'
+----+--------------------+--------------------+---------+------------+------------+------------+
| id | name | email | country | created_at | updated_at | deleted_at |
+----+--------------------+--------------------+---------+------------+------------+------------+
| 4 | Chris Martin | chris@world.com | UK | | | |
| 3 | Richard A Cause... | richard@world.c... | US | | | |
| 2 | Ahmadinejad | ahmadinejad@wor... | Iran | | | |
| 1 | Derek Jones | derek@world.com | US | | | |
+----+--------------------+--------------------+---------+------------+------------+------------+
EOL;
$this->assertStringContainsString($expected, $result);
}
public function testDbTableLimitFieldValueLength(): void
{
command('db:table db_user --limit-field-value 5');
$result = $this->getNormalizedResult();
$expected = 'Data of Table "db_user":';
$this->assertStringContainsString($expected, $result);
$expected = <<<'EOL'
+----+----------+----------+---------+------------+------------+------------+
| id | name | email | country | created_at | updated_at | deleted_at |
+----+----------+----------+---------+------------+------------+------------+
| 1 | Derek... | derek... | US | | | |
| 2 | Ahmad... | ahmad... | Iran | | | |
| 3 | Richa... | richa... | US | | | |
| 4 | Chris... | chris... | UK | | | |
+----+----------+----------+---------+------------+------------+------------+
EOL;
$this->assertStringContainsString($expected, $result);
}
public function testDbTableLimitRows(): void
{
command('db:table db_user --limit-rows 2');
$result = $this->getNormalizedResult();
$expected = 'Data of Table "db_user":';
$this->assertStringContainsString($expected, $result);
$expected = <<<'EOL'
+----+-------------+--------------------+---------+------------+------------+------------+
| id | name | email | country | created_at | updated_at | deleted_at |
+----+-------------+--------------------+---------+------------+------------+------------+
| 1 | Derek Jones | derek@world.com | US | | | |
| 2 | Ahmadinejad | ahmadinejad@wor... | Iran | | | |
+----+-------------+--------------------+---------+------------+------------+------------+
EOL;
$this->assertStringContainsString($expected, $result);
}
public function testDbTableAllOptions(): void
{
command('db:table db_user --limit-rows 2 --limit-field-value 5 --desc');
$result = $this->getNormalizedResult();
$expected = 'Data of Table "db_user":';
$this->assertStringContainsString($expected, $result);
$expected = <<<'EOL'
+----+----------+----------+---------+------------+------------+------------+
| id | name | email | country | created_at | updated_at | deleted_at |
+----+----------+----------+---------+------------+------------+------------+
| 4 | Chris... | chris... | UK | | | |
| 3 | Richa... | richa... | US | | | |
+----+----------+----------+---------+------------+------------+------------+
EOL;
$this->assertStringContainsString($expected, $result);
}
}