ComponentTest.php 11.9 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 21 22
/**
 * @group base
 */
23
class ComponentTest extends TestCase
w  
Qiang Xue committed
24
{
Qiang Xue committed
25 26 27
	/**
	 * @var NewComponent
	 */
w  
Qiang Xue committed
28 29
	protected $component;

Carsten Brandt committed
30
	protected function setUp()
w  
Qiang Xue committed
31
	{
Carsten Brandt committed
32
		parent::setUp();
Qiang Xue committed
33
		$this->mockApplication();
w  
Qiang Xue committed
34 35 36
		$this->component = new NewComponent();
	}

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

	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
50
		$this->assertTrue($component->hasEventHandlers('test'));
51 52 53 54

		$clone = clone $component;
		$this->assertNotSame($component, $clone);
		$this->assertNull($clone->getBehavior('a'));
Qiang Xue committed
55
		$this->assertFalse($clone->hasEventHandlers('test'));
56
	}
Qiang Xue committed
57
	
w  
Qiang Xue committed
58 59
	public function testHasProperty()
	{
Qiang Xue committed
60 61 62 63 64 65
		$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
66 67 68 69 70 71 72
	}

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

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

		// 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
94 95 96 97
	}

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

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

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

Qiang Xue committed
117
		$this->component->Text = '';
w  
Qiang Xue committed
118 119
		$this->assertTrue(isset($this->component->Text));
		$this->assertTrue(empty($this->component->Text));
Qiang Xue committed
120 121 122 123

		$this->component->Text = null;
		$this->assertFalse(isset($this->component->Text));
		$this->assertTrue(empty($this->component->Text));
124 125 126 127 128 129


		$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
130 131
	}

132 133 134 135 136 137
	public function testCallUnknownMethod()
	{
		$this->setExpectedException('yii\base\UnknownMethodException');
		$this->component->unknownMethod();
	}

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

		$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
156 157
	}

Qiang Xue committed
158
	public function testOn()
w  
Qiang Xue committed
159
	{
Qiang Xue committed
160
		$this->assertFalse($this->component->hasEventHandlers('click'));
Qiang Xue committed
161
		$this->component->on('click', 'foo');
Qiang Xue committed
162
		$this->assertTrue($this->component->hasEventHandlers('click'));
w  
Qiang Xue committed
163

Qiang Xue committed
164 165 166 167
		$this->assertFalse($this->component->hasEventHandlers('click2'));
		$p = 'on click2';
		$this->component->$p = 'foo2';
		$this->assertTrue($this->component->hasEventHandlers('click2'));
w  
Qiang Xue committed
168 169
	}

Qiang Xue committed
170
	public function testOff()
w  
Qiang Xue committed
171
	{
Qiang Xue committed
172
		$this->assertFalse($this->component->hasEventHandlers('click'));
Qiang Xue committed
173
		$this->component->on('click', 'foo');
Qiang Xue committed
174 175 176 177 178 179 180 181 182 183 184 185
		$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
186 187
	}

Qiang Xue committed
188
	public function testTrigger()
w  
Qiang Xue committed
189
	{
Alexander Makarov committed
190
		$this->component->on('click', [$this->component, 'myEventHandler']);
w  
Qiang Xue committed
191
		$this->assertFalse($this->component->eventHandled);
Qiang Xue committed
192 193
		$this->assertNull($this->component->event);
		$this->component->raiseEvent();
w  
Qiang Xue committed
194
		$this->assertTrue($this->component->eventHandled);
Qiang Xue committed
195 196 197
		$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
198

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

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

Qiang Xue committed
215
	public function testHasEventHandlers()
w  
Qiang Xue committed
216
	{
Qiang Xue committed
217 218 219
		$this->assertFalse($this->component->hasEventHandlers('click'));
		$this->component->on('click', 'foo');
		$this->assertTrue($this->component->hasEventHandlers('click'));
w  
Qiang Xue committed
220 221 222 223
	}

	public function testStopEvent()
	{
Qiang Xue committed
224 225
		$component = new NewComponent;
		$component->on('click', 'yiiunit\framework\base\globalEventHandler2');
Alexander Makarov committed
226
		$component->on('click', [$this->component, 'myEventHandler']);
Qiang Xue committed
227
		$component->raiseEvent();
w  
Qiang Xue committed
228 229 230 231
		$this->assertTrue($component->eventHandled);
		$this->assertFalse($this->component->eventHandled);
	}

Qiang Xue committed
232
	public function testAttachBehavior()
w  
Qiang Xue committed
233
	{
Qiang Xue committed
234
		$component = new NewComponent;
Qiang Xue committed
235 236 237
		$this->assertFalse($component->hasProperty('p'));
		$this->assertFalse($component->behaviorCalled);
		$this->assertNull($component->getBehavior('a'));
w  
Qiang Xue committed
238

w  
Qiang Xue committed
239
		$behavior = new NewBehavior;
Qiang Xue committed
240
		$component->attachBehavior('a', $behavior);
241
		$this->assertSame($behavior, $component->getBehavior('a'));
Qiang Xue committed
242 243 244
		$this->assertTrue($component->hasProperty('p'));
		$component->test();
		$this->assertTrue($component->behaviorCalled);
w  
Qiang Xue committed
245

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

		$p = 'as b';
		$component = new NewComponent;
Alexander Makarov committed
253
		$component->$p = ['class' => 'NewBehavior'];
254 255 256 257
		$this->assertSame($behavior, $component->getBehavior('a'));
		$this->assertTrue($component->hasProperty('p'));
		$component->test();
		$this->assertTrue($component->behaviorCalled);
w  
Qiang Xue committed
258
	}
259 260 261 262 263 264 265 266 267

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

		$behavior = new NewBehavior;

Alexander Makarov committed
268
		$component->attachBehaviors([
269 270
			'a' => $behavior,
			'b' => $behavior,
Alexander Makarov committed
271
		]);
272

Alexander Makarov committed
273
		$this->assertSame(['a' => $behavior, 'b' => $behavior], $component->getBehaviors());
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 303 304 305
	}

	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'));
	}
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

	public function testSetReadOnlyProperty()
	{
		$this->setExpectedException(
			'\yii\base\InvalidCallException',
			'Setting read-only property: yiiunit\framework\base\NewComponent::object'
		);
		$this->component->object = 'z';
	}

	public function testSetPropertyOfBehavior()
	{
		$this->assertNull($this->component->getBehavior('a'));

		$behavior = new NewBehavior;
		$this->component->attachBehaviors([
			'a' => $behavior,
		]);
		$this->component->p = 'Yii is cool.';

		$this->assertSame('Yii is cool.', $this->component->getBehavior('a')->p);
	}

	public function testSettingBehaviorWithSetter()
	{
		$behaviorName = 'foo';
		$this->assertNull($this->component->getBehavior($behaviorName));
		$p = 'as ' . $behaviorName;
		$this->component->$p = __NAMESPACE__ .  '\NewBehavior';
		$this->assertSame(__NAMESPACE__ .  '\NewBehavior', get_class($this->component->getBehavior($behaviorName)));
	}

	public function testWriteOnlyProperty()
	{
		$this->setExpectedException(
			'\yii\base\InvalidCallException',
			'Getting write-only property: yiiunit\framework\base\NewComponent::writeOnly'
		);
		$this->component->writeOnly;
	}

	public function testSuccessfulMethodCheck()
	{
		$this->assertTrue($this->component->hasMethod('hasProperty'));
	}

	public function testTurningOffNonExistingBehavior()
	{
		$this->assertFalse($this->component->hasEventHandlers('foo'));
		$this->assertFalse($this->component->off('foo'));
	}
w  
Qiang Xue committed
357
}
Qiang Xue committed
358

359
class NewComponent extends Component
Qiang Xue committed
360 361 362
{
	private $_object = null;
	private $_text = 'default';
Alexander Makarov committed
363
	private $_items = [];
Qiang Xue committed
364
	public $content;
Qiang Xue committed
365 366 367 368 369 370 371 372

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

	public function setText($value)
	{
Qiang Xue committed
373
		$this->_text = $value;
Qiang Xue committed
374 375 376 377
	}

	public function getObject()
	{
Qiang Xue committed
378
		if (!$this->_object) {
Qiang Xue committed
379
			$this->_object = new self;
Qiang Xue committed
380
			$this->_object->_text = 'object text';
Qiang Xue committed
381 382 383 384
		}
		return $this->_object;
	}

Qiang Xue committed
385 386
	public function getExecute()
	{
Alexander Makarov committed
387
		return function ($param) {
Qiang Xue committed
388 389 390 391 392 393 394 395 396 397 398 399 400
			return $param * 2;
		};
	}

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

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

Qiang Xue committed
401
	public function myEventHandler($event)
Qiang Xue committed
402
	{
Qiang Xue committed
403 404
		$this->eventHandled = true;
		$this->event = $event;
Qiang Xue committed
405 406
	}

Qiang Xue committed
407
	public function raiseEvent()
Qiang Xue committed
408
	{
Qiang Xue committed
409
		$this->trigger('click', new Event);
Qiang Xue committed
410
	}
411 412 413 414

	public function setWriteOnly()
	{
	}
Qiang Xue committed
415 416
}

417
class NewBehavior extends Behavior
Qiang Xue committed
418
{
Qiang Xue committed
419
	public $p;
420 421 422 423 424 425 426 427 428 429 430
	private $p2;

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

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

Qiang Xue committed
432 433
	public function test()
	{
Qiang Xue committed
434
		$this->owner->behaviorCalled = true;
Qiang Xue committed
435 436 437
		return 2;
	}
}
w  
Qiang Xue committed
438

439
class NewComponent2 extends Component
w  
Qiang Xue committed
440 441 442 443
{
	public $a;
	public $b;
	public $c;
Qiang Xue committed
444

w  
Qiang Xue committed
445 446 447 448 449
	public function __construct($b, $c)
	{
		$this->b = $b;
		$this->c = $c;
	}
Zander Baldwin committed
450
}