BehaviorTest.php 1.29 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
use yii\base\Behavior;
use yii\base\Component;
use yiiunit\TestCase;

class BarClass extends Component
Alexander Makarov committed
10 11 12 13
{

}

Alexander Makarov committed
14
class FooClass extends Component
Qiang Xue committed
15 16 17 18 19 20 21 22 23
{
	public function behaviors()
	{
		return array(
			'foo' => __NAMESPACE__ . '\BarBehavior',
		);
	}
}

Alexander Makarov committed
24
class BarBehavior extends Behavior
Alexander Makarov committed
25 26 27 28 29 30 31 32 33
{
	public $behaviorProperty = 'behavior property';

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

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

		$behavior = new BarBehavior(array('behaviorProperty' => 'reattached'));
		$bar->attachBehavior('bar', $behavior);
		$this->assertEquals('reattached', $bar->behaviorProperty);
Qiang Xue committed
49 50 51 52 53 54 55
	}

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