ObjectTest.php 4.41 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
 * @group base
Alexander Makarov committed
9
 */
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
		$this->mockApplication();
Qiang Xue committed
21 22 23
		$this->object = new NewObject;
	}

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

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

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

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

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

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

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

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

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

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

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

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

107 108 109 110 111 112 113 114 115 116 117 118
	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
119 120
	public function testArrayProperty()
	{
Alexander Makarov committed
121
		$this->assertEquals([], $this->object->items);
Qiang Xue committed
122 123 124
		// the following won't work
		/*
		$this->object->items[] = 1;
Alexander Makarov committed
125
		$this->assertEquals([1], $this->object->items);
Qiang Xue committed
126 127 128 129 130 131 132 133 134 135 136
		*/
	}

	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);
	}

137 138
	public function testConstruct()
	{
Alexander Makarov committed
139
		$object = new NewObject(['text' => 'test text']);
140 141
		$this->assertEquals('test text', $object->getText());
	}
Alexander Makarov committed
142
}
Qiang Xue committed
143 144


145
class NewObject extends Object
Qiang Xue committed
146 147 148
{
	private $_object = null;
	private $_text = 'default';
Alexander Makarov committed
149
	private $_items = [];
Qiang Xue committed
150
	public $content;
Qiang Xue committed
151 152 153 154 155 156 157 158

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

	public function setText($value)
	{
159
		$this->_text = $value;
Qiang Xue committed
160 161 162 163
	}

	public function getObject()
	{
164 165 166
		if (!$this->_object) {
			$this->_object = new self;
			$this->_object->_text = 'object text';
Qiang Xue committed
167 168 169 170
		}
		return $this->_object;
	}

Qiang Xue committed
171 172
	public function getExecute()
	{
Alexander Makarov committed
173
		return function ($param) {
Qiang Xue committed
174 175 176 177 178
			return $param * 2;
		};
	}

	public function getItems()
Qiang Xue committed
179
	{
Qiang Xue committed
180
		return $this->_items;
Qiang Xue committed
181
	}
Zander Baldwin committed
182
}