doumixiang2227 2012-12-10 12:41
浏览 53

PHPUnit使用数据集测试方法中的select

I try to test a method that makes a SELECT query and return the rows found. I would like to check that this method doesn't return something else, given a dataset. I found so many docs about creating the dataset but nothing about using it in my case... Thanks for your help.

The method to test is:

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

The test class is:

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()!
  }
}

Here is the content of mydataset.xml:

<?xml version="1.0" ?>
<dataset>
  <user iduser="1" name="John" />
  <user iduser="2" name="James" />
</dataset>
  • 写回答

2条回答 默认 最新

  • doulu5717 2012-12-10 13:01
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度