Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So far I've understood that: <strong>Should I create a testdb database or is that all emulated/virtual ?</strong></p> <p>It create a real database, and using the getSetUpOperation method, it's really slow as the tables are truncated and re-imported for each test, and it's demanding alot on the harddrive even for a small amount of data. ( ~ 1 sec/test )</p> <p><strong>I've seen many examples using sqlite::memory: , is it a good idea to test MySQL based query with sqlite ? Can I use mysql::memory: instead ?</strong></p> <p>I still don't know. I think it's now really possible with MySQL.</p> <p><strong>If it's a real DB, how do I restore all the data from dbData.xml in the DB before each test run ?</strong></p> <p>There are getSetUpOperation and getTearDownOperation that act like setup and tearDown method. Adding this will truncate the table mentionned in the dataSet, and re-insert all the data of that xml file:</p> <pre><code>/** * Executed before each * * @return PHPUnit_Extensions_Database_Operation_DatabaseOperation */ protected function getSetUpOperation() { return PHPUnit_Extensions_Database_Operation_Factory::CLEAN_INSERT(); } </code></pre> <p><strong>Where am I supposed to call getConnection() and getDataSet() ?</strong></p> <p>Nowhere. Theses are magic method that are called automatically. getConnection is called before the tests ( a bit like __construct would be, but I'm not sure about the order ) and getDataSet will be called when a dataSet is needed. I <em>think</em> that in my case, only getSetUpOperation have a dependency for a dataSet... so in the background it calls the getDataSet method before each tests to make the CLEAN_INSERT operation.</p> <p>Also, I discovered that we need to create the table structure ( the dataset doesn't handle that ), so my full --slow-- working code is:</p> <pre><code>&lt;?php require_once 'PHPUnit/Extensions/Database/TestCase.php'; class NewsFactoryTest extends PHPUnit_Extensions_Database_TestCase { /** * Custom PDO instance required by the SUT. * * @var Core_Db_Driver_iConnector */ protected $db; /** * Create a connexion. * Note: les constantes de connexion sont définit dans bootstrap.php. * * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection */ protected function getConnection() { //Instanciate the connexion required by the system under test. $this-&gt;db = new Core_Db_Driver_PDO('mysql:host=' . TEST_DB_HOST . ';dbname=' . TEST_DB_BASE, TEST_DB_USER, TEST_DB_PASS, array()); //Create a genuine PDO connexion, required for PHPUnit_Extensions_Database_TestCase. $db = new PDO('mysql:host=' . TEST_DB_HOST . ';dbname=' . TEST_DB_BASE, TEST_DB_USER, TEST_DB_PASS); $this-&gt;createTableSchema($db); return $this-&gt;createDefaultDBConnection($db, TEST_DB_BASE); } /** * Load the required table schemes. * * @param PDO $db * @return void */ protected function createTableSchema(PDO $db) { $schemaPath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sql_schema' . DIRECTORY_SEPARATOR; $query = file_get_contents($schemaPath . 'news.sql'); $db-&gt;exec($query); $query = file_get_contents($schemaPath . 'news_locale.sql'); $db-&gt;exec($query); } /** * Load the dataSet in memory. * * @return PHPUnit_Extensions_Database_DataSet_IDataSet */ protected function getDataSet() { return $this-&gt;createXMLDataSet(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'newsFactory_dataSet.xml'); } /** * Method executed before each test * * @return PHPUnit_Extensions_Database_Operation_DatabaseOperation */ protected function getSetUpOperation() { //TRUNCATE the table mentionned in the dataSet, then re-insert the content of the dataset. return PHPUnit_Extensions_Database_Operation_Factory::CLEAN_INSERT(); } /** * Method executed after each test * * @return PHPUnit_Extensions_Database_Operation_DatabaseOperation */ protected function getTearDownOperation() { //Do nothing ( yup, their's a code for that ) return PHPUnit_Extensions_Database_Operation_Factory::NONE(); } /** * @covers NewsFactory::getNewsById */ public function testGetNewsById() { $newsFactory = new NewsFactory($this-&gt;db); $news = $newsFactory-&gt;getNewsById(999); $this-&gt;assertFalse($news); } } </code></pre> <p>Hope that will help other people that needed some extra explanations. If you have any comment, suggestion or idea, your input is welcome, as I don't consider this solution as a fully efficient one. ( Slow and long to setup, and needs a double connection. )</p>
 

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