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

198 lines
131 KiB
PHP
Raw Normal View History

<?php
/**
2009-03-19 21:10:13 +00:00
* MagicDbTest file
*
* PHP 5
*
2009-11-06 06:46:59 +00:00
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
2010-01-26 19:18:20 +00:00
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT 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)
2009-11-06 06:00:11 +00:00
* @link http://cakephp.org CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP(tm) v 1.2.0
2009-11-06 06:51:51 +00:00
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
if (!class_exists('MagicDb')) {
require LIBS . 'magic_db.php';
}
/**
2009-03-19 21:10:13 +00:00
* The test class for the MagicDb class
*
2009-03-19 21:10:13 +00:00
* @package cake
* @subpackage cake.tests.cases.libs
*/
class MagicDbTest extends UnitTestCase {
/**
* The MagicDb instance to be tested
*
2009-03-19 21:10:13 +00:00
* @var MagicDb
* @access public
*/
public $Db = null;
/**
* Sets up a MagicDb class instance for testing
*
*/
public function setUp() {
$this->Db = new MagicDb();
}
/**
* MagicDb::analyze should properly detect the file type and output additional info as requested.
*
*/
public function testAnalyze() {
$r = $this->Db->read(MagicDbTestData::get('magic.db'));
$this->assertTrue($r === true);
$r = $this->Db->analyze(array());
$this->assertTrue($r === false);
$r = $this->Db->analyze(WWW_ROOT.'img'.DS.'cake.icon.gif');
// TODO: Check several serialized file samples for accurate detection
}
/**
* MagicDb::read should properly read MagicDb databases from .php-/.db-files and plain data arguments passed in and return false if the file wasn't found or
* if the readed data did not validate.
*
*/
public function testRead() {
$this->Db->db = array();
$r = $this->Db->read(true);
$this->assertTrue($r === false);
$r = $this->Db->read(5);
$this->assertTrue($r === false);
$this->Db->db = array('a');
$r = $this->Db->read(array('foo' => 'bar'));
$this->assertTrue($r === false);
$this->assertTrue($this->Db->db === array('a'));
$magicDb = array('header' => array(), 'database' => array());
$r = $this->Db->read($magicDb);
$this->assertTrue($r === true);
$this->assertTrue($this->Db->db === $magicDb);
// @TODO: Test parsing an actual magic.db file
$r = $this->Db->read('does-not-exist.db');
$this->assertTrue($r === false);
$this->assertTrue($this->Db->db === $magicDb);
if (file_exists(VENDORS.'magic.php')) {
$r = $this->Db->read(VENDORS.'magic.php');
$this->assertTrue($r === true);
$this->assertTrue($this->Db->db === array('header' => array(), 'database' => array()));
}
$r = $this->Db->read(MagicDbTestData::get('magic.snippet.db'));
$this->assertTrue($r === true);
}
/**
* MagicDb::toArray should either return the MagicDb::db property, or the parsed array data if a magic.db dump is passed in as the first argument
*
*/
public function testToArray() {
$this->Db->db = array();
$r = $this->Db->toArray();
$this->assertTrue($r === array());
$this->Db->db = array('foo' => 'bar');
$r = $this->Db->toArray();
$this->assertTrue($r === array('foo' => 'bar'));
$r = $this->Db->toArray(array('yeah'));
$this->assertTrue($r === array('yeah'));
$r = $this->Db->toArray("# FILE_ID DB\r\n# Date:2009-10-10\r\n# Source:xxx.php");
$this->assertTrue($r === array());
$r = $this->Db->toArray('foo');
$this->assertTrue($r === array());
$r = $this->Db->toArray(MagicDbTestData::get('magic.snippet.db'));
$this->assertTrue($r === MagicDbTestData::get('magic.snippet.db.result'));
}
/**
* The MagicDb::validates function should return if the array passed to it or the local db property contains a valid MagicDb record set
*
*/
public function testValidates() {
$r = $this->Db->validates(array());
$this->assertTrue($r === false);
$r = $this->Db->validates(array('header' => true, 'database' => true));
$this->assertTrue($r === false);
$magicDb = array('header' => array(), 'database' => array());
$r = $this->Db->validates($magicDb);
$this->assertTrue($r === true);
$this->Db->db = array();
$r = $this->Db->validates();
$this->assertTrue($r === false);
$this->Db->db = $magicDb;
$r = $this->Db->validates();
$this->assertTrue($r === true);
}
}
/**
* Test data holding object for MagicDb tests
*
2009-03-19 21:10:13 +00:00
* @package cake
* @subpackage cake.tests.cases.libs
*/
/**
* MagicDbTestData class
*
* @package cake
* @subpackage cake.tests.cases.libs
*/
class MagicDbTestData extends Object {
/**
* Base64 encoded data
*
* @var array
* @access public
*/
public $data = array(
'magic.snippet.db' => 'IyBGSUxFX0lEIERCDQojIERhdGU6MjAwNS0wMy0yOQ0KIyBTb3VyY2U6aHR0cDovL3d3dy5tYWdpY2RiLm9yZw0KDQojIE1hZ2ljIElEIGZvciBXb3JkcGVyZmVjdCBmaWxlcy4NCiMgU3VibWl0dGVkIG9uIDIwMDQtMDItMTQgYnkgQ2FybA0KMAlzdHJpbmcJXFx4RkZXUEMJW2ZpZD0wMDAwMDEwMDgtMDAtMDAwMDAwMTtleHQ9O21pbWU9O11Xb3JkcGVyZmVjdCBoZWxwIGZpbGUNCiY5CWJ5dGUJMHgwMgkNCj4xMCBieXRlCXgJLCB2ZXJzaW9uICVkDQo+MTEJYnl0ZQl4CS4lZA0KDQojIE1hZ2ljIElEIGZvciBXb3JkcGVyZmVjdCBmaWxlcy4NCiMgU3VibWl0dGVkIG9uIDIwMDQtMDItMTQgYnkgQ2FybA0KMAlzdHJpbmcJXFx4RkZXUEMJW2ZpZD0wMDAwMDEwMDgtMDAtMDAwMDAwMTtleHQ9O21pbWU9O11Xb3JkcGVyZmVjdCBhcHBsaWNhdGlvbiByZXNvdXJjZSBsaWJyYXJ5DQomOQlieXRlCTUxCQ0KPjEwCWJ5dGUJeAksIHZlcnNpb24gJWQNCj4xMQlieXRlCXgJLiVkDQoNCiMgTWFnaWMgSUQgZm9yIFdvcmRwZXJmZWN0IGZpbGVzLg0KIyBTdWJtaXR0ZWQgb24gMjAwNC0wMi0xNCBieSBDYXJsDQowCXN0cmluZwlcXHhGRldQQwlbZmlkPTAwMDAwMTAwOC0wMC0wMDAwMDAxO2V4dD07bWltZT07XVdvcmRwZXJmZWN0IGJsb2NrIGZpbGUNCiY5CWJ5dGUJMTMJDQo+MTAJYnl0ZQl4CSwgdm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ns the test data for a given key
*
* @param string $key
*/
public function get($key) {
/**
* data property
*
* @var array
* @access public
*/
static $data = array();
if (empty($data)) {
$vars = get_class_vars(__CLASS__);
foreach ($vars['data'] as $k => $v) {
$data[$k] = base64_decode($v);
if (preg_match('/^[ais]:[0-9]+/', $data[$k])) {
$data[$k] = unserialize($data[$k]);
}
}
}
if (!isset($data[$key])) {
return false;
}
return $data[$key];
}
}