- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我目前正在学习如何在 PHPUnit 框架中测试数据库,遇到了我不想在测试中连接到真实数据库的问题。这是因为当我在另一台计算机上运行测试时,这台计算机可能没有相同的数据库。
我实现了 \PHPUnit\DbUnit\TestCaseTrait
特性并设置了以下方法:
/**
* Returns the test database connection.
*
* @return \PHPUnit\DbUnit\Database\Connection
*/
protected function getConnection()
{
$pdo = new PDO('sqlite::memory:');
return $this->createDefaultDBConnection($pdo, ':memory:');
}
/**
* Returns the test dataset.
*
* @return \PHPUnit\DbUnit\DataSet\IDataSet
*/
protected function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/test-dataset.xml');
}
数据集文件存在,并且已正确找到。
在我测试的 setUp
方法中,我将对象中的一个变量设置为 \PDO
实例。
/**
* @var PDO $databaseServerConnection
*/
private $databaseServerConnection;
public function setUp()
{
$this->databaseServerConnection = $this->getConnection()->getConnection();
}
我希望我现在可以在 getDataSet()
方法中将 PDO 连接与来自数据集文件的数据一起使用。
为了我自己的尝试,我尝试将它们与以下代码进行比较:
# Specify the tables we want to have in our connection dataset
$tables = ['users'];
# Create the dataset in the connection with the tables
$dataset = $this->getConnection()->createDataSet($tables);
# Query all results from the user table in the connection
$queryTable = $this->getConnection()->createQueryTable(
'users', 'SELECT * FROM users'
);
# Get the raw table data from the dataset file
$expectedTable = $this->getDataSet()->getTable('users');
# Check if theyre equal
$this->assertTablesEqual($queryTable, $expectedTable);
调试时,我注意到 $dataset
中的 $tables
数组变量只是一个空变量。这里是 $dataset
变量的 var_dump
。
class PHPUnit\DbUnit\Database\FilteredDataSet#18 (3) {
protected $tableNames =>
array(1) {
[0] =>
string(5) "users"
}
protected $tables =>
array(0) {
}
protected $databaseConnection =>
class PHPUnit\DbUnit\Database\DefaultConnection#16 (2) {
protected $connection =>
class PDO#15 (0) {
}
protected $metaData =>
class PHPUnit\DbUnit\Database\Metadata\Sqlite#17 (6) {
protected $columns =>
array(0) {
...
}
protected $keys =>
array(0) {
...
}
protected $truncateCommand =>
string(11) "DELETE FROM"
protected $pdo =>
class PDO#15 (0) {
...
}
protected $schema =>
string(8) ":memory:"
protected $schemaObjectQuoteChar =>
string(1) """
}
}
}
此外,$queryTable
变量中的 $data
数组为 null
。这里是 $queryTable
变量的 var_dump
。
class PHPUnit\DbUnit\DataSet\QueryTable#22 (6) {
protected $query =>
string(19) "SELECT * FROM users"
protected $databaseConnection =>
class PHPUnit\DbUnit\Database\DefaultConnection#20 (2) {
protected $connection =>
class PDO#19 (0) {
}
protected $metaData =>
class PHPUnit\DbUnit\Database\Metadata\Sqlite#21 (6) {
protected $columns =>
array(0) {
...
}
protected $keys =>
array(0) {
...
}
protected $truncateCommand =>
string(11) "DELETE FROM"
protected $pdo =>
class PDO#19 (0) {
...
}
protected $schema =>
string(8) ":memory:"
protected $schemaObjectQuoteChar =>
string(1) """
}
}
protected $tableName =>
string(5) "users"
protected $tableMetaData =>
NULL
protected $data =>
NULL
private $other =>
NULL
}
此时 $expectedTable
变量中的 $data
数组充满了在数据集文件中创建的数据。
class PHPUnit\DbUnit\DataSet\DefaultTable#30 (3) {
protected $tableMetaData =>
class PHPUnit\DbUnit\DataSet\DefaultTableMetadata#34 (3) {
protected $columns =>
array(3) {
[0] =>
string(2) "id"
[1] =>
string(4) "name"
[2] =>
string(5) "email"
}
protected $primaryKeys =>
array(0) {
}
protected $tableName =>
string(5) "users"
}
protected $data =>
array(4) {
[0] =>
array(3) {
'id' =>
string(1) "1"
'name' =>
string(3) "test1"
'email' =>
string(9) "test1@me.nl"
}
[1] =>
array(3) {
'id' =>
string(1) "2"
'name' =>
string(3) "test2"
'email' =>
string(9) "test2@me.nl"
}
[2] =>
array(3) {
'id' =>
string(1) "3"
'name' =>
string(6) "test3"
'email' =>
string(12) "test3@me.nl"
}
[3] =>
array(3) {
'id' =>
string(1) "4"
'name' =>
string(4) "test4"
'email' =>
string(10) "test4@me.nl"
}
}
private $other =>
NULL
}
我还尝试在 getConnection()
方法内对 pdo 连接对象执行 2 个查询,以创建其中包含值的表:
protected function getConnection()
{
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (id PRIMARY KEY, name VARCHAR(50), email VARCHAR(50))");
$pdo->exec("INSERT INTO users (id, name, email) VALUES (20, 'Bas', 'aa@me')");
return $this->createDefaultDBConnection($pdo, ':memory:');
}
我的连接中没有任何可用数据是怎么来的,我怎样才能将数据集文件中的数据导入到这里以使测试通过?
此外,这样做是一种好的做法吗?
最佳答案
据我所见,setUp()
在你的 TestCase 中覆盖了 \PHPUnit\DbUnit\TestCaseTrait
的 setUp()
包含逻辑负责setting up and tearing down .
如果您需要在 TestCases 中使用不同的 setUp
,那么将您自己的基类设为 described 可能是更好的主意在文档中并从中扩展您的 TestCase,然后从子 TestCase 调用 parent::setUp()
更新:
The Database, tables, sequences, triggers and views have to be created before you run the test suite.
取自here ,那里还有更多有用的提示。
基本上,这意味着在运行测试之前,所有表、列、索引、约束和其他与数据库相关的代码在测试中处理和依赖的东西都应该就位。运行前该数据库中的任何内容都无关紧要,TestCase 数据集中的所有表都将被截断并填充来自该数据集的数据。
更新 2:(免责声明:以下是我的个人偏好)
通常对数据库的访问是通过某种网关实现的。它们是我最后实现的(至少在包范围内)。当我开始做这些网关时,这让我有机会真正了解需要存储在数据库中的数据。因此,当开始在某个网关上编写一些 TestCase 时,我只是使用管理工具(通常是一些 GUI,如 phpMyAdmin)并创建表(表)以及我认为需要存在的列来存储网关处理的数据。然后编写测试并运行它们,可能会在更改表结构之间更好地适应。
通过这种方法,db 的所有结构都是手动创建的(而不是通过测试代码),并且它与在 db 上运行的代码一起增长。我发现它很方便有几个原因。
首先,这更简单,因为我不必在每个 TestCase 之前管理创建(或重新创建)结构。尤其是当我有几个使用相同表的测试用例时。
第二,我总是有适合测试通过的数据库结构。结构中任何不合适的变化都会被捕获。此外,我始终可以为这个适当的结构生成一个 sql 导出指令,以启动具有所有需要的表、列、索引、键等的现实生活数据库。
第三,有时需要(甚至必须)查看数据库,只考虑特定于数据库的问题。所以我总是可以打开当前的测试数据库并清楚地看到它是由什么组成的。
关于内存数据库的注释。在这种情况下,需要在代码中创建结构。这里可能有两个明显的选择——为整个测试套件或特定测试用例(或其中的一组)设置具有特定结构的数据库。至于我,基于上述原因,我会做第一个。
最简单的实现方式是在 bootrstrap 文件中建立连接和创建结构。但我会投入一些时间并像这样添加一些动态:
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit>
<php>
<var name="MAKE_IN_MEMORY_DB" value="yes" />
</php>
</phpunit>
和:
abstract class AbstractDbTestCase extends \PHPUnit\DbUnit\TestCase
{
private $connection;
protected function getConnection() {
if($this->connection === NULL){
$this->connection = $this->createConnection();
}
return $this->connection;
}
private function createConnection(){
if($GLOBALS['MAKE_IN_MEMORY_DB'] == 'yes'){
return $this->createInMemory();
}
else {
return $this->createRealDbConnection();
}
}
private function createInMemory(){
// create connection and set up db;
}
private function createRealDbConnection(){
// create connection using some data from phpunit.xml
}
}
这将使测试与环境约束更加分离——运行测试只需要配置调整。实际上我会做更多,并使用带有 sql 语句的文件将它们加载到 createInMemory()
中(需要一些额外的工作,但我认为这是值得的)。
关于php - 在 PHPUnit 中的内存 SQLITE 数据库中模拟数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44647283/
我在 JavaScript 文件中运行 PHP,例如...... var = '';). 我需要使用 JavaScript 来扫描字符串中的 PHP 定界符(打开和关闭 PHP 的 )。 我已经知道使
我希望能够做这样的事情: php --determine-oldest-supported-php-version test.php 并得到这个输出: 7.2 也就是说,php 二进制检查 test.
我正在开发一个目前不使用任何框架的大型 php 站点。我的大问题是,随着时间的推移慢慢尝试将框架融入应用程序是否可取,例如在创建的新部件和更新的旧部件中? 比如所有的页面都是直接通过url服务的,有几
下面是我的源代码,我想在同一页面顶部的另一个 php 脚本中使用位于底部 php 脚本的变量 $r1。我需要一个简单的解决方案来解决这个问题。我想在代码中存在的更新查询中使用该变量。 $name)
我正在制作一个网站,根据不同的情况进行大量 PHP 重定向。就像这样...... header("Location: somesite.com/redirectedpage.php"); 为了安全起见
我有一个旧网站,我的 php 标签从 因为短标签已经显示出安全问题,并且在未来的版本中将不被支持。 关于php - 如何避免在 php 文件中写入
我有一个用 PHP 编写的配置文件,如下所示, 所以我想用PHP开发一个接口(interface),它可以编辑文件值,如$WEBPATH , $ACCOUNTPATH和 const值(value)观
我试图制作一个登录页面来学习基本的PHP,首先我希望我的独立PHP文件存储HTML文件的输入(带有表单),但是当我按下按钮时(触发POST到PHP脚本) )我一直收到令人不愉快的错误。 我已经搜索了S
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: What is the max key size for an array in PHP? 正如标题所说,我想知道
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我在 MySQL 数据库中有一个表,其中存储餐厅在每个工作日和时段提供的菜单。 表结构如下: i_type i_name i_cost i_day i_start i_
我有两页。 test1.php 和 test2.php。 我想做的就是在 test1.php 上点击提交,并将 test2.php 显示在 div 中。这实际上工作正常,但我需要向 test2.php
我得到了这个代码。我想通过textarea更新mysql。我在textarea中回显我的MySQL,但我不知道如何更新它,我应该把所有东西都放进去吗,因为_GET模式没有给我任何东西,我也尝试_GET
首先,我是 php 的新手,所以我仍在努力学习。我在 Wordpress 上创建了一个表单,我想将值插入一个表(data_test 表,我已经管理了),然后从 data_test 表中获取所有列(id
我有以下函数可以清理用户或网址的输入: function SanitizeString($var) { $var=stripslashes($var); $va
我有一个 html 页面,它使用 php 文件查询数据库,然后让用户登录,否则拒绝访问。我遇到的问题是它只是重定向到 php 文件的 url,并且从不对发生的事情提供反馈。这是我第一次使用 html、
我有一个页面充满了指向 pdf 的链接,我想跟踪哪些链接被单击。我以为我可以做如下的事情,但遇到了问题: query($sql); if($result){
我正在使用 从外部文本文件加载 HTML/PHP 代码 $f = fopen($filename, "r"); while ($line = fgets($f, 4096)) { print $l
我是一名优秀的程序员,十分优秀!