ObjectTest.php 3.73 KB
Newer Older
Alexander Makarov committed
1
<?php
Qiang Xue committed
2 3 4

namespace yiiunit\framework\base;

Alexander Makarov committed
5 6 7
/**
 * ObjectTest
 */
Qiang Xue committed
8
class ObjectTest extends \yiiunit\TestCase
Alexander Makarov committed
9
{
Qiang Xue committed
10 11 12
	/**
	 * @var NewObject
	 */
Qiang Xue committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26
	protected $object;

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

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

	public function testHasProperty()
	{
Qiang Xue committed
27 28 29 30 31 32
		$this->assertTrue($this->object->hasProperty('Text'));
		$this->assertTrue($this->object->hasProperty('text'));
		$this->assertFalse($this->object->hasProperty('Caption'));
		$this->assertTrue($this->object->hasProperty('content'));
		$this->assertFalse($this->object->hasProperty('content', false));
		$this->assertFalse($this->object->hasProperty('Content'));
Qiang Xue committed
33 34 35 36 37 38 39
	}

	public function testCanGetProperty()
	{
		$this->assertTrue($this->object->canGetProperty('Text'));
		$this->assertTrue($this->object->canGetProperty('text'));
		$this->assertFalse($this->object->canGetProperty('Caption'));
Qiang Xue committed
40 41 42
		$this->assertTrue($this->object->canGetProperty('content'));
		$this->assertFalse($this->object->canGetProperty('content', false));
		$this->assertFalse($this->object->canGetProperty('Content'));
Qiang Xue committed
43 44 45 46 47 48
	}

	public function testCanSetProperty()
	{
		$this->assertTrue($this->object->canSetProperty('Text'));
		$this->assertTrue($this->object->canSetProperty('text'));
Qiang Xue committed
49
		$this->assertFalse($this->object->canSetProperty('Object'));
Qiang Xue committed
50
		$this->assertFalse($this->object->canSetProperty('Caption'));
Qiang Xue committed
51 52 53
		$this->assertTrue($this->object->canSetProperty('content'));
		$this->assertFalse($this->object->canSetProperty('content', false));
		$this->assertFalse($this->object->canSetProperty('Content'));
Qiang Xue committed
54 55 56 57
	}

	public function testGetProperty()
	{
58
		$this->assertTrue('default' === $this->object->Text);
Qiang Xue committed
59
		$this->setExpectedException('yii\base\UnknownPropertyException');
60
		$value2 = $this->object->Caption;
Qiang Xue committed
61 62 63 64
	}

	public function testSetProperty()
	{
65 66
		$value = 'new value';
		$this->object->Text = $value;
Qiang Xue committed
67
		$this->assertEquals($value, $this->object->Text);
Qiang Xue committed
68
		$this->setExpectedException('yii\base\UnknownPropertyException');
69
		$this->object->NewMember = $value;
Qiang Xue committed
70 71 72 73 74
	}

	public function testIsset()
	{
		$this->assertTrue(isset($this->object->Text));
Qiang Xue committed
75
		$this->assertFalse(empty($this->object->Text));
Qiang Xue committed
76

77
		$this->object->Text = '';
Qiang Xue committed
78 79
		$this->assertTrue(isset($this->object->Text));
		$this->assertTrue(empty($this->object->Text));
Qiang Xue committed
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

		$this->object->Text = null;
		$this->assertFalse(isset($this->object->Text));
		$this->assertTrue(empty($this->object->Text));
	}

	public function testUnset()
	{
		unset($this->object->Text);
		$this->assertFalse(isset($this->object->Text));
		$this->assertTrue(empty($this->object->Text));
	}

	public function testArrayProperty()
	{
		$this->assertEquals(array(), $this->object->items);
		// the following won't work
		/*
		$this->object->items[] = 1;
		$this->assertEquals(array(1), $this->object->items);
		*/
	}

	public function testObjectProperty()
	{
		$this->assertTrue($this->object->object instanceof NewObject);
		$this->assertEquals('object text', $this->object->object->text);
		$this->object->object->text = 'new text';
		$this->assertEquals('new text', $this->object->object->text);
	}

	public function testAnonymousFunctionProperty()
	{
		$this->assertEquals(2, $this->object->execute(1));
Qiang Xue committed
114
	}
Alexander Makarov committed
115
}
Qiang Xue committed
116 117 118 119 120 121


class NewObject extends \yii\base\Component
{
	private $_object = null;
	private $_text = 'default';
Qiang Xue committed
122 123
	private $_items = array();
	public $content;
Qiang Xue committed
124 125 126 127 128 129 130 131

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

	public function setText($value)
	{
132
		$this->_text = $value;
Qiang Xue committed
133 134 135 136
	}

	public function getObject()
	{
137 138 139
		if (!$this->_object) {
			$this->_object = new self;
			$this->_object->_text = 'object text';
Qiang Xue committed
140 141 142 143
		}
		return $this->_object;
	}

Qiang Xue committed
144 145 146 147 148 149 150 151
	public function getExecute()
	{
		return function($param) {
			return $param * 2;
		};
	}

	public function getItems()
Qiang Xue committed
152
	{
Qiang Xue committed
153
		return $this->_items;
Qiang Xue committed
154 155
	}
}