ObjectTest.php 3.1 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
class Foo extends \yii\base\Object
{
	public $prop;
}

Qiang Xue committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
class Bar extends \yii\base\Component implements \yii\base\Initable
{
	public $prop1;
	public $prop2;
	public $prop3;

	public function __construct($a, $b)
	{
		$this->prop1 = $a + $b;
	}

	public function init()
	{
		$this->prop3 = 3;
	}
}

Alexander Makarov committed
27 28 29
/**
 * ObjectTest
 */
Qiang Xue committed
30
class ObjectTest extends \yiiunit\TestCase
Alexander Makarov committed
31
{
Qiang Xue committed
32 33 34 35 36 37 38 39 40 41 42 43
	protected $object;

	public function setUp()
	{
		$this->object = new NewObject;
	}

	public function tearDown()
	{
		$this->object = null;
	}

Qiang Xue committed
44
	public function testNewInstance()
Alexander Makarov committed
45
	{
Qiang Xue committed
46
		$foo = Foo::newInstance(array(
Alexander Makarov committed
47 48 49 50 51 52
			'prop' => array(
				'test' => 'test',
			),
		));

		$this->assertEquals('test', $foo->prop['test']);
Qiang Xue committed
53

Qiang Xue committed
54
		$bar = Bar::newInstance(array(), 10, 20);
Qiang Xue committed
55 56 57 58
		$this->assertEquals(30, $bar->prop1);
		$this->assertEquals(null, $bar->prop2);
		$this->assertEquals(3, $bar->prop3);

Qiang Xue committed
59
		$bar = Bar::newInstance(array(
Qiang Xue committed
60 61
			'prop2' => 'x',
			'prop3' => 400,
Qiang Xue committed
62
		), 100, 200);
Qiang Xue committed
63 64 65
		$this->assertEquals(300, $bar->prop1);
		$this->assertEquals('x', $bar->prop2);
		$this->assertEquals(3, $bar->prop3);
Alexander Makarov committed
66
	}
Qiang Xue committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

	public function testHasProperty()
	{
		$this->assertTrue($this->object->hasProperty('Text'), "Component hasn't property Text");
		$this->assertTrue($this->object->hasProperty('text'), "Component hasn't property text");
		$this->assertFalse($this->object->hasProperty('Caption'), "Component as property Caption");
	}

	public function testCanGetProperty()
	{
		$this->assertTrue($this->object->canGetProperty('Text'));
		$this->assertTrue($this->object->canGetProperty('text'));
		$this->assertFalse($this->object->canGetProperty('Caption'));
	}

	public function testCanSetProperty()
	{
		$this->assertTrue($this->object->canSetProperty('Text'));
		$this->assertTrue($this->object->canSetProperty('text'));
		$this->assertFalse($this->object->canSetProperty('Caption'));
	}

	public function testGetProperty()
	{
		$this->assertTrue('default'===$this->object->Text);
		$this->setExpectedException('yii\base\Exception');
		$value2=$this->object->Caption;
	}

	public function testSetProperty()
	{
		$value='new value';
		$this->object->Text=$value;
		$text=$this->object->Text;
		$this->assertTrue($value===$this->object->Text);
		$this->setExpectedException('yii\base\Exception');
		$this->object->NewMember=$value;
	}

	public function testIsset()
	{
		$this->assertTrue(isset($this->object->Text));
		$this->assertTrue(!empty($this->object->Text));

		unset($this->object->Text);
		$this->assertFalse(isset($this->object->Text));
		$this->assertFalse(!empty($this->object->Text));

		$this->object->Text='';
		$this->assertTrue(isset($this->object->Text));
		$this->assertTrue(empty($this->object->Text));
	}
Alexander Makarov committed
119
}
Qiang Xue committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151


class NewObject extends \yii\base\Component
{
	private $_object = null;
	private $_text = 'default';

	public function getText()
	{
		return $this->_text;
	}

	public function setText($value)
	{
		$this->_text=$value;
	}

	public function getObject()
	{
		if(!$this->_object)
		{
			$this->_object=new self;
			$this->_object->_text='object text';
		}
		return $this->_object;
	}

	public function exprEvaluator($p1,$comp)
	{
		return "Hello $p1";
	}
}