[Solar-svn] Revision 2740

pmjones at solarphp.com pmjones at solarphp.com
Fri Sep 7 08:53:03 CDT 2007


branch: updated Model tests


Modified: branches/orm/tests/Test/Solar/Sql/Model/Catalog.php
===================================================================
--- branches/orm/tests/Test/Solar/Sql/Model/Catalog.php	2007-09-07 13:52:21 UTC (rev 2739)
+++ branches/orm/tests/Test/Solar/Sql/Model/Catalog.php	2007-09-07 13:53:03 UTC (rev 2740)
@@ -125,4 +125,5 @@
     }
 
 
+
 }

Modified: branches/orm/tests/Test/Solar/Sql/Model/Collection.php
===================================================================
--- branches/orm/tests/Test/Solar/Sql/Model/Collection.php	2007-09-07 13:52:21 UTC (rev 2739)
+++ branches/orm/tests/Test/Solar/Sql/Model/Collection.php	2007-09-07 13:53:03 UTC (rev 2740)
@@ -395,4 +395,15 @@
 
 
 
+    
+    /**
+     * 
+     * Test -- Loads *related* data for the collection.
+     * 
+     */
+    public function testLoadRelated()
+    {
+        $this->todo('stub');
+    }
+
 }

Modified: branches/orm/tests/Test/Solar/Sql/Model/Filter/ValidateConfirm.php
===================================================================
--- branches/orm/tests/Test/Solar/Sql/Model/Filter/ValidateConfirm.php	2007-09-07 13:52:21 UTC (rev 2739)
+++ branches/orm/tests/Test/Solar/Sql/Model/Filter/ValidateConfirm.php	2007-09-07 13:53:03 UTC (rev 2740)
@@ -95,4 +95,5 @@
 
 
 
+
 }

Modified: branches/orm/tests/Test/Solar/Sql/Model/Filter/ValidateUnique.php
===================================================================
--- branches/orm/tests/Test/Solar/Sql/Model/Filter/ValidateUnique.php	2007-09-07 13:52:21 UTC (rev 2739)
+++ branches/orm/tests/Test/Solar/Sql/Model/Filter/ValidateUnique.php	2007-09-07 13:53:03 UTC (rev 2740)
@@ -95,4 +95,5 @@
 
 
 
+
 }

Modified: branches/orm/tests/Test/Solar/Sql/Model/Filter.php
===================================================================
--- branches/orm/tests/Test/Solar/Sql/Model/Filter.php	2007-09-07 13:52:21 UTC (rev 2739)
+++ branches/orm/tests/Test/Solar/Sql/Model/Filter.php	2007-09-07 13:53:03 UTC (rev 2740)
@@ -265,4 +265,5 @@
 
 
 
+
 }

Modified: branches/orm/tests/Test/Solar/Sql/Model/Record.php
===================================================================
--- branches/orm/tests/Test/Solar/Sql/Model/Record.php	2007-09-07 13:52:21 UTC (rev 2739)
+++ branches/orm/tests/Test/Solar/Sql/Model/Record.php	2007-09-07 13:53:03 UTC (rev 2740)
@@ -395,4 +395,5 @@
 
 
 
+
 }

Modified: branches/orm/tests/Test/Solar/Sql/Model.php
===================================================================
--- branches/orm/tests/Test/Solar/Sql/Model.php	2007-09-07 13:52:21 UTC (rev 2739)
+++ branches/orm/tests/Test/Solar/Sql/Model.php	2007-09-07 13:53:03 UTC (rev 2740)
@@ -1007,4 +1007,55 @@
     }
 
 
+    
+    /**
+     * 
+     * Test -- Counts the number of records in a related model for a given record.
+     * 
+     */
+    public function testCountPagesRelated()
+    {
+        $this->todo('stub');
+    }
+    
+    /**
+     * 
+     * Test -- 
+     * 
+     */
+    public function testFetchRelatedArray()
+    {
+        $this->todo('stub');
+    }
+    
+    /**
+     * 
+     * Test -- Given a record, fetches a related record or collection for a named relationship.
+     * 
+     */
+    public function testFetchRelatedObject()
+    {
+        $this->todo('stub');
+    }
+    
+    /**
+     * 
+     * Test -- Returns a new Solar_Sql_Select tool, with the proper SQL object injected automatically, and with eager "to-one" associations joined.
+     * 
+     */
+    public function testNewSelect()
+    {
+        $this->todo('stub');
+    }
+    
+    /**
+     * 
+     * Test -- Returns a new Solar_Sql_Select tool for selecting related records.
+     * 
+     */
+    public function testNewSelectRelated()
+    {
+        $this->todo('stub');
+    }
+
 }

Modified: branches/orm/tests/Test/Solar/Sql/ModelRelated/BelongsTo.php
===================================================================
--- branches/orm/tests/Test/Solar/Sql/ModelRelated/BelongsTo.php	2007-09-07 13:52:21 UTC (rev 2739)
+++ branches/orm/tests/Test/Solar/Sql/ModelRelated/BelongsTo.php	2007-09-07 13:53:03 UTC (rev 2740)
@@ -38,7 +38,7 @@
             'having' => NULL,
             'order' =>  array('area.id'),
             'paging' => 10,
-            'page' => 1,
+            'page' => 0,
             'cols' =>  array(
                 0 => 'id',
                 1 => 'created',

Modified: branches/orm/tests/Test/Solar/Sql/ModelRelated/HasMany.php
===================================================================
--- branches/orm/tests/Test/Solar/Sql/ModelRelated/HasMany.php	2007-09-07 13:52:21 UTC (rev 2739)
+++ branches/orm/tests/Test/Solar/Sql/ModelRelated/HasMany.php	2007-09-07 13:53:03 UTC (rev 2740)
@@ -33,7 +33,7 @@
             'having' => NULL,
             'order' => array('nodes.id'),
             'paging' => 10,
-            'page' => 1,
+            'page' => 0,
             'cols' => array (
                 0 => 'id',
                 1 => 'created',

Added: branches/orm/tests/Test/Solar/Sql/ModelRelated/HasManyThrough.php
===================================================================
--- branches/orm/tests/Test/Solar/Sql/ModelRelated/HasManyThrough.php	                        (rev 0)
+++ branches/orm/tests/Test/Solar/Sql/ModelRelated/HasManyThrough.php	2007-09-07 13:53:03 UTC (rev 2740)
@@ -0,0 +1,250 @@
+<?php
+class Test_Solar_Sql_ModelRelated_HasManyThrough extends Test_Solar_Sql_ModelRelated {
+    
+    /**
+     * 
+     * Depends on the Solar_Example_Model_Nodes having added $this->_hasOne().
+     * 
+     */
+    public function testAddRelated()
+    {
+        $nodes = $this->_newModel('nodes');
+        
+        // make sure that nodes has-many taggings
+        $expect = array (
+            'name' => 'taggings',
+            'type' => 'has_many',
+            'foreign_model' => 'Solar_Example_Model_Taggings',
+            'foreign_table' => 'test_solar_taggings',
+            'foreign_alias' => 'taggings',
+            'foreign_col' => 'node_id',
+            'foreign_inherit_col' => NULL,
+            'foreign_inherit_val' => NULL,
+            'foreign_primary_col' => 'id',
+            'native_col' => 'id',
+            'through' => NULL,
+            'through_table' => NULL,
+            'through_alias' => NULL,
+            'through_native_col' => NULL,
+            'through_foreign_col' => NULL,
+            'distinct' => NULL,
+            'where' => array(),
+            'group' => NULL,
+            'having' => NULL,
+            'order' => array(
+                0 => 'taggings.id',
+            ),
+            'paging' => 10,
+            'page' => 0,
+            'cols' => array(
+                0 => 'id',
+                1 => 'node_id',
+                2 => 'tag_id',
+            ),
+        );
+        
+        $this->assertSame($nodes->related['taggings'], $expect);
+        
+        // make sure that nodes has-many tags through taggings
+        $expect = array (
+            'name' => 'tags',
+            'type' => 'has_many',
+            'foreign_model' => 'Solar_Example_Model_Tags',
+            'foreign_table' => 'test_solar_tags',
+            'foreign_alias' => 'tags',
+            'foreign_col' => 'id',
+            'foreign_inherit_col' => NULL,
+            'foreign_inherit_val' => NULL,
+            'foreign_primary_col' => 'id',
+            'native_col' => 'id',
+            'through' => 'taggings',
+            'through_table' => 'test_solar_taggings',
+            'through_alias' => 'taggings',
+            'through_native_col' => 'node_id',
+            'through_foreign_col' => 'tag_id',
+            'distinct' => NULL,
+            'where' => array(),
+            'group' => NULL,
+            'having' => NULL,
+            'order' => array(
+                0 => 'tags.id',
+            ),
+            'paging' => 10,
+            'page' => 0,
+            'cols' => array(
+                0 => 'id',
+                1 => 'name',
+                2 => 'summ',
+            ),
+        );
+        
+        $this->assertSame($nodes->related['tags'], $expect);
+    }
+    
+    public function testLazyFetchOne()
+    {
+        $this->_populateAll();
+        
+        // fetch one node, then see how many sql calls so far
+        $nodes = $this->_newModel('nodes');
+        $params = array(
+            'where' => array(
+                'id = ?' => rand(1, 10),
+            ),
+        );
+        $node = $nodes->fetchOne($params);
+        $count0 = count($this->_sql->getProfile());
+        
+        // lazy-fetch the taggings and check that the node_id's match
+        $taggings = $node->taggings;
+        $this->assertInstance($taggings, 'Solar_Example_Model_Taggings_Collection');
+        foreach ($taggings as $tagging) {
+            $this->assertEquals($tagging->node_id, $node->id);
+        }
+        
+        // make sure we got an extra SQL call
+        $count1 = count($this->_sql->getProfile());
+        $this->assertEquals($count1, $count0 + 1);
+        
+        // lazy fetch the tags through the taggings
+        $tags = $node->tags;
+        $this->assertInstance($tags, 'Solar_Example_Model_Tags_Collection');
+        
+        // make sure the tags/taggings counts match
+        $this->assertEquals(count($taggings), count($tags));
+        
+        // make sure each tag has a match with a tagging
+        foreach ($tags as $tag) {
+            $found = false;
+            foreach ($taggings as $tagging) {
+                if ($tagging->tag_id == $tag->id) {
+                    $found = true;
+                    break;
+                }
+            }
+            $this->assertTrue($found);
+        }
+        
+        // make sure we got only one extra SQL call overall
+        $count2 = count($this->_sql->getProfile());
+        $this->assertEquals($count2, $count1 + 1);
+        
+        // a second check should *not* make a new SQL call
+        $tags = $node->tags;
+        $this->assertInstance($tags, 'Solar_Example_Model_Tags_Collection');
+        $count3 = count($this->_sql->getProfile());
+        $this->assertEquals($count3, $count2);
+    }
+    
+    public function testLazyFetchAll()
+    {
+        $this->_populateAll();
+        
+        // fetch all nodes, then see how many sql calls so far
+        $nodes = $this->_newModel('nodes');
+        $node_coll = $nodes->fetchAll();
+        $count_before = count($this->_sql->getProfile());
+        
+        // lazy-fetch each collection of taggings and tags on each node
+        $extra_calls = 0;
+        foreach ($node_coll as $node) {
+            
+            // get the taggings
+            $taggings = $node->taggings;
+            $this->assertInstance($taggings, 'Solar_Example_Model_Taggings_Collection');
+            $extra_calls ++;
+            
+            // get the tags
+            $tags = $node->tags;
+            $this->assertInstance($tags, 'Solar_Example_Model_Tags_Collection');
+            $extra_calls ++;
+            
+            // make sure the taggings/tags counts match
+            $this->assertEquals(count($taggings), count($tags));
+            
+            // make sure each tagging has the right node ID
+            foreach ($taggings as $tagging) {
+                $this->assertEquals($tagging->node_id, $node->id);
+            }
+            
+            // make sure each tag has a match with a tagging
+            foreach ($tags as $tag) {
+                $found = false;
+                foreach ($taggings as $tagging) {
+                    if ($tagging->tag_id == $tag->id) {
+                        $found = true;
+                        break;
+                    }
+                }
+                $this->assertTrue($found);
+            }
+        }
+        
+        // make sure we have the right number of SQL calls
+        $count_after = count($this->_sql->getProfile());
+        $this->assertEquals($count_after, $count_before + $extra_calls);
+        
+        // a second check should *not* make new SQL calls
+        foreach ($node_coll as $node) {
+            $taggings = $node->taggings;
+            $this->assertInstance($taggings, 'Solar_Example_Model_Taggings_Collection');
+            $tags = $node->tags;
+            $this->assertInstance($tags, 'Solar_Example_Model_Tags_Collection');
+        }
+        
+        $count_final = count($this->_sql->getProfile());
+        $this->assertEquals($count_final, $count_after);
+    }
+    
+    public function testEagerFetchOne()
+    {
+        $this->_populateAll();
+        
+        // fetch one node with an eager tags
+        // then see how many sql calls so far
+        $nodes = $this->_newModel('nodes');
+        $params = array(
+            'where' => array(
+                'id = ?' => rand(1, 10),
+            ),
+            'eager' => 'tags',
+        );
+        
+        $node = $nodes->fetchOne($params);
+        $count_before = count($this->_sql->getProfile());
+        
+        // get the tags, make sure there are some.
+        // (can't tell how many there should have been without taggings.)
+        $tags = $node->tags;
+        $this->assertInstance($tags, 'Solar_Example_Model_Tags_Collection');
+        $this->assertTrue(count($tags) > 0);
+        
+        // should have been no extra SQL calls
+        $count_after = count($this->_sql->getProfile());
+        $this->assertEquals($count_after, $count_before);
+    }
+    
+    public function testEagerFetchAll()
+    {
+        $this->_populateAll();
+        
+        // fetch all nodes with eager tags
+        // then see how many sql calls so far
+        $nodes = $this->_newModel('nodes');
+        $params = array('eager' => 'tags');
+        $node_coll = $nodes->fetchAll($params);
+        $count_before = count($this->_sql->getProfile());
+        
+        // get the tags, make sure there are some.
+        // (can't tell how many there should have been without taggings.)
+        foreach ($node_coll as $node) {
+            $tags = $node->tags;
+            $this->assertInstance($tags, 'Solar_Example_Model_Tags_Collection');
+            $this->assertTrue(count($tags) > 0);
+        }
+        
+        // should have been no extra SQL calls
+        $count_after = count($this->_sql->getProfile());
+        $this->assertEquals($count_after, $count_before);
+    }
+}

Modified: branches/orm/tests/Test/Solar/Sql/ModelRelated/HasOne.php
===================================================================
--- branches/orm/tests/Test/Solar/Sql/ModelRelated/HasOne.php	2007-09-07 13:52:21 UTC (rev 2739)
+++ branches/orm/tests/Test/Solar/Sql/ModelRelated/HasOne.php	2007-09-07 13:53:03 UTC (rev 2740)
@@ -33,7 +33,7 @@
             'having' => NULL,
             'order' => array('meta.id'),
             'paging' => 10,
-            'page' => 1,
+            'page' => 0,
             'cols' => array(
                 0 => 'id',
                 1 => 'node_id',

Modified: branches/orm/tests/Test/Solar/Sql/ModelRelated.php
===================================================================
--- branches/orm/tests/Test/Solar/Sql/ModelRelated.php	2007-09-07 13:52:21 UTC (rev 2739)
+++ branches/orm/tests/Test/Solar/Sql/ModelRelated.php	2007-09-07 13:53:03 UTC (rev 2740)
@@ -124,6 +124,8 @@
         $this->_populateAreas();
         $this->_populateNodes();
         $this->_populateMetas();
+        $this->_populateTags();
+        $this->_populateTaggings();
     }
     
     protected function _populateUsers()
@@ -181,4 +183,63 @@
             $this->assertTrue($result);
         }
     }
+    
+    protected function _populateTags()
+    {
+        // some generic tags
+        $list = array('foo', 'bar', 'baz', 'zab', 'rab', 'oof');
+        
+        // save them
+        $tags = $this->_newModel('tags');
+        foreach ($list as $name) {
+            $tag = $tags->fetchNew();
+            $tag->name = $name;
+            $result = $tag->save();
+            $this->assertTrue($result);
+        }
+    }
+    
+    protected function _populateTaggings()
+    {
+        $tags = $this->_newModel('tags');
+        $tag_coll = $tags->fetchAll();
+        $tag_last = count($tag_coll) - 1;
+        
+        $nodes = $this->_newModel('nodes');
+        $node_coll = $nodes->fetchAll();
+        
+        $taggings = $this->_newModel('taggings');
+        
+        // add some tags on each node through taggings
+        foreach ($node_coll as $node) {
+            
+            // add 2-5 tags on this node
+            $tags_to_add = rand(2,5);
+            
+            // which tags have we used already?
+            $tags_used = array();
+            
+            // add each of the tags
+            for ($i = 0; $i < $tags_to_add; $i ++) {
+                
+                // pick a random tag that has not been used yet
+                do {
+                    $tagno = rand(0, $tag_last);
+                } while (in_array($tagno, $tags_used));
+                
+                // mark it as used
+                $tags_used[] = $tagno;
+                
+                // get the tag from the collection
+                $tag = $tag_coll[$tagno];
+                
+                // match the node to the tag with a tagging
+                $tagging = $taggings->fetchNew();
+                $tagging->node_id = $node->id;
+                $tagging->tag_id = $tag->id;
+                $result = $tagging->save();
+                $this->assertTrue($result);
+            }
+        }
+    }
 }
\ No newline at end of file




More information about the Solar-svn mailing list