我正在使用SQLite连接和理论迁移来使用PHPUnit进行功能测试。我正在setUp方法中从头开始进行DB迁移:
public function setUp()
{
parent::setUp();
@unlink(__DIR__ . '/../../../../../../../var/sqlite.db');
exec('./vendor/bin/doctrine-migrations migrations:migrate --db-configuration=migrations-db-test.php --configuration=migrations_test.yml --no-interaction');
}然后我就可以从DB写/读了。例如:
public function test_add_event_should_add_event()
{
$service = $this->getAdEventComparativesUpdateService();
$request = AdEventComparativesUpdateServiceRequest::make(self::AD_ID, self::USER_IP);
$response = $service->execute($request);
$this->assertEquals(1, $response->getTotal());
}而且起作用了。即使我用相同的参数调用两次服务,它也能工作。在这种情况下,它只需第一次编写:
public function test_add_two_same_events_should_add_one_event()
{
$service = $this->getAdEventComparativesUpdateService();
$request = AdEventComparativesUpdateServiceRequest::make(self::AD_ID, self::USER_IP);
// Call twice
$service->execute($request);
$response = $service->execute($request);
$this->assertEquals(1, $response->getTotal());
}当我必须测试两个必须同时写两个调用时,问题就来了:
public function test_add_two_different_events_should_add_two_events()
{
$service = $this->getAdEventComparativesUpdateService();
$request = AdEventComparativesUpdateServiceRequest::make(self::AD_ID, self::USER_IP);
$response = $service->execute($request);
$service = $this->getAdEventComparativesUpdateService();
$request = AdEventComparativesUpdateServiceRequest::make(self::AD_ID, self::OTHER_USER_IP);
$response = $service->execute($request); // **** It fails here
$this->assertEquals(2, $response->getTotal());
}这是一个错误:
xxx/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractSQLiteDriver.php:78 xxx/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:128 xxx/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php:178 xxx/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:281 xxx/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1014 SQLSTATEHY000:一般错误:8尝试写入只读数据库
xxx/SQLSTATEHY000/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:378 xxx/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:356 xxx/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:235 xxx/DoctrineSession.php:32 xxx/TransactionalApplicationService.php:39 xxx/xxx/test.php:100
我尝试在调用之间更改DB文件权限,但是没有什么改变:
public function test_add_two_different_events_should_add_two_event()
{
$service = $this->getAdEventComparativesUpdateService();
$request = AdEventComparativesUpdateServiceRequest::make(self::AD_ID, self::USER_IP);
$response = $service->execute($request);
chmod(__DIR__ . '/../../../../../../../var', 0777);
chmod(__DIR__ . '/../../../../../../../var/sqlite.db', 0777);
chown(__DIR__ . '/../../../../../../../var', 'www-data');
chgrp(__DIR__ . '/../../../../../../../var', 'www-data');
chown(__DIR__ . '/../../../../../../../var/sqlite.db', 'www-data');
chgrp(__DIR__ . '/../../../../../../../var/sqlite.db', 'www-data');
//die;
// Here I checked the /var sqlite.db permissions. They are 0777
$service = $this->getAdEventComparativesUpdateService();
$request = AdEventComparativesUpdateServiceRequest::make(self::AD_ID, self::OTHER_USER_IP);
$response = $service->execute($request);
$this->assertEquals(2, $response->getTotal());
}知道错误从何而来吗?在这种情况下,每个服务调用都将调用persist + flush。
发布于 2020-08-09 03:14:47
这是很难回答的,因为我们没有真正的先生。最常见的情况是,这是一个权限问题,但看起来您已经探索过此路径。我想我预感到这里发生了什么:
看起来,单元测试在每个测试上重新创建DB,即断开DB文件,然后当连接打开时,如果DB文件不存在,则自动创建DB文件。因此,在两次尝试写的情况下,首先打开连接并保持打开。然后,通过相同或另一个测试调用DB文件取消链接,随后的写命令失败。
https://stackoverflow.com/questions/63189635
复制相似问题