I'm trying to add a unit testing framework to an already existing Laravel project. I've read the PHPUnit manual and I'm trying to set up a sqlite environment using .xml datasets to populate the test data and a main migration to create the schema but the actual test data does not seem to be populating into the tables.
I started out adding phpunit and dbunit to the composer.json file and updating them. I added vendor/bin to $PATH so the VM will know where to find it.
I then expanding the existing TestCase class with getConnection(), getDataSet() and setUp() which contains
class TestCase extends Illuminate\Foundation\Testing\TestCase {
static private $pdo = null;
private $conn = null;
public function setUp() {
parent::setUp();
Artisan::call('migrate');
$this->seed();
}
public function createApplication() {
$unitTesting = true;
$testEnvironment = 'testing';
return require __DIR__ . '/../../bootstrap/start.php';
}
public function getDataSet() {
parent::getDataSet();
}
public function getConnection() {
if ($this->conn === null) {
if (self::$pdo == null) {
self::$pdo = new PDO('sqlite::memory:');
}
$this->conn = $this->createDefaultDBConnection(self::$pdo, ':memory:');
}
return $this->conn;
}
}
The Illuminate\Foundation\Testing\Testcase I changed to :
abstract class TestCase extends \PHPUnit_Extensions_Database_TestCase {
....
}
This done, I created an XML dataset
<?xml version="1.0" ?>
<dataset>
<table name="a_table">
<column>id</column>
<column>data1</column>
<column>data2</column>
<row>
<value>1</value>
.....
.....
</row>
<row>
.....
</row>
</table>
</dataset>
I used the artisan convert migrations command to create the schema.
Then I wrote my first test.
class ImportantClassTest extends TestCase {
/**
* @author Me
*
*
* Tests the functionality of some function
* @dataProvider ImportantFunctionData();
*/
public function testthisImportantFunction($an_id,$an_amount,$another_amount,$starting_rows,$expected_rows)
{
$this->assertEquals($this->getConnection()->getRowCount('a_table'),$starting_rows);
$result = ImportantClass::ImportantFunction($an_id, $an_amount);
$this->assertEquals($another_amount,$result);
$this->assertEquals($this->getConnection()->getRowCount('a_table'),$expected_rows);
}
public function ImportantFunctionData()
{
return array(
array(1337,1000,180,10,10),
array(1338,3900,900,10,11)
);
}
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/_files/ImportantFunctionData.xml');
} }
I added config/testing/database.php :
return array(
'default' => 'sqlite',
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
)
),
);
When I run the test, the migration occurs to create the db in sqlite memory, but the data does not appear to populate. With the row checking I get this error :
1) ImportantClass::testthisImportantFunction with data set #0 (1337, 1000, 180, 10, 10) PDOException: SQLSTATE[HY000]: General error: 1 no such table: a_table
and just the result assertion on it's own
1) ImportantClass::testthisImportantFunction with data set #0 (1337, 1000, 180, 10, 10) Failed asserting that 1000 matches expected 180.
So in neither case does it appear that phpunit is populating the fixtures correctly.
Can anyone see where I'm going wrong?
Aucun commentaire:
Enregistrer un commentaire