ComponentTest.php 10.5 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
	{
Qiang Xue committed
190
		$this->component->on('click', array($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 226 227
		$component = new NewComponent;
		$component->on('click', 'yiiunit\framework\base\globalEventHandler2');
		$component->on('click', array($this->component, 'myEventHandler'));
		$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 253 254 255 256 257

		$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
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 303 304 305 306

	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
307
}
Qiang Xue committed
308

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

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

	public function setText($value)
	{
Qiang Xue committed
323
		$this->_text = $value;
Qiang Xue committed
324 325 326 327
	}

	public function getObject()
	{
Qiang Xue committed
328
		if (!$this->_object) {
Qiang Xue committed
329
			$this->_object = new self;
Qiang Xue committed
330
			$this->_object->_text = 'object text';
Qiang Xue committed
331 332 333 334
		}
		return $this->_object;
	}

Qiang Xue committed
335 336
	public function getExecute()
	{
Alexander Makarov committed
337
		return function ($param) {
Qiang Xue committed
338 339 340 341 342 343 344 345 346 347 348 349 350
			return $param * 2;
		};
	}

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

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

Qiang Xue committed
351
	public function myEventHandler($event)
Qiang Xue committed
352
	{
Qiang Xue committed
353 354
		$this->eventHandled = true;
		$this->event = $event;
Qiang Xue committed
355 356
	}

Qiang Xue committed
357
	public function raiseEvent()
Qiang Xue committed
358
	{
Qiang Xue committed
359
		$this->trigger('click', new Event);
Qiang Xue committed
360 361 362
	}
}

363
class NewBehavior extends Behavior
Qiang Xue committed
364
{
Qiang Xue committed
365
	public $p;
366 367 368 369 370 371 372 373 374 375 376
	private $p2;

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

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

Qiang Xue committed
378 379
	public function test()
	{
Qiang Xue committed
380
		$this->owner->behaviorCalled = true;
Qiang Xue committed
381 382 383
		return 2;
	}
}
w  
Qiang Xue committed
384

385
class NewComponent2 extends Component
w  
Qiang Xue committed
386 387 388 389
{
	public $a;
	public $b;
	public $c;
Qiang Xue committed
390

w  
Qiang Xue committed
391 392 393 394 395
	public function __construct($b, $c)
	{
		$this->b = $b;
		$this->c = $c;
	}
Zander Baldwin committed
396
}