123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- <?php
- /**
- * @CreateTime: 2019/9/9 下午11:28
- * @Author: huizhang <tuzisir@163.com>
- * @Copyright: copyright(2019) Easyswoole all rights reserved
- * @Description: SplArray 单元测试
- */
- namespace EasySwoole\Spl\Test;
- use PHPUnit\Framework\TestCase;
- use EasySwoole\Spl\SplArray;
- class ArrayTest extends TestCase {
- /**
- * 设置参数
- *
- * @return SplArray
- * CreateTime: 2019/9/10 下午11:30
- */
- public function testSet() {
- $data = [
- 'fruit' => [
- 'apple' => 2,
- 'orange' => 1,
- 'grape' => 4
- ],
- 'color' => [
- 'red' => 12,
- 'blue' => 8,
- 'green' => 6
- ]
- ];
- $splArrayObj = new SplArray($data);
- $splArrayObj->set('fruit.apple', 3);
- $this->assertEquals(3, $splArrayObj->get('fruit.apple'));
- return $splArrayObj;
- }
- /**
- * 获取参数
- *
- * @depends clone testSet
- * @param SplArray $splArrayObj
- */
- public function testGet( SplArray $splArrayObj) {
- // 测试第一层的key
- $this->assertEquals(
- [
- 'red' => 12,
- 'blue' => 8,
- 'green' => 6
- ],
- $splArrayObj->get('color')
- );
- // 测试第二层的key
- $this->assertEquals(
- 12,
- $splArrayObj->get('color.red')
- );
- }
- /**
- * 转字符
- *
- * @depends clone testSet
- * CreateTime: 2019/9/10 下午11:29
- */
- public function testTostring(SplArray $splArrayObj) {
- $this->assertJsonStringEqualsJsonString(
- json_encode($splArrayObj, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
- $splArrayObj->__toString()
- );
- }
- /**
- * 数组的复制
- *
- * @depends clone testSet
- * @param SplArray $splArrayObj
- * CreateTime: 2019/9/10 下午11:37
- */
- public function testGetArrayCopy(SplArray $splArrayObj) {
- $this->assertEquals(
- [
- 'fruit' => [
- 'apple' => 3,
- 'orange' => 1,
- 'grape' => 4
- ],
- 'color' => [
- 'red' => 12,
- 'blue' => 8,
- 'green' => 6
- ]
- ],
- $splArrayObj->getArrayCopy()
- );
- }
- /**
- * 销毁数组元素
- *
- * @depends clone testSet
- * @param $splArrayObj SplArray
- * CreateTime: 2019/9/10 下午11:44
- */
- public function testUnset(SplArray $splArrayObj) {
- // 销毁red元素
- $splArrayObj->unset('color.red');
- $this->assertEquals(
- [
- 'fruit' => [
- 'apple' => 3,
- 'orange' => 1,
- 'grape' => 4
- ],
- 'color' => [
- 'blue' => 8,
- 'green' => 6
- ]
- ],
- $splArrayObj->getArrayCopy()
- );
- // 销毁color元素
- $splArrayObj->unset('color');
- $this->assertEquals(
- [
- 'fruit' => [
- 'apple' => 3,
- 'orange' => 1,
- 'grape' => 4
- ]
- ],
- $splArrayObj->getArrayCopy()
- );
- }
- /**
- * 去除某个数据项(unset和delete方法其实是实现统一效果,因考虑旧版本用户使用情况,故而保留。)
- *
- * @depends clone testSet
- * @param SplArray $splArrayObj
- */
- public function testDelete(SplArray $splArrayObj) {
- $splArrayObj->delete('color');
- $this->assertEquals(
- [
- 'fruit' => [
- 'apple' => 3,
- 'orange' => 1,
- 'grape' => 4
- ]
- ],
- $splArrayObj->getArrayCopy()
- );
- }
- /**
- * 数组值唯一
- *
- * CreateTime: 2019/9/10 下午11:55
- * @param SplArray $splArrayObj
- * @return bool
- */
- public function testUnique() {
- $splArrayObj = new SplArray(
- [
- 'name1' => 'es',
- 'name2' => 'es'
- ]
- );
- $this->assertEquals(
- ['name1'=>'es']
- , $splArrayObj->unique()->getArrayCopy());
- }
- /**
- * 获取数组中重复的值
- *
- * @depends clone testSet
- * CreateTime: 2019/9/11 上午12:22
- * @return bool
- */
- public function testMultiple() {
- $splArrayObj = new SplArray(
- [
- 'name1' => 'es',
- 'name2' => 'es'
- ]
- );
- $this->assertEquals(['name2'=>'es'], $splArrayObj->multiple()->getArrayCopy());
- }
- /**
- * 进行排序并保持索引关系
- *
- * @depends clone testSet
- * CreateTime: 2019/9/11 上午12:32
- * @param SplArray $splArrayObj
- */
- public function testAsort(SplArray $splArrayObj) {
- $splArrayObj->asort();
- $this->assertEquals(
- [
- 'color' => [
- 'red' => 12,
- 'blue' => 8,
- 'green' => 6
- ],
- 'fruit' => [
- 'apple' => 3,
- 'orange' => 1,
- 'grape' => 4
- ]
- ]
- ,$splArrayObj->getArrayCopy()
- );
- }
- /**
- * 按照键名排序
- *
- * @depends clone testSet
- * CreateTime: 2019/9/11 上午12:35
- * @param SplArray $splArrayObj
- */
- public function testKsort(SplArray $splArrayObj) {
- $splArrayObj->ksort();
- $this->assertEquals(
- [
- 'color' => [
- 'red' => 12,
- 'blue' => 8,
- 'green' => 6
- ],
- 'fruit' => [
- 'apple' => 3,
- 'orange' => 1,
- 'grape' => 4
- ]
- ],
- $splArrayObj->getArrayCopy()
- );
- }
- /**
- * 排序
- *
- * @depends clone testSet
- * CreateTime: 2019/9/11 上午12:35
- * @param SplArray $splArrayObj
- */
- public function testSort(SplArray $splArrayObj) {
- $this->assertEquals(
- [
- [
- 'red' => 12,
- 'blue' => 8,
- 'green' => 6
- ],
- [
- 'apple' => 3,
- 'orange' => 1,
- 'grape' => 4
- ]
- ],
- $splArrayObj->sort()->getArrayCopy()
- );
- }
- /**
- * 取得某一列
- *
- * @depends clone testSet
- * CreateTime: 2019/9/11 上午12:35
- * @param SplArray $splArrayObj
- * @return bool
- */
- public function testColumn(SplArray $splArrayObj) {
- $this->assertEquals(
- [12],
- $splArrayObj->column('red')->getArrayCopy()
- );
- }
- /**
- * 交换数组中的键和值
- */
- public function testFlip() {
- $splArrayObj = new SplArray([
- 'es' => 'easyswoole'
- ]);
- $this->assertEquals(
- [
- 'easyswoole' => 'es'
- ],
- $splArrayObj->flip()->getArrayCopy()
- );
- }
- /**
- * 过滤数组数据
- */
- public function testFilter() {
- $splArrayObj = new SplArray(
- [
- 'apple' => 2,
- 'orange' => 1,
- 'grape' => 2,
- 'pear' => 4,
- 'banana' => 8
- ]
- );
- // 获取设置的键名
- $this->assertEquals(
- [
- 'apple' => 2,
- 'pear' => 4
- ],
- $splArrayObj->filter('apple,pear', false)->getArrayCopy()
- );
- // 排除设置的键名
- $this->assertEquals(
- [
- 'apple' => 2,
- 'pear' => 4
- ],
- $splArrayObj->filter('orange,grape,banana', true)->getArrayCopy()
- );
- }
- /**
- * 获取数组索引
- *
- * @depends clone testSet
- * @param SplArray $splArrayObj
- */
- public function testKeys(SplArray $splArrayObj) {
- $this->assertEquals(
- ['red', 'blue', 'green'],
- $splArrayObj->keys('color')
- );
- }
- /**
- * 获取数组中所有的值
- *
- * @depends clone testSet
- * @param SplArray $splArrayObj
- */
- public function testValues(SplArray $splArrayObj) {
- $this->assertEquals(
- [
- [
- 'apple' => 3,
- 'orange' => 1,
- 'grape' => 4
- ],
- [
- 'red' => 12,
- 'blue' => 8,
- 'green' => 6
- ]
- ],
- $splArrayObj->values()->getArrayCopy()
- );
- }
- /**
- * 清空数据
- *
- * @depends clone testSet
- * @param SplArray $splArrayObj
- */
- public function testFlush(SplArray $splArrayObj) {
- $this->assertEquals(
- [],
- $splArrayObj->flush()->getArrayCopy()
- );
- }
- /**
- * 重新加载数据
- *
- * @depends clone testSet
- * @param SplArray $splArrayObj
- */
- public function testLoadArray(SplArray $splArrayObj) {
- $this->assertEquals(
- [
- 'name' => 'easyswoole'
- ],
- $splArrayObj->loadArray(
- [
- 'name' => 'easyswoole'
- ]
- )->getArrayCopy()
- );
- }
- /**
- * 转化成xml
- *
- * @depends clone testSet
- * @param SplArray $splArrayObj
- */
- public function testToXML(SplArray $splArrayObj) {
- $this->assertEquals(
- "<xml><fruit><apple>3</apple><orange>1</orange><grape>4</grape></fruit><color><red>12</red><blue>8</blue><green>6</green></color></xml>\n",
- $splArrayObj->toXML()
- );
- }
- public function testMulti()
- {
- $splArray = new SplArray(
- [
- 'a'=>[
- "sum"=>'a1',
- [
- "sum"=>'s1',
- ],
- [
- "sum"=>'s2',
- ],
- ],
- 'b'=>'b',
- 'c'=>[
- "sum"=>'c1'
- ],
- ]
- );
- $this->assertEquals([
- null,'s1','s2'
- ],$splArray->get('a.*.sum'));
- $this->assertEquals([
- 'a1',null,'c1'
- ],$splArray->get('*.sum'));
- $this->assertEquals([
- 'a1',
- [
- "sum"=>'s1',
- ],
- [
- "sum"=>'s2',
- ],
- ],$splArray->get('a.*'));
- }
- }
|