cakephp2-php8/lib/Cake/Test/Case/Utility/ClassRegistryTest.php

346 lines
9.3 KiB
PHP
Raw Normal View History

<?php
/**
2009-03-19 21:10:13 +00:00
* ClassRegistryTest file
*
2017-06-10 22:15:34 +00:00
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
2017-06-10 22:15:34 +00:00
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Utility
* @since CakePHP(tm) v 1.2.0.5432
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
App::uses('ClassRegistry', 'Utility');
/**
* ClassRegisterModel class
*
* @package Cake.Test.Case.Utility
*/
class ClassRegisterModel extends CakeTestModel {
/**
* useTable property
*
* @var bool
*/
public $useTable = false;
}
/**
* RegisterArticle class
*
* @package Cake.Test.Case.Utility
*/
class RegisterArticle extends ClassRegisterModel {
}
/**
* RegisterArticleFeatured class
*
* @package Cake.Test.Case.Utility
*/
class RegisterArticleFeatured extends ClassRegisterModel {
}
/**
* RegisterArticleTag class
*
* @package Cake.Test.Case.Utility
*/
class RegisterArticleTag extends ClassRegisterModel {
}
/**
* RegistryPluginAppModel class
*
* @package Cake.Test.Case.Utility
*/
class RegistryPluginAppModel extends ClassRegisterModel {
/**
* tablePrefix property
*
* @var string
*/
public $tablePrefix = 'something_';
}
/**
* TestRegistryPluginModel class
*
* @package Cake.Test.Case.Utility
*/
class TestRegistryPluginModel extends RegistryPluginAppModel {
}
/**
* RegisterCategory class
*
* @package Cake.Test.Case.Utility
*/
class RegisterCategory extends ClassRegisterModel {
}
/**
2011-12-12 04:20:19 +00:00
* RegisterPrefixedDs class
*
* @package Cake.Test.Case.Utility
*/
class RegisterPrefixedDs extends ClassRegisterModel {
/**
* useDbConfig property
*
* @var string
2011-12-12 04:20:19 +00:00
*/
public $useDbConfig = 'doesnotexist';
}
/**
* Abstract class for testing ClassRegistry.
*/
abstract class ClassRegistryAbstractModel extends ClassRegisterModel {
public abstract function doSomething();
}
/**
* Interface for testing ClassRegistry
*/
interface ClassRegistryInterfaceTest {
2012-02-17 07:13:12 +00:00
public function doSomething();
}
/**
* ClassRegistryTest class
*
* @package Cake.Test.Case.Utility
*/
class ClassRegistryTest extends CakeTestCase {
/**
* testAddModel method
*
* @return void
*/
public function testAddModel() {
2010-07-06 02:19:22 +00:00
$Tag = ClassRegistry::init('RegisterArticleTag');
$this->assertInstanceOf('RegisterArticleTag', $Tag);
$TagCopy = ClassRegistry::isKeySet('RegisterArticleTag');
$this->assertTrue($TagCopy);
$Tag->name = 'SomeNewName';
2010-06-11 01:57:53 +00:00
$TagCopy = ClassRegistry::getObject('RegisterArticleTag');
$this->assertInstanceOf('RegisterArticleTag', $TagCopy);
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->assertInstanceOf('RegisterArticleTag', $NewTag);
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->assertInstanceOf('AppModel', $User);
2010-06-11 01:57:53 +00:00
$UserCopy = ClassRegistry::init(array('class' => 'RegisterUser', 'alias' => 'User', 'table' => false));
$this->assertInstanceOf('AppModel', $UserCopy);
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'));
2013-09-25 02:12:51 +00:00
$this->assertInstanceOf('RegisterCategory', $Category);
2010-06-11 01:57:53 +00:00
$ParentCategory = ClassRegistry::init(array('class' => 'RegisterCategory', 'alias' => 'ParentCategory'));
$this->assertInstanceOf('RegisterCategory', $ParentCategory);
2010-06-11 01:57:53 +00:00
$this->assertNotSame($Category, $ParentCategory);
$this->assertNotEquals($Category->alias, $ParentCategory->alias);
$this->assertEquals('RegisterCategory', $Category->alias);
$this->assertEquals('ParentCategory', $ParentCategory->alias);
}
/**
* Test that init() can make models with alias set properly
*
* @return void
*/
2016-11-22 01:51:12 +00:00
public function testAddModelWithAlias() {
$tag = ClassRegistry::init(array('class' => 'RegisterArticleTag', 'alias' => 'NewTag'));
$this->assertInstanceOf('RegisterArticleTag', $tag);
$this->assertSame('NewTag', $tag->alias);
$this->assertSame('RegisterArticleTag', $tag->name);
$newTag = ClassRegistry::init(array('class' => 'RegisterArticleTag', 'alias' => 'OtherTag'));
$this->assertInstanceOf('RegisterArticleTag', $tag);
$this->assertSame('OtherTag', $newTag->alias);
$this->assertSame('RegisterArticleTag', $newTag->name);
}
/**
* Test that init() can make the Aco models with alias set properly
*
* @return void
*/
2016-11-22 01:51:12 +00:00
public function testAddModelWithAliasAco() {
$aco = ClassRegistry::init(array('class' => 'Aco', 'alias' => 'CustomAco'));
$this->assertInstanceOf('Aco', $aco);
$this->assertSame('Aco', $aco->name);
$this->assertSame('CustomAco', $aco->alias);
}
/**
* testClassRegistryFlush method
*
* @return void
*/
public function testClassRegistryFlush() {
2013-01-23 12:45:50 +00:00
ClassRegistry::init('RegisterArticleTag');
$ArticleTag = ClassRegistry::getObject('RegisterArticleTag');
$this->assertInstanceOf('RegisterArticleTag', $ArticleTag);
ClassRegistry::flush();
$NoArticleTag = ClassRegistry::isKeySet('RegisterArticleTag');
$this->assertFalse($NoArticleTag);
2013-09-25 02:12:51 +00:00
$this->assertInstanceOf('RegisterArticleTag', $ArticleTag);
}
/**
* testAddMultipleModels method
*
* @return void
*/
public 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->assertInstanceOf('RegisterArticle', $Article);
$Featured = ClassRegistry::getObject('Featured');
$this->assertInstanceOf('RegisterArticleFeatured', $Featured);
$Tag = ClassRegistry::getObject('Tag');
$this->assertInstanceOf('RegisterArticleTag', $Tag);
}
/**
* testPluginAppModel method
*
* @return void
*/
public function testPluginAppModel() {
$TestRegistryPluginModel = ClassRegistry::isKeySet('TestRegistryPluginModel');
$this->assertFalse($TestRegistryPluginModel);
//Faking a plugin
CakePlugin::load('RegistryPlugin', array('path' => '/fake/path'));
$TestRegistryPluginModel = ClassRegistry::init('RegistryPlugin.TestRegistryPluginModel');
$this->assertInstanceOf('TestRegistryPluginModel', $TestRegistryPluginModel);
$this->assertEquals('something_', $TestRegistryPluginModel->tablePrefix);
2010-06-11 01:57:53 +00:00
$PluginUser = ClassRegistry::init(array('class' => 'RegistryPlugin.RegisterUser', 'alias' => 'RegistryPluginUser', 'table' => false));
$this->assertInstanceOf('RegistryPluginAppModel', $PluginUser);
2010-06-11 01:57:53 +00:00
$PluginUserCopy = ClassRegistry::getObject('RegistryPluginUser');
$this->assertInstanceOf('RegistryPluginAppModel', $PluginUserCopy);
2010-06-11 01:57:53 +00:00
$this->assertSame($PluginUser, $PluginUserCopy);
2011-05-09 03:55:32 +00:00
CakePlugin::unload();
}
/**
* Tests prefixed datasource names for test purposes
*
2014-04-02 01:02:37 +00:00
* @return void
*/
public function testPrefixedTestDatasource() {
2011-12-12 04:20:19 +00:00
ClassRegistry::config(array('testing' => true));
$Model = ClassRegistry::init('RegisterPrefixedDs');
2011-12-12 04:20:19 +00:00
$this->assertEquals('test', $Model->useDbConfig);
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);
$Model = ClassRegistry::init('RegisterPrefixedDs');
2011-12-12 04:20:19 +00:00
$this->assertEquals('test_doesnotexist', $Model->useDbConfig);
}
/**
* Tests that passing the string parameter to init() will return false if the model does not exists
*
2014-04-02 01:02:37 +00:00
* @return void
*/
public function testInitStrict() {
$this->assertFalse(ClassRegistry::init('NonExistent', true));
}
/**
* Test that you cannot init() an abstract class. An exception will be raised.
*
* @expectedException CakeException
* @return void
*/
public function testInitAbstractClass() {
ClassRegistry::init('ClassRegistryAbstractModel');
}
/**
* Test that you cannot init() an abstract class. A exception will be raised.
*
* @expectedException CakeException
* @return void
*/
public function testInitInterface() {
ClassRegistry::init('ClassRegistryInterfaceTest');
}
}