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 27 28 29 30 31 32 33 34 35
	protected $component;

	public function setUp()
	{
		$this->component = new NewComponent();
	}

	public function tearDown()
	{
		$this->component = null;
	}
36 37 38 39 40 41 42 43

	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
44
		$this->assertTrue($component->hasEventHandlers('test'));
45 46 47 48

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

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

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

		// 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
88 89 90 91
	}

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

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

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

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

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


		$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
124 125
	}

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

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

		$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
150 151
	}

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

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

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

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

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

		// 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
207 208
	}

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

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

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

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

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

		$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
252
	}
253 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

	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
301
}
Qiang Xue committed
302

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

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

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

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

Qiang Xue committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	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
345
	public function myEventHandler($event)
Qiang Xue committed
346
	{
Qiang Xue committed
347 348
		$this->eventHandled = true;
		$this->event = $event;
Qiang Xue committed
349 350
	}

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

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

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

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

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

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

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