cakephp2-php8/cake/tests/cases/libs/class_registry.test.php

288 lines
7.2 KiB
PHP
Raw Normal View History

<?php
/**
2009-03-19 21:10:13 +00:00
* ClassRegistryTest file
*
* PHP versions 4 and 5
*
2010-05-19 01:15:13 +00:00
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
2010-01-26 19:18:20 +00:00
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
2010-01-26 19:18:20 +00:00
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
2010-05-19 01:15:13 +00:00
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
2009-03-19 21:10:13 +00:00
* @package cake
* @subpackage cake.tests.cases.libs
* @since CakePHP(tm) v 1.2.0.5432
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Core', 'ClassRegistry');
/**
* ClassRegisterModel class
*
2009-03-19 21:10:13 +00:00
* @package cake
* @subpackage cake.tests.cases.libs
*/
class ClassRegisterModel extends CakeTestModel {
/**
* useTable property
*
* @var bool false
* @access public
*/
public $useTable = false;
}
/**
* RegisterArticle class
*
2009-03-19 21:10:13 +00:00
* @package cake
* @subpackage cake.tests.cases.libs
*/
class RegisterArticle extends ClassRegisterModel {
/**
* name property
*
* @var string 'RegisterArticle'
* @access public
*/
public $name = 'RegisterArticle';
}
/**
* RegisterArticleFeatured class
*
2009-03-19 21:10:13 +00:00
* @package cake
* @subpackage cake.tests.cases.libs
*/
class RegisterArticleFeatured extends ClassRegisterModel {
/**
* name property
*
* @var string 'RegisterArticleFeatured'
* @access public
*/
public $name = 'RegisterArticleFeatured';
}
/**
* RegisterArticleTag class
*
2009-03-19 21:10:13 +00:00
* @package cake
* @subpackage cake.tests.cases.libs
*/
class RegisterArticleTag extends ClassRegisterModel {
/**
* name property
*
* @var string 'RegisterArticleTag'
* @access public
*/
public $name = 'RegisterArticleTag';
}
/**
* RegistryPluginAppModel class
*
2009-03-19 21:10:13 +00:00
* @package cake
* @subpackage cake.tests.cases.libs
*/
class RegistryPluginAppModel extends ClassRegisterModel {
/**
* tablePrefix property
*
* @var string 'something_'
* @access public
*/
public $tablePrefix = 'something_';
}
/**
* TestRegistryPluginModel class
*
2009-03-19 21:10:13 +00:00
* @package cake
* @subpackage cake.tests.cases.libs
*/
class TestRegistryPluginModel extends RegistryPluginAppModel {
/**
* name property
*
* @var string 'TestRegistryPluginModel'
* @access public
*/
public $name = 'TestRegistryPluginModel';
}
/**
* RegisterCategory class
*
2009-03-19 21:10:13 +00:00
* @package cake
* @subpackage cake.tests.cases.libs
*/
class RegisterCategory extends ClassRegisterModel {
/**
* name property
*
* @var string 'RegisterCategory'
* @access public
*/
public $name = 'RegisterCategory';
}
/**
* ClassRegistryTest class
*
2009-03-19 21:10:13 +00:00
* @package cake
* @subpackage cake.tests.cases.libs
*/
class ClassRegistryTest extends CakeTestCase {
/**
* testAddModel method
*
* @access public
* @return void
*/
function testAddModel() {
2010-07-06 02:19:22 +00:00
$Tag = ClassRegistry::init('RegisterArticleTag');
$this->assertTrue(is_a($Tag, 'RegisterArticleTag'));
$TagCopy = ClassRegistry::isKeySet('RegisterArticleTag');
$this->assertTrue($TagCopy);
$Tag->name = 'SomeNewName';
2010-06-11 01:57:53 +00:00
$TagCopy = ClassRegistry::getObject('RegisterArticleTag');
$this->assertTrue(is_a($TagCopy, 'RegisterArticleTag'));
2010-06-11 01:57:53 +00:00
$this->assertSame($Tag, $TagCopy);
2010-06-11 01:57:53 +00:00
$NewTag = ClassRegistry::init(array('class' => 'RegisterArticleTag', 'alias' => 'NewTag'));
$this->assertTrue(is_a($Tag, 'RegisterArticleTag'));
2010-06-11 01:57:53 +00:00
$NewTagCopy = ClassRegistry::init(array('class' => 'RegisterArticleTag', 'alias' => 'NewTag'));
$this->assertNotSame($Tag, $NewTag);
$this->assertSame($NewTag, $NewTagCopy);
$NewTag->name = 'SomeOtherName';
2010-06-11 01:57:53 +00:00
$this->assertNotSame($Tag, $NewTag);
$this->assertSame($NewTag, $NewTagCopy);
$Tag->name = 'SomeOtherName';
2010-06-11 01:57:53 +00:00
$this->assertNotSame($Tag, $NewTag);
$this->assertTrue($TagCopy->name === 'SomeOtherName');
2010-06-11 01:57:53 +00:00
$User = ClassRegistry::init(array('class' => 'RegisterUser', 'alias' => 'User', 'table' => false));
$this->assertTrue(is_a($User, 'AppModel'));
2010-06-11 01:57:53 +00:00
$UserCopy = ClassRegistry::init(array('class' => 'RegisterUser', 'alias' => 'User', 'table' => false));
$this->assertTrue(is_a($UserCopy, 'AppModel'));
2010-06-11 01:57:53 +00:00
$this->assertEquals($User, $UserCopy);
2010-06-11 01:57:53 +00:00
$Category = ClassRegistry::init(array('class' => 'RegisterCategory'));
$this->assertTrue(is_a($Category, 'RegisterCategory'));
2010-06-11 01:57:53 +00:00
$ParentCategory = ClassRegistry::init(array('class' => 'RegisterCategory', 'alias' => 'ParentCategory'));
$this->assertTrue(is_a($ParentCategory, 'RegisterCategory'));
2010-06-11 01:57:53 +00:00
$this->assertNotSame($Category, $ParentCategory);
$this->assertNotEqual($Category->alias, $ParentCategory->alias);
$this->assertEqual('RegisterCategory', $Category->alias);
$this->assertEqual('ParentCategory', $ParentCategory->alias);
}
/**
* testClassRegistryFlush method
*
* @access public
* @return void
*/
function testClassRegistryFlush() {
$ArticleTag = ClassRegistry::getObject('RegisterArticleTag');
$this->assertTrue(is_a($ArticleTag, 'RegisterArticleTag'));
ClassRegistry::flush();
$NoArticleTag = ClassRegistry::isKeySet('RegisterArticleTag');
$this->assertFalse($NoArticleTag);
$this->assertTrue(is_a($ArticleTag, 'RegisterArticleTag'));
}
/**
* testAddMultipleModels method
*
* @access public
* @return void
*/
function testAddMultipleModels() {
$Article = ClassRegistry::isKeySet('Article');
$this->assertFalse($Article);
$Featured = ClassRegistry::isKeySet('Featured');
$this->assertFalse($Featured);
$Tag = ClassRegistry::isKeySet('Tag');
$this->assertFalse($Tag);
$models = array(array('class' => 'RegisterArticle', 'alias' => 'Article'),
array('class' => 'RegisterArticleFeatured', 'alias' => 'Featured'),
array('class' => 'RegisterArticleTag', 'alias' => 'Tag'));
$added = ClassRegistry::init($models);
$this->assertTrue($added);
$Article = ClassRegistry::isKeySet('Article');
$this->assertTrue($Article);
$Featured = ClassRegistry::isKeySet('Featured');
$this->assertTrue($Featured);
$Tag = ClassRegistry::isKeySet('Tag');
$this->assertTrue($Tag);
$Article = ClassRegistry::getObject('Article');
$this->assertTrue(is_a($Article, 'RegisterArticle'));
$Featured = ClassRegistry::getObject('Featured');
$this->assertTrue(is_a($Featured, 'RegisterArticleFeatured'));
$Tag = ClassRegistry::getObject('Tag');
$this->assertTrue(is_a($Tag, 'RegisterArticleTag'));
}
/**
* testPluginAppModel method
*
* @access public
* @return void
*/
function testPluginAppModel() {
$TestRegistryPluginModel = ClassRegistry::isKeySet('TestRegistryPluginModel');
$this->assertFalse($TestRegistryPluginModel);
$TestRegistryPluginModel = ClassRegistry::init('RegistryPlugin.TestRegistryPluginModel');
$this->assertTrue(is_a($TestRegistryPluginModel, 'TestRegistryPluginModel'));
$this->assertEqual($TestRegistryPluginModel->tablePrefix, 'something_');
2010-06-11 01:57:53 +00:00
$PluginUser = ClassRegistry::init(array('class' => 'RegistryPlugin.RegisterUser', 'alias' => 'RegistryPluginUser', 'table' => false));
$this->assertTrue(is_a($PluginUser, 'RegistryPluginAppModel'));
2010-06-11 01:57:53 +00:00
$PluginUserCopy = ClassRegistry::getObject('RegistryPluginUser');
$this->assertTrue(is_a($PluginUserCopy, 'RegistryPluginAppModel'));
2010-06-11 01:57:53 +00:00
$this->assertSame($PluginUser, $PluginUserCopy);
}
}