ObjectTest.php 4.46 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 17 18 19 20 21 22 23 24 25 26 27 28
	protected $object;

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

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

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

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

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

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

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

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

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

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

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

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

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

104 105 106 107 108 109 110 111 112 113 114 115
	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
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	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
137
	}
138 139 140 141 142 143

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


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

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

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

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

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

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