LargeQueryTest.php in Drupal 8
File
core/tests/Drupal/KernelTests/Core/Database/LargeQueryTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Database;
use Drupal\Component\Utility\Environment;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseException;
class LargeQueryTest extends DatabaseTestBase {
public function testMaxAllowedPacketQueryTruncating() {
$driver = $this->connection
->driver();
if ($driver !== 'mysql') {
$this
->markTestSkipped("MySql tests can not run for driver '{$driver}'.");
}
$max_allowed_packet = $this->connection
->query('SELECT @@global.max_allowed_packet')
->fetchField();
if (!Environment::checkMemoryLimit($max_allowed_packet + 16 * 1024 * 1024)) {
$this
->markTestSkipped('The configured max_allowed_packet exceeds the php memory limit. Therefore the test is skipped.');
}
$long_name = str_repeat('a', $max_allowed_packet + 1);
try {
$this->connection
->query('SELECT name FROM {test} WHERE name = :name', [
':name' => $long_name,
]);
$this
->fail("An exception should be thrown for queries larger than 'max_allowed_packet'");
} catch (DatabaseException $e) {
Database::closeConnection();
Database::getConnection();
$this
->assertEquals(1153, $e
->getPrevious()->errorInfo[1]);
$this
->assertLessThanOrEqual($max_allowed_packet, strlen($e
->getMessage()));
}
}
}