04.07
php
saved
mariano_iglesias
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
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
- <?php
- /**
- * Test cases for expects().
- *
- * Test methods to test expects() functionality. See the following Bakery article for more
- * information regarding expects: http://bakery.cakephp.org/articles/view/185
- *
- * PHP versions 4 and 5
- *
- * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
- * Copyright 2005-2007, Cake Software Foundation, Inc.
- * 1785 E. Sahara Avenue, Suite 490-204
- * Las Vegas, Nevada 89104
- *
- * Licensed under The Open Group Test Suite License
- * Redistributions of files must retain the above copyright notice.
- *
- * @filesource
- * @author Mariano Iglesias - mariano@cricava.com
- * @link http://bakery.cakephp.org/articles/view/185 Bakery article on expects()
- * @package app.tests
- * @subpackage app.tests.cases.models
- * @since CakePHP(tm) v 1.2.0.4813
- * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
- */
- require_once (LIBS . 'model' . DS . 'model.php');
- /**
- * Base Application Model that adds expects().
- *
- * @package app.tests
- * @subpackage app.tests.cases.models
- */
- class AppModel extends Model
- {
- function afterFind($results)
- {
- {
- $this->__resetExpects();
- }
- return parent::afterFind($results);
- }
- function expects()
- {
- $innerCall = false;
- {
- $innerCall = $arguments[0];
- }
- foreach($arguments as $index => $argument)
- {
- {
- {
- $arguments = am($arguments, $argument);
- }
- }
- }
- foreach($arguments as $index => $argument)
- {
- {
- }
- }
- {
- }
- else
- {
- foreach($arguments as $argument)
- {
- {
- if ($child == $model)
- {
- }
- else
- {
- $models[$model][] = $child;
- }
- }
- else
- {
- $models[$this->name][] = $argument;
- }
- }
- }
- foreach($models as $bindingName => $children)
- {
- $model = null;
- foreach($relationTypes as $relationType)
- {
- {
- $model = $currentRelation[$bindingName]['className'];
- break;
- }
- }
- {
- $model = $bindingName;
- }
- {
- {
- }
- $this->__backInnerAssociation[] = $model;
- $this->$model->expects(true, $children);
- }
- }
- {
- foreach($models as $model => $children)
- {
- if ($model != $this->name)
- {
- $models[$this->name][] = $model;
- }
- }
- foreach($relationTypes as $relation)
- {
- {
- foreach($this->$relation as $bindingName => $bindingData)
- {
- {
- $unbind[$relation][] = $bindingName;
- }
- }
- }
- }
- {
- $this->unbindModel($unbind);
- }
- }
- if (!$innerCall)
- {
- $this->__runResetExpects = true;
- }
- }
- function __resetExpects()
- {
- {
- $this->__resetAssociations();
- }
- {
- foreach($this->__backInnerAssociation as $model)
- {
- $this->$model->__resetExpects();
- }
- }
- }
- }
- /**
- * Model used for testing.
- *
- * @package app.tests
- * @subpackage app.tests.cases.models
- */
- class User extends AppModel
- {
- var $useTable = false;
- var $name = 'User';
- }
- /**
- * Model used for testing.
- *
- * @package app.tests
- * @subpackage app.tests.cases.models
- */
- class Profile extends AppModel
- {
- var $useTable = false;
- var $name = 'Profile';
- }
- /**
- * Model used for testing.
- *
- * @package app.tests
- * @subpackage app.tests.cases.models
- */
- class Article extends AppModel
- {
- var $useTable = false;
- var $name = 'Article';
- }
- /**
- * Model used for testing.
- *
- * @package app.tests
- * @subpackage app.tests.cases.models
- */
- class Comment extends AppModel
- {
- var $useTable = false;
- var $name = 'Comment';
- }
- /**
- * Model used for testing.
- *
- * @package app.tests
- * @subpackage app.tests.cases.models
- */
- class Attachment extends AppModel
- {
- var $useTable = false;
- var $name = 'Attachment';
- }
- /**
- * Model used for testing.
- *
- * @package app.tests
- * @subpackage app.tests.cases.models
- */
- class Tag extends AppModel
- {
- var $useTable = false;
- var $name = 'Tag';
- }
- /**
- * Test case for expects().
- *
- * @package app.tests
- * @subpackage app.tests.cases.models
- */
- class ExpectsTestCase extends CakeTestCase
- {
- function setUp()
- {
- $this->User = $this->_loadModel('User');
- }
- function tearDown()
- {
- }
- function testSimple()
- {
- $this->User->expects();
- $result = $this->_linkedModels($this->User);
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User);
- ),
- )
- );
- $this->assertEqual($result, $expected);
- $this->User->expects('Profile');
- $result = $this->_linkedModels($this->User);
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User);
- ),
- )
- );
- $this->assertEqual($result, $expected);
- $this->User->expects('Comment', 'Profile');
- $result = $this->_linkedModels($this->User);
- ),
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User);
- ),
- )
- );
- $this->assertEqual($result, $expected);
- }
- function testFirstLevel()
- {
- $this->User->expects('Article');
- $result = $this->_linkedModels($this->User);
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User->Article);
- ),
- ),
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User);
- ),
- )
- );
- $this->assertEqual($result, $expected);
- $this->User->expects('Article', 'Article.Tag');
- $result = $this->_linkedModels($this->User);
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User->Article);
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User);
- ),
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User->Article);
- ),
- ),
- )
- );
- $this->assertEqual($result, $expected);
- $this->User->expects('Article', 'Article.Article');
- $result = $this->_linkedModels($this->User);
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User->Article);
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User);
- ),
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User->Article);
- ),
- ),
- )
- );
- $this->assertEqual($result, $expected);
- }
- function testSecondLevel()
- {
- $this->User->expects('Article', 'Article.Comment', 'Article.Comment.Attachment');
- $result = $this->_linkedModels($this->User);
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User->Article);
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User->Article->Comment);
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User);
- ),
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User->Article);
- ),
- ),
- )
- );
- $this->assertEqual($result, $expected);
- $result = $this->_linkedModels($this->User->Article->Comment);
- ),
- )
- );
- $this->assertEqual($result, $expected);
- }
- /**
- * Get the list of models linked to a specific model. Returns an associative array,
- * where first level key is the association type (i.e: hasOne, belongsTo, hasMany or
- * hasAndBelongsToMany), and for each first level key an array of elements where
- * each element is of the form associationName => className (e.g: Comment => Comment)
- *
- * @param object $object Instance of the model
- *
- * @return array Set of linked models
- *
- * @access private
- */
- function _linkedModels(&$object)
- {
- foreach($object->__associations as $type)
- {
- foreach($object->{$type} as $assoc => $value)
- {
- }
- }
- return $linked;
- }
- /**
- * Instantiate a model and its linked models.
- *
- * @param string $model Model name (e.g: Article)
- *
- * @return object Instance of the model
- *
- * @access private
- */
- function &_loadModel($model)
- {
- $object =& new $model();
- // Create links
- $object->__createLinks();
- // Emulate __createLinks() to run __createLinks() on all inner models
- foreach($object->__associations as $type)
- {
- // Sanitize the association
- {
- foreach($object->{$type} as $i => $className)
- {
- }
- }
- // Navigate association types
- foreach($object->{$type} as $assoc => $value)
- {
- {
- $assoc = $value;
- $object->{$type}[$assoc] = $value;
- }
- $className = $assoc;
- {
- $className = $value['className'];
- }
- // Create linked models
- $object->{$assoc}->__createLinks();
- {
- $object->{$value['with']}->__createLinks();
- }
- }
- }
- return $object;
- }
- }
- ?>
Parsed in 0.948 seconds, using GeSHi 1.0.7.14