diff --git a/CLAUDE.md b/CLAUDE.md index 5b3a1c3f..d2b65a0d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -23,7 +23,7 @@ composer schema-diff # compares those two databases: exit 0 c PHPStan is pinned to an exact version in `composer.json`, because `composer.lock` is not committed and the baseline records exact error counts per file; a commit that raises that version also regenerates `phpstan-baseline.neon`. -Test database: `tests/Unit/ConversationTest.php` and `tests/Unit/DBTest.php` touch MySQL. They read the `PHPUNIT_DB_*` constants from the PHPUnit config and expect a database (default name `telegrambot`) loaded from `structure.sql`. To use other credentials, copy `phpunit.xml.dist` to `phpunit.xml` (gitignored) and edit the constants there. `TestHelpers::emptyDb()` deletes every row from the core tables, so never point it at a database holding real data. +Test database: `tests/Unit/ConversationTest.php` and `tests/Unit/DBTest.php` touch MySQL. They read the `PHPUNIT_DB_*` constants from the PHPUnit config and expect a database (default name `telegrambot`) loaded from `structure.sql`. To use other credentials, copy `phpunit.xml.dist` to `phpunit.xml` (gitignored) and edit the constants there. `TestHelpers::emptyDb()` deletes every row from every table of that database, so never point it at a database holding real data. CI runs every job against its own fresh database server: PHP 8.1 through 8.5 on MariaDB 12.3, and PHP 8.5 on MariaDB 10.11, MySQL 9.7 and MySQL 8.4. On each of these four servers it runs the migration chain and the schema diff (see Database schema), then the test suite against the `telegrambot` database that `composer db-prepare` built. MySQL is stricter than MariaDB (for example, it rejects a foreign key that references a non-unique column), so keep `structure.sql` loadable on all four with default settings. diff --git a/tests/Unit/DBTest.php b/tests/Unit/DBTest.php index e7d99fa8..83e1c661 100644 --- a/tests/Unit/DBTest.php +++ b/tests/Unit/DBTest.php @@ -21,9 +21,14 @@ */ class DBTest extends TestCase { + /** + * @var array + */ + private array $credentials; + protected function setUp(): void { - $credentials = [ + $this->credentials = [ 'host' => PHPUNIT_DB_HOST, 'port' => PHPUNIT_DB_PORT, 'database' => PHPUNIT_DB_NAME, @@ -32,9 +37,9 @@ protected function setUp(): void ]; $telegram = new Telegram(self::$dummy_api_key, 'testbot'); - $telegram->enableMySql($credentials); + $telegram->enableMySql($this->credentials); - TestHelpers::emptyDb($credentials); + TestHelpers::emptyDb($this->credentials); } public function testInsertMessageReactionCountUpdateForChannelPost(): void @@ -113,4 +118,32 @@ public function testInsertCallbackQueryWithInaccessibleMessage(): void self::assertEquals(222, $row['chat_id']); self::assertNull($row['message_id']); } + + public function testEmptyDbClearsTablesThatReferenceAMessage(): void + { + $message = [ + 'message_id' => 45, + 'from' => ['id' => 111, 'is_bot' => false, 'first_name' => 'First'], + 'chat' => ['id' => 222, 'type' => 'private', 'first_name' => 'First'], + 'date' => time(), + 'text' => 'text', + ]; + + self::assertTrue(DB::insertRequest(new Update(['update_id' => 1005, 'message' => $message], 'testbot'))); + self::assertTrue(DB::insertRequest(new Update([ + 'update_id' => 1006, + 'edited_message' => $message + ['edit_date' => time()], + ], 'testbot'))); + self::assertEquals(1, DB::getPdo()->query('SELECT COUNT(*) FROM `edited_message`')->fetchColumn()); + + TestHelpers::emptyDb($this->credentials); + + foreach (['edited_message', 'message', 'chat', 'user'] as $table) { + self::assertEquals( + 0, + DB::getPdo()->query('SELECT COUNT(*) FROM `' . $table . '`')->fetchColumn(), + 'Rows left in ' . $table + ); + } + } } diff --git a/tests/Unit/TestHelpers.php b/tests/Unit/TestHelpers.php index 5536a0fb..73fc2567 100644 --- a/tests/Unit/TestHelpers.php +++ b/tests/Unit/TestHelpers.php @@ -236,18 +236,20 @@ public static function emptyDb(array $credentials): void $options = [$init_command => 'SET NAMES utf8']; $pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options); - $pdo->prepare(' - DELETE FROM `conversation`; - DELETE FROM `telegram_update`; - DELETE FROM `message_reaction`; - DELETE FROM `message_reaction_count`; - DELETE FROM `callback_query`; - DELETE FROM `chosen_inline_result`; - DELETE FROM `inline_query`; - DELETE FROM `message`; - DELETE FROM `user_chat`; - DELETE FROM `chat`; - DELETE FROM `user`; - ')->execute(); + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + + // The tables come from the server, so a table added to the schema is emptied too. + // One statement per call: in a batch, a failing statement ends the batch unnoticed. + $tables = $pdo->query(" + SELECT `TABLE_NAME` + FROM `information_schema`.`TABLES` + WHERE `TABLE_SCHEMA` = DATABASE() + AND `TABLE_TYPE` = 'BASE TABLE' + ")->fetchAll(\PDO::FETCH_COLUMN); + + $pdo->exec('SET FOREIGN_KEY_CHECKS = 0'); + foreach ($tables as $table) { + $pdo->exec('DELETE FROM `' . str_replace('`', '``', $table) . '`'); + } } }