Skip to content

Commit 4ffc6a9

Browse files
Clean all the tests. (#59)
1 parent cf3baa5 commit 4ffc6a9

File tree

16 files changed

+339
-112
lines changed

16 files changed

+339
-112
lines changed

src/DbCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
/**
3030
* DbCache stores cache data in a database table.
3131
*
32-
* Use {@see Migration::ensureTable()} to initialize database schema.
32+
* Use {@see DbHelper::ensureTable()} to initialize database schema.
3333
*/
3434
final class DbCache implements CacheInterface
3535
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Yiisoft\Db\Exception\NotSupportedException;
1212
use Yiisoft\Db\Schema\SchemaInterface;
1313

14-
final class Migration
14+
final class DbHelper
1515
{
1616
/**
1717
* @throws Exception
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Cache\Db\Tests\Common;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Throwable;
9+
use Yiisoft\Cache\Db\DbCache;
10+
use Yiisoft\Cache\Db\DbHelper;
11+
use Yiisoft\Db\Connection\ConnectionInterface;
12+
use Yiisoft\Db\Constraint\IndexConstraint;
13+
use Yiisoft\Db\Exception\Exception;
14+
use Yiisoft\Db\Exception\InvalidConfigException;
15+
use Yiisoft\Db\Exception\NotSupportedException;
16+
use Yiisoft\Db\Schema\SchemaInterface;
17+
18+
/**
19+
* @group Mssql
20+
*
21+
* @psalm-suppress PropertyNotSetInConstructor
22+
*/
23+
abstract class AbstractDbHelperTest extends TestCase
24+
{
25+
protected ConnectionInterface $db;
26+
27+
/**
28+
* @throws Exception
29+
* @throws InvalidConfigException
30+
* @throws NotSupportedException
31+
* @throws Throwable
32+
*/
33+
public function testDropTable(): void
34+
{
35+
DbHelper::ensureTable($this->db);
36+
37+
$this->assertNotNull($this->db->getTableSchema('{{%cache}}', true));
38+
39+
DbHelper::dropTable($this->db);
40+
41+
$this->assertNull($this->db->getTableSchema('{{%cache}}', true));
42+
43+
DbHelper::dropTable($this->db);
44+
45+
$this->assertNull($this->db->getTableSchema('{{%cache}}', true));
46+
}
47+
48+
/**
49+
* @throws Exception
50+
* @throws InvalidConfigException
51+
* @throws NotSupportedException
52+
* @throws Throwable
53+
*/
54+
public function testDropTableWithCustomTableName(): void
55+
{
56+
DbHelper::ensureTable($this->db, '{{%custom_cache}}');
57+
58+
$this->assertNotNull($this->db->getTableSchema('{{%custom_cache}}', true));
59+
60+
DbHelper::dropTable($this->db, '{{%custom_cache}}');
61+
62+
$this->assertNull($this->db->getTableSchema('{{%custom_cache}}', true));
63+
64+
DbHelper::dropTable($this->db, '{{%custom_cache}}');
65+
66+
$this->assertNull($this->db->getTableSchema('{{%custom_cache}}', true));
67+
}
68+
69+
/**
70+
* @throws Exception
71+
* @throws InvalidConfigException
72+
* @throws NotSupportedException
73+
* @throws Throwable
74+
*/
75+
public function testEnsureTable(): void
76+
{
77+
DbHelper::ensureTable($this->db);
78+
79+
$this->assertNotNull($this->db->getTableSchema('{{%cache}}', true));
80+
81+
DbHelper::dropTable($this->db);
82+
83+
$this->assertNull($this->db->getTableSchema('{{%cache}}', true));
84+
}
85+
86+
/**
87+
* @throws Exception
88+
* @throws InvalidConfigException
89+
* @throws NotSupportedException
90+
* @throws Throwable
91+
*/
92+
public function testEnsureTableWithCustomTableName(): void
93+
{
94+
DbHelper::ensureTable($this->db, '{{%custom_cache}}');
95+
96+
$this->assertNotNull($this->db->getTableSchema('{{%custom_cache}}', true));
97+
98+
DbHelper::dropTable($this->db, '{{%custom_cache}}');
99+
100+
$this->assertNull($this->db->getTableSchema('{{%custom_cache}}', true));
101+
}
102+
103+
/**
104+
* @throws InvalidConfigException
105+
* @throws NotSupportedException
106+
* @throws Exception
107+
* @throws Throwable
108+
*/
109+
public function testEnsureTableExist(): void
110+
{
111+
DbHelper::ensureTable($this->db);
112+
113+
$this->assertNotNull($this->db->getTableSchema('{{%cache}}'));
114+
115+
DbHelper::ensureTable($this->db, '{{%cache}}');
116+
117+
$this->assertNotNull($this->db->getTableSchema('{{%cache}}'));
118+
119+
DbHelper::dropTable($this->db);
120+
121+
$this->assertNull($this->db->getTableSchema('{{%cache}}', true));
122+
}
123+
124+
/**
125+
* @throws InvalidConfigException
126+
* @throws NotSupportedException
127+
* @throws Exception
128+
* @throws Throwable
129+
*/
130+
public function testEnsureTableExistWithCustomTableName(): void
131+
{
132+
DbHelper::ensureTable($this->db, '{{%custom_cache}}');
133+
134+
$this->assertNotNull($this->db->getTableSchema('{{%custom_cache}}'));
135+
136+
DbHelper::ensureTable($this->db, '{{%custom_cache}}');
137+
138+
$this->assertNotNull($this->db->getTableSchema('{{%custom_cache}}'));
139+
140+
DbHelper::dropTable($this->db, '{{%custom_cache}}');
141+
142+
$this->assertNull($this->db->getTableSchema('{{%custom_cache}}', true));
143+
}
144+
145+
/**
146+
* @throws Exception
147+
* @throws InvalidConfigException
148+
* @throws NotSupportedException
149+
* @throws Throwable
150+
*/
151+
public function testVerifyTableIndexes(): void
152+
{
153+
$dbCache = new DbCache($this->db, '{{%cache}}', 1_000_000);
154+
155+
DbHelper::ensureTable($this->db, $dbCache->getTable());
156+
157+
$schema = $this->db->getSchema();
158+
159+
/** @psalm-var IndexConstraint[] $indexes */
160+
$indexes = $schema->getTableIndexes($dbCache->getTable(), true);
161+
162+
$this->assertSame(['id'], $indexes[0]->getColumnNames());
163+
$this->assertTrue($indexes[0]->isUnique());
164+
$this->assertTrue($indexes[0]->isPrimary());
165+
166+
DbHelper::dropTable($this->db, $dbCache->getTable());
167+
168+
$this->assertNull($this->db->getTableSchema($dbCache->getTable(), true));
169+
}
170+
171+
/**
172+
* @throws Exception
173+
* @throws InvalidConfigException
174+
* @throws NotSupportedException
175+
* @throws Throwable
176+
*/
177+
public function testVerifyTableIndexesWithCustomTableName(): void
178+
{
179+
$dbCache = new DbCache($this->db, '{{%custom_cache}}', 1_000_000);
180+
181+
DbHelper::ensureTable($this->db, $dbCache->getTable());
182+
183+
$schema = $this->db->getSchema();
184+
185+
/** @psalm-var IndexConstraint[] $indexes */
186+
$indexes = $schema->getTableIndexes($dbCache->getTable(), true);
187+
188+
$this->assertSame(['id'], $indexes[0]->getColumnNames());
189+
$this->assertTrue($indexes[0]->isUnique());
190+
$this->assertTrue($indexes[0]->isPrimary());
191+
192+
DbHelper::dropTable($this->db, $dbCache->getTable());
193+
194+
$this->assertNull($this->db->getTableSchema($dbCache->getTable(), true));
195+
}
196+
197+
/**
198+
* @throws Exception
199+
* @throws InvalidConfigException
200+
* @throws NotSupportedException
201+
* @throws Throwable
202+
*/
203+
public function testVerifyTableStructure(): void
204+
{
205+
$dbCache = new DbCache($this->db, '{{%cache}}', 1_000_000);
206+
207+
DbHelper::ensureTable($this->db, $dbCache->getTable());
208+
209+
$prefix = $this->db->getTablePrefix();
210+
$tableSchema = $this->db->getTableSchema($dbCache->getTable());
211+
212+
$this->assertNotNull($tableSchema);
213+
214+
$this->assertSame($prefix . 'cache', $tableSchema->getName());
215+
$this->assertSame(['id'], $tableSchema->getPrimaryKey());
216+
$this->assertSame(['id', 'data', 'expire'], $tableSchema->getColumnNames());
217+
$this->assertSame(SchemaInterface::TYPE_STRING, $tableSchema->getColumn('id')?->getType());
218+
$this->assertSame(128, $tableSchema->getColumn('id')?->getSize());
219+
$this->assertSame(SchemaInterface::TYPE_BINARY, $tableSchema->getColumn('data')?->getType());
220+
$this->assertSame(SchemaInterface::TYPE_INTEGER, $tableSchema->getColumn('expire')?->getType());
221+
222+
DbHelper::dropTable($this->db, $dbCache->getTable());
223+
224+
$this->assertNull($this->db->getTableSchema($dbCache->getTable(), true));
225+
}
226+
227+
/**
228+
* @throws Exception
229+
* @throws InvalidConfigException
230+
* @throws NotSupportedException
231+
* @throws Throwable
232+
*/
233+
public function testVerifyTableStructureWithCustomTableName(): void
234+
{
235+
$dbCache = new DbCache($this->db, '{{%custom_cache}}', 1_000_000);
236+
237+
DbHelper::ensureTable($this->db, $dbCache->getTable());
238+
239+
$prefix = $this->db->getTablePrefix();
240+
$tableSchema = $this->db->getTableSchema($dbCache->getTable());
241+
242+
$this->assertNotNull($tableSchema);
243+
244+
$this->assertSame($prefix . 'custom_cache', $tableSchema->getName());
245+
$this->assertSame(['id'], $tableSchema->getPrimaryKey());
246+
$this->assertSame(['id', 'data', 'expire'], $tableSchema->getColumnNames());
247+
$this->assertSame(SchemaInterface::TYPE_STRING, $tableSchema->getColumn('id')?->getType());
248+
$this->assertSame(128, $tableSchema->getColumn('id')?->getSize());
249+
$this->assertSame(SchemaInterface::TYPE_BINARY, $tableSchema->getColumn('data')?->getType());
250+
$this->assertSame(SchemaInterface::TYPE_INTEGER, $tableSchema->getColumn('expire')?->getType());
251+
252+
DbHelper::dropTable($this->db, $dbCache->getTable());
253+
254+
$this->assertNull($this->db->getTableSchema($dbCache->getTable(), true));
255+
}
256+
}

tests/Common/AbstractMigrationTest.php

Lines changed: 0 additions & 87 deletions
This file was deleted.

tests/Common/TestCase.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
use ReflectionClass;
88
use ReflectionObject;
9+
use Throwable;
910
use Yiisoft\Cache\Db\DbCache;
10-
use Yiisoft\Cache\Db\Migration;
11+
use Yiisoft\Cache\Db\DbHelper;
1112
use Yiisoft\Db\Connection\ConnectionInterface;
13+
use Yiisoft\Db\Exception\Exception;
14+
use Yiisoft\Db\Exception\InvalidConfigException;
1215
use Yiisoft\Log\Logger;
1316

1417
abstract class TestCase extends \PHPUnit\Framework\TestCase
@@ -24,10 +27,15 @@ protected function setup(): void
2427
$this->dbCache = new DbCache($this->db, gcProbability: 1_000_000);
2528
}
2629

30+
/**
31+
* @throws Exception
32+
* @throws InvalidConfigException
33+
* @throws Throwable
34+
*/
2735
protected function tearDown(): void
2836
{
2937
// drop table
30-
Migration::dropTable($this->db);
38+
DbHelper::dropTable($this->db);
3139

3240
$this->db->close();
3341

0 commit comments

Comments
 (0)