set.test.php

来自「Cake Framwork , Excellent」· PHP 代码 · 共 1,605 行 · 第 1/5 页

PHP
1,605
字号
<?php/* SVN FILE: $Id: set.test.php 7118 2008-06-04 20:49:29Z gwoo $ *//** * Short description for file. * * Long description for file * * PHP versions 4 and 5 * * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite> * Copyright 2005-2008, Cake Software Foundation, Inc. *								1785 E. Sahara Avenue, Suite 490-204 *								Las Vegas, Nevada 89104 * *  Licensed under The Open Group Test Suite License *  Redistributions of files must retain the above copyright notice. * * @filesource * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc. * @link				https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests * @package			cake.tests * @subpackage		cake.tests.cases.libs * @since			CakePHP(tm) v 1.2.0.4206 * @version			$Revision: 7118 $ * @modifiedby		$LastChangedBy: gwoo $ * @lastmodified	$Date: 2008-06-04 13:49:29 -0700 (Wed, 04 Jun 2008) $ * @license			http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */App::import('Core', 'Set');/** * UnitTestCase for the Set class * * @package		cake.tests * @subpackage	cake.tests.cases.libs */class SetTest extends UnitTestCase {/** * testNumericKeyExtraction method *  * @access public * @return void */	function testNumericKeyExtraction() {		$data = array('plugin' => null, 'controller' => '', 'action' => '', 1, 'whatever');		$this->assertIdentical(Set::extract($data, '{n}'), array(1, 'whatever'));		$this->assertIdentical(Set::diff($data, Set::extract($data, '{n}')), array('plugin' => null, 'controller' => '', 'action' => ''));	}/** * testEnum method *  * @access public * @return void */	function testEnum() {		$result = Set::enum(1, 'one, two');		$this->assertIdentical($result, 'two');		$result = Set::enum(2, 'one, two');		$this->assertNull($result);				$result = Set::enum(1, array('one', 'two'));		$this->assertIdentical($result, 'two');		$result = Set::enum(2, array('one', 'two'));		$this->assertNull($result);				$result = Set::enum('first', array('first' => 'one', 'second' => 'two'));		$this->assertIdentical($result, 'one');		$result = Set::enum('third', array('first' => 'one', 'second' => 'two'));		$this->assertNull($result);				$result = Set::enum('no', array('no' => 0, 'yes' => 1));		$this->assertIdentical($result, 0);		$result = Set::enum('not sure', array('no' => 0, 'yes' => 1));		$this->assertNull($result);	}/** * testFilter method *  * @access public * @return void */	function testFilter() {		$result = Set::filter(array('0', false, true, 0, array('one thing', 'I can tell you', 'is you got to be', false)));		$expected = array('0', 2 => true, 3 => 0, 4 => array('one thing', 'I can tell you', 'is you got to be', false));		$this->assertIdentical($result, $expected);	}/** * testNumericArrayCheck method *  * @access public * @return void */	function testNumericArrayCheck() {		$data = array('one', 'two', 'three', 'four', 'five');		$this->assertTrue(Set::numeric(array_keys($data)));		$data = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');		$this->assertTrue(Set::numeric(array_keys($data)));		$data = array('1' => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');		$this->assertTrue(Set::numeric(array_keys($data)));		$data = array('one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five');		$this->assertFalse(Set::numeric(array_keys($data)));	}/** * testKeyCheck method *  * @access public * @return void */	function testKeyCheck() {		$data = array('Multi' => array('dimensonal' => array('array')));		$this->assertTrue(Set::check($data, 'Multi.dimensonal'));		$this->assertFalse(Set::check($data, 'Multi.dimensonal.array'));		$data = array(			array(				'Article' => array('id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),				'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),				'Comment' => array(					array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'),					array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),				),				'Tag' => array(					array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),					array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')				)			),			array(				'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),				'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),				'Comment' => array(),				'Tag' => array()			)		);		$this->assertTrue(Set::check($data, '0.Article.user_id'));		$this->assertTrue(Set::check($data, '0.Comment.0.id'));		$this->assertFalse(Set::check($data, '0.Comment.0.id.0'));		$this->assertTrue(Set::check($data, '0.Article.user_id'));		$this->assertFalse(Set::check($data, '0.Article.user_id.a'));	}/** * testMerge method *  * @access public * @return void */	function testMerge() {		$r = Set::merge(array('foo'));		$this->assertIdentical($r, array('foo'));		$r = Set::merge('foo');		$this->assertIdentical($r, array('foo'));		$r = Set::merge('foo', 'bar');		$this->assertIdentical($r, array('foo', 'bar'));				if (substr(phpversion(), 0, 1) >= 5) {			$r = eval('class StaticSetCaller{static function merge($a, $b){return Set::merge($a, $b);}} return StaticSetCaller::merge("foo", "bar");');			$this->assertIdentical($r, array('foo', 'bar'));		}				$r = Set::merge('foo', array('user' => 'bob', 'no-bar'), 'bar');		$this->assertIdentical($r, array('foo', 'user' => 'bob', 'no-bar', 'bar'));		$a = array('foo', 'foo2');		$b = array('bar', 'bar2');		$this->assertIdentical(Set::merge($a, $b), array('foo', 'foo2', 'bar', 'bar2'));		$a = array('foo' => 'bar', 'bar' => 'foo');		$b = array('foo' => 'no-bar', 'bar' => 'no-foo');		$this->assertIdentical(Set::merge($a, $b), array('foo' => 'no-bar', 'bar' => 'no-foo'));		$a = array('users' => array('bob', 'jim'));		$b = array('users' => array('lisa', 'tina'));		$this->assertIdentical(Set::merge($a, $b), array('users' => array('bob', 'jim', 'lisa', 'tina')));		$a = array('users' => array('jim', 'bob'));		$b = array('users' => 'none');		$this->assertIdentical(Set::merge($a, $b), array('users' => 'none'));		$a = array('users' => array('lisa' => array('id' => 5, 'pw' => 'secret')), 'cakephp');		$b = array('users' => array('lisa' => array('pw' => 'new-pass', 'age' => 23)), 'ice-cream');		$this->assertIdentical(Set::merge($a, $b), array('users' => array('lisa' => array('id' => 5, 'pw' => 'new-pass', 'age' => 23)), 'cakephp', 'ice-cream'));		$c = array('users' => array('lisa' => array('pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')), 'chocolate');		$expected = array('users' => array('lisa' => array('id' => 5, 'pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')), 'cakephp', 'ice-cream', 'chocolate');		$this->assertIdentical(Set::merge($a, $b, $c), $expected);		$this->assertIdentical(Set::merge($a, $b, array(), $c), $expected);		$Set =& new Set($a);		$r = $Set->merge($b, array(), $c);		$this->assertIdentical($r, $expected);		$this->assertIdentical($Set->value, $expected);		unset($Set);		$Set =& new Set();		$SetA =& new Set($a);		$SetB =& new Set($b);		$SetC =& new Set($c);		$r = $Set->merge($SetA, $SetB, $SetC);		$this->assertIdentical($r, $expected);		$this->assertIdentical($Set->value, $expected);		$a = array('Tree', 'CounterCache',				'Upload' => array('folder' => 'products',					'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')));		$b =  array('Cacheable' => array('enabled' => false),				'Limit',				'Bindable',				'Validator',				'Transactional');		$expected = array('Tree', 'CounterCache',				'Upload' => array('folder' => 'products',					'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')),				'Cacheable' => array('enabled' => false),				'Limit',				'Bindable',				'Validator',				'Transactional');		$this->assertIdentical(Set::merge($a, $b), $expected);		$expected = array('Tree' => null, 'CounterCache' => null,				'Upload' => array('folder' => 'products',					'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')),				'Cacheable' => array('enabled' => false),				'Limit' => null,				'Bindable' => null,				'Validator' => null,				'Transactional' => null);		$this->assertIdentical(Set::normalize(Set::merge($a, $b)), $expected);	}/** * testSort method *  * @access public * @return void */	function testSort() {		$a = array(			0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),			1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))		);		$b = array(			0 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),			1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))		);		$a = Set::sort($a, '{n}.Friend.{n}.name', 'asc');		$this->assertIdentical($a, $b);		$b = array(			0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),			1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))		);		$a = array(			0 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),			1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))		);		$a = Set::sort($a, '{n}.Friend.{n}.name', 'desc');		$this->assertIdentical($a, $b);		$a = array(			0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),			1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),			2 => array('Person' => array('name' => 'Adam'),'Friend' => array(array('name' => 'Bob')))		);		$b = array(			0 => array('Person' => array('name' => 'Adam'),'Friend' => array(array('name' => 'Bob'))),			1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),			2 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))		);		$a = Set::sort($a, '{n}.Person.name', 'asc');		$this->assertIdentical($a, $b);		$a = array(			array(7,6,4),			array(3,4,5),			array(3,2,1),		);		$b = array(			array(3,2,1),			array(3,4,5),			array(7,6,4),		);		$a = Set::sort($a, '{n}.{n}', 'asc');		$this->assertIdentical($a, $b);		$a = array(			array(7,6,4),			array(3,4,5),			array(3,2,array(1,1,1)),		);		$b = array(			array(3,2,array(1,1,1)),			array(3,4,5),			array(7,6,4),		);		$a = Set::sort($a, '{n}', 'asc');		$this->assertIdentical($a, $b);		$a = array(			0 => array('Person' => array('name' => 'Jeff')),			1 => array('Shirt' => array('color' => 'black'))		);		$b = array(			0 => array('Shirt' => array('color' => 'black')),			1 => array('Person' => array('name' => 'Jeff')),		);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?