BehaviorTest.php 1.37 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
{
}

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

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

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

Alexander Makarov committed
33
class BehaviorTest extends TestCase
Alexander Makarov committed
34
{
Qiang Xue committed
35 36 37 38 39 40
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}

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

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

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