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
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();
Qiang Xue committed
30
		$this->mockApplication();
w  
Qiang Xue committed
31 32 33
		$this->component = new NewComponent();
	}

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

	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
47
		$this->assertTrue($component->hasEventHandlers('test'));
48 49 50 51

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

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

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

		// 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
91 92 93 94
	}

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

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

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

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

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


		$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
127 128
	}

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

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

		$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
153 154
	}

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

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

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

Qiang Xue committed
185
	public function testTrigger()
w  
Qiang Xue committed
186
	{
Qiang Xue committed
187
		$this->component->on('click', array($this->component, 'myEventHandler'));
w  
Qiang Xue committed
188
		$this->assertFalse($this->component->eventHandled);
Qiang Xue committed
189 190
		$this->assertNull($this->component->event);
		$this->component->raiseEvent();
w  
Qiang Xue committed
191
		$this->assertTrue($this->component->eventHandled);
Qiang Xue committed
192 193 194
		$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
195

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

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

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

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

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

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

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

		$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
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 303

	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
304
}
Qiang Xue committed
305

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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