Browse Source

more tests for Behavior and Component

tags/2.0.0-beta
Alexander Makarov 12 years ago
parent
commit
f0676a8038
  1. 4
      tests/unit/framework/base/BehaviorTest.php
  2. 124
      tests/unit/framework/base/ComponentTest.php

4
tests/unit/framework/base/BehaviorTest.php

@ -42,6 +42,10 @@ class BehaviorTest extends TestCase
$this->assertEquals('behavior method', $bar->behaviorMethod()); $this->assertEquals('behavior method', $bar->behaviorMethod());
$this->assertEquals('behavior property', $bar->getBehavior('bar')->behaviorProperty); $this->assertEquals('behavior property', $bar->getBehavior('bar')->behaviorProperty);
$this->assertEquals('behavior method', $bar->getBehavior('bar')->behaviorMethod()); $this->assertEquals('behavior method', $bar->getBehavior('bar')->behaviorMethod());
$behavior = new BarBehavior(array('behaviorProperty' => 'reattached'));
$bar->attachBehavior('bar', $behavior);
$this->assertEquals('reattached', $bar->behaviorProperty);
} }
public function testAutomaticAttach() public function testAutomaticAttach()

124
tests/unit/framework/base/ComponentTest.php

@ -1,7 +1,11 @@
<?php <?php
namespace yiiunit\framework\base; namespace yiiunit\framework\base;
use yii\base\Behavior;
use yii\base\Component;
use yii\base\Event;
use yiiunit\TestCase;
function globalEventHandler($event) function globalEventHandler($event)
{ {
$event->sender->eventHandled = true; $event->sender->eventHandled = true;
@ -13,7 +17,7 @@ function globalEventHandler2($event)
$event->handled = true; $event->handled = true;
} }
class ComponentTest extends \yiiunit\TestCase class ComponentTest extends TestCase
{ {
/** /**
* @var NewComponent * @var NewComponent
@ -30,6 +34,21 @@ class ComponentTest extends \yiiunit\TestCase
$this->component = null; $this->component = null;
} }
public function testClone()
{
$component = new NewComponent();
$behavior = new NewBehavior();
$component->attachBehavior('a', $behavior);
$this->assertSame($behavior, $component->getBehavior('a'));
$component->on('test', 'fake');
$this->assertEquals(1, $component->getEventHandlers('test')->count);
$clone = clone $component;
$this->assertNotSame($component, $clone);
$this->assertNull($clone->getBehavior('a'));
$this->assertEquals(0, $clone->getEventHandlers('test')->count);
}
public function testHasProperty() public function testHasProperty()
{ {
$this->assertTrue($this->component->hasProperty('Text')); $this->assertTrue($this->component->hasProperty('Text'));
@ -59,6 +78,13 @@ class ComponentTest extends \yiiunit\TestCase
$this->assertTrue($this->component->canSetProperty('content')); $this->assertTrue($this->component->canSetProperty('content'));
$this->assertFalse($this->component->canSetProperty('content', false)); $this->assertFalse($this->component->canSetProperty('content', false));
$this->assertFalse($this->component->canSetProperty('Content')); $this->assertFalse($this->component->canSetProperty('Content'));
// behavior
$this->assertFalse($this->component->canSetProperty('p2'));
$behavior = new NewBehavior();
$this->component->attachBehavior('a', $behavior);
$this->assertTrue($this->component->canSetProperty('p2'));
$this->component->detachBehavior('a');
} }
public function testGetProperty() public function testGetProperty()
@ -89,6 +115,12 @@ class ComponentTest extends \yiiunit\TestCase
$this->component->Text = null; $this->component->Text = null;
$this->assertFalse(isset($this->component->Text)); $this->assertFalse(isset($this->component->Text));
$this->assertTrue(empty($this->component->Text)); $this->assertTrue(empty($this->component->Text));
$this->assertFalse(isset($this->component->p2));
$this->component->attachBehavior('a', new NewBehavior());
$this->component->setP2('test');
$this->assertTrue(isset($this->component->p2));
} }
public function testUnset() public function testUnset()
@ -96,6 +128,19 @@ class ComponentTest extends \yiiunit\TestCase
unset($this->component->Text); unset($this->component->Text);
$this->assertFalse(isset($this->component->Text)); $this->assertFalse(isset($this->component->Text));
$this->assertTrue(empty($this->component->Text)); $this->assertTrue(empty($this->component->Text));
$this->component->attachBehavior('a', new NewBehavior());
$this->component->setP2('test');
$this->assertEquals('test', $this->component->getP2());
unset($this->component->p2);
$this->assertNull($this->component->getP2());
}
public function testUnsetReadonly()
{
$this->setExpectedException('yii\base\InvalidCallException');
unset($this->component->object);
} }
public function testOn() public function testOn()
@ -147,6 +192,14 @@ class ComponentTest extends \yiiunit\TestCase
}); });
$this->component->raiseEvent(); $this->component->raiseEvent();
$this->assertTrue($eventRaised); $this->assertTrue($eventRaised);
// raise event w/o parameters
$eventRaised = false;
$this->component->on('test', function($event) use (&$eventRaised) {
$eventRaised = true;
});
$this->component->trigger('test');
$this->assertTrue($eventRaised);
} }
public function testHasEventHandlers() public function testHasEventHandlers()
@ -193,9 +246,57 @@ class ComponentTest extends \yiiunit\TestCase
$component->test(); $component->test();
$this->assertTrue($component->behaviorCalled); $this->assertTrue($component->behaviorCalled);
} }
public function testAttachBehaviors()
{
$component = new NewComponent;
$this->assertNull($component->getBehavior('a'));
$this->assertNull($component->getBehavior('b'));
$behavior = new NewBehavior;
$component->attachBehaviors(array(
'a' => $behavior,
'b' => $behavior,
));
$this->assertSame(array('a' => $behavior, 'b' => $behavior), $component->getBehaviors());
}
public function testDetachBehavior()
{
$component = new NewComponent;
$behavior = new NewBehavior;
$component->attachBehavior('a', $behavior);
$this->assertSame($behavior, $component->getBehavior('a'));
$detachedBehavior = $component->detachBehavior('a');
$this->assertSame($detachedBehavior, $behavior);
$this->assertNull($component->getBehavior('a'));
$detachedBehavior = $component->detachBehavior('z');
$this->assertNull($detachedBehavior);
} }
class NewComponent extends \yii\base\Component public function testDetachBehaviors()
{
$component = new NewComponent;
$behavior = new NewBehavior;
$component->attachBehavior('a', $behavior);
$this->assertSame($behavior, $component->getBehavior('a'));
$component->attachBehavior('b', $behavior);
$this->assertSame($behavior, $component->getBehavior('b'));
$component->detachBehaviors();
$this->assertNull($component->getBehavior('a'));
$this->assertNull($component->getBehavior('b'));
}
}
class NewComponent extends Component
{ {
private $_object = null; private $_object = null;
private $_text = 'default'; private $_text = 'default';
@ -245,13 +346,24 @@ class NewComponent extends \yii\base\Component
public function raiseEvent() public function raiseEvent()
{ {
$this->trigger('click', new \yii\base\Event($this)); $this->trigger('click', new Event($this));
} }
} }
class NewBehavior extends \yii\base\Behavior class NewBehavior extends Behavior
{ {
public $p; public $p;
private $p2;
public function getP2()
{
return $this->p2;
}
public function setP2($value)
{
$this->p2 = $value;
}
public function test() public function test()
{ {
@ -260,7 +372,7 @@ class NewBehavior extends \yii\base\Behavior
} }
} }
class NewComponent2 extends \yii\base\Component class NewComponent2 extends Component
{ {
public $a; public $a;
public $b; public $b;

Loading…
Cancel
Save