[Solar-svn] Revision 2700
pmjones at solarphp.com
pmjones at solarphp.com
Wed Aug 15 10:31:00 CDT 2007
Branch: continued work on Solar_Sql_Model testing
Modified: branches/orm/tests/Test/Solar/Sql/Model.php
===================================================================
--- branches/orm/tests/Test/Solar/Sql/Model.php 2007-08-14 13:08:23 UTC (rev 2699)
+++ branches/orm/tests/Test/Solar/Sql/Model.php 2007-08-15 15:31:00 UTC (rev 2700)
@@ -36,26 +36,8 @@
*
* - delete a record
*
- * - special-column behaviors (test_solar_special_cols)
- *
- * - created
- *
- * - updated
- *
- * - sequence
- *
- * - autoinc
- *
- * - inherit
- *
- * - serialize/unserialize
- *
- * - primary
- *
* - fetching
*
- * - fetchAll, One, Pairs, etc
- *
* - magic fetchAllBy, fetchOneBy
*
* - lazy-fetch a has_one related
@@ -88,6 +70,8 @@
*
* - single table inheritance
*
+ * - record and collection class hierarchy traversal
+ *
*/
class Test_Solar_Sql_Model extends Solar_Test {
@@ -195,6 +179,18 @@
$this->_catalog->reset();
}
+ protected function _populateSpecialColsTable()
+ {
+ $model = $this->_getModel('TestSolarSpecialCols');
+ for ($i = 1; $i <= 10; $i++) {
+ $record = $model->fetchNew();
+ $record->name = chr($i+96); //ascii 'a', 'b', etc
+ $record->save();
+ }
+
+ }
+
+
// -----------------------------------------------------------------
//
// Test methods.
@@ -219,110 +215,151 @@
/**
*
- * Test -- special column behaviors.
+ * Test -- Magic call implements "fetchOneBy...()" and "fetchAllBy...()" for columns listed in the method name.
*
*/
- public function testSpecialColumns()
+ public function test__call()
{
+ /**
+ * populate the table, along with some extra records so we have more
+ * to work with
+ */
+ $this->_populateSpecialColsTable();
$model = $this->_getModel('TestSolarSpecialCols');
+ for ($i = 0; $i < 5; $i++) {
+ // id's are 11-15, all named 'z' with seq_foo of 88
+ $record = $model->fetchNew();
+ $record->name = 'z';
+ $record->seq_foo = 88;
+ $record->save();
+ }
+ // add some extras so we can see if fetchAll() grabs more than it should
+ for ($i = 0; $i < 5; $i++) {
+ // id's are 16-20, all named 'z' with seq_foo of 88
+ $record = $model->fetchNew();
+ $record->name = 'z';
+ $record->seq_foo = 99;
+ $record->save();
+ }
+
/**
- * Correct population of new columns
+ * fetchOneBy*() single-col
*/
- $record = $model->fetchNew();
- $record->save();
- $now = date('Y-m-d H:i:s');
+ // make sure the method "fetchOneByName" does not actually exist
+ $exists = method_exists($model, 'fetchOneByName');
+ $this->assertFalse($exists);
- // autoincremented id
- $this->assertEquals($record->id, 1);
+ // call the magic method
+ $record = $model->fetchOneByName('z');
+ $this->assertInstance($record, 'Solar_Sql_Model_Record');
+ $this->assertInstance($record, 'Solar_Example_Model_TestSolarSpecialCols_Record');
+ $this->assertEquals($record->id, 11);
+ $this->assertEquals($record->name, 'z');
- // created & updated
- $created = $record->created;
- $this->assertEquals($record->created, $now);
- $this->assertEquals($record->updated, $now);
-
- // auto-sequence foo & bar
- $this->assertEquals($record->seq_foo, 1);
- $this->assertEquals($record->seq_bar, 1);
-
/**
- * Correct "updated" and sequence numbering
+ * fetchOneBy*() multi-col
*/
- $record = $model->fetch(1);
- $record->seq_bar = null;
- $record->save();
- $now = date('Y-m-d H:i:s');
- // created should be as original
- $this->assertEquals($record->created, $created);
+ // make sure the method "fetchOneByNameAndSeqFoo" does not actually exist
+ $exists = method_exists($model, 'fetchOneByNameAndSeqFoo');
+ $this->assertFalse($exists);
- // updated should have changed
- $this->assertEquals($record->updated, $now);
+ // call the magic method
+ $record = $model->fetchOneByNameAndSeqFoo('z', 88);
+ $this->assertInstance($record, 'Solar_Sql_Model_Record');
+ $this->assertInstance($record, 'Solar_Example_Model_TestSolarSpecialCols_Record');
+ $this->assertEquals($record->id, 11);
+ $this->assertEquals($record->name, 'z');
+ $this->assertEquals($record->seq_foo, '88');
- // seq_foo should still be 1, but seq_bar should have been increased
- $this->assertEquals($record->seq_foo, 1);
- $this->assertEquals($record->seq_bar, 2);
-
/**
- * Serializing
+ * fetchAllBy*() single-col
*/
- // first, save something to be serialized
- $expect = array('foo', 'bar', 'baz');
- $record->serialize = $expect;
- $record->save();
- // should have been unserialized after saving
- $this->assertSame($record->serialize, $expect);
+ // make sure the method "fetchAllByName" does not actually exist
+ $exists = method_exists($model, 'fetchAllByName');
+ $this->assertFalse($exists);
- // now retrieve from the database and see if it unserialized
- $record = $model->fetch(1);
- $this->assertSame($record->serialize, $expect);
+ // call the magic method
+ $collection = $model->fetchAllByName('z');
+ $this->assertInstance($collection, 'Solar_Sql_Model_Collection');
+ $this->assertInstance($collection, 'Solar_Example_Model_TestSolarSpecialCols_Collection');
+ $this->assertEquals(count($collection), 10);
+ foreach ($collection as $key => $record) {
+ $this->assertEquals($record->id, $key + 11);
+ $this->assertEquals($record->name, 'z');
+ }
/**
- *
- * Autoinc and sequences on a second record
- *
+ * fetchAllBy*() multi-col
*/
- $record = $model->fetchNew();
- $record->save();
- $this->assertEquals($record->id, 2);
- $this->assertEquals($record->seq_foo, 2);
- $this->assertEquals($record->seq_bar, 3);
+
+ // make sure the method "fetchAllByNameAndSeqFoo" does not actually exist
+ $exists = method_exists($model, 'fetchAllByNameAndSeqFoo');
+ $this->assertFalse($exists);
+
+ // call the magic method
+ $collection = $model->fetchAllByNameAndSeqFoo('z', 88);
+ $this->assertInstance($collection, 'Solar_Sql_Model_Collection');
+ $this->assertInstance($collection, 'Solar_Example_Model_TestSolarSpecialCols_Collection');
+ $this->assertEquals(count($collection), 5);
+ foreach ($collection as $key => $record) {
+ $this->assertEquals($record->id, $key + 11);
+ $this->assertEquals($record->name, 'z');
+ $this->assertEquals($record->seq_foo, 88);
+ }
}
+ /**
+ *
+ * Test -- Read-only access to protected model properties.
+ *
+ */
+ public function test__get()
+ {
+ $model = $this->_getModel('TestSolarSpecialCols');
+
+ // reads from protected $_primary_col; should be no exception
+ $actual = $model->primary_col;
+ $expect = 'id';
+ $this->assertSame($actual, $expect);
+
+ // try for a property that doesn't exist
+ try {
+ $actual = $model->no_such_property;
+ $this->fail('should have thrown an exception here');
+ } catch (Solar_Exception $e) {
+ // do nothing, this is the expected case :-)
+ }
+ }
+
+ /**
+ *
+ * Test -- Fetches count and pages of available records.
+ *
+ */
+ public function testCountPages()
+ {
+ $this->_populateSpecialColsTable();
+ $model = $this->_getModel('TestSolarSpecialCols');
+ $model->setPaging(3);
+ $actual = $model->countPages();
+ $expect = array('count' => 10, 'pages' => 4);
+ $this->assertEquals($actual, $expect);
+
+ // now count on a WHERE clause
+ $where = array(
+ 'id > 5'
+ );
+ $actual = $model->countPages(array('where' => $where));
+ $expect = array('count' => 5, 'pages' => 2);
+ $this->assertEquals($actual, $expect);
+ }
+
// /**
// *
- // * Test -- Magic call implements "fetchOneBy...()" and "fetchAllBy...()" for columns listed in the method name.
- // *
- // */
- // public function test__call()
- // {
- // $this->todo('stub');
- // }
- //
- // /**
- // *
- // * Test -- Read-only access to protected model properties.
- // *
- // */
- // public function test__get()
- // {
- // $this->todo('stub');
- // }
- //
- // /**
- // *
- // * Test -- Fetches count and pages of available records.
- // *
- // */
- // public function testCountPages()
- // {
- // $this->todo('stub');
- // }
- //
- // /**
- // *
// * Test -- Deletes a record from the database.
// *
// */
@@ -339,14 +376,10 @@
public function testFetch()
{
// insert a set of records
- $model = $this->_getModel('TestSolarSpecialCols');
- for ($i = 1; $i <= 10; $i++) {
- $record = $model->fetchNew();
- $record->name = chr($i+96); //ascii 'a', 'b', etc
- $record->save();
- }
+ $this->_populateSpecialColsTable();
// fetch by number to get a Record
+ $model = $this->_getModel('TestSolarSpecialCols');
$record = $model->fetch(3);
$this->assertInstance($record, 'Solar_Sql_Model_Record');
$this->assertInstance($record, 'Solar_Example_Model_TestSolarSpecialCols_Record');
@@ -372,14 +405,10 @@
public function testFetchAll()
{
// insert a set of records
+ $this->_populateSpecialColsTable();
+
+ // fetch by some WHERE clause
$model = $this->_getModel('TestSolarSpecialCols');
- for ($i = 1; $i <= 10; $i++) {
- $record = $model->fetchNew();
- $record->name = chr($i+96); //ascii 'a', 'b', etc
- $record->save();
- }
-
- // fetch by some WHERE clausw
$collection = $model->fetchAll(array('where' => 'id > 5'));
// tests
@@ -400,14 +429,10 @@
public function testFetchAssoc()
{
// insert a set of records
- $model = $this->_getModel('TestSolarSpecialCols');
- for ($i = 1; $i <= 10; $i++) {
- $record = $model->fetchNew();
- $record->name = chr($i+96); //ascii 'a', 'b', etc
- $record->save();
- }
+ $this->_populateSpecialColsTable();
// fetch by some WHERE clause
+ $model = $this->_getModel('TestSolarSpecialCols');
$collection = $model->fetchAssoc(array(
'where' => 'id > 5',
'order' => 'name',
@@ -436,14 +461,10 @@
public function testFetchCol()
{
// insert a set of records
- $model = $this->_getModel('TestSolarSpecialCols');
- for ($i = 1; $i <= 10; $i++) {
- $record = $model->fetchNew();
- $record->name = chr($i+96); //ascii 'a', 'b', etc
- $record->save();
- }
+ $this->_populateSpecialColsTable();
// fetch by some WHERE clause
+ $model = $this->_getModel('TestSolarSpecialCols');
$actual = $model->fetchCol(array(
'where' => 'id > 5',
'order' => 'name',
@@ -482,14 +503,10 @@
public function testFetchOne()
{
// insert a set of records
- $model = $this->_getModel('TestSolarSpecialCols');
- for ($i = 1; $i <= 10; $i++) {
- $record = $model->fetchNew();
- $record->name = chr($i+96); //ascii 'a', 'b', etc
- $record->save();
- }
+ $this->_populateSpecialColsTable();
// fetch by number to get a Record
+ $model = $this->_getModel('TestSolarSpecialCols');
$record = $model->fetchOne(array(
'where' => array('name = ?' => 'c'),
));
@@ -507,14 +524,10 @@
public function testFetchPairs()
{
// insert a set of records
- $model = $this->_getModel('TestSolarSpecialCols');
- for ($i = 1; $i <= 10; $i++) {
- $record = $model->fetchNew();
- $record->name = chr($i+96); //ascii 'a', 'b', etc
- $record->save();
- }
+ $this->_populateSpecialColsTable();
// fetch by some WHERE clause
+ $model = $this->_getModel('TestSolarSpecialCols');
$actual = $model->fetchPairs(array(
'where' => 'id > 5',
'order' => 'name',
@@ -597,13 +610,40 @@
*/
public function testSetPaging()
{
+ $this->_populateSpecialColsTable();
+
+ // set it
$model = $this->_getModel('TestSolarSpecialCols');
-
- $expect = 50;
+ $expect = 3;
$model->setPaging($expect);
+ // make sure it's recognized
$actual = $model->getPaging();
$this->assertEquals($actual, $expect);
+
+ // make sure it's honored
+ // get the first page of 3 records
+ $collection = $model->fetchAll(array('order' => 'id', 'page' => 1));
+ $this->assertEquals(count($collection), 3);
+
+ // make sure they're the right ones: 1, 2, 3
+ foreach ($collection as $key => $record) {
+ $this->assertEquals($record->id, $key + 1);
+ }
+
+ // get the third page of 3 records; this should also be 3 records
+ $collection = $model->fetchAll(array('order' => 'id', 'page' => 3));
+ $this->assertEquals(count($collection), 3);
+
+ // make sure they're the right ones: 7, 8, 9
+ foreach ($collection as $key => $record) {
+ $this->assertEquals($record->id, $key + 7);
+ }
+
+ // get the 4th page of 3 records: this should be 1 record, #10
+ $collection = $model->fetchAll(array('order' => 'id', 'page' => 4));
+ $this->assertEquals(count($collection), 1);
+ $this->assertEquals($collection[0]->id, 10);
}
// /**
@@ -615,4 +655,78 @@
// {
// $this->todo('stub');
// }
+
+ /**
+ *
+ * Test -- special column behaviors.
+ *
+ */
+ public function testSpecialColumns()
+ {
+ $model = $this->_getModel('TestSolarSpecialCols');
+
+ /**
+ * Correct population of new columns
+ */
+
+ $record = $model->fetchNew();
+ $record->save();
+ $now = date('Y-m-d H:i:s');
+
+ // autoincremented id
+ $this->assertEquals($record->id, 1);
+
+ // created & updated
+ $created = $record->created;
+ $this->assertEquals($record->created, $now);
+ $this->assertEquals($record->updated, $now);
+
+ // auto-sequence foo & bar
+ $this->assertEquals($record->seq_foo, 1);
+ $this->assertEquals($record->seq_bar, 1);
+
+ /**
+ * Correct "updated" and sequence numbering
+ */
+ $record = $model->fetch(1);
+ $record->seq_bar = null;
+ $record->save();
+ $now = date('Y-m-d H:i:s');
+
+ // created should be as original
+ $this->assertEquals($record->created, $created);
+
+ // updated should have changed
+ $this->assertEquals($record->updated, $now);
+
+ // seq_foo should still be 1, but seq_bar should have been increased
+ $this->assertEquals($record->seq_foo, 1);
+ $this->assertEquals($record->seq_bar, 2);
+
+ /**
+ * Serializing
+ */
+ // first, save something to be serialized
+ $expect = array('foo', 'bar', 'baz');
+ $record->serialize = $expect;
+ $record->save();
+
+ // should have been unserialized after saving
+ $this->assertSame($record->serialize, $expect);
+
+ // now retrieve from the database and see if it unserialized
+ $record = $model->fetch(1);
+ $this->assertSame($record->serialize, $expect);
+
+ /**
+ *
+ * Autoinc and sequences on a second record
+ *
+ */
+ $record = $model->fetchNew();
+ $record->save();
+ $this->assertEquals($record->id, 2);
+ $this->assertEquals($record->seq_foo, 2);
+ $this->assertEquals($record->seq_bar, 3);
+ }
}
More information about the Solar-svn
mailing list