ComponentTest.php 7.54 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php

Qiang Xue committed
3 4
namespace yiiunit\framework\base;

w  
Qiang Xue committed
5 6
function globalEventHandler($event)
{
Qiang Xue committed
7
	$event->sender->eventHandled = true;
w  
Qiang Xue committed
8 9 10 11
}

function globalEventHandler2($event)
{
Qiang Xue committed
12 13
	$event->sender->eventHandled = true;
	$event->handled = true;
w  
Qiang Xue committed
14 15
}

Qiang Xue committed
16
class ComponentTest extends \yiiunit\TestCase
w  
Qiang Xue committed
17
{
Qiang Xue committed
18 19 20
	/**
	 * @var NewComponent
	 */
w  
Qiang Xue committed
21 22 23 24 25 26 27 28 29 30 31
	protected $component;

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

	public function tearDown()
	{
		$this->component = null;
	}
Qiang Xue committed
32
	
w  
Qiang Xue committed
33 34
	public function testHasProperty()
	{
Qiang Xue committed
35 36 37 38 39 40
		$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
41 42 43 44 45 46 47
	}

	public function testCanGetProperty()
	{
		$this->assertTrue($this->component->canGetProperty('Text'));
		$this->assertTrue($this->component->canGetProperty('text'));
		$this->assertFalse($this->component->canGetProperty('Caption'));
Qiang Xue committed
48 49 50
		$this->assertTrue($this->component->canGetProperty('content'));
		$this->assertFalse($this->component->canGetProperty('content', false));
		$this->assertFalse($this->component->canGetProperty('Content'));
w  
Qiang Xue committed
51 52 53 54 55 56
	}

	public function testCanSetProperty()
	{
		$this->assertTrue($this->component->canSetProperty('Text'));
		$this->assertTrue($this->component->canSetProperty('text'));
Qiang Xue committed
57
		$this->assertFalse($this->component->canSetProperty('Object'));
w  
Qiang Xue committed
58
		$this->assertFalse($this->component->canSetProperty('Caption'));
Qiang Xue committed
59 60 61
		$this->assertTrue($this->component->canSetProperty('content'));
		$this->assertFalse($this->component->canSetProperty('content', false));
		$this->assertFalse($this->component->canSetProperty('Content'));
w  
Qiang Xue committed
62 63 64 65
	}

	public function testGetProperty()
	{
Qiang Xue committed
66
		$this->assertTrue('default' === $this->component->Text);
Qiang Xue committed
67
		$this->setExpectedException('yii\base\UnknownPropertyException');
Qiang Xue committed
68
		$value2 = $this->component->Caption;
w  
Qiang Xue committed
69 70 71 72
	}

	public function testSetProperty()
	{
Qiang Xue committed
73 74
		$value = 'new value';
		$this->component->Text = $value;
Qiang Xue committed
75
		$this->assertEquals($value, $this->component->Text);
Qiang Xue committed
76
		$this->setExpectedException('yii\base\UnknownPropertyException');
Qiang Xue committed
77
		$this->component->NewMember = $value;
w  
Qiang Xue committed
78 79 80 81 82
	}

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

Qiang Xue committed
85
		$this->component->Text = '';
w  
Qiang Xue committed
86 87
		$this->assertTrue(isset($this->component->Text));
		$this->assertTrue(empty($this->component->Text));
Qiang Xue committed
88 89 90 91 92 93 94 95 96 97 98

		$this->component->Text = null;
		$this->assertFalse(isset($this->component->Text));
		$this->assertTrue(empty($this->component->Text));
	}

	public function testUnset()
	{
		unset($this->component->Text);
		$this->assertFalse(isset($this->component->Text));
		$this->assertTrue(empty($this->component->Text));
w  
Qiang Xue committed
99 100
	}

Qiang Xue committed
101
	public function testOn()
w  
Qiang Xue committed
102
	{
Qiang Xue committed
103 104 105 106 107
		$this->assertEquals(0, $this->component->getEventHandlers('click')->getCount());
		$this->component->on('click', 'foo');
		$this->assertEquals(1, $this->component->getEventHandlers('click')->getCount());
		$this->component->on('click', 'bar');
		$this->assertEquals(2, $this->component->getEventHandlers('click')->getCount());
108 109 110
		$p = 'on click';
		$this->component->$p = 'foo2';
		$this->assertEquals(3, $this->component->getEventHandlers('click')->getCount());
w  
Qiang Xue committed
111

Qiang Xue committed
112
		$this->component->getEventHandlers('click')->add('test');
113
		$this->assertEquals(4, $this->component->getEventHandlers('click')->getCount());
w  
Qiang Xue committed
114 115
	}

Qiang Xue committed
116
	public function testOff()
w  
Qiang Xue committed
117
	{
Qiang Xue committed
118 119 120
		$this->component->on('click', 'foo');
		$this->component->on('click', array($this->component, 'myEventHandler'));
		$this->assertEquals(2, $this->component->getEventHandlers('click')->getCount());
w  
Qiang Xue committed
121

Qiang Xue committed
122 123 124 125 126 127 128 129 130
		$result = $this->component->off('click', 'foo');
		$this->assertTrue($result);
		$this->assertEquals(1, $this->component->getEventHandlers('click')->getCount());
		$result = $this->component->off('click', 'foo');
		$this->assertFalse($result);
		$this->assertEquals(1, $this->component->getEventHandlers('click')->getCount());
		$result = $this->component->off('click', array($this->component, 'myEventHandler'));
		$this->assertTrue($result);
		$this->assertEquals(0, $this->component->getEventHandlers('click')->getCount());
w  
Qiang Xue committed
131 132
	}

Qiang Xue committed
133
	public function testTrigger()
w  
Qiang Xue committed
134
	{
Qiang Xue committed
135
		$this->component->on('click', array($this->component, 'myEventHandler'));
w  
Qiang Xue committed
136
		$this->assertFalse($this->component->eventHandled);
Qiang Xue committed
137 138
		$this->assertNull($this->component->event);
		$this->component->raiseEvent();
w  
Qiang Xue committed
139
		$this->assertTrue($this->component->eventHandled);
Qiang Xue committed
140 141 142
		$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
143

Qiang Xue committed
144 145 146 147 148 149
		$eventRaised = false;
		$this->component->on('click', function($event) use (&$eventRaised) {
			$eventRaised = true;
		});
		$this->component->raiseEvent();
		$this->assertTrue($eventRaised);
w  
Qiang Xue committed
150 151
	}

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

	public function testStopEvent()
	{
Qiang Xue committed
161 162 163 164
		$component = new NewComponent;
		$component->on('click', 'yiiunit\framework\base\globalEventHandler2');
		$component->on('click', array($this->component, 'myEventHandler'));
		$component->raiseEvent();
w  
Qiang Xue committed
165 166 167 168
		$this->assertTrue($component->eventHandled);
		$this->assertFalse($this->component->eventHandled);
	}

Qiang Xue committed
169
	public function testAttachBehavior()
w  
Qiang Xue committed
170
	{
Qiang Xue committed
171
		$component = new NewComponent;
Qiang Xue committed
172 173 174
		$this->assertFalse($component->hasProperty('p'));
		$this->assertFalse($component->behaviorCalled);
		$this->assertNull($component->getBehavior('a'));
w  
Qiang Xue committed
175

w  
Qiang Xue committed
176
		$behavior = new NewBehavior;
Qiang Xue committed
177
		$component->attachBehavior('a', $behavior);
178
		$this->assertSame($behavior, $component->getBehavior('a'));
Qiang Xue committed
179 180 181
		$this->assertTrue($component->hasProperty('p'));
		$component->test();
		$this->assertTrue($component->behaviorCalled);
w  
Qiang Xue committed
182

Qiang Xue committed
183 184
		$this->assertSame($behavior, $component->detachBehavior('a'));
		$this->assertFalse($component->hasProperty('p'));
Qiang Xue committed
185
		$this->setExpectedException('yii\base\UnknownMethodException');
Qiang Xue committed
186
		$component->test();
187 188 189 190 191 192 193 194

		$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
195
	}
w  
Qiang Xue committed
196
}
Qiang Xue committed
197 198 199 200 201

class NewComponent extends \yii\base\Component
{
	private $_object = null;
	private $_text = 'default';
Qiang Xue committed
202 203
	private $_items = array();
	public $content;
Qiang Xue committed
204 205 206 207 208 209 210 211

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

	public function setText($value)
	{
Qiang Xue committed
212
		$this->_text = $value;
Qiang Xue committed
213 214 215 216
	}

	public function getObject()
	{
Qiang Xue committed
217
		if (!$this->_object) {
Qiang Xue committed
218
			$this->_object = new self;
Qiang Xue committed
219
			$this->_object->_text = 'object text';
Qiang Xue committed
220 221 222 223
		}
		return $this->_object;
	}

Qiang Xue committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	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
240
	public function myEventHandler($event)
Qiang Xue committed
241
	{
Qiang Xue committed
242 243
		$this->eventHandled = true;
		$this->event = $event;
Qiang Xue committed
244 245
	}

Qiang Xue committed
246
	public function raiseEvent()
Qiang Xue committed
247
	{
Qiang Xue committed
248
		$this->trigger('click', new \yii\base\Event($this));
Qiang Xue committed
249 250 251 252 253
	}
}

class NewBehavior extends \yii\base\Behavior
{
Qiang Xue committed
254 255
	public $p;

Qiang Xue committed
256 257
	public function test()
	{
Qiang Xue committed
258
		$this->owner->behaviorCalled = true;
Qiang Xue committed
259 260 261
		return 2;
	}
}
w  
Qiang Xue committed
262 263 264 265 266 267

class NewComponent2 extends \yii\base\Component
{
	public $a;
	public $b;
	public $c;
Qiang Xue committed
268

w  
Qiang Xue committed
269 270 271 272 273 274
	public function __construct($b, $c)
	{
		$this->b = $b;
		$this->c = $c;
	}
}