BehaviorTest.php 1.08 KB
Newer Older
Alexander Makarov committed
1
<?php
Qiang Xue committed
2 3 4

namespace yiiunit\framework\base;

Alexander Makarov committed
5 6 7 8 9
class BarClass extends \yii\base\Component
{

}

Qiang Xue committed
10 11 12 13 14 15 16 17 18 19
class FooClass extends \yii\base\Component
{
	public function behaviors()
	{
		return array(
			'foo' => __NAMESPACE__ . '\BarBehavior',
		);
	}
}

Alexander Makarov committed
20 21 22 23 24 25 26 27 28 29
class BarBehavior extends \yii\base\Behavior
{
	public $behaviorProperty = 'behavior property';

	public function behaviorMethod()
	{
		return 'behavior method';
	}
}

Qiang Xue committed
30
class BehaviorTest extends \yiiunit\TestCase
Alexander Makarov committed
31 32 33
{
	public function testAttachAndAccessing()
	{
Qiang Xue committed
34
		$bar = new BarClass();
Alexander Makarov committed
35
		$behavior = new BarBehavior();
Qiang Xue committed
36
		$bar->attachBehavior('bar', $behavior);
Alexander Makarov committed
37
		$this->assertEquals('behavior property', $bar->behaviorProperty);
Qiang Xue committed
38
		$this->assertEquals('behavior method', $bar->behaviorMethod());
Qiang Xue committed
39 40 41 42 43 44 45 46 47
		$this->assertEquals('behavior property', $bar->asa('bar')->behaviorProperty);
		$this->assertEquals('behavior method', $bar->asa('bar')->behaviorMethod());
	}

	public function testAutomaticAttach()
	{
		$foo = new FooClass();
		$this->assertEquals('behavior property', $foo->behaviorProperty);
		$this->assertEquals('behavior method', $foo->behaviorMethod());
Alexander Makarov committed
48 49
	}
}