ObjectTest.php 4.5 KB
Newer Older
Alexander Makarov committed
1
<?php
Qiang Xue committed
2 3
namespace yiiunit\framework\base;

4 5 6
use yii\base\Object;
use yiiunit\TestCase;

Alexander Makarov committed
7 8 9
/**
 * ObjectTest
 */
10
class ObjectTest extends TestCase
Alexander Makarov committed
11
{
Qiang Xue committed
12 13 14
	/**
	 * @var NewObject
	 */
Qiang Xue committed
15 16
	protected $object;

Carsten Brandt committed
17
	protected function setUp()
Qiang Xue committed
18
	{
Carsten Brandt committed
19
		parent::setUp();
Qiang Xue committed
20 21 22
		$this->object = new NewObject;
	}

Carsten Brandt committed
23
	protected function tearDown()
Qiang Xue committed
24
	{
Carsten Brandt committed
25
		parent::tearDown();
Qiang Xue committed
26 27 28 29 30
		$this->object = null;
	}

	public function testHasProperty()
	{
Qiang Xue committed
31 32 33 34 35 36
		$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
37 38 39 40 41 42 43
	}

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

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

	public function testGetProperty()
	{
62
		$this->assertTrue('default' === $this->object->Text);
Qiang Xue committed
63
		$this->setExpectedException('yii\base\UnknownPropertyException');
64
		$value2 = $this->object->Caption;
Qiang Xue committed
65 66 67 68
	}

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

76 77 78 79 80 81
	public function testSetReadOnlyProperty()
	{
		$this->setExpectedException('yii\base\InvalidCallException');
		$this->object->object = 'test';
	}

Qiang Xue committed
82 83 84
	public function testIsset()
	{
		$this->assertTrue(isset($this->object->Text));
Qiang Xue committed
85
		$this->assertFalse(empty($this->object->Text));
Qiang Xue committed
86

87
		$this->object->Text = '';
Qiang Xue committed
88 89
		$this->assertTrue(isset($this->object->Text));
		$this->assertTrue(empty($this->object->Text));
Qiang Xue committed
90 91 92 93

		$this->object->Text = null;
		$this->assertFalse(isset($this->object->Text));
		$this->assertTrue(empty($this->object->Text));
94 95 96

		$this->assertFalse(isset($this->object->unknownProperty));
		$this->assertTrue(empty($this->object->unknownProperty));
Qiang Xue committed
97 98 99 100 101 102 103 104 105
	}

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

106 107 108 109 110 111 112 113 114 115 116 117
	public function testUnsetReadOnlyProperty()
	{
		$this->setExpectedException('yii\base\InvalidCallException');
		unset($this->object->object);
	}

	public function testCallUnknownMethod()
	{
		$this->setExpectedException('yii\base\UnknownMethodException');
		$this->object->unknownMethod();
	}

Qiang Xue committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	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
139
	}
140 141 142 143 144 145

	public function testConstruct()
	{
		$object = new NewObject(array('text' => 'test text'));
		$this->assertEquals('test text', $object->getText());
	}
Alexander Makarov committed
146
}
Qiang Xue committed
147 148


149
class NewObject extends Object
Qiang Xue committed
150 151 152
{
	private $_object = null;
	private $_text = 'default';
Qiang Xue committed
153 154
	private $_items = array();
	public $content;
Qiang Xue committed
155 156 157 158 159 160 161 162

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

	public function setText($value)
	{
163
		$this->_text = $value;
Qiang Xue committed
164 165 166 167
	}

	public function getObject()
	{
168 169 170
		if (!$this->_object) {
			$this->_object = new self;
			$this->_object->_text = 'object text';
Qiang Xue committed
171 172 173 174
		}
		return $this->_object;
	}

Qiang Xue committed
175 176 177 178 179 180 181 182
	public function getExecute()
	{
		return function($param) {
			return $param * 2;
		};
	}

	public function getItems()
Qiang Xue committed
183
	{
Qiang Xue committed
184
		return $this->_items;
Qiang Xue committed
185
	}
Zander Baldwin committed
186
}