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
f0676a80
Commit
f0676a80
authored
Feb 25, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more tests for Behavior and Component
parent
3f6704b8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
122 additions
and
6 deletions
+122
-6
BehaviorTest.php
tests/unit/framework/base/BehaviorTest.php
+4
-0
ComponentTest.php
tests/unit/framework/base/ComponentTest.php
+118
-6
No files found.
tests/unit/framework/base/BehaviorTest.php
View file @
f0676a80
...
...
@@ -42,6 +42,10 @@ class BehaviorTest extends TestCase
$this
->
assertEquals
(
'behavior method'
,
$bar
->
behaviorMethod
());
$this
->
assertEquals
(
'behavior property'
,
$bar
->
getBehavior
(
'bar'
)
->
behaviorProperty
);
$this
->
assertEquals
(
'behavior method'
,
$bar
->
getBehavior
(
'bar'
)
->
behaviorMethod
());
$behavior
=
new
BarBehavior
(
array
(
'behaviorProperty'
=>
'reattached'
));
$bar
->
attachBehavior
(
'bar'
,
$behavior
);
$this
->
assertEquals
(
'reattached'
,
$bar
->
behaviorProperty
);
}
public
function
testAutomaticAttach
()
...
...
tests/unit/framework/base/ComponentTest.php
View file @
f0676a80
<?php
namespace
yiiunit\framework\base
;
use
yii\base\Behavior
;
use
yii\base\Component
;
use
yii\base\Event
;
use
yiiunit\TestCase
;
function
globalEventHandler
(
$event
)
{
$event
->
sender
->
eventHandled
=
true
;
...
...
@@ -13,7 +17,7 @@ function globalEventHandler2($event)
$event
->
handled
=
true
;
}
class
ComponentTest
extends
\yiiunit\
TestCase
class
ComponentTest
extends
TestCase
{
/**
* @var NewComponent
...
...
@@ -29,6 +33,21 @@ class ComponentTest extends \yiiunit\TestCase
{
$this
->
component
=
null
;
}
public
function
testClone
()
{
$component
=
new
NewComponent
();
$behavior
=
new
NewBehavior
();
$component
->
attachBehavior
(
'a'
,
$behavior
);
$this
->
assertSame
(
$behavior
,
$component
->
getBehavior
(
'a'
));
$component
->
on
(
'test'
,
'fake'
);
$this
->
assertEquals
(
1
,
$component
->
getEventHandlers
(
'test'
)
->
count
);
$clone
=
clone
$component
;
$this
->
assertNotSame
(
$component
,
$clone
);
$this
->
assertNull
(
$clone
->
getBehavior
(
'a'
));
$this
->
assertEquals
(
0
,
$clone
->
getEventHandlers
(
'test'
)
->
count
);
}
public
function
testHasProperty
()
{
...
...
@@ -59,6 +78,13 @@ class ComponentTest extends \yiiunit\TestCase
$this
->
assertTrue
(
$this
->
component
->
canSetProperty
(
'content'
));
$this
->
assertFalse
(
$this
->
component
->
canSetProperty
(
'content'
,
false
));
$this
->
assertFalse
(
$this
->
component
->
canSetProperty
(
'Content'
));
// behavior
$this
->
assertFalse
(
$this
->
component
->
canSetProperty
(
'p2'
));
$behavior
=
new
NewBehavior
();
$this
->
component
->
attachBehavior
(
'a'
,
$behavior
);
$this
->
assertTrue
(
$this
->
component
->
canSetProperty
(
'p2'
));
$this
->
component
->
detachBehavior
(
'a'
);
}
public
function
testGetProperty
()
...
...
@@ -89,6 +115,12 @@ class ComponentTest extends \yiiunit\TestCase
$this
->
component
->
Text
=
null
;
$this
->
assertFalse
(
isset
(
$this
->
component
->
Text
));
$this
->
assertTrue
(
empty
(
$this
->
component
->
Text
));
$this
->
assertFalse
(
isset
(
$this
->
component
->
p2
));
$this
->
component
->
attachBehavior
(
'a'
,
new
NewBehavior
());
$this
->
component
->
setP2
(
'test'
);
$this
->
assertTrue
(
isset
(
$this
->
component
->
p2
));
}
public
function
testUnset
()
...
...
@@ -96,6 +128,19 @@ class ComponentTest extends \yiiunit\TestCase
unset
(
$this
->
component
->
Text
);
$this
->
assertFalse
(
isset
(
$this
->
component
->
Text
));
$this
->
assertTrue
(
empty
(
$this
->
component
->
Text
));
$this
->
component
->
attachBehavior
(
'a'
,
new
NewBehavior
());
$this
->
component
->
setP2
(
'test'
);
$this
->
assertEquals
(
'test'
,
$this
->
component
->
getP2
());
unset
(
$this
->
component
->
p2
);
$this
->
assertNull
(
$this
->
component
->
getP2
());
}
public
function
testUnsetReadonly
()
{
$this
->
setExpectedException
(
'yii\base\InvalidCallException'
);
unset
(
$this
->
component
->
object
);
}
public
function
testOn
()
...
...
@@ -147,6 +192,14 @@ class ComponentTest extends \yiiunit\TestCase
});
$this
->
component
->
raiseEvent
();
$this
->
assertTrue
(
$eventRaised
);
// raise event w/o parameters
$eventRaised
=
false
;
$this
->
component
->
on
(
'test'
,
function
(
$event
)
use
(
&
$eventRaised
)
{
$eventRaised
=
true
;
});
$this
->
component
->
trigger
(
'test'
);
$this
->
assertTrue
(
$eventRaised
);
}
public
function
testHasEventHandlers
()
...
...
@@ -193,9 +246,57 @@ class ComponentTest extends \yiiunit\TestCase
$component
->
test
();
$this
->
assertTrue
(
$component
->
behaviorCalled
);
}
public
function
testAttachBehaviors
()
{
$component
=
new
NewComponent
;
$this
->
assertNull
(
$component
->
getBehavior
(
'a'
));
$this
->
assertNull
(
$component
->
getBehavior
(
'b'
));
$behavior
=
new
NewBehavior
;
$component
->
attachBehaviors
(
array
(
'a'
=>
$behavior
,
'b'
=>
$behavior
,
));
$this
->
assertSame
(
array
(
'a'
=>
$behavior
,
'b'
=>
$behavior
),
$component
->
getBehaviors
());
}
public
function
testDetachBehavior
()
{
$component
=
new
NewComponent
;
$behavior
=
new
NewBehavior
;
$component
->
attachBehavior
(
'a'
,
$behavior
);
$this
->
assertSame
(
$behavior
,
$component
->
getBehavior
(
'a'
));
$detachedBehavior
=
$component
->
detachBehavior
(
'a'
);
$this
->
assertSame
(
$detachedBehavior
,
$behavior
);
$this
->
assertNull
(
$component
->
getBehavior
(
'a'
));
$detachedBehavior
=
$component
->
detachBehavior
(
'z'
);
$this
->
assertNull
(
$detachedBehavior
);
}
public
function
testDetachBehaviors
()
{
$component
=
new
NewComponent
;
$behavior
=
new
NewBehavior
;
$component
->
attachBehavior
(
'a'
,
$behavior
);
$this
->
assertSame
(
$behavior
,
$component
->
getBehavior
(
'a'
));
$component
->
attachBehavior
(
'b'
,
$behavior
);
$this
->
assertSame
(
$behavior
,
$component
->
getBehavior
(
'b'
));
$component
->
detachBehaviors
();
$this
->
assertNull
(
$component
->
getBehavior
(
'a'
));
$this
->
assertNull
(
$component
->
getBehavior
(
'b'
));
}
}
class
NewComponent
extends
\yii\base\
Component
class
NewComponent
extends
Component
{
private
$_object
=
null
;
private
$_text
=
'default'
;
...
...
@@ -245,13 +346,24 @@ class NewComponent extends \yii\base\Component
public
function
raiseEvent
()
{
$this
->
trigger
(
'click'
,
new
\yii\base\
Event
(
$this
));
$this
->
trigger
(
'click'
,
new
Event
(
$this
));
}
}
class
NewBehavior
extends
\yii\base\
Behavior
class
NewBehavior
extends
Behavior
{
public
$p
;
private
$p2
;
public
function
getP2
()
{
return
$this
->
p2
;
}
public
function
setP2
(
$value
)
{
$this
->
p2
=
$value
;
}
public
function
test
()
{
...
...
@@ -260,7 +372,7 @@ class NewBehavior extends \yii\base\Behavior
}
}
class
NewComponent2
extends
\yii\base\
Component
class
NewComponent2
extends
Component
{
public
$a
;
public
$b
;
...
...
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