Note that there are some explanatory texts on larger screens.

plurals
  1. POPHPUnit - Asserting that a query returns the good data from a database
    primarykey
    data
    text
    <p>I'm testing a Factory that simply retrieves all the "post" of a news system. I'll cut the example to something as simple as possible:</p> <pre><code>$newsFactory-&gt;getAllNews(); </code></pre> <p>The table looks like this:</p> <pre><code>+---------+---------------------+-------------+ | news_id | news_publishedDate | news_active | +---------+---------------------+-------------+ | 1 | 2010-03-22 13:20:22 | 1 | | 2 | 2010-03-23 13:20:22 | 1 | | 14 | 2010-03-23 13:20:22 | 0 | | 15 | 2010-03-23 13:20:22 | 1 | +---------+---------------------+-------------+ </code></pre> <p>I want to test that behaviour; for now, we'll focus only on the first one:</p> <ul> <li>Make sure the query will return only news_active=1</li> <li>Make sure the query will return the element ordered by news_publishedDate, from newest to older.</li> </ul> <p>So I've made an <code>dbData.xml</code> dataset of what I consider as good testing data:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;dataset&gt; &lt;table name="news"&gt; &lt;column&gt;news_id&lt;/column&gt; &lt;column&gt;news_publishedDate&lt;/column&gt; &lt;column&gt;news_active&lt;/column&gt; &lt;row&gt; &lt;value&gt;1&lt;/value&gt; &lt;value&gt;2010-03-20 08:55:05&lt;/value&gt; &lt;value&gt;1&lt;/value&gt; &lt;/row&gt; &lt;row&gt; &lt;value&gt;2&lt;/value&gt; &lt;value&gt;2010-03-20 08:55:05&lt;/value&gt; &lt;value&gt;0&lt;/value&gt; &lt;/row&gt; &lt;row&gt; &lt;value&gt;3&lt;/value&gt; &lt;value&gt;2011-03-20 08:55:05&lt;/value&gt; &lt;value&gt;1&lt;/value&gt; &lt;/row&gt; &lt;/table&gt; &lt;/dataset&gt; </code></pre> <p>Ok, so let's just check the first test (not returning the <code>news_id</code> #2 from the XML data set)</p> <p>I must extend the <code>PHPUnit_Extensions_Database_TestCase</code> class to make my NewsFactoryTest class:</p> <pre><code>&lt;?php require_once 'PHPUnit/Extensions/Database/TestCase.php'; class NewsFactoryTest extends PHPUnit_Extensions_Database_TestCase { protected $db; protected function getConnection() { $this-&gt;db = new PDO('mysql:host=localhost;dbname=testdb', 'root', ''); return $this-&gt;createDefaultDBConnection($this-&gt;db, 'testdb'); } protected function getDataSet() { return $this-&gt;createXMLDataSet(dir(__FILE__) . DIRECTORY_SEPARATOR . 'dbData.xml'); } public function testGetNewsById() { $newsFactory = new NewsFactory($this-&gt;db); $news = $newsFactory-&gt;getNewsById(); // ??? $this-&gt;assertEquals(2, count($news), "Should return only 2 results"); } } </code></pre> <p>My main question would be <em>how do I setup that test ?</em></p> <p>In details, I try to understand:</p> <ul> <li>Should I create a testdb database or is that all emulated/virtual ? <ul> <li>I've seen many examples using <code>sqlite::memory:</code>, is it a good idea to test MySQL based query with sqlite? Can I use <code>mysql::memory:</code> instead ?</li> <li>If it's a real DB, how do I restore all the data from <code>dbData.xml</code> in the DB before each test run ?</li> </ul></li> <li>Where am I supposed to call <code>getConnection()</code> and <code>getDataSet()</code>?</li> </ul> <p>Thanks for reading &amp; sharing your knowledge!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload