ComponentTest.php 10.4 KB
Newer Older
w  
Qiang Xue committed
1
<?php
Qiang Xue committed
2 3
namespace yiiunit\framework\base;

4 5 6 7 8
use yii\base\Behavior;
use yii\base\Component;
use yii\base\Event;
use yiiunit\TestCase;

w  
Qiang Xue committed
9 10
function globalEventHandler($event)
{
Qiang Xue committed
11
	$event->sender->eventHandled = true;
w  
Qiang Xue committed
12 13 14 15
}

function globalEventHandler2($event)
{
Qiang Xue committed
16 17
	$event->sender->eventHandled = true;
	$event->handled = true;
w  
Qiang Xue committed
18 19
}

20
class ComponentTest extends TestCase
w  
Qiang Xue committed
21
{
Qiang Xue committed
22 23 24
	/**
	 * @var NewComponent
	 */
w  
Qiang Xue committed
25 26
	protected $component;

Carsten Brandt committed
27
	protected function setUp()
w  
Qiang Xue committed
28
	{
Carsten Brandt committed
29
		parent::setUp();
w  
Qiang Xue committed
30 31 32
		$this->component = new NewComponent();
	}

Carsten Brandt committed
33
	protected function tearDown()
w  
Qiang Xue committed
34
	{
Carsten Brandt committed
35
		parent::tearDown();
w  
Qiang Xue committed
36 37
		$this->component = null;
	}
38 39 40 41 42 43 44 45

	public function testClone()
	{
		$component = new NewComponent();
		$behavior = new NewBehavior();
		$component->attachBehavior('a', $behavior);
		$this->assertSame($behavior, $component->getBehavior('a'));
		$component->on('test', 'fake');
Qiang Xue committed
46
		$this->assertTrue($component->hasEventHandlers('test'));
47 48 49 50

		$clone = clone $component;
		$this->assertNotSame($component, $clone);
		$this->assertNull($clone->getBehavior('a'));
Qiang Xue committed
51
		$this->assertFalse($clone->hasEventHandlers('test'));
52
	}
Qiang Xue committed
53
	
w  
Qiang Xue committed
54 55
	public function testHasProperty()
	{
Qiang Xue committed
56 57 58 59 60 61
		$this->assertTrue($this->component->hasProperty('Text'));
		$this->assertTrue($this->component->hasProperty('text'));
		$this->assertFalse($this->component->hasProperty('Caption'));
		$this->assertTrue($this->component->hasProperty('content'));
		$this->assertFalse($this->component->hasProperty('content', false));
		$this->assertFalse($this->component->hasProperty('Content'));
w  
Qiang Xue committed
62 63 64 65 66 67 68
	}

	public function testCanGetProperty()
	{
		$this->assertTrue($this->component->canGetProperty('Text'));
		$this->assertTrue($this->component->canGetProperty('text'));
		$this->assertFalse($this->component->canGetProperty('Caption'));
Qiang Xue committed
69 70 71
		$this->assertTrue($this->component->canGetProperty('content'));
		$this->assertFalse($this->component->canGetProperty('content', false));
		$this->assertFalse($this->component->canGetProperty('Content'));
w  
Qiang Xue committed
72 73 74 75 76 77
	}

	public function testCanSetProperty()
	{
		$this->assertTrue($this->component->canSetProperty('Text'));
		$this->assertTrue($this->component->canSetProperty('text'));
Qiang Xue committed
78
		$this->assertFalse($this->component->canSetProperty('Object'));
w  
Qiang Xue committed
79
		$this->assertFalse($this->component->canSetProperty('Caption'));
Qiang Xue committed
80 81 82
		$this->assertTrue($this->component->canSetProperty('content'));
		$this->assertFalse($this->component->canSetProperty('content', false));
		$this->assertFalse($this->component->canSetProperty('Content'));
83 84 85 86 87 88 89

		// behavior
		$this->assertFalse($this->component->canSetProperty('p2'));
		$behavior = new NewBehavior();
		$this->component->attachBehavior('a', $behavior);
		$this->assertTrue($this->component->canSetProperty('p2'));
		$this->component->detachBehavior('a');
w  
Qiang Xue committed
90 91 92 93
	}

	public function testGetProperty()
	{
Qiang Xue committed
94
		$this->assertTrue('default' === $this->component->Text);
Qiang Xue committed
95
		$this->setExpectedException('yii\base\UnknownPropertyException');
Qiang Xue committed
96
		$value2 = $this->component->Caption;
w  
Qiang Xue committed
97 98 99 100
	}

	public function testSetProperty()
	{
Qiang Xue committed
101 102
		$value = 'new value';
		$this->component->Text = $value;
Qiang Xue committed
103
		$this->assertEquals($value, $this->component->Text);
Qiang Xue committed
104
		$this->setExpectedException('yii\base\UnknownPropertyException');
Qiang Xue committed
105
		$this->component->NewMember = $value;
w  
Qiang Xue committed
106 107 108 109 110
	}

	public function testIsset()
	{
		$this->assertTrue(isset($this->component->Text));
Qiang Xue committed
111
		$this->assertFalse(empty($this->component->Text));
w  
Qiang Xue committed
112

Qiang Xue committed
113
		$this->component->Text = '';
w  
Qiang Xue committed
114 115
		$this->assertTrue(isset($this->component->Text));
		$this->assertTrue(empty($this->component->Text));
Qiang Xue committed
116 117 118 119

		$this->component->Text = null;
		$this->assertFalse(isset($this->component->Text));
		$this->assertTrue(empty($this->component->Text));
120 121 122 123 124 125


		$this->assertFalse(isset($this->component->p2));
		$this->component->attachBehavior('a', new NewBehavior());
		$this->component->setP2('test');
		$this->assertTrue(isset($this->component->p2));
Qiang Xue committed
126 127
	}

128 129 130 131 132 133
	public function testCallUnknownMethod()
	{
		$this->setExpectedException('yii\base\UnknownMethodException');
		$this->component->unknownMethod();
	}

Qiang Xue committed
134 135 136 137 138
	public function testUnset()
	{
		unset($this->component->Text);
		$this->assertFalse(isset($this->component->Text));
		$this->assertTrue(empty($this->component->Text));
139 140 141 142 143 144 145 146 147 148 149 150 151

		$this->component->attachBehavior('a', new NewBehavior());
		$this->component->setP2('test');
		$this->assertEquals('test', $this->component->getP2());

		unset($this->component->p2);
		$this->assertNull($this->component->getP2());
	}

	public function testUnsetReadonly()
	{
		$this->setExpectedException('yii\base\InvalidCallException');
		unset($this->component->object);
w  
Qiang Xue committed
152 153
	}

Qiang Xue committed
154
	public function testOn()
w  
Qiang Xue committed
155
	{
Qiang Xue committed
156
		$this->assertFalse($this->component->hasEventHandlers('click'));
Qiang Xue committed
157
		$this->component->on('click', 'foo');
Qiang Xue committed
158
		$this->assertTrue($this->component->hasEventHandlers('click'));
w  
Qiang Xue committed
159

Qiang Xue committed
160 161 162 163
		$this->assertFalse($this->component->hasEventHandlers('click2'));
		$p = 'on click2';
		$this->component->$p = 'foo2';
		$this->assertTrue($this->component->hasEventHandlers('click2'));
w  
Qiang Xue committed
164 165
	}

Qiang Xue committed
166
	public function testOff()
w  
Qiang Xue committed
167
	{
Qiang Xue committed
168
		$this->assertFalse($this->component->hasEventHandlers('click'));
Qiang Xue committed
169
		$this->component->on('click', 'foo');
Qiang Xue committed
170 171 172 173 174 175 176 177 178 179 180 181
		$this->assertTrue($this->component->hasEventHandlers('click'));
		$this->component->off('click', 'foo');
		$this->assertFalse($this->component->hasEventHandlers('click'));

		$this->component->on('click2', 'foo');
		$this->component->on('click2', 'foo2');
		$this->component->on('click2', 'foo3');
		$this->assertTrue($this->component->hasEventHandlers('click2'));
		$this->component->off('click2', 'foo3');
		$this->assertTrue($this->component->hasEventHandlers('click2'));
		$this->component->off('click2');
		$this->assertFalse($this->component->hasEventHandlers('click2'));
w  
Qiang Xue committed
182 183
	}

Qiang Xue committed
184
	public function testTrigger()
w  
Qiang Xue committed
185
	{
Qiang Xue committed
186
		$this->component->on('click', array($this->component, 'myEventHandler'));
w  
Qiang Xue committed
187
		$this->assertFalse($this->component->eventHandled);
Qiang Xue committed
188 189
		$this->assertNull($this->component->event);
		$this->component->raiseEvent();
w  
Qiang Xue committed
190
		$this->assertTrue($this->component->eventHandled);
Qiang Xue committed
191 192 193
		$this->assertEquals('click', $this->component->event->name);
		$this->assertEquals($this->component, $this->component->event->sender);
		$this->assertFalse($this->component->event->handled);
w  
Qiang Xue committed
194

Qiang Xue committed
195 196 197 198 199 200
		$eventRaised = false;
		$this->component->on('click', function($event) use (&$eventRaised) {
			$eventRaised = true;
		});
		$this->component->raiseEvent();
		$this->assertTrue($eventRaised);
201 202 203 204 205 206 207 208

		// raise event w/o parameters
		$eventRaised = false;
		$this->component->on('test', function($event) use (&$eventRaised) {
			$eventRaised = true;
		});
		$this->component->trigger('test');
		$this->assertTrue($eventRaised);
w  
Qiang Xue committed
209 210
	}

Qiang Xue committed
211
	public function testHasEventHandlers()
w  
Qiang Xue committed
212
	{
Qiang Xue committed
213 214 215
		$this->assertFalse($this->component->hasEventHandlers('click'));
		$this->component->on('click', 'foo');
		$this->assertTrue($this->component->hasEventHandlers('click'));
w  
Qiang Xue committed
216 217 218 219
	}

	public function testStopEvent()
	{
Qiang Xue committed
220 221 222 223
		$component = new NewComponent;
		$component->on('click', 'yiiunit\framework\base\globalEventHandler2');
		$component->on('click', array($this->component, 'myEventHandler'));
		$component->raiseEvent();
w  
Qiang Xue committed
224 225 226 227
		$this->assertTrue($component->eventHandled);
		$this->assertFalse($this->component->eventHandled);
	}

Qiang Xue committed
228
	public function testAttachBehavior()
w  
Qiang Xue committed
229
	{
Qiang Xue committed
230
		$component = new NewComponent;
Qiang Xue committed
231 232 233
		$this->assertFalse($component->hasProperty('p'));
		$this->assertFalse($component->behaviorCalled);
		$this->assertNull($component->getBehavior('a'));
w  
Qiang Xue committed
234

w  
Qiang Xue committed
235
		$behavior = new NewBehavior;
Qiang Xue committed
236
		$component->attachBehavior('a', $behavior);
237
		$this->assertSame($behavior, $component->getBehavior('a'));
Qiang Xue committed
238 239 240
		$this->assertTrue($component->hasProperty('p'));
		$component->test();
		$this->assertTrue($component->behaviorCalled);
w  
Qiang Xue committed
241

Qiang Xue committed
242 243
		$this->assertSame($behavior, $component->detachBehavior('a'));
		$this->assertFalse($component->hasProperty('p'));
Qiang Xue committed
244
		$this->setExpectedException('yii\base\UnknownMethodException');
Qiang Xue committed
245
		$component->test();
246 247 248 249 250 251 252 253

		$p = 'as b';
		$component = new NewComponent;
		$component->$p = array('class' => 'NewBehavior');
		$this->assertSame($behavior, $component->getBehavior('a'));
		$this->assertTrue($component->hasProperty('p'));
		$component->test();
		$this->assertTrue($component->behaviorCalled);
w  
Qiang Xue committed
254
	}
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

	public function testAttachBehaviors()
	{
		$component = new NewComponent;
		$this->assertNull($component->getBehavior('a'));
		$this->assertNull($component->getBehavior('b'));

		$behavior = new NewBehavior;

		$component->attachBehaviors(array(
			'a' => $behavior,
			'b' => $behavior,
		));

		$this->assertSame(array('a' => $behavior, 'b' => $behavior), $component->getBehaviors());
	}

	public function testDetachBehavior()
	{
		$component = new NewComponent;
		$behavior = new NewBehavior;

		$component->attachBehavior('a', $behavior);
		$this->assertSame($behavior, $component->getBehavior('a'));

		$detachedBehavior = $component->detachBehavior('a');
		$this->assertSame($detachedBehavior, $behavior);
		$this->assertNull($component->getBehavior('a'));

		$detachedBehavior = $component->detachBehavior('z');
		$this->assertNull($detachedBehavior);
	}

	public function testDetachBehaviors()
	{
		$component = new NewComponent;
		$behavior = new NewBehavior;

		$component->attachBehavior('a', $behavior);
		$this->assertSame($behavior, $component->getBehavior('a'));
		$component->attachBehavior('b', $behavior);
		$this->assertSame($behavior, $component->getBehavior('b'));

		$component->detachBehaviors();
		$this->assertNull($component->getBehavior('a'));
		$this->assertNull($component->getBehavior('b'));

	}
w  
Qiang Xue committed
303
}
Qiang Xue committed
304

305
class NewComponent extends Component
Qiang Xue committed
306 307 308
{
	private $_object = null;
	private $_text = 'default';
Qiang Xue committed
309 310
	private $_items = array();
	public $content;
Qiang Xue committed
311 312 313 314 315 316 317 318

	public function getText()
	{
		return $this->_text;
	}

	public function setText($value)
	{
Qiang Xue committed
319
		$this->_text = $value;
Qiang Xue committed
320 321 322 323
	}

	public function getObject()
	{
Qiang Xue committed
324
		if (!$this->_object) {
Qiang Xue committed
325
			$this->_object = new self;
Qiang Xue committed
326
			$this->_object->_text = 'object text';
Qiang Xue committed
327 328 329 330
		}
		return $this->_object;
	}

Qiang Xue committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	public function getExecute()
	{
		return function($param) {
			return $param * 2;
		};
	}

	public function getItems()
	{
		return $this->_items;
	}

	public $eventHandled = false;
	public $event;
	public $behaviorCalled = false;

Qiang Xue committed
347
	public function myEventHandler($event)
Qiang Xue committed
348
	{
Qiang Xue committed
349 350
		$this->eventHandled = true;
		$this->event = $event;
Qiang Xue committed
351 352
	}

Qiang Xue committed
353
	public function raiseEvent()
Qiang Xue committed
354
	{
Qiang Xue committed
355
		$this->trigger('click', new Event);
Qiang Xue committed
356 357 358
	}
}

359
class NewBehavior extends Behavior
Qiang Xue committed
360
{
Qiang Xue committed
361
	public $p;
362 363 364 365 366 367 368 369 370 371 372
	private $p2;

	public function getP2()
	{
		return $this->p2;
	}

	public function setP2($value)
	{
		$this->p2 = $value;
	}
Qiang Xue committed
373

Qiang Xue committed
374 375
	public function test()
	{
Qiang Xue committed
376
		$this->owner->behaviorCalled = true;
Qiang Xue committed
377 378 379
		return 2;
	}
}
w  
Qiang Xue committed
380

381
class NewComponent2 extends Component
w  
Qiang Xue committed
382 383 384 385
{
	public $a;
	public $b;
	public $c;
Qiang Xue committed
386

w  
Qiang Xue committed
387 388 389 390 391
	public function __construct($b, $c)
	{
		$this->b = $b;
		$this->c = $c;
	}
Zander Baldwin committed
392
}