几年前,我已经在 StackOverflow 上提出了一个非常相似甚至几乎相同的问题。我得到了非常详细的答案,但它们并没有解决我的问题。现在,问题变得更大,我在这里开始第二次尝试解决这个问题。由于代码已更改,我不想更新原始问题。这将是一个太大的更新,答案可能与新版本的问题不匹配。所以我将它制定为一个新的:
我正在使用 Zend Framework 3 应用程序编写功能测试
- zendframework/zend-test
3.2.2
, - phpunit/phpunit
6.5.14
和 - phpunit/dbunit
3.0.3
大多数测试是一种控制器测试。Zend\Test\PHPUnit\Controller\AbstractControllerTestCase#dispatch(...)
测试代码通过使用并分析 1. 响应/输出数据和 2. 数据库中的更改(如果它是像“create foo”这样的写入调用)来调用 URI / 操作,例如:
/**
...
* @dataProvider provideDataForShowOrderAccess
*/
public function testShowOrderAccess(string $username, int $responseStatusCode)
{
...
$this->createOrder(...);
$this->reset();
$_SERVER['AUTH_USER'] = $username;
...
$this->dispatch($showUrl);
$this->assertResponseStatusCode($responseStatusCode);
}
/**
...
* @dataProvider provideDataForShowOrder
*/
public function testShowOrder(string $username, bool $isOwner)
{
...
$this->createOrder($connectionType, $endpointSourceType);
$this->reset();
$_SERVER['AUTH_USER'] = $username;
// testing the access by the owner
$orderId = 1;
$showUrl = '/order/show/' . $orderId;
$this->dispatch($showUrl);
if ($isOwner) {
$this->assertResponseStatusCode(Response::STATUS_CODE_200);
$this->assertModuleName('Order');
$this->assertControllerName('Order\Controller\Process');
$this->assertControllerClass('ProcessController');
$this->assertMatchedRouteName('order/show');
/** @var Foo $foo */
$foo = $this->getApplication()->getMvcEvent()->getResult()->getVariable('foo', null);
$fooData = $createParams['foo'];
$barData = $barData['bar'];
$this->assertNotNull($bar);
$this->assertInstanceOf(Foo::class, $foo);
$this->assertEquals($orderId, $foo->getId());
$this->assertEquals($fooData['bar'], $foo->getBar());
...
} else {
$this->assertResponseStatusCode(Response::STATUS_CODE_302);
}
}
对于每一个测试,数据库都会重置。
问题是,数据库连接的数量在不断增长——随着每次下一次测试。目前有大约350
( SHOW GLOBAL STATUS LIKE 'max_used_connections';
) 个连接用于102
测试。(作为一种解决方法,我必须max_connections
越来越多地增加 MySQL。)
$this->dbAdapter->getDriver()->getConnection()->disconnect();
我试图通过将类似或/和$this->entityManager->getConnection()->close();
之类的逻辑放在tearDown()
我的超类的控制器测试中来减少连接数。这样我的连接数减少了大约90
. 但是大多数连接仍然没有被杀死。
如何在 ZF3 应用程序的功能/控制器 PHPUnit 测试中关闭数据库连接并显着减少同时打开的连接数?
附加信息:我的代码中最相关的部分
AbstractControllerTest
namespace Base\Test;
use Doctrine\ORM\EntityManager;
use PDO;
use PHPUnit\DbUnit\Database\DefaultConnection;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
/**
* Class AbstractControllerTest
*
* @package Base\Test
*/
abstract class AbstractControllerTest extends AbstractHttpControllerTestCase
{
use DatabaseConnectionTrait;
/**
* @var string
*/
static private $applicationConfigPath;
/** @var Adapter */
protected $dbAdapter;
/** @var EntityManager */
protected $entityManager;
public function __construct($name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->setApplicationConfig(include self::$applicationConfigPath);
}
public static function setApplicationConfigPath(string $applicationConfigPath)
{
self::$applicationConfigPath = $applicationConfigPath;
}
protected function tearDown()
{
// Connections: 354
// Time: 5.7 minutes, Memory: 622.00MB
// OK (102 tests, 367 assertions)
// no optimization
// Connections: 326 (26 connections less)
// Time: 5.86 minutes, Memory: 620.00MB
// OK (102 tests, 367 assertions)
// if ($this->dbAdapter && $this->dbAdapter instanceof Adapter) {
// $this->dbAdapter->getDriver()->getConnection()->disconnect();
// }
// Connections: 354
// Time: 5.67 minutes, Memory: 620.00MB
// OK (102 tests, 367 assertions)
// $this->entityManager->close();
// Connections: 291 (63 connections less)
// Time: 5.63 minutes, Memory: 622.00MB
// OK (102 tests, 367 assertions)
// $this->entityManager->getConnection()->close();
// Connections: 264 (90 connections less)
// Time: 5.7 minutes, Memory: 620.00MB
// OK (102 tests, 367 assertions)
// if ($this->dbAdapter && $this->dbAdapter instanceof Adapter) {
// $this->dbAdapter->getDriver()->getConnection()->disconnect();
// }
// $this->entityManager->getConnection()->close();
// Connections: 251
// Time: 4.71 minutes, Memory: 574.00MB
// OK (102 tests, 367 assertions)
// After removing initialization of the EntityManager and the DbAdapter in the constructor and the setUp().
// closing DB connections
if ($this->dbAdapter && $this->dbAdapter instanceof Adapter) {
$this->dbAdapter->getDriver()->getConnection()->disconnect();
}
if ($this->entityManager && $this->entityManager instanceof EntityManager) {
$this->entityManager->getConnection()->close();
}
$reflectionObject = new \ReflectionObject($this);
foreach ($reflectionObject->getProperties() as $prop) {
if (!$prop->isStatic() && 0 !== strpos($prop->getDeclaringClass()->getName(), 'PHPUnit_')) {
$prop->setAccessible(true);
$prop->setValue($this, null);
}
}
$this->reset();
$this->application = null;
gc_collect_cycles();
unset($_SERVER['AUTH_USER']);
parent::tearDown();
}
protected function retrieveActualData($table, $idColumn, $idValue)
{
$sql = new Sql($this->getDbAdapter());
$select = $sql->select($table);
$select->where([$table . '.' . $idColumn . ' = ?' => $idValue]);
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
$data = $result->current();
// Decreases the total number of the connections by 1 less.
// $this->dbAdapter->getDriver()->getConnection()->disconnect();
return $data;
}
protected function getEntityManager()
{
$this->entityManager = $this->entityManager
?: $this->getApplicationServiceLocator()->get('doctrine.entitymanager.orm_default')
;
return $this->entityManager;
}
protected function getDbAdapter()
{
$this->dbAdapter = $this->dbAdapter
?: $this->getApplicationServiceLocator()->get('Zend\Db\Adapter\Adapter')
;
return $this->dbAdapter;
}
}
DatabaseConnectionTrait
namespace Base\Test;
use PDO;
use PHPUnit\DbUnit\Database\Connection;
use PHPUnit\DbUnit\Database\DefaultConnection;
use PHPUnit\DbUnit\InvalidArgumentException;
trait DatabaseConnectionTrait
{
/**
* @var array
*/
static private $dbConfigs;
/**
* @var PDO
*/
static private $pdo;
/**
* @var Connection
*/
private $connection;
public function __construct($name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
}
/**
* @return Connection
*/
public function getConnection()
{
if (! $this->connection) {
if (! self::$dbConfigs) {
throw new InvalidArgumentException(
'Set the database configuration first.'
. ' '. 'Use the ' . self::class . '::setDbConfigs(...).'
);
}
if (! self::$pdo) {
self::$pdo = new PDO(
self::$dbConfigs['dsn'],
self::$dbConfigs['username'],
self::$dbConfigs['password'],
[PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'']
);
}
$this->connection = $this->createDefaultDBConnection(self::$pdo, self::$dbConfigs['database']);
}
return $this->connection;
}
public static function setDbConfigs(array $dbConfigs)
{
self::$dbConfigs = $dbConfigs;
}
/**
* Creates a new DefaultDatabaseConnection using the given PDO connection
* and database schema name.
*
* @see The original PHPUnit\DbUnit\TestCaseTrait#createDefaultDBConnection(...).
*
* @param PDO $connection
* @param string $schema
*
* @return DefaultConnection
*/
protected function createDefaultDBConnection(PDO $connection, $schema = '')
{
return new DefaultConnection($connection, $schema);
}
}
DatabaseInitializer
namespace Base\Test;
/**
* Class DatabaseInitializer
*
* @package Base\Test
*/
class DatabaseInitializer
{
use DatabaseConnectionTrait;
/**
* @var string
*/
private $database;
public function __construct(array $dbConfigs)
{
$this->database = $dbConfigs['database'];
self::$dbConfigs = $dbConfigs;
}
public function setUp()
{
$schemaSql = file_get_contents(self::$dbConfigs['scripts']['schema']);
$storedProceduresSql = file_get_contents(self::$dbConfigs['scripts']['stored-procedures']);
$basicDataSql = file_get_contents(self::$dbConfigs['scripts']['basic-data']);
$testDataSqlSet = array_map(function ($sqlFile) {
return file_get_contents($sqlFile);
}, self::$dbConfigs['scripts']['test-data']);
$this->dropDatabase();
$this->createDatabase();
$this->useDatabase();
$this->createSchema($schemaSql);
$this->createStoredProcedures($storedProceduresSql);
$this->createBasicData($basicDataSql);
$this->createTestData($testDataSqlSet);
}
public function tearDown()
{
self::$pdo = null;
}
protected function createDatabase()
{
$this->getDatabaseConnection()->exec('CREATE DATABASE IF NOT EXISTS ' . $this->database . ';');
}
protected function useDatabase()
{
$this->getDatabaseConnection()->exec('USE ' . $this->database . ';');
}
protected function createSchema(string $sql)
{
$this->getDatabaseConnection()->exec($sql);
}
protected function createBasicData(string $sql)
{
$this->getDatabaseConnection()->exec($sql);
}
protected function createTestData(array $sqlSet = [])
{
foreach ($sqlSet as $sql) {
$this->getDatabaseConnection()->exec($sql);
}
}
protected function createStoredProcedures(string $sql)
{
$statement = $this->getDatabaseConnection()->prepare($sql);
$statement->execute();
}
protected function dropDatabase()
{
$this->getDatabaseConnection()->exec('DROP DATABASE IF EXISTS ' . $this->database . ';');
}
protected function getDatabaseConnection()
{
return $this->getConnection()->getConnection();
}
}
Bootstrap
namespace Test;
use Base\Test\AbstractControllerTest;
use Base\Test\AbstractDbTest;
use Base\Test\DatabaseInitializer;
use Doctrine\ORM\EntityManager;
use RuntimeException;
use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
error_reporting(E_ALL | E_STRICT);
ini_set('memory_limit', '2048M');
chdir(__DIR__);
/**
* Sets up the MVC (application, service manager, autoloading) and the database.
*/
class Bootstrap
{
/** @var ServiceManager */
protected $serviceManager;
protected $applicationConfigPath;
/** @var EntityManager */
protected $entityManager;
public function __construct()
{
$this->applicationConfigPath = __DIR__ . '/../config/application.config.php';
}
/**
* Sets up the
*/
public function init()
{
// autoloading setup
static::initAutoloader();
// application configuration & setup
$applicationConfig = require_once $this->applicationConfigPath;
$this->prepareApplication($applicationConfig);
// database configuration & setup
$dbConfigs = $this->serviceManager->get('Config')['db'];
$this->setUpDatabase($dbConfigs);
// listeners & application bootstrap
$listeners = $this->prepareListeners();
$this->bootstrapApplication($listeners);
}
public function chroot()
{
$rootPath = dirname(static::findParentPath('module'));
chdir($rootPath);
}
protected function prepareApplication($config)
{
$serviceManagerConfig = isset($config['service_manager']) ? $config['service_manager'] : [];
$serviceManagerConfigObject = new ServiceManagerConfig($serviceManagerConfig);
$this->serviceManager = new ServiceManager();
$serviceManagerConfigObject->configureServiceManager($this->serviceManager);
$this->serviceManager->setService('ApplicationConfig', $config);
$this->serviceManager->get('ModuleManager')->loadModules();
}
protected function prepareListeners()
{
$listenersFromAppConfig = [];
$config = $this->serviceManager->get('config');
$listenersFromConfigService = isset($config['listeners']) ? $config['listeners'] : [];
$listeners = array_unique(array_merge($listenersFromConfigService, $listenersFromAppConfig));
return $listeners;
}
protected function bootstrapApplication($listeners)
{
$application = $this->serviceManager->get('Application');
$application->bootstrap($listeners);
}
protected function setUpDatabase(array $dbConfigs)
{
$databaseInitializer = new DatabaseInitializer($dbConfigs);
$databaseInitializer->setUp();
AbstractDbTest::setDbConfigs($dbConfigs);
AbstractControllerTest::setApplicationConfigPath($this->applicationConfigPath);
AbstractControllerTest::setDbConfigs($dbConfigs);
}
protected function initAutoloader()
{
$vendorPath = static::findParentPath('vendor');
if (file_exists($vendorPath.'/autoload.php')) {
include $vendorPath.'/autoload.php';
}
if (! class_exists('Zend\Loader\AutoloaderFactory')) {
throw new RuntimeException(
'Unable to load ZF2. Run `php composer.phar install`'
);
}
AutoloaderFactory::factory(array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true,
'namespaces' => array(
__NAMESPACE__ => __DIR__,
),
),
));
}
protected function findParentPath($path)
{
$dir = __DIR__;
$previousDir = '.';
while (!is_dir($dir . '/' . $path)) {
$dir = dirname($dir);
if ($previousDir === $dir) {
return false;
}
$previousDir = $dir;
}
return $dir . '/' . $path;
}
}
$bootstrap = new Bootstrap();
$bootstrap->init();
$bootstrap->chroot();