04.07 php saved
mariano_iglesias
Tags add more
case, expects, model and test  
Note
Test cases for expects() to be run on CakePHP 1.2. Place the following content as a file named expects.test.php on your app/tests/models directory, and having DEBUG set to a value of at least 1, point your browser to http://www.example.com/test.php. Click then on App Test Cases, and click on link entitled "models/expects.test.php"

For more information regarding expects() please visit:

http://bakery.cakephp.org/articles/view/185
  1. <?php
  2. /**
  3. * Test cases for expects().
  4. *
  5. * Test methods to test expects() functionality. See the following Bakery article for more
  6. * information regarding expects: http://bakery.cakephp.org/articles/view/185
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2007, Cake Software Foundation, Inc.
  12. *                1785 E. Sahara Avenue, Suite 490-204
  13. *                Las Vegas, Nevada 89104
  14. *
  15. *  Licensed under The Open Group Test Suite License
  16. *  Redistributions of files must retain the above copyright notice.
  17. *
  18. * @filesource
  19. * @author      Mariano Iglesias - mariano@cricava.com
  20. * @link                http://bakery.cakephp.org/articles/view/185 Bakery article on expects()
  21. * @package   app.tests
  22. * @subpackage  app.tests.cases.models
  23. * @since            CakePHP(tm) v 1.2.0.4813
  24. * @license   http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  25. */
  26.  
  27. require_once (LIBS . 'model' . DS . 'model.php');
  28.  
  29. /**
  30. * Base Application Model that adds expects().
  31. *
  32. * @package  app.tests
  33. * @subpackage  app.tests.cases.models
  34. */
  35. class AppModel extends Model
  36. {
  37.     function afterFind($results)
  38.     {
  39.         if (isset($this->__runResetExpects) && $this->__runResetExpects)
  40.         {
  41.             $this->__resetExpects();
  42.             unset($this->__runResetExpects);
  43.         }
  44.        
  45.         return parent::afterFind($results);
  46.     }
  47.  
  48.     function expects()
  49.     {
  50.         $models = array();
  51.         $arguments = func_get_args();
  52.         $innerCall = false;
  53.  
  54.         if (!empty($arguments) && is_bool($arguments[0]))
  55.         {
  56.             $innerCall = $arguments[0];
  57.         }
  58.        
  59.         foreach($arguments as $index => $argument)
  60.         {
  61.             if (is_array($argument))
  62.             {
  63.                 if (count($argument) > 0)
  64.                 {
  65.                     $arguments = am($arguments, $argument);
  66.                 }
  67.  
  68.                 unset($arguments[$index]);
  69.             }
  70.         }
  71.        
  72.         foreach($arguments as $index => $argument)
  73.         {
  74.             if (!is_string($argument))
  75.             {
  76.                 unset($arguments[$index]);
  77.             }
  78.         }
  79.  
  80.         if (count($arguments) == 0)
  81.         {
  82.             $models[$this->name] = array();
  83.         }
  84.         else
  85.         {
  86.             foreach($arguments as $argument)
  87.             {
  88.                 if (strpos($argument, '.') !== false)
  89.                 {
  90.                     $model = substr($argument, 0, strpos($argument, '.'));
  91.                     $child = substr($argument, strpos($argument, '.') + 1);
  92.  
  93.                     if ($child == $model)
  94.                     {
  95.                         $models[$model] = array();
  96.                     }
  97.                     else
  98.                     {
  99.                         $models[$model][] = $child;
  100.                     }
  101.                 }
  102.                 else
  103.                 {
  104.                     $models[$this->name][] = $argument;
  105.                 }
  106.             }
  107.         }
  108.        
  109.         $relationTypes = array ('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
  110.  
  111.         foreach($models as $bindingName => $children)
  112.         {
  113.             $model = null;
  114.            
  115.             foreach($relationTypes as $relationType)
  116.             {
  117.                 $currentRelation = (isset($this->$relationType) ? $this->$relationType : null);
  118.                
  119.                 if (isset($currentRelation) && isset($currentRelation[$bindingName]) && is_array($currentRelation[$bindingName]) && isset($currentRelation[$bindingName]['className']))
  120.                 {
  121.                     $model = $currentRelation[$bindingName]['className'];
  122.                     break;
  123.                 }
  124.             }
  125.            
  126.             if (!isset($model))
  127.             {
  128.                 $model = $bindingName;
  129.             }
  130.            
  131.             if (isset($model) && $model != $this->name && isset($this->$model))
  132.             {
  133.                 if (!isset($this->__backInnerAssociation))
  134.                 {
  135.                     $this->__backInnerAssociation = array();
  136.                 }
  137.                
  138.                 $this->__backInnerAssociation[] = $model;
  139.                
  140.                 $this->$model->expects(true, $children);
  141.             }
  142.         }
  143.        
  144.         if (isset($models[$this->name]))
  145.         {
  146.             foreach($models as $model => $children)
  147.             {
  148.                 if ($model != $this->name)
  149.                 {
  150.                     $models[$this->name][] = $model;
  151.                 }
  152.             }
  153.    
  154.             $models = array_unique($models[$this->name]);
  155.             $unbind = array();
  156.    
  157.             foreach($relationTypes as $relation)
  158.             {
  159.                 if (isset($this->$relation))
  160.                 {
  161.                     foreach($this->$relation as $bindingName => $bindingData)
  162.                     {
  163.                         if (!in_array($bindingName, $models))
  164.                         {
  165.                             $unbind[$relation][] = $bindingName;
  166.                         }
  167.                     }
  168.                 }
  169.             }
  170.    
  171.             if (count($unbind) > 0)
  172.             {
  173.                 $this->unbindModel($unbind);
  174.             }
  175.         }
  176.  
  177.         if (!$innerCall)
  178.         {
  179.             $this->__runResetExpects = true;
  180.         }
  181.     }
  182.    
  183.     function __resetExpects()
  184.     {
  185.         if (isset($this->__backAssociation))
  186.         {
  187.             $this->__resetAssociations();
  188.         }
  189.        
  190.         if (isset($this->__backInnerAssociation))
  191.         {
  192.             foreach($this->__backInnerAssociation as $model)
  193.             {
  194.                 $this->$model->__resetExpects();
  195.             }
  196.            
  197.             unset($this->__backInnerAssociation);
  198.         }
  199.     }
  200. }
  201.  
  202. /**
  203. * Model used for testing.
  204. *
  205. * @package  app.tests
  206. * @subpackage  app.tests.cases.models
  207. */
  208. class User extends AppModel
  209. {
  210.     var $useTable = false;
  211.     var $name = 'User';
  212.    
  213.     var $hasOne = array('Profile');
  214.     var $hasMany = array('Article', 'Comment');
  215. }
  216.  
  217. /**
  218. * Model used for testing.
  219. *
  220. * @package  app.tests
  221. * @subpackage  app.tests.cases.models
  222. */
  223. class Profile extends AppModel
  224. {
  225.     var $useTable = false;
  226.     var $name = 'Profile';
  227.    
  228.     var $belongsTo = array('User');
  229. }
  230.  
  231. /**
  232. * Model used for testing.
  233. *
  234. * @package  app.tests
  235. * @subpackage  app.tests.cases.models
  236. */
  237. class Article extends AppModel
  238. {
  239.     var $useTable = false;
  240.     var $name = 'Article';
  241.    
  242.     var $belongsTo = array('User');
  243.     var $hasMany = array('Comment');
  244.     var $hasAndBelongsToMany = array('Tag');
  245. }
  246.  
  247. /**
  248. * Model used for testing.
  249. *
  250. * @package  app.tests
  251. * @subpackage  app.tests.cases.models
  252. */
  253. class Comment extends AppModel
  254. {
  255.     var $useTable = false;
  256.     var $name = 'Comment';
  257.    
  258.     var $belongsTo = array('Article', 'User');
  259.     var $hasMany = array('Attachment');
  260. }
  261.  
  262. /**
  263. * Model used for testing.
  264. *
  265. * @package  app.tests
  266. * @subpackage  app.tests.cases.models
  267. */
  268. class Attachment extends AppModel
  269. {
  270.     var $useTable = false;
  271.     var $name = 'Attachment';
  272.    
  273.     var $belongsTo = array('Comment');
  274. }
  275.  
  276. /**
  277. * Model used for testing.
  278. *
  279. * @package  app.tests
  280. * @subpackage  app.tests.cases.models
  281. */
  282. class Tag extends AppModel
  283. {
  284.     var $useTable = false;
  285.     var $name = 'Tag';
  286. }
  287.  
  288. /**
  289. * Test case for expects().
  290. *
  291. * @package  app.tests
  292. * @subpackage  app.tests.cases.models
  293. */
  294. class ExpectsTestCase extends CakeTestCase
  295. {
  296.     function setUp()
  297.     {
  298.         $this->User = $this->_loadModel('User');
  299.     }
  300.    
  301.     function tearDown()
  302.     {
  303.         unset($this->User);
  304.     }
  305.    
  306.     function testSimple()
  307.     {
  308.         $this->User->expects();
  309.        
  310.         $result = $this->_linkedModels($this->User);
  311.         $expected = array();
  312.        
  313.         $this->assertEqual($result, $expected);
  314.        
  315.         $this->User->afterFind(array());
  316.        
  317.         $result = $this->_linkedModels($this->User);
  318.         $expected = array(
  319.             'hasOne' => array(
  320.                 array('Profile' => 'Profile')
  321.             ),
  322.             'hasMany' => array(
  323.                 array('Article' => 'Article'),
  324.                 array('Comment' => 'Comment')
  325.             )
  326.         );
  327.        
  328.         $this->assertEqual($result, $expected);
  329.        
  330.         $this->User->expects('Profile');
  331.        
  332.         $result = $this->_linkedModels($this->User);
  333.         $expected = array(
  334.             'hasOne' => array(
  335.                 array('Profile' => 'Profile')
  336.             )
  337.         );
  338.        
  339.         $this->assertEqual($result, $expected);
  340.  
  341.         $this->User->afterFind(array());
  342.        
  343.         $result = $this->_linkedModels($this->User);
  344.         $expected = array(
  345.             'hasOne' => array(
  346.                 array('Profile' => 'Profile')
  347.             ),
  348.             'hasMany' => array(
  349.                 array('Article' => 'Article'),
  350.                 array('Comment' => 'Comment')
  351.             )
  352.         );
  353.        
  354.         $this->assertEqual($result, $expected);
  355.        
  356.         $this->User->expects('Comment', 'Profile');
  357.        
  358.         $result = $this->_linkedModels($this->User);
  359.         $expected = array(
  360.             'hasOne' => array(
  361.                 array('Profile' => 'Profile')
  362.             ),
  363.             'hasMany' => array(
  364.                 array('Comment' => 'Comment')
  365.             )
  366.         );
  367.        
  368.         $this->assertEqual($result, $expected);
  369.  
  370.         $this->User->afterFind(array());
  371.        
  372.         $result = $this->_linkedModels($this->User);
  373.         $expected = array(
  374.             'hasOne' => array(
  375.                 array('Profile' => 'Profile')
  376.             ),
  377.             'hasMany' => array(
  378.                 array('Article' => 'Article'),
  379.                 array('Comment' => 'Comment')
  380.             )
  381.         );
  382.        
  383.         $this->assertEqual($result, $expected);
  384.     }
  385.    
  386.     function testFirstLevel()
  387.     {
  388.         $this->User->expects('Article');
  389.        
  390.         $result = $this->_linkedModels($this->User);
  391.         $expected = array(
  392.             'hasMany' => array(
  393.                 array('Article' => 'Article')
  394.             )
  395.         );
  396.        
  397.         $this->assertEqual($result, $expected);
  398.        
  399.         $result = $this->_linkedModels($this->User->Article);
  400.         $expected = array(
  401.             'belongsTo' => array(
  402.                 array('User' => 'User')
  403.             ),
  404.             'hasMany' => array(
  405.                 array('Comment' => 'Comment')
  406.             ),
  407.             'hasAndBelongsToMany' => array(
  408.                 array('Tag' => 'Tag')
  409.             )
  410.         );
  411.        
  412.         $this->assertEqual($result, $expected);
  413.        
  414.         $this->User->afterFind(array());
  415.        
  416.         $result = $this->_linkedModels($this->User);
  417.         $expected = array(
  418.             'hasOne' => array(
  419.                 array('Profile' => 'Profile')
  420.             ),
  421.             'hasMany' => array(
  422.                 array('Article' => 'Article'),
  423.                 array('Comment' => 'Comment')
  424.             )
  425.         );
  426.        
  427.         $this->assertEqual($result, $expected);
  428.        
  429.         $this->User->expects('Article', 'Article.Tag');
  430.        
  431.         $result = $this->_linkedModels($this->User);
  432.         $expected = array(
  433.             'hasMany' => array(
  434.                 array('Article' => 'Article')
  435.             )
  436.         );
  437.        
  438.         $this->assertEqual($result, $expected);
  439.        
  440.         $result = $this->_linkedModels($this->User->Article);
  441.         $expected = array(
  442.             'hasAndBelongsToMany' => array(
  443.                 array('Tag' => 'Tag')
  444.             )
  445.         );
  446.        
  447.         $this->assertEqual($result, $expected);
  448.        
  449.         $this->User->afterFind(array());
  450.        
  451.         $result = $this->_linkedModels($this->User);
  452.         $expected = array(
  453.             'hasOne' => array(
  454.                 array('Profile' => 'Profile')
  455.             ),
  456.             'hasMany' => array(
  457.                 array('Article' => 'Article'),
  458.                 array('Comment' => 'Comment')
  459.             )
  460.         );
  461.        
  462.         $this->assertEqual($result, $expected);
  463.        
  464.         $result = $this->_linkedModels($this->User->Article);
  465.         $expected = array(
  466.             'belongsTo' => array(
  467.                 array('User' => 'User')
  468.             ),
  469.             'hasMany' => array(
  470.                 array('Comment' => 'Comment')
  471.             ),
  472.             'hasAndBelongsToMany' => array(
  473.                 array('Tag' => 'Tag')
  474.             )
  475.         );
  476.        
  477.         $this->assertEqual($result, $expected);
  478.        
  479.         $this->User->expects('Article', 'Article.Article');
  480.        
  481.         $result = $this->_linkedModels($this->User);
  482.         $expected = array(
  483.             'hasMany' => array(
  484.                 array('Article' => 'Article')
  485.             )
  486.         );
  487.        
  488.         $this->assertEqual($result, $expected);
  489.        
  490.         $result = $this->_linkedModels($this->User->Article);
  491.         $expected = array();
  492.        
  493.         $this->assertEqual($result, $expected);
  494.        
  495.         $this->User->afterFind(array());
  496.        
  497.         $result = $this->_linkedModels($this->User);
  498.         $expected = array(
  499.             'hasOne' => array(
  500.                 array('Profile' => 'Profile')
  501.             ),
  502.             'hasMany' => array(
  503.                 array('Article' => 'Article'),
  504.                 array('Comment' => 'Comment')
  505.             )
  506.         );
  507.        
  508.         $this->assertEqual($result, $expected);
  509.        
  510.         $result = $this->_linkedModels($this->User->Article);
  511.         $expected = array(
  512.             'belongsTo' => array(
  513.                 array('User' => 'User')
  514.             ),
  515.             'hasMany' => array(
  516.                 array('Comment' => 'Comment')
  517.             ),
  518.             'hasAndBelongsToMany' => array(
  519.                 array('Tag' => 'Tag')
  520.             )
  521.         );
  522.        
  523.         $this->assertEqual($result, $expected);
  524.     }
  525.    
  526.     function testSecondLevel()
  527.     {
  528.         $this->User->expects('Article', 'Article.Comment', 'Article.Comment.Attachment');
  529.        
  530.         $result = $this->_linkedModels($this->User);
  531.         $expected = array(
  532.             'hasMany' => array(
  533.                 array('Article' => 'Article')
  534.             )
  535.         );
  536.        
  537.         $this->assertEqual($result, $expected);
  538.        
  539.         $result = $this->_linkedModels($this->User->Article);
  540.         $expected = array(
  541.             'hasMany' => array(
  542.                 array('Comment' => 'Comment')
  543.             )
  544.         );
  545.        
  546.         $this->assertEqual($result, $expected);
  547.        
  548.         $result = $this->_linkedModels($this->User->Article->Comment);
  549.         $expected = array(
  550.             'hasMany' => array(
  551.                 array('Attachment' => 'Attachment')
  552.             )
  553.         );
  554.        
  555.         $this->assertEqual($result, $expected);
  556.        
  557.         $this->User->afterFind(array());
  558.        
  559.         $result = $this->_linkedModels($this->User);
  560.         $expected = array(
  561.             'hasOne' => array(
  562.                 array('Profile' => 'Profile')
  563.             ),
  564.             'hasMany' => array(
  565.                 array('Article' => 'Article'),
  566.                 array('Comment' => 'Comment')
  567.             )
  568.         );
  569.        
  570.         $this->assertEqual($result, $expected);
  571.        
  572.         $result = $this->_linkedModels($this->User->Article);
  573.         $expected = array(
  574.             'belongsTo' => array(
  575.                 array('User' => 'User')
  576.             ),
  577.             'hasMany' => array(
  578.                 array('Comment' => 'Comment')
  579.             ),
  580.             'hasAndBelongsToMany' => array(
  581.                 array('Tag' => 'Tag')
  582.             )
  583.         );
  584.        
  585.         $this->assertEqual($result, $expected);
  586.        
  587.         $result = $this->_linkedModels($this->User->Article->Comment);
  588.         $expected = array(
  589.             'belongsTo' => array(
  590.                 array('Article' => 'Article'),
  591.                 array('User' => 'User')
  592.             ),
  593.             'hasMany' => array(
  594.                 array('Attachment' => 'Attachment')
  595.             )
  596.         );
  597.        
  598.         $this->assertEqual($result, $expected);
  599.     }
  600.    
  601.     /**
  602.      * Get the list of models linked to a specific model. Returns an associative array,
  603.      * where first level key is the association type (i.e: hasOne, belongsTo, hasMany or
  604.      * hasAndBelongsToMany), and for each first level key an array of elements where
  605.      * each element is of the form associationName => className (e.g: Comment => Comment)
  606.      *
  607.      * @param object $object    Instance of the model
  608.      *
  609.      * @return array    Set of linked models
  610.      *
  611.      * @access private
  612.      */
  613.     function _linkedModels(&$object)
  614.     {
  615.         $linked = array();
  616.        
  617.         foreach($object->__associations as $type)
  618.         {
  619.             foreach($object->{$type} as $assoc => $value)
  620.             {
  621.                 $linked[$type][] = array($assoc => $value['className']);
  622.             }
  623.         }
  624.        
  625.         return $linked;
  626.     }
  627.  
  628.     /**
  629.      * Instantiate a model and its linked models.
  630.      *
  631.      * @param string $model Model name (e.g: Article)
  632.      *
  633.      * @return object   Instance of the model
  634.      *
  635.      * @access private
  636.      */
  637.     function &_loadModel($model)
  638.     {
  639.         $object =& new $model();
  640.        
  641.         // Create links
  642.        
  643.         $object->__createLinks();
  644.        
  645.         // Emulate __createLinks() to run __createLinks() on all inner models
  646.        
  647.         foreach($object->__associations as $type)
  648.         {
  649.             // Sanitize the association
  650.            
  651.             if (!is_array($object->{$type}))
  652.             {
  653.                 $object->{$type} = explode(',', $object->{$type});
  654.  
  655.                 foreach($object->{$type} as $i => $className)
  656.                 {
  657.                     $className = trim($className);
  658.                     unset ($object->{$type}[$i]);
  659.                     $object->{$type}[$className] = array();
  660.                 }
  661.             }
  662.  
  663.             // Navigate association types
  664.            
  665.             foreach($object->{$type} as $assoc => $value)
  666.             {
  667.                 if (is_numeric($assoc))
  668.                 {
  669.                     unset ($object->{$type}[$assoc]);
  670.                     $assoc = $value;
  671.                     $value = array();
  672.                     $object->{$type}[$assoc] = $value;
  673.                 }
  674.                
  675.                 $className = $assoc;
  676.  
  677.                 if (isset($value['className']) && !empty($value['className']))
  678.                 {
  679.                     $className = $value['className'];
  680.                 }
  681.                
  682.                 // Create linked models
  683.                
  684.                 $object->{$assoc}->__createLinks();
  685.  
  686.                 if (isset($value['with']) && !empty($value['with']))
  687.                 {
  688.                     $object->{$value['with']}->__createLinks();
  689.                 }
  690.             }
  691.         }
  692.        
  693.         return $object;
  694.     }
  695. }
  696.  
  697. ?>
Parsed in 0.924 seconds, using GeSHi 1.0.7.14

Modify this Paste