My website will have an advanced search. Pleople can go there and search about an entitiy (cars, for example). I've created some tests that check the number of results based on the search parameters. I think about what tests should I write, then i write it, then I add data to the test database. But here comes the issue. When I insert new values to the database my old tests break. That's because I'm checking the number of records...
<?php defined('SYSPATH') or die('No direct access allowed!');
class Search_Test extends PHPUnit_Extensions_Database_TestCase
{
/**
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
public function getConnection()
{
$pdo = new PDO('mysql:dbname=db_test;host=127.0.0.1', 'root', null);
return $this->createDefaultDBConnection($pdo, 'db_test');
}
/**
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
public function getDataSet()
{
$fixture = realpath(dirname(__FILE__).'/../data/fixture.xml');
return $this->createXMLDataSet($fixture);
}
public function numberOfResultsDataProvider()
{
return array(
array(1, null, null, 1),
array(2, null, null, 3),
array(3, null, null, 0),
array('abc', null, null, 5),
array(null, 1996, 2003, 3),
array(null, 1996, 1999, 2),
array(null, 2002, 2003, 1),
array(null, 1500, 1800, 0),
array(null, 2003, 2003, 1),
array(null, null, 2005, 4),
array(null, 1996, null, 4),
array(null, null, null, 4),
array(null, 2003, 1996, 0),
array(null, 'abc', 2003, 4),
array(null, '1996', '1999', 2),
array(2, 2003, 2005, 2),
array(null, null, null, 4),
);
}
/**
* @dataProvider numberOfResultsDataProvider
*/
public function testNumberOfResults($brandId, $startYear,
$endYear, $numberOfResults
) {
$search = ORM::factory('search');
$search->setBrand($brandId)
->setYearRange($startYear, $endYear);
$results = $search->results();
$this->assertEquals($results->count(), $numberOfResults);
}
}
?>
Is it normal? Should my old tests break when I create new tests?
Should my tests be bounded to data?
My search has too many parameters and they will be used in the same form (view). Should I create tests searching for each parameter or should I test they together? Should I split it up in more test classes?
Thanks.