Yii2 Bootstrap 3
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.

244 lines
6.4 KiB

14 years ago
<?php
namespace yiiunit\framework\base;
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 \yiiunit\TestCase
14 years ago
{
/**
* @var NewComponent
*/
14 years ago
protected $component;
public function setUp()
{
$this->component = new NewComponent();
}
public function tearDown()
{
$this->component = null;
}
public function testHasProperty()
{
$this->assertTrue($this->component->hasProperty('Text'), "Component hasn't property Text");
$this->assertTrue($this->component->hasProperty('text'), "Component hasn't property text");
$this->assertFalse($this->component->hasProperty('Caption'), "Component as property Caption");
}
public function testCanGetProperty()
{
$this->assertTrue($this->component->canGetProperty('Text'));
$this->assertTrue($this->component->canGetProperty('text'));
$this->assertFalse($this->component->canGetProperty('Caption'));
}
public function testCanSetProperty()
{
$this->assertTrue($this->component->canSetProperty('Text'));
$this->assertTrue($this->component->canSetProperty('text'));
$this->assertFalse($this->component->canSetProperty('Caption'));
}
public function testGetProperty()
{
$this->assertTrue('default' === $this->component->Text);
$this->setExpectedException('yii\base\Exception');
$value2 = $this->component->Caption;
14 years ago
}
public function testSetProperty()
{
$value = 'new value';
$this->component->Text = $value;
$text = $this->component->Text;
$this->assertTrue($value === $this->component->Text);
$this->setExpectedException('yii\base\Exception');
$this->component->NewMember = $value;
14 years ago
}
public function testIsset()
{
$this->assertTrue(isset($this->component->Text));
$this->assertTrue(!empty($this->component->Text));
unset($this->component->Text);
$this->assertFalse(isset($this->component->Text));
$this->assertFalse(!empty($this->component->Text));
$this->component->Text = '';
14 years ago
$this->assertTrue(isset($this->component->Text));
$this->assertTrue(empty($this->component->Text));
}
public function testOn()
14 years ago
{
$this->assertEquals(0, $this->component->getEventHandlers('click')->getCount());
$this->component->on('click', 'foo');
$this->assertEquals(1, $this->component->getEventHandlers('click')->getCount());
$this->component->on('click', 'bar');
$this->assertEquals(2, $this->component->getEventHandlers('click')->getCount());
14 years ago
$this->component->getEventHandlers('click')->add('test');
$this->assertEquals(3, $this->component->getEventHandlers('click')->getCount());
14 years ago
}
public function testOff()
14 years ago
{
$this->component->on('click', 'foo');
$this->component->on('click', array($this->component, 'myEventHandler'));
$this->assertEquals(2, $this->component->getEventHandlers('click')->getCount());
14 years ago
$result = $this->component->off('click', 'foo');
$this->assertTrue($result);
$this->assertEquals(1, $this->component->getEventHandlers('click')->getCount());
$result = $this->component->off('click', 'foo');
$this->assertFalse($result);
$this->assertEquals(1, $this->component->getEventHandlers('click')->getCount());
$result = $this->component->off('click', array($this->component, 'myEventHandler'));
$this->assertTrue($result);
$this->assertEquals(0, $this->component->getEventHandlers('click')->getCount());
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);
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);
}
14 years ago
public function testDetachBehavior()
{
$component = new NewComponent;
14 years ago
$behavior = new NewBehavior;
$component->attachBehavior('a', $behavior);
$this->assertSame($behavior, $component->detachBehavior('a'));
14 years ago
}
14 years ago
public function testDetachingBehaviors()
{
$component = new NewComponent;
14 years ago
$behavior = new NewBehavior;
$component->attachBehavior('a', $behavior);
14 years ago
$component->detachBehaviors();
$this->setExpectedException('yii\base\Exception');
14 years ago
$component->test();
}
14 years ago
public function testGetBehavior()
14 years ago
{
$component = new NewComponent;
14 years ago
$behavior = new NewBehavior;
$component->attachBehavior('a', $behavior);
$this->assertSame($behavior, $component->getBehavior('a'));
14 years ago
}
14 years ago
14 years ago
public function testCreate()
{
$component = NewComponent2::newInstance(array('a' => 3), 1, 2);
14 years ago
$this->assertEquals(1, $component->b);
$this->assertEquals(2, $component->c);
$this->assertEquals(3, $component->a);
}
14 years ago
}
14 years ago
class NewComponent extends \yii\base\Component
{
private $_object = null;
private $_text = 'default';
public $eventHandled = false;
public $event;
14 years ago
public $behaviorCalled = false;
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 NewComponent;
$this->_object->_text = 'object text';
14 years ago
}
return $this->_object;
}
public function myEventHandler($event)
14 years ago
{
$this->eventHandled = true;
$this->event = $event;
14 years ago
}
public function raiseEvent()
14 years ago
{
$this->trigger('click', new \yii\base\Event($this));
14 years ago
}
}
class NewBehavior extends \yii\base\Behavior
{
public function test()
{
$this->owner->behaviorCalled = true;
14 years ago
return 2;
}
}
14 years ago
class NewComponent2 extends \yii\base\Component
{
public $a;
public $b;
public $c;
14 years ago
public function __construct($b, $c)
{
$this->b = $b;
$this->c = $c;
}
}