Reputation: 66
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>
Upvotes: 3
Views: 2248
Reputation: 350
what I did in my case was I made the connection to the database using pdo like so:
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;
}
},
then as for the test, this block below gets all the values in the testdb and compares it with the xml file.
public function testGetAll()
{
$resultingTable = $this->db
->createQueryTable("yourtable",
"SELECT * FROM yourtable");
$expectedTable = $this->getDataSet()
->getTable("yourtable");
$this->assertTablesEqual($expectedTable,
$resultingTable);
}
That worked fine for me and should allow you get all the values from the testdb and assert that they are the same with the xml values.
Upvotes: 2
Reputation: 14222
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.
Upvotes: 0