2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-19 21:10:13 +00:00
|
|
|
* ClassRegistryTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 16:31:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5432
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2010-12-09 05:13:11 +00:00
|
|
|
App::uses('ClassRegistry', 'Utility');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* ClassRegisterModel class
|
2008-08-25 22:33:36 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class ClassRegisterModel extends CakeTestModel {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* useTable property
|
2008-08-25 22:33:36 +00:00
|
|
|
*
|
2013-09-17 12:44:34 +00:00
|
|
|
* @var boolean
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $useTable = false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* RegisterArticle class
|
2008-08-25 22:33:36 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class RegisterArticle extends ClassRegisterModel {
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* RegisterArticleFeatured class
|
2008-08-25 22:33:36 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class RegisterArticleFeatured extends ClassRegisterModel {
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* RegisterArticleTag class
|
2008-08-25 22:33:36 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class RegisterArticleTag extends ClassRegisterModel {
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* RegistryPluginAppModel class
|
2008-08-25 22:33:36 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class RegistryPluginAppModel extends ClassRegisterModel {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* tablePrefix property
|
2008-08-25 22:33:36 +00:00
|
|
|
*
|
2013-06-08 02:29:08 +00:00
|
|
|
* @var string
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $tablePrefix = 'something_';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* TestRegistryPluginModel class
|
2008-08-25 22:33:36 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class TestRegistryPluginModel extends RegistryPluginAppModel {
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-27 02:38:15 +00:00
|
|
|
/**
|
|
|
|
* RegisterCategory class
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-09-27 02:38:15 +00:00
|
|
|
*/
|
|
|
|
class RegisterCategory extends ClassRegisterModel {
|
|
|
|
}
|
2012-03-14 02:59:20 +00:00
|
|
|
/**
|
2011-12-12 04:20:19 +00:00
|
|
|
* RegisterPrefixedDs class
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Utility
|
|
|
|
*/
|
|
|
|
class RegisterPrefixedDs extends ClassRegisterModel {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* useDbConfig property
|
|
|
|
*
|
2013-06-08 02:29:08 +00:00
|
|
|
* @var string
|
2011-12-12 04:20:19 +00:00
|
|
|
*/
|
|
|
|
public $useDbConfig = 'doesnotexist';
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-11-05 06:55:59 +00:00
|
|
|
/**
|
2011-12-08 01:58:01 +00:00
|
|
|
* Abstract class for testing ClassRegistry.
|
2011-11-05 06:55:59 +00:00
|
|
|
*/
|
2011-12-08 01:58:01 +00:00
|
|
|
abstract class ClassRegistryAbstractModel extends ClassRegisterModel {
|
|
|
|
|
2012-03-14 02:59:20 +00:00
|
|
|
public abstract function doSomething();
|
|
|
|
|
2011-12-08 01:58:01 +00:00
|
|
|
}
|
2011-11-05 06:55:59 +00:00
|
|
|
|
|
|
|
/**
|
2011-12-08 01:58:01 +00:00
|
|
|
* Interface for testing ClassRegistry
|
2011-11-05 06:55:59 +00:00
|
|
|
*/
|
2011-12-08 01:58:01 +00:00
|
|
|
interface ClassRegistryInterfaceTest {
|
|
|
|
|
2012-02-17 07:13:12 +00:00
|
|
|
public function doSomething();
|
2012-03-14 02:59:20 +00:00
|
|
|
|
2011-11-05 06:55:59 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* ClassRegistryTest class
|
2008-08-25 22:33:36 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-07-21 02:40:58 +00:00
|
|
|
class ClassRegistryTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testAddModel method
|
2008-08-25 22:33:36 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddModel() {
|
2010-07-06 02:19:22 +00:00
|
|
|
$Tag = ClassRegistry::init('RegisterArticleTag');
|
2008-05-30 11:40:08 +00:00
|
|
|
$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');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$this->assertTrue(is_a($TagCopy, 'RegisterArticleTag'));
|
2010-06-11 01:57:53 +00:00
|
|
|
$this->assertSame($Tag, $TagCopy);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-06-11 01:57:53 +00:00
|
|
|
$NewTag = ClassRegistry::init(array('class' => 'RegisterArticleTag', 'alias' => 'NewTag'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$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);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$NewTag->name = 'SomeOtherName';
|
2010-06-11 01:57:53 +00:00
|
|
|
$this->assertNotSame($Tag, $NewTag);
|
|
|
|
$this->assertSame($NewTag, $NewTagCopy);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$Tag->name = 'SomeOtherName';
|
2010-06-11 01:57:53 +00:00
|
|
|
$this->assertNotSame($Tag, $NewTag);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$this->assertTrue($TagCopy->name === 'SomeOtherName');
|
2008-09-24 02:00:57 +00:00
|
|
|
|
2010-06-11 01:57:53 +00:00
|
|
|
$User = ClassRegistry::init(array('class' => 'RegisterUser', 'alias' => 'User', 'table' => false));
|
2008-09-24 02:00:57 +00:00
|
|
|
$this->assertTrue(is_a($User, 'AppModel'));
|
|
|
|
|
2010-06-11 01:57:53 +00:00
|
|
|
$UserCopy = ClassRegistry::init(array('class' => 'RegisterUser', 'alias' => 'User', 'table' => false));
|
2008-09-24 02:00:57 +00:00
|
|
|
$this->assertTrue(is_a($UserCopy, 'AppModel'));
|
2010-06-11 01:57:53 +00:00
|
|
|
$this->assertEquals($User, $UserCopy);
|
2008-09-27 02:38:15 +00:00
|
|
|
|
2010-06-11 01:57:53 +00:00
|
|
|
$Category = ClassRegistry::init(array('class' => 'RegisterCategory'));
|
2008-09-27 02:38:15 +00:00
|
|
|
$this->assertTrue(is_a($Category, 'RegisterCategory'));
|
|
|
|
|
2010-06-11 01:57:53 +00:00
|
|
|
$ParentCategory = ClassRegistry::init(array('class' => 'RegisterCategory', 'alias' => 'ParentCategory'));
|
2008-09-27 02:38:15 +00:00
|
|
|
$this->assertTrue(is_a($ParentCategory, 'RegisterCategory'));
|
2010-06-11 01:57:53 +00:00
|
|
|
$this->assertNotSame($Category, $ParentCategory);
|
2008-09-27 02:38:15 +00:00
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertNotEquals($Category->alias, $ParentCategory->alias);
|
|
|
|
$this->assertEquals('RegisterCategory', $Category->alias);
|
|
|
|
$this->assertEquals('ParentCategory', $ParentCategory->alias);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testClassRegistryFlush method
|
2008-08-25 22:33:36 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testClassRegistryFlush() {
|
2013-01-23 12:45:50 +00:00
|
|
|
ClassRegistry::init('RegisterArticleTag');
|
2010-09-26 01:50:28 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$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'));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-08-28 12:08:36 +00:00
|
|
|
* testAddMultipleModels method
|
2008-08-25 22:33:36 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddMultipleModels() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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'));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testPluginAppModel method
|
2008-08-25 22:33:36 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPluginAppModel() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$TestRegistryPluginModel = ClassRegistry::isKeySet('TestRegistryPluginModel');
|
|
|
|
$this->assertFalse($TestRegistryPluginModel);
|
|
|
|
|
2011-05-09 02:45:50 +00:00
|
|
|
//Faking a plugin
|
|
|
|
CakePlugin::load('RegistryPlugin', array('path' => '/fake/path'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$TestRegistryPluginModel = ClassRegistry::init('RegistryPlugin.TestRegistryPluginModel');
|
|
|
|
$this->assertTrue(is_a($TestRegistryPluginModel, 'TestRegistryPluginModel'));
|
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('something_', $TestRegistryPluginModel->tablePrefix);
|
2008-09-24 02:00:57 +00:00
|
|
|
|
2010-06-11 01:57:53 +00:00
|
|
|
$PluginUser = ClassRegistry::init(array('class' => 'RegistryPlugin.RegisterUser', 'alias' => 'RegistryPluginUser', 'table' => false));
|
2008-09-24 02:00:57 +00:00
|
|
|
$this->assertTrue(is_a($PluginUser, 'RegistryPluginAppModel'));
|
|
|
|
|
2010-06-11 01:57:53 +00:00
|
|
|
$PluginUserCopy = ClassRegistry::getObject('RegistryPluginUser');
|
2008-09-24 02:00:57 +00:00
|
|
|
$this->assertTrue(is_a($PluginUserCopy, 'RegistryPluginAppModel'));
|
2010-06-11 01:57:53 +00:00
|
|
|
$this->assertSame($PluginUser, $PluginUserCopy);
|
2011-05-09 03:55:32 +00:00
|
|
|
CakePlugin::unload();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-07-14 04:36:01 +00:00
|
|
|
|
2011-11-05 06:55:59 +00:00
|
|
|
/**
|
|
|
|
* Tests prefixed datasource names for test purposes
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testPrefixedTestDatasource() {
|
2011-12-12 04:20:19 +00:00
|
|
|
ClassRegistry::config(array('testing' => true));
|
2011-11-05 06:55:59 +00:00
|
|
|
$Model = ClassRegistry::init('RegisterPrefixedDs');
|
2011-12-12 04:20:19 +00:00
|
|
|
$this->assertEquals('test', $Model->useDbConfig);
|
2011-11-05 06:55:59 +00:00
|
|
|
ClassRegistry::removeObject('RegisterPrefixedDs');
|
|
|
|
|
|
|
|
$testConfig = ConnectionManager::getDataSource('test')->config;
|
|
|
|
ConnectionManager::create('test_doesnotexist', $testConfig);
|
|
|
|
|
|
|
|
$Model = ClassRegistry::init('RegisterArticle');
|
2011-12-12 04:20:19 +00:00
|
|
|
$this->assertEquals('test', $Model->useDbConfig);
|
2011-11-05 06:55:59 +00:00
|
|
|
$Model = ClassRegistry::init('RegisterPrefixedDs');
|
2011-12-12 04:20:19 +00:00
|
|
|
$this->assertEquals('test_doesnotexist', $Model->useDbConfig);
|
2011-11-05 06:55:59 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 04:36:01 +00:00
|
|
|
/**
|
|
|
|
* Tests that passing the string parameter to init() will return false if the model does not exists
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testInitStrict() {
|
|
|
|
$this->assertFalse(ClassRegistry::init('NonExistent', true));
|
|
|
|
}
|
2011-12-08 01:58:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that you cannot init() an abstract class. An exception will be raised.
|
|
|
|
*
|
|
|
|
* @expectedException CakeException
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInitAbstractClass() {
|
|
|
|
ClassRegistry::init('ClassRegistryAbstractModel');
|
|
|
|
}
|
2012-03-14 02:59:20 +00:00
|
|
|
|
2011-12-08 01:58:01 +00:00
|
|
|
/**
|
|
|
|
* Test that you cannot init() an abstract class. A exception will be raised.
|
|
|
|
*
|
|
|
|
* @expectedException CakeException
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInitInterface() {
|
|
|
|
ClassRegistry::init('ClassRegistryInterfaceTest');
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|