DictionaryTest.php 5.36 KB
Newer Older
1 2
<?php

Qiang Xue committed
3 4
namespace yiiunit\framework\base;

w  
Qiang Xue committed
5 6
use yii\base\Dictionary;

7 8 9 10 11
class MapItem
{
	public $data='data';
}

Qiang Xue committed
12
class DictionaryTest extends \yiiunit\TestCase
13
{
Qiang Xue committed
14 15 16
	/**
	 * @var \yii\base\Dictionary
	 */
17 18 19 20 21
	protected $dictionary;
	protected $item1,$item2,$item3;

	public function setUp()
	{
w  
Qiang Xue committed
22
		$this->dictionary=new Dictionary;
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
		$this->item1=new MapItem;
		$this->item2=new MapItem;
		$this->item3=new MapItem;
		$this->dictionary->add('key1',$this->item1);
		$this->dictionary->add('key2',$this->item2);
	}

	public function tearDown()
	{
		$this->dictionary=null;
		$this->item1=null;
		$this->item2=null;
		$this->item3=null;
	}

	public function testConstruct()
	{
		$a=array(1,2,'key3'=>3);
w  
Qiang Xue committed
41
		$dictionary=new Dictionary($a);
42
		$this->assertEquals(3,$dictionary->getCount());
w  
Qiang Xue committed
43
		$dictionary2=new Dictionary($this->dictionary);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		$this->assertEquals(2,$dictionary2->getCount());
	}

	public function testGetCount()
	{
		$this->assertEquals(2,$this->dictionary->getCount());
	}

	public function testGetKeys()
	{
		$keys=$this->dictionary->getKeys();
		$this->assertEquals(2,count($keys));
		$this->assertEquals('key1',$keys[0]);
		$this->assertEquals('key2',$keys[1]);
	}

	public function testAdd()
	{
		$this->dictionary->add('key3',$this->item3);
		$this->assertEquals(3,$this->dictionary->getCount());
		$this->assertTrue($this->dictionary->contains('key3'));
	}

	public function testRemove()
	{
		$this->dictionary->remove('key1');
		$this->assertEquals(1,$this->dictionary->getCount());
		$this->assertTrue(!$this->dictionary->contains('key1'));
		$this->assertTrue($this->dictionary->remove('unknown key')===null);
	}

	public function testClear()
	{
		$this->dictionary->clear();
		$this->assertEquals(0,$this->dictionary->getCount());
		$this->assertTrue(!$this->dictionary->contains('key1') && !$this->dictionary->contains('key2'));
	}

	public function testContains()
	{
		$this->assertTrue($this->dictionary->contains('key1'));
		$this->assertTrue($this->dictionary->contains('key2'));
		$this->assertFalse($this->dictionary->contains('key3'));
	}

Qiang Xue committed
89
	public function testFromArray()
90 91
	{
		$array=array('key3'=>$this->item3,'key4'=>$this->item1);
Qiang Xue committed
92
		$this->dictionary->copyFrom($array);
93 94 95 96 97

		$this->assertEquals(2, $this->dictionary->getCount());
		$this->assertEquals($this->item3, $this->dictionary['key3']);
		$this->assertEquals($this->item1, $this->dictionary['key4']);

Qiang Xue committed
98
		$this->setExpectedException('yii\base\InvalidCallException');
Qiang Xue committed
99
		$this->dictionary->copyFrom($this);
100 101 102 103 104 105 106
	}

	public function testMergeWith()
	{
		$a=array('a'=>'v1','v2',array('2'),'c'=>array('3','c'=>'a'));
		$b=array('v22','a'=>'v11',array('2'),'c'=>array('c'=>'3','a'));
		$c=array('a'=>'v11','v2',array('2'),'c'=>array('3','c'=>'3','a'),'v22',array('2'));
w  
Qiang Xue committed
107 108
		$dictionary=new Dictionary($a);
		$dictionary2=new Dictionary($b);
109 110 111 112 113 114 115 116
		$dictionary->mergeWith($dictionary2);
		$this->assertTrue($dictionary->toArray()===$c);

		$array=array('key2'=>$this->item1,'key3'=>$this->item3);
		$this->dictionary->mergeWith($array,false);
		$this->assertEquals(3,$this->dictionary->getCount());
		$this->assertEquals($this->item1,$this->dictionary['key2']);
		$this->assertEquals($this->item3,$this->dictionary['key3']);
Qiang Xue committed
117
		$this->setExpectedException('yii\base\InvalidCallException');
118 119 120 121
		$this->dictionary->mergeWith($this,false);
	}

	public function testRecursiveMergeWithTraversable(){
w  
Qiang Xue committed
122
		$dictionary = new Dictionary();
Qiang Xue committed
123
		$obj = new \ArrayObject(array(
124 125
			'k1' => $this->item1,
			'k2' => $this->item2,
Qiang Xue committed
126
			'k3' => new \ArrayObject(array(
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
				'k4' => $this->item3,
			))
		));
		$dictionary->mergeWith($obj,true);

		$this->assertEquals(3, $dictionary->getCount());
		$this->assertEquals($this->item1, $dictionary['k1']);
		$this->assertEquals($this->item2, $dictionary['k2']);
		$this->assertEquals($this->item3, $dictionary['k3']['k4']);
	}

	public function testArrayRead()
	{
		$this->assertEquals($this->item1,$this->dictionary['key1']);
		$this->assertEquals($this->item2,$this->dictionary['key2']);
		$this->assertEquals(null,$this->dictionary['key3']);
	}

	public function testArrayWrite()
	{
		$this->dictionary['key3']=$this->item3;
		$this->assertEquals(3,$this->dictionary->getCount());
		$this->assertEquals($this->item3,$this->dictionary['key3']);

		$this->dictionary['key1']=$this->item3;
		$this->assertEquals(3,$this->dictionary->getCount());
		$this->assertEquals($this->item3,$this->dictionary['key1']);

		unset($this->dictionary['key2']);
		$this->assertEquals(2,$this->dictionary->getCount());
		$this->assertTrue(!$this->dictionary->contains('key2'));

		unset($this->dictionary['unknown key']);
	}

	public function testArrayForeach()
	{
		$n=0;
		$found=0;
		foreach($this->dictionary as $index=>$item)
		{
			$n++;
			if($index==='key1' && $item===$this->item1)
				$found++;
			if($index==='key2' && $item===$this->item2)
				$found++;
		}
		$this->assertTrue($n==2 && $found==2);
	}

	public function testArrayMisc()
	{
		$this->assertEquals($this->dictionary->Count,count($this->dictionary));
		$this->assertTrue(isset($this->dictionary['key1']));
		$this->assertFalse(isset($this->dictionary['unknown key']));
	}

	public function testToArray()
	{
w  
Qiang Xue committed
186
		$dictionary = new Dictionary(array('key' => 'value'));
187 188 189 190 191
		$this->assertEquals(array('key' => 'value'), $dictionary->toArray());
	}

	public function testIteratorCurrent()
	{
w  
Qiang Xue committed
192
		$dictionary = new Dictionary(array('key1' => 'value1', 'key2' => 'value2'));
193 194 195 196
		$val = $dictionary->getIterator()->current();
		$this->assertEquals('value1', $val);
	}
}