3

我尝试测试一种进行 SELECT 查询并返回找到的行的方法。在给定数据集的情况下,我想检查此方法是否不会返回其他内容。我发现了很多关于创建数据集的文档,但没有关于在我的案例中使用它...感谢您的帮助。

测试的方法是:

class A
{
  public static function myMethod()
  {
    $result = mysql_query("SELECT * FROM user");
    [...]
    return $rows;
  }
}

测试类是:

class ATest extends PHPUnit_Extensions_Database_TestCase
{
  protected $pdo;

  public function __construct()
  {
    $this->pdo = new PDO('mysql:host=localhost;dbname=db_name',
                         'login', 'password');
  }

  public function getConnection()
  {
    return $this->createDefaultDBConnection($this->pdo, 'db_name');
  }

  public function getDataSet()
  {
    return $this->createFlatXMLDataSet('mydataset.xml');
  }

  public function testMyMethod()
  {
    $actual = A::myMethod();
    $this->assertEquals(array([...]), $actual);
    // For this test, I get a mySQL error "No database selected"
    // in A::myMethod()!
  }
}

这是 mydataset.xml 的内容:

<?xml version="1.0" ?>
<dataset>
  <user iduser="1" name="John" />
  <user iduser="2" name="James" />
</dataset>
4

2 回答 2

2

在我的情况下,我所做的是使用 pdo 连接到数据库,如下所示:

static private $pdo=null;
private $conn=null;
    public function getConnection()
        {
            if($this->conn===null)
            {
                if(self::$pdo===null)
                {
                    self::$pdo = new PDO("mysql:host=yourhost;dbname=yourdbname_",
                "yourusername", "yourpassword");
                }
                $this->conn= $this->createDefaultDBConnection(self::$pdo, 'yourdbname_');
                return $this->conn;
            }
        }, 

然后对于测试,下面的这个块获取 testdb 中的所有值并将其与 xml 文件进行比较。

 public function testGetAll()
            {
                $resultingTable = $this->db
                ->createQueryTable("yourtable",
                "SELECT * FROM yourtable");
                 $expectedTable = $this->getDataSet()
                ->getTable("yourtable");
            $this->assertTablesEqual($expectedTable,
                $resultingTable);   
            }

这对我来说很好,应该允许您从 testdb 中获取所有值并断言它们与 xml 值相同。

于 2013-03-14T03:27:32.937 回答
1

Just like in your real application, you need to connect to the database before you can call it. The mysql_query() function relies on a previous call having been made to mysql_connect(). If you're only calling the one class method in isolation, then you won't have called the connect function, so you won't have a connection, and so your query command won't know what database it needs to query.

The PDO connection being used in the test class is explicitly separate from and not used by the methods being tested (this applies even if those methods also use PDO); you need to create a connection for the query.

The solution here is to include a call to your DB connection method at the start of the test. This could be in the unit test method, or in the setUp() method of the test class, or in a bootstrap file that PHPUnit calls before running any of the tests. Which of these you use will depend on how many DB tests you need to run and across how many test classes.

Straying slightly off topic, but still relevant: I note that your code is using the old mysql_xxx() functions (ie mysql_query()). It would be advisable to avoid using these functions, as they are obsolete and insecure, and in the process of being deprecated by the PHP dev team. You will therefore find yourself unable to upgrade your PHP version in the future if you keep using them.

Use mysqli_xxx() instead (or PDO, as per your test class).

Back on topic, and in fact doing this will also lead you toward fixing the problem you're having anyway, since both mysqli and PDO require the connection object to be available when calling their functions/methods, so you'll have to have a valid connection object in order to even have valid syntax to call the query. Switching to mysqli or PDO will therefore force you to write your code in a way that solves the problem in the question.

于 2012-12-10T13:01:12.977 回答