<?php
/**
* Test cases for the Cakephp mongoDB datasource.
*
* This datasource uses Pecl Mongo (http://php.net/mongo)
* and is thus dependent on PHP 5.0 and greater.
*
* Copyright 2010, Yasushi Ichikawa http://github.com/ichikaway/
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) 2010, Yasushi Ichikawa http://github.com/ichikaway/
* @package mongodb
* @subpackage mongodb.tests.cases.datasources
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Import relevant classes for testing
*/
App::import('Model', 'Mongodb.MongodbSource');
/**
* Generate Mock Model
*/
Mock::generate('AppModel', 'MockPost');
/**
* Post Model for the test
*
* @package app
* @subpackage app.model.post
*/
class Post extends AppModel {
public $useDbConfig = 'mongo_test';
/**
* mongoSchema property
*
* @public array
* @access public
*/
public $primaryKey='_id';
public $validate = array(
'uniquefield1' => array(
'rule' => 'isUnique',
'required' => false
),
'uniquefield2' => array(
'rule' => 'manualUniqueValidation',
'required' => false
),
);
public $mongoSchema = array(
'title' => array('type' => 'string'),
'body' => array('type' => 'string'),
'text' => array('type' => 'text'),
'uniquefield1' => array('type' => 'text'),
'uniquefield2' => array('type' => 'text'),
'count' => array('type' => 'integer'),
'created' => array('type' => 'datetime'),
'modified' => array('type' => 'datetime'),
);
function manualUniqueValidation($check) {
$c = $this->find('count', array(
'conditions' => array(
'uniquefield2' => $check['uniquefield2']
)
));
if ($c === 0) {
return true;
}
return false;
}
}
/**
* MongoArticle class
*
* @uses AppModel
* @package mongodb
* @subpackage mongodb.tests.cases.datasources
*/
class MongoArticle extends AppModel {
public $useDbConfig = 'mongo_test';
}
/**
* MongoDB Source test class
*
* @package app
* @subpackage app.model.datasources
*/
class MongodbSourceTest extends CakeTestCase {
/**
* Database Instance
*
* @var resource
* @access public
*/
public $mongodb;
/**
* Base Config
*
* @var array
* @access public
*
*/
protected $_config = array(
'datasource' => 'mongodb',
'host' => 'localhost',
'login' => '',
'password' => '',
'database' => 'test_mongo',
'port' => 27017,
'prefix' => '',
'persistent' => true,
);
/**
* Sets up the environment for each test method
*
* @return void
* @access public
*/
public function startTest() {
$connections = ConnectionManager::enumConnectionObjects();
if (!empty($connections['test']['classname']) && $connections['test']['classname'] === 'mongodbSource') {
$config = new DATABASE_CONFIG();
$this->_config = $config->test;
}
ConnectionManager::create('mongo_test', $this->_config);
$this->Mongo = new MongodbSource($this->_config);
$this->Post = ClassRegistry::init('Post');
$this->Post->setDataSource('mongo_test');
$this->mongodb =& ConnectionManager::getDataSource($this->Post->useDbConfig);
$this->mongodb->connect();
$this->dropData();
}
/**
* Destroys the environment after each test method is run
*
* @return void
* @access public
*/
public function endTest() {
$this->dropData();
unset($this->Post);
unset($this->Mongo);
unset($this->mongodb);
ClassRegistry::flush();
}
/**
* get Mongod server version
*
* @return numeric
* @access public
*/
public function getMongodVersion() {
$mongo = $this->Post->getDataSource();
return $mongo->execute('db.version()');
}
/**
* Insert data method for mongodb.
*
* @param array insert data
* @return void
* @access public
*/
public function insertData($data) {
try {
$this->mongodb
->connection
->selectDB($this->_config['database'])
->selectCollection($this->Post->table)
->insert($data, true);
} catch (MongoException $e) {
trigger_error($e->getMessage());
}
}
/**
* Drop database
*
* @return void
* @access public
*/
public function dropData() {
try {
$db = $this->mongodb
->connection
->selectDB($this->_config['database']);
foreach($db->listCollections() as $collection) {
$collection->drop();
}
} catch (MongoException $e) {
trigger_error($e->getMessage());
}
}
/**
* testCreateConnectionName
*
* @return void
* @access public
*/
public function testCreateConnectionName() {
$config = array(
'datasource' => 'mongodb',
'host' => 'localhost',
'login' => '',
'password' => '',
'database' => 'test_mongo',
'port' => 27017,
'prefix' => '',
'persistent' => false,
);
$version = '1.2.2';
$expect = 'mongodb://localhost:27017';
$host = $this->mongodb->createConnectionName($config, $version);
$this->assertIdentical($expect, $host);
$config = array(
'datasource' => 'mongodb',
'host' => 'localhost',
'login' => 'user',
'password' => 'pass',
'database' => 'test_mongo',
'port' => 27017,
'prefix' => '',
'persistent' => false,
);
$version = '1.2.2';
$expect = 'mongodb://user:pass@localhost:27017/test_mongo';
$host = $this->mongodb->createConnectionName($config, $version);
$this->assertIdentical($expect, $host);
$config = array(
'datasource' => 'mongodb',
'host' => 'localhost',
'login' => 'user',
'password' => 'pass',
'database' => 'test_mongo',
'port' => 27017,
'prefix' => '',
'persistent' => false,
);
$version = '1.0.0';
$expect = 'user:pass@localhost:27017/test_mongo';
$host = $this->mongodb->createConnectionName($config, $version);
$this->assertIdentical($expect, $host);
}
/**
* Tests connection
*
* @return void
* @access public
*/
public function testConnect() {
$result = $this->Mongo->connect();
$this->assertTrue($result);
$this->assertTrue($this->Mongo->connected);
$this->assertTrue($this->Mongo->isConnected());
}
/**
* Tests the disconnect method of the Mongodb DataSource
*
* @return void
* @access public
*/
public function testDisconnect() {
$result = $this->Mongo->disconnect();
$this->assertTrue($result);
$this->assertFalse($this->Mongo->connected);
}
/**
* Tests the listSources method of the Mongodb DataSource
*
* @return void
* @access public
*/
public function testListSources() {
$this->assertTrue(is_array($this->mongodb->listSources()));
}
/**
* Tests the getMongoDb method of the Mongodb DataSource
*
* @return void
* @access public
*/
public function testGetMongoDb() {
$obj = $this->mongodb->getMongoDb();
$this->assertTrue(is_object($obj));
$objName = get_class($obj);
$this->assertEqual('MongoDB', $objName);
}
/**
* Tests the Model::getMongoDb() call MongodbSource::getMongoDb
*
* @return void
* @access public
*/
public function testGetMongoDbFromModel() {
$obj = $this->Post->getMongoDb();
$this->assertTrue(is_object($obj));
$objName = get_class($obj);
$this->assertEqual('MongoDB', $objName);
}
/**
* Tests the getMongoCollection method of the Mongodb DataSource
*
* @return void
* @access public
*/
public function testGetMongoCollection() {
$obj = $this->mongodb->getMongoCollection($this->Post);
$this->assertTrue(is_object($obj));
$objName = get_class($obj);
$this->assertEqual('MongoCollection', $objName);
}
/**
* Tests the describe method of the Mongodb DataSource
*
* @return void
* @access public
*/
public function testDescribe() {
$mockObj = new MockPost();
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
mongodb.rar (63个子文件)
mongodb
models
behaviors
sql_compatible.php 7KB
schemaless.php 2KB
datasources
mongodb_source.php 36KB
README.markdown 2KB
samples
views
geos
add.ctp 794B
index.ctp 857B
subdocuments
add.ctp 1001B
index.ctp 665B
edit.ctp 736B
posts
add.ctp 481B
index.ctp 492B
edit.ctp 518B
createindex.ctp 76B
models
post.php 315B
subdocument.php 358B
geo.php 875B
controllers
geos_controller.php 2KB
subdocuments_controller.php 2KB
posts_controller.php 3KB
.git
logs
HEAD 188B
refs
heads
master 188B
packed-refs 751B
info
exclude 240B
index 4KB
objects
pack
pack-52a0731a4c0eed560c2767b1f46e52754660b6ac.idx 51KB
pack-52a0731a4c0eed560c2767b1f46e52754660b6ac.pack 251KB
info
HEAD 23B
config 336B
refs
heads
master 41B
tags
remotes
origin
HEAD 32B
hooks
pre-applypatch.sample 398B
post-commit.sample 160B
commit-msg.sample 896B
pre-commit.sample 2KB
applypatch-msg.sample 452B
post-receive.sample 552B
prepare-commit-msg.sample 1KB
update.sample 4KB
post-update.sample 189B
pre-rebase.sample 5KB
description 73B
docs
elementindex_mongodb.html 32KB
index.html 975B
elementindex.html 32KB
mongodb
mongodb-models-behaviors
_behaviors---sql_compatible.php.html 3KB
SqlCompatibleBehavior.html 17KB
_behaviors---schemaless.php.html 3KB
SchemalessBehavior.html 10KB
mongodb-models-datasources
_datasources---mongodb_source.php.html 4KB
MongodbSource.html 75KB
classtrees_mongodb.html 1KB
media
banner.css 417B
stylesheet.css 5KB
li_mongodb.html 3KB
packages.html 935B
errors.html 1KB
blank.html 428B
__filesource
fsource_mongodb_mongodb-models-behaviors_behaviorsschemaless.php.html 15KB
fsource_mongodb_mongodb-models-behaviors_behaviorssql_compatible.php.html 69KB
tests
cases
behaviors
sql_compatible.test.php 9KB
1st
first_mongodb_source.test.php 4KB
datasources
mongodb_source.test.php 44KB
.gitignore 7B
共 63 条
- 1
资源评论
- cruxqixin2013-04-18感谢!这个挺有帮助的
xuyanlu
- 粉丝: 172
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功