ComponentTest.php 6.41 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	protected $component;

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

	public function tearDown()
	{
		$this->component = null;
	}

	public function testHasProperty()
	{
		$this->assertTrue($this->component->hasProperty('Text'), "Component hasn't property Text");
		$this->assertTrue($this->component->hasProperty('text'), "Component hasn't property text");
		$this->assertFalse($this->component->hasProperty('Caption'), "Component as property Caption");
	}

	public function testCanGetProperty()
	{
		$this->assertTrue($this->component->canGetProperty('Text'));
		$this->assertTrue($this->component->canGetProperty('text'));
		$this->assertFalse($this->component->canGetProperty('Caption'));
	}

	public function testCanSetProperty()
	{
		$this->assertTrue($this->component->canSetProperty('Text'));
		$this->assertTrue($this->component->canSetProperty('text'));
		$this->assertFalse($this->component->canSetProperty('Caption'));
	}

	public function testGetProperty()
	{
Qiang Xue committed
56
		$this->assertTrue('default' === $this->component->Text);
57
		$this->setExpectedException('yii\base\Exception');
Qiang Xue committed
58
		$value2 = $this->component->Caption;
w  
Qiang Xue committed
59 60 61 62
	}

	public function testSetProperty()
	{
Qiang Xue committed
63 64 65 66
		$value = 'new value';
		$this->component->Text = $value;
		$text = $this->component->Text;
		$this->assertTrue($value === $this->component->Text);
67
		$this->setExpectedException('yii\base\Exception');
Qiang Xue committed
68
		$this->component->NewMember = $value;
w  
Qiang Xue committed
69 70 71 72 73 74 75 76 77 78 79
	}

	public function testIsset()
	{
		$this->assertTrue(isset($this->component->Text));
		$this->assertTrue(!empty($this->component->Text));

		unset($this->component->Text);
		$this->assertFalse(isset($this->component->Text));
		$this->assertFalse(!empty($this->component->Text));

Qiang Xue committed
80
		$this->component->Text = '';
w  
Qiang Xue committed
81 82 83 84
		$this->assertTrue(isset($this->component->Text));
		$this->assertTrue(empty($this->component->Text));
	}

Qiang Xue committed
85
	public function testOn()
w  
Qiang Xue committed
86
	{
Qiang Xue committed
87 88 89 90 91
		$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());
w  
Qiang Xue committed
92

Qiang Xue committed
93 94
		$this->component->getEventHandlers('click')->add('test');
		$this->assertEquals(3, $this->component->getEventHandlers('click')->getCount());
w  
Qiang Xue committed
95 96
	}

Qiang Xue committed
97
	public function testOff()
w  
Qiang Xue committed
98
	{
Qiang Xue committed
99 100 101
		$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
102

Qiang Xue committed
103 104 105 106 107 108 109 110 111
		$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
112 113
	}

Qiang Xue committed
114
	public function testTrigger()
w  
Qiang Xue committed
115
	{
Qiang Xue committed
116
		$this->component->on('click', array($this->component, 'myEventHandler'));
w  
Qiang Xue committed
117
		$this->assertFalse($this->component->eventHandled);
Qiang Xue committed
118 119
		$this->assertNull($this->component->event);
		$this->component->raiseEvent();
w  
Qiang Xue committed
120
		$this->assertTrue($this->component->eventHandled);
Qiang Xue committed
121 122 123
		$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
124

Qiang Xue committed
125 126 127 128 129 130
		$eventRaised = false;
		$this->component->on('click', function($event) use (&$eventRaised) {
			$eventRaised = true;
		});
		$this->component->raiseEvent();
		$this->assertTrue($eventRaised);
w  
Qiang Xue committed
131 132
	}

Qiang Xue committed
133
	public function testHasEventHandlers()
w  
Qiang Xue committed
134
	{
Qiang Xue committed
135 136 137
		$this->assertFalse($this->component->hasEventHandlers('click'));
		$this->component->on('click', 'foo');
		$this->assertTrue($this->component->hasEventHandlers('click'));
w  
Qiang Xue committed
138 139 140 141
	}

	public function testStopEvent()
	{
Qiang Xue committed
142 143 144 145
		$component = new NewComponent;
		$component->on('click', 'yiiunit\framework\base\globalEventHandler2');
		$component->on('click', array($this->component, 'myEventHandler'));
		$component->raiseEvent();
w  
Qiang Xue committed
146 147 148 149
		$this->assertTrue($component->eventHandled);
		$this->assertFalse($this->component->eventHandled);
	}

w  
Qiang Xue committed
150 151
	public function testDetachBehavior()
	{
Qiang Xue committed
152
		$component = new NewComponent;
w  
Qiang Xue committed
153
		$behavior = new NewBehavior;
Qiang Xue committed
154 155
		$component->attachBehavior('a', $behavior);
		$this->assertSame($behavior, $component->detachBehavior('a'));
w  
Qiang Xue committed
156
	}
w  
Qiang Xue committed
157 158 159

	public function testDetachingBehaviors()
	{
Qiang Xue committed
160
		$component = new NewComponent;
w  
Qiang Xue committed
161
		$behavior = new NewBehavior;
Qiang Xue committed
162
		$component->attachBehavior('a', $behavior);
w  
Qiang Xue committed
163
		$component->detachBehaviors();
164
		$this->setExpectedException('yii\base\Exception');
w  
Qiang Xue committed
165 166
		$component->test();
	}
w  
Qiang Xue committed
167 168 169

	public function testAsa()
	{
Qiang Xue committed
170
		$component = new NewComponent;
w  
Qiang Xue committed
171
		$behavior = new NewBehavior;
Qiang Xue committed
172 173
		$component->attachBehavior('a', $behavior);
		$this->assertSame($behavior, $component->asa('a'));
w  
Qiang Xue committed
174
	}
w  
Qiang Xue committed
175

w  
Qiang Xue committed
176 177
	public function testCreate()
	{
Qiang Xue committed
178
		$component = NewComponent2::newInstance(array('a' => 3), 1, 2);
w  
Qiang Xue committed
179 180 181 182
		$this->assertEquals(1, $component->b);
		$this->assertEquals(2, $component->c);
		$this->assertEquals(3, $component->a);
	}
w  
Qiang Xue committed
183
}
Qiang Xue committed
184 185 186 187 188 189

class NewComponent extends \yii\base\Component
{
	private $_object = null;
	private $_text = 'default';
	public $eventHandled = false;
Qiang Xue committed
190
	public $event;
Qiang Xue committed
191 192 193 194 195 196 197 198 199
	public $behaviorCalled = false;

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

	public function setText($value)
	{
Qiang Xue committed
200
		$this->_text = $value;
Qiang Xue committed
201 202 203 204
	}

	public function getObject()
	{
Qiang Xue committed
205 206 207
		if (!$this->_object) {
			$this->_object = new NewComponent;
			$this->_object->_text = 'object text';
Qiang Xue committed
208 209 210 211
		}
		return $this->_object;
	}

Qiang Xue committed
212
	public function myEventHandler($event)
Qiang Xue committed
213
	{
Qiang Xue committed
214 215
		$this->eventHandled = true;
		$this->event = $event;
Qiang Xue committed
216 217
	}

Qiang Xue committed
218
	public function raiseEvent()
Qiang Xue committed
219
	{
Qiang Xue committed
220
		$this->trigger('click', new \yii\base\Event($this));
Qiang Xue committed
221 222 223 224 225 226 227
	}
}

class NewBehavior extends \yii\base\Behavior
{
	public function test()
	{
Qiang Xue committed
228
		$this->owner->behaviorCalled = true;
Qiang Xue committed
229 230 231
		return 2;
	}
}
w  
Qiang Xue committed
232 233 234 235 236 237

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

w  
Qiang Xue committed
239 240 241 242 243 244
	public function __construct($b, $c)
	{
		$this->b = $b;
		$this->c = $c;
	}
}