[英]'Create Table' sometimes fails in PHPUnit Database query
My issue is, that two identical queries for creating a table give different results. 我的问题是,用于创建表的两个相同的查询给出了不同的结果。 /classes/database.php /classes/database.php
<?php
/**
* Created by IntelliJ IDEA.
* User: Timon
* Date: 10/12/18
* Time: 3:10 PM
*/
namespace canvas\classes;
use \PDO;
class Database
{
private $connction;
public function __construct($configuration = [])
{
include 'values.php';
$this->connction = $pdo = new PDO('mysql:host='.$host.';dbname='.$db, $user, $passwd);
}
public function query($query)
{
return $this->connction->query($query);
}
}
$database = new Database();
Now the test unit: /Test/databaseTest.php 现在测试单元:/Test/databaseTest.php
<?php
namespace canvas\classes;
use PHPUnit\Framework\TestCase;
class DatabaseTest extends TestCase
{
/**
* @param string $query
*
* @dataProvider providerTestQuery
*/
public function testQuery($query){
$database= new Database();
var_dump($query);
$result=$database->query($query);
var_dump($result);
self::assertNotFalse($result);
}
public function providerTestQuery()
{
return array(
array('Create Database Test'),
array('Use Test'),
array("CREATE TABLE employees (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50)
)"),
);
}
public function testCreateTable(){
$database= new Database();
var_dump($database->query('Use Test'));
self::assertNotFalse($database->query("CREATE TABLE persons(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(70) NOT NULL UNIQUE
)"));
}
}
The test of testQuery with the last data set is failing, but testCreateTable is success. 使用最后一个数据集测试testQuery失败,但是testCreateTable成功。 If I write my queries by hand, it will fail, if I copy it from the internet, it may or may not be a success. 如果我手动编写查询,它将失败,如果我从互联网复制它,则可能成功也可能不会成功。 PS: I am currently using an idea from JetBrains. PS:我目前正在使用JetBrains的想法。 Might it be the formatting? 可能是格式吗? The queries are valid in my database SQL executer. 这些查询在我的数据库SQL执行程序中有效。
The first Create Table went into a different database and didn't get deleted. 第一个创建表进入了另一个数据库,并且没有被删除。 Therefore it failed, because the table already existed. 因此失败,因为该表已经存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.