Skip to content

Instantly share code, notes, and snippets.

@chartjes
Created December 22, 2011 15:15
Show Gist options
  • Save chartjes/1510647 to your computer and use it in GitHub Desktop.
Save chartjes/1510647 to your computer and use it in GitHub Desktop.
Here's my test:
<?php
include 'test_bootstrap.php';
class FranchiseModelTest extends \PHPUnit_Extensions_Database_TestCase
{
protected $_conn;
/**
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
public function getConnection()
{
return $this->createDefaultDbConnection(
$this->_conn,
'ibl_stats'
);
}
/**
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
public function getDataSet()
{
return $this->createFlatXmlDataSet(
'./fixtures/franchise-dataset.xml'
);
}
public function setUp()
{
$this->_conn = new PDO(
'pgsql:host=localhost;dbname=ibl_stats',
'stats',
'st@ts=Fun'
);
}
public function tearDown()
{
unset($this->_conn);
}
public function testIdOnlySetOnce()
{
$franchise = new IBL\Franchise();
$id = 25;
$franchise->setId($id);
$this->assertEquals($id, $franchise->getId());
$anotherId = 26;
$franchise->setId($anotherId);
$this->assertEquals($id, $franchise->getId());
}
public function testSaveUpdatesDatabase()
{
$rowCount = $this
->getConnection()
->getRowCount('franchises');
$this->assertEquals(
24,
$rowCount,
'Making sure we have 24 franchises'
);
$mapper = new IBL\FranchiseMapper($this->_conn);
$franchise = new IBL\Franchise();
$franchise->setId(25);
$franchise->setNickname('TST');
$franchise->setName('Test Team');
$franchise->setConference('Conference');
$franchise->setDivision('Division');
$franchise->setIp(0);
$mapper->save($franchise);
// Update existing model
$franchise->setIp(35);
$mapper->save($franchise);
// Reload Franchise record and compare them
$franchise2 = $mapper->findById($franchise->getId());
$this->assertEquals(35, $franchise2->getIp());
// Make sure we have all the expected data in the table
$queryTable = $this
->getConnection()
->createQueryTable('franchises', 'SELECT * FROM franchises');
$expectedTable = $this
->createFlatXmlDataSet('./fixtures/expected-franchises.xml')
->getTable('franchises');
$this->assertTablesEqual(
$expectedTable,
$queryTable
);
// Clean up the franchise
$mapper->delete($franchise);
}
}
expected-franchises.xml is the same as franchise-dataset.xml except I added a record that matches what the test would have added to the database
Error message:
-> % phpunit FranchiseModelTest.php
PHPUnit 3.6.4 by Sebastian Bergmann.
.E
Time: 0 seconds, Memory: 6.50Mb
There was 1 error:
1) FranchiseModelTest::testSaveUpdatesDatabase
Argument 1 passed to PHPUnit_Extensions_Database_TestCase::assertTablesEqual() must implement interface PHPUnit_Extensions_Database_DataSet_ITable, null given, called in /Users/chartjes/Sites/local.ibl/tests/FranchiseModelTest.php on line 93 and defined
/usr/local/Cellar/php/5.3.8/lib/php/PHPUnit/Extensions/Database/TestCase.php:230
/Users/chartjes/Sites/local.ibl/tests/FranchiseModelTest.php:93
FAILURES!
Tests: 2, Assertions: 4, Errors: 1.
@chartjes
Copy link
Author

I tried using the dirname(FILE) thing and no difference.

When the XML file through xmllint and it reported no errors and the format is correct

expected-franchises.xml https://gist.github.com/1510736
franchises-dataset.xml https://gist.github.com/1510738

Files were generated via a CLI script

findAll(); echo "Creating XML out of Franchise records...\n"; $xmlDoc = new DOMDocument(); $root = $xmlDoc->appendChild( $xmlDoc->createElement('dataset') ); foreach ($allFranchises as $franchise) { $franchiseRow = $root->appendChild( $xmlDoc->createElement('franchise') ); $franchiseRow->appendChild( $xmlDoc->createAttribute('id') )->appendChild( $xmlDoc->createTextNode($franchise->getId()) ); $franchiseRow->appendChild( $xmlDoc->createAttribute('nickname') )->appendChild( $xmlDoc->createTextNode($franchise->getNickname()) ); $franchiseRow->appendChild( $xmlDoc->createAttribute('name') )->appendChild( $xmlDoc->createTextNode($franchise->getName()) ); $franchiseRow->appendChild( $xmlDoc->createAttribute('conference') )->appendChild( $xmlDoc->createTextNode($franchise->getConference()) ); $franchiseRow->appendChild( $xmlDoc->createAttribute('division') )->appendChild( $xmlDoc->createTextNode($franchise->getDivision()) ); $franchiseRow->appendChild( $xmlDoc->createAttribute('ip') )->appendChild( $xmlDoc->createTextNode($franchise->getIp()) ); } $xmlDoc->formatOutput = true; file_put_contents('./fixtures/franchise-dataset.xml', $xmlDoc->saveXml()); echo "Done\n"; and then I manually added the extra row to get the expected data set

@elazar
Copy link

elazar commented Dec 22, 2011

Darn. Then yeah, best I can suggest is tracing through the code to figure out why it's returning null, because it shouldn't be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment