You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
10 KiB

14 years ago
<?php
namespace yiiunit\framework\base;
use yii\base\Behavior;
use yii\base\Component;
use yii\base\Event;
use yiiunit\TestCase;
14 years ago
function globalEventHandler($event)
{
$event->sender->eventHandled = true;
14 years ago
}
function globalEventHandler2($event)
{
$event->sender->eventHandled = true;
$event->handled = true;
14 years ago
}
class ComponentTest extends TestCase
14 years ago
{
/**
* @var NewComponent
*/
14 years ago
protected $component;
12 years ago
protected function setUp()
14 years ago
{
12 years ago
parent::setUp();
14 years ago
$this->component = new NewComponent();
}
12 years ago
protected function tearDown()
14 years ago
{
12 years ago
parent::tearDown();
14 years ago
$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->assertTrue($component->hasEventHandlers('test'));
$clone = clone $component;
$this->assertNotSame($component, $clone);
$this->assertNull($clone->getBehavior('a'));
$this->assertFalse($clone->hasEventHandlers('test'));
}
14 years ago
public function testHasProperty()
{
$this->assertTrue($this->component->hasProperty('Text'));
$this->assertTrue($this->component->hasProperty('text'));
$this->assertFalse($this->component->hasProperty('Caption'));
$this->assertTrue($this->component->hasProperty('content'));
$this->assertFalse($this->component->hasProperty('content', false));
$this->assertFalse($this->component->hasProperty('Content'));
14 years ago
}
public function testCanGetProperty()
{
$this->assertTrue($this->component->canGetProperty('Text'));
$this->assertTrue($this->component->canGetProperty('text'));
$this->assertFalse($this->component->canGetProperty('Caption'));
$this->assertTrue($this->component->canGetProperty('content'));
$this->assertFalse($this->component->canGetProperty('content', false));
$this->assertFalse($this->component->canGetProperty('Content'));
14 years ago
}
public function testCanSetProperty()
{
$this->assertTrue($this->component->canSetProperty('Text'));
$this->assertTrue($this->component->canSetProperty('text'));
$this->assertFalse($this->component->canSetProperty('Object'));
14 years ago
$this->assertFalse($this->component->canSetProperty('Caption'));
$this->assertTrue($this->component->canSetProperty('content'));
$this->assertFalse($this->component->canSetProperty('content', false));
$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');
14 years ago
}
public function testGetProperty()
{
$this->assertTrue('default' === $this->component->Text);
$this->setExpectedException('yii\base\UnknownPropertyException');
$value2 = $this->component->Caption;
14 years ago
}
public function testSetProperty()
{
$value = 'new value';
$this->component->Text = $value;
$this->assertEquals($value, $this->component->Text);
$this->setExpectedException('yii\base\UnknownPropertyException');
$this->component->NewMember = $value;
14 years ago
}
public function testIsset()
{
$this->assertTrue(isset($this->component->Text));
$this->assertFalse(empty($this->component->Text));
14 years ago
$this->component->Text = '';
14 years ago
$this->assertTrue(isset($this->component->Text));
$this->assertTrue(empty($this->component->Text));
$this->component->Text = null;
$this->assertFalse(isset($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 testCallUnknownMethod()
{
$this->setExpectedException('yii\base\UnknownMethodException');
$this->component->unknownMethod();
}
public function testUnset()
{
unset($this->component->Text);
$this->assertFalse(isset($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);
14 years ago
}
public function testOn()
14 years ago
{
$this->assertFalse($this->component->hasEventHandlers('click'));
$this->component->on('click', 'foo');
$this->assertTrue($this->component->hasEventHandlers('click'));
14 years ago
$this->assertFalse($this->component->hasEventHandlers('click2'));
$p = 'on click2';
$this->component->$p = 'foo2';
$this->assertTrue($this->component->hasEventHandlers('click2'));
14 years ago
}
public function testOff()
14 years ago
{
$this->assertFalse($this->component->hasEventHandlers('click'));
$this->component->on('click', 'foo');
$this->assertTrue($this->component->hasEventHandlers('click'));
$this->component->off('click', 'foo');
$this->assertFalse($this->component->hasEventHandlers('click'));
$this->component->on('click2', 'foo');
$this->component->on('click2', 'foo2');
$this->component->on('click2', 'foo3');
$this->assertTrue($this->component->hasEventHandlers('click2'));
$this->component->off('click2', 'foo3');
$this->assertTrue($this->component->hasEventHandlers('click2'));
$this->component->off('click2');
$this->assertFalse($this->component->hasEventHandlers('click2'));
14 years ago
}
public function testTrigger()
14 years ago
{
$this->component->on('click', array($this->component, 'myEventHandler'));
14 years ago
$this->assertFalse($this->component->eventHandled);
$this->assertNull($this->component->event);
$this->component->raiseEvent();
14 years ago
$this->assertTrue($this->component->eventHandled);
$this->assertEquals('click', $this->component->event->name);
$this->assertEquals($this->component, $this->component->event->sender);
$this->assertFalse($this->component->event->handled);
14 years ago
$eventRaised = false;
$this->component->on('click', function($event) use (&$eventRaised) {
$eventRaised = true;
});
$this->component->raiseEvent();
$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);
14 years ago
}
public function testHasEventHandlers()
14 years ago
{
$this->assertFalse($this->component->hasEventHandlers('click'));
$this->component->on('click', 'foo');
$this->assertTrue($this->component->hasEventHandlers('click'));
14 years ago
}
public function testStopEvent()
{
$component = new NewComponent;
$component->on('click', 'yiiunit\framework\base\globalEventHandler2');
$component->on('click', array($this->component, 'myEventHandler'));
$component->raiseEvent();
14 years ago
$this->assertTrue($component->eventHandled);
$this->assertFalse($this->component->eventHandled);
}
public function testAttachBehavior()
14 years ago
{
$component = new NewComponent;
$this->assertFalse($component->hasProperty('p'));
$this->assertFalse($component->behaviorCalled);
$this->assertNull($component->getBehavior('a'));
14 years ago
14 years ago
$behavior = new NewBehavior;
$component->attachBehavior('a', $behavior);
$this->assertSame($behavior, $component->getBehavior('a'));
$this->assertTrue($component->hasProperty('p'));
$component->test();
$this->assertTrue($component->behaviorCalled);
14 years ago
$this->assertSame($behavior, $component->detachBehavior('a'));
$this->assertFalse($component->hasProperty('p'));
$this->setExpectedException('yii\base\UnknownMethodException');
$component->test();
$p = 'as b';
$component = new NewComponent;
$component->$p = array('class' => 'NewBehavior');
$this->assertSame($behavior, $component->getBehavior('a'));
$this->assertTrue($component->hasProperty('p'));
$component->test();
$this->assertTrue($component->behaviorCalled);
14 years ago
}
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);
}
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'));
}
14 years ago
}
14 years ago
class NewComponent extends Component
14 years ago
{
private $_object = null;
private $_text = 'default';
private $_items = array();
public $content;
14 years ago
public function getText()
{
return $this->_text;
}
public function setText($value)
{
$this->_text = $value;
14 years ago
}
public function getObject()
{
if (!$this->_object) {
$this->_object = new self;
$this->_object->_text = 'object text';
14 years ago
}
return $this->_object;
}
public function getExecute()
{
return function($param) {
return $param * 2;
};
}
public function getItems()
{
return $this->_items;
}
public $eventHandled = false;
public $event;
public $behaviorCalled = false;
public function myEventHandler($event)
14 years ago
{
$this->eventHandled = true;
$this->event = $event;
14 years ago
}
public function raiseEvent()
14 years ago
{
12 years ago
$this->trigger('click', new Event);
14 years ago
}
}
class NewBehavior extends Behavior
14 years ago
{
public $p;
private $p2;
public function getP2()
{
return $this->p2;
}
public function setP2($value)
{
$this->p2 = $value;
}
14 years ago
public function test()
{
$this->owner->behaviorCalled = true;
14 years ago
return 2;
}
}
14 years ago
class NewComponent2 extends Component
14 years ago
{
public $a;
public $b;
public $c;
14 years ago
public function __construct($b, $c)
{
$this->b = $b;
$this->c = $c;
}
}