Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
faef457f
Commit
faef457f
authored
Jan 07, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed Object class.
parent
ac9b3159
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
39 deletions
+67
-39
Object.php
framework/base/Object.php
+3
-5
ObjectTest.php
tests/unit/framework/base/ObjectTest.php
+64
-34
No files found.
framework/base/Object.php
View file @
faef457f
...
...
@@ -107,7 +107,6 @@ class Object
{
$getter
=
'get'
.
$name
;
if
(
method_exists
(
$this
,
$getter
))
{
// property is not null
return
$this
->
$getter
()
!==
null
;
}
else
{
return
false
;
...
...
@@ -129,7 +128,6 @@ class Object
{
$setter
=
'set'
.
$name
;
if
(
method_exists
(
$this
,
$setter
))
{
// write property
$this
->
$setter
(
null
);
}
elseif
(
method_exists
(
$this
,
'get'
.
$name
))
{
throw
new
BadPropertyException
(
'Unsetting read-only property: '
.
get_class
(
$this
)
.
'.'
.
$name
);
...
...
@@ -150,9 +148,9 @@ class Object
*/
public
function
__call
(
$name
,
$params
)
{
if
(
$this
->
canGetProperty
(
$name
,
false
))
{
$getter
=
'get'
.
$name
;
$func
=
$this
->
$getter
;
$getter
=
'get'
.
$name
;
if
(
method_exists
(
$this
,
$getter
))
{
$func
=
$this
->
$getter
()
;
if
(
$func
instanceof
\Closure
)
{
return
call_user_func_array
(
$func
,
$params
);
}
...
...
tests/unit/framework/base/ObjectTest.php
View file @
faef457f
...
...
@@ -2,33 +2,14 @@
namespace
yiiunit\framework\base
;
class
Foo
extends
\yii\base\Object
{
public
$prop
;
}
class
Bar
extends
\yii\base\Component
{
public
$prop1
;
public
$prop2
;
public
$prop3
;
public
function
__construct
(
$a
,
$b
)
{
$this
->
prop1
=
$a
+
$b
;
}
public
function
init
()
{
$this
->
prop3
=
3
;
}
}
/**
* ObjectTest
*/
class
ObjectTest
extends
\yiiunit\TestCase
{
/**
* @var NewObject
*/
protected
$object
;
public
function
setUp
()
...
...
@@ -43,9 +24,12 @@ class ObjectTest extends \yiiunit\TestCase
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"
);
$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'
));
}
public
function
testCanGetProperty
()
...
...
@@ -53,13 +37,20 @@ class ObjectTest extends \yiiunit\TestCase
$this
->
assertTrue
(
$this
->
object
->
canGetProperty
(
'Text'
));
$this
->
assertTrue
(
$this
->
object
->
canGetProperty
(
'text'
));
$this
->
assertFalse
(
$this
->
object
->
canGetProperty
(
'Caption'
));
$this
->
assertTrue
(
$this
->
object
->
canGetProperty
(
'content'
));
$this
->
assertFalse
(
$this
->
object
->
canGetProperty
(
'content'
,
false
));
$this
->
assertFalse
(
$this
->
object
->
canGetProperty
(
'Content'
));
}
public
function
testCanSetProperty
()
{
$this
->
assertTrue
(
$this
->
object
->
canSetProperty
(
'Text'
));
$this
->
assertTrue
(
$this
->
object
->
canSetProperty
(
'text'
));
$this
->
assertFalse
(
$this
->
object
->
canSetProperty
(
'Object'
));
$this
->
assertFalse
(
$this
->
object
->
canSetProperty
(
'Caption'
));
$this
->
assertTrue
(
$this
->
object
->
canSetProperty
(
'content'
));
$this
->
assertFalse
(
$this
->
object
->
canSetProperty
(
'content'
,
false
));
$this
->
assertFalse
(
$this
->
object
->
canSetProperty
(
'Content'
));
}
public
function
testGetProperty
()
...
...
@@ -73,8 +64,7 @@ class ObjectTest extends \yiiunit\TestCase
{
$value
=
'new value'
;
$this
->
object
->
Text
=
$value
;
$text
=
$this
->
object
->
Text
;
$this
->
assertTrue
(
$value
===
$this
->
object
->
Text
);
$this
->
assertEquals
(
$value
,
$this
->
object
->
Text
);
$this
->
setExpectedException
(
'yii\base\BadPropertyException'
);
$this
->
object
->
NewMember
=
$value
;
}
...
...
@@ -82,15 +72,45 @@ class ObjectTest extends \yiiunit\TestCase
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
->
assertFalse
(
empty
(
$this
->
object
->
Text
));
$this
->
object
->
Text
=
''
;
$this
->
assertTrue
(
isset
(
$this
->
object
->
Text
));
$this
->
assertTrue
(
empty
(
$this
->
object
->
Text
));
$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
));
}
}
...
...
@@ -99,6 +119,8 @@ class NewObject extends \yii\base\Component
{
private
$_object
=
null
;
private
$_text
=
'default'
;
private
$_items
=
array
();
public
$content
;
public
function
getText
()
{
...
...
@@ -119,8 +141,15 @@ class NewObject extends \yii\base\Component
return
$this
->
_object
;
}
public
function
exprEvaluator
(
$p1
,
$comp
)
public
function
getExecute
()
{
return
function
(
$param
)
{
return
$param
*
2
;
};
}
public
function
getItems
()
{
return
"Hello
$p1
"
;
return
$this
->
_items
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment