diff --git a/src/Schema/TestCase.php b/src/Schema/TestCase.php index 85c608a0d0..937373a682 100644 --- a/src/Schema/TestCase.php +++ b/src/Schema/TestCase.php @@ -7,9 +7,7 @@ use Atk4\Core\Phpunit\TestCase as BaseTestCase; use Atk4\Data\Model; use Atk4\Data\Persistence; -use Doctrine\DBAL\Logging\SQLLogger; use Doctrine\DBAL\Platforms\AbstractPlatform; -use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Platforms\OraclePlatform; use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\DBAL\Platforms\SQLServerPlatform; @@ -43,66 +41,7 @@ protected function setUp(): void { parent::setUp(); - // create SQL persistence with lazy connect - $this->db = new class() extends Persistence\Sql { - public function __construct() - { - } - - public function getConnection(): Persistence\Sql\Connection - { - \Closure::bind(function () { - if ($this->_connection === null) { - $connection = Persistence::connect($_ENV['DB_DSN'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'])->_connection; // @phpstan-ignore-line - $this->_connection = $connection; - - if ($connection->getDatabasePlatform() instanceof MySQLPlatform) { - $connection->expr( - 'SET SESSION auto_increment_increment = 1, SESSION auto_increment_offset = 1' - )->executeStatement(); - } - - $connection->getConnection()->getConfiguration()->setSQLLogger( - // @phpstan-ignore-next-line SQLLogger is deprecated - null ?? new class() implements SQLLogger { - public function startQuery($sql, array $params = null, array $types = null): void - { - $test = TestCase::getTestFromBacktrace(); - if (!$test->debug) { - return; - } - - echo "\n" . $sql . (substr($sql, -1) !== ';' ? ';' : '') . "\n" - . (is_array($params) && count($params) > 0 ? substr(print_r(array_map(function ($v) { - if ($v === null) { - $v = 'null'; - } elseif (is_bool($v)) { - $v = $v ? 'true' : 'false'; - } elseif (is_float($v) && (string) $v === (string) (int) $v) { - $v = $v . '.0'; - } elseif (is_string($v)) { - if (strlen($v) > 4096) { - $v = '*long string* (length: ' . strlen($v) . ' bytes, sha256: ' . hash('sha256', $v) . ')'; - } else { - $v = '\'' . $v . '\''; - } - } - - return $v; - }, $params), true), 6) : '') . "\n"; - } - - public function stopQuery(): void - { - } - } - ); - } - }, $this, Persistence\Sql::class)(); - - return parent::getConnection(); - } - }; + $this->db = new TestPersistenceSql(); } protected function tearDown(): void diff --git a/src/Schema/TestPersistenceSql.php b/src/Schema/TestPersistenceSql.php new file mode 100644 index 0000000000..07976f7b99 --- /dev/null +++ b/src/Schema/TestPersistenceSql.php @@ -0,0 +1,75 @@ +_connection === null) { + $connection = Persistence::connect($_ENV['DB_DSN'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'])->_connection; // @phpstan-ignore-line + $this->_connection = $connection; + + if ($connection->getDatabasePlatform() instanceof MySQLPlatform) { + $connection->expr( + 'SET SESSION auto_increment_increment = 1, SESSION auto_increment_offset = 1' + )->executeStatement(); + } + + $connection->getConnection()->getConfiguration()->setSQLLogger( + // @phpstan-ignore-next-line SQLLogger is deprecated + null ?? new class() implements SQLLogger { + public function startQuery($sql, array $params = null, array $types = null): void + { + $test = TestCase::getTestFromBacktrace(); + if (!$test->debug) { + return; + } + + echo "\n" . $sql . (substr($sql, -1) !== ';' ? ';' : '') . "\n" + . (is_array($params) && count($params) > 0 ? substr(print_r(array_map(function ($v) { + if ($v === null) { + $v = 'null'; + } elseif (is_bool($v)) { + $v = $v ? 'true' : 'false'; + } elseif (is_float($v) && (string) $v === (string) (int) $v) { + $v = $v . '.0'; + } elseif (is_string($v)) { + if (strlen($v) > 4096) { + $v = '*long string* (length: ' . strlen($v) . ' bytes, sha256: ' . hash('sha256', $v) . ')'; + } else { + $v = '\'' . $v . '\''; + } + } + + return $v; + }, $params), true), 6) : '') . "\n"; + } + + public function stopQuery(): void + { + } + } + ); + } + }, $this, Persistence\Sql::class)(); + + return parent::getConnection(); + } +}