BehaviorTest.php 1.38 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
{
Qiang Xue committed
36 37 38 39 40 41
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}

Alexander Makarov committed
42 43
	public function testAttachAndAccessing()
	{
Qiang Xue committed
44
		$bar = new BarClass();
Alexander Makarov committed
45
		$behavior = new BarBehavior();
Qiang Xue committed
46
		$bar->attachBehavior('bar', $behavior);
Alexander Makarov committed
47
		$this->assertEquals('behavior property', $bar->behaviorProperty);
Qiang Xue committed
48
		$this->assertEquals('behavior method', $bar->behaviorMethod());
49 50
		$this->assertEquals('behavior property', $bar->getBehavior('bar')->behaviorProperty);
		$this->assertEquals('behavior method', $bar->getBehavior('bar')->behaviorMethod());
51 52 53 54

		$behavior = new BarBehavior(array('behaviorProperty' => 'reattached'));
		$bar->attachBehavior('bar', $behavior);
		$this->assertEquals('reattached', $bar->behaviorProperty);
Qiang Xue committed
55 56 57 58 59 60 61
	}

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