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
43fb937a
Commit
43fb937a
authored
Mar 18, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added unit tests.
parent
530d7baa
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
215 additions
and
4 deletions
+215
-4
Container.php
framework/di/Container.php
+1
-1
ContainerInterface.php
framework/di/ContainerInterface.php
+1
-1
ContainerTrait.php
framework/di/ContainerTrait.php
+2
-2
ContainerTest.php
tests/unit/framework/di/ContainerTest.php
+179
-0
InstanceTest.php
tests/unit/framework/di/InstanceTest.php
+32
-0
No files found.
framework/di/Container.php
View file @
43fb937a
...
...
@@ -85,7 +85,7 @@ use yii\base\Component;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
Container
extends
Component
class
Container
extends
Component
implements
ContainerInterface
{
use
ContainerTrait
;
...
...
framework/di/ContainerInterface.php
View file @
43fb937a
...
...
@@ -58,7 +58,7 @@ interface ContainerInterface
* If a component is not shared, this method will create a new instance every time.
*
* @param string $typeOrID component type (a fully qualified namespaced class/interface name, e.g. `yii\db\Connection`) or ID (e.g. `db`).
* @param array $params the
named parameters (name => value)
to be passed to the object constructor
* @param array $params the
parameters
to be passed to the object constructor
* if the method needs to create a new object instance.
* @param boolean $create whether to create an instance of a component if it is not previously created.
* This is mainly useful for shared instance.
...
...
framework/di/ContainerTrait.php
View file @
43fb937a
...
...
@@ -53,7 +53,7 @@ trait ContainerTrait
* If a component is not shared, this method will create a new instance every time.
*
* @param string $typeOrID component type (a fully qualified namespaced class/interface name, e.g. `yii\db\Connection`) or ID (e.g. `db`).
* @param array $params the
named parameters (name => value)
to be passed to the object constructor
* @param array $params the
parameters
to be passed to the object constructor
* if the method needs to create a new object instance.
* @param boolean $create whether to create an instance of a component if it is not previously created.
* This is mainly useful for shared instance.
...
...
@@ -89,7 +89,7 @@ trait ContainerTrait
$component
=
$this
->
get
(
$definition
,
$params
);
}
elseif
(
$definition
instanceof
Closure
||
is_array
(
$definition
)
&&
isset
(
$definition
[
0
],
$definition
[
1
]))
{
// a PHP callable
$component
=
call_user_func
(
$definition
,
$params
,
$this
);
$component
=
call_user_func
(
$definition
,
$
typeOrID
,
$
params
,
$this
);
}
elseif
(
is_object
(
$definition
))
{
// an object
$component
=
$definition
;
...
...
tests/unit/framework/di/ContainerTest.php
0 → 100644
View file @
43fb937a
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yiiunit\framework\di
;
use
yii\base\Object
;
use
yii\di\Container
;
use
yiiunit\TestCase
;
class
Creator
{
public
static
function
create
(
$type
,
$params
,
$container
)
{
return
new
$type
;
}
}
class
TestClass
extends
Object
{
public
$prop1
=
1
;
public
$prop2
;
}
class
TestClass2
extends
Object
{
public
$prop1
=
1
;
public
$prop2
;
public
function
__construct
(
$a
,
$config
=
[])
{
$this
->
prop1
=
$a
;
parent
::
__construct
(
$config
);
}
}
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
ContainerTest
extends
TestCase
{
public
function
testDefault
()
{
// without configuring anything
$container
=
new
Container
;
$className
=
TestClass
::
className
();
$object
=
$container
->
get
(
$className
);
$this
->
assertEquals
(
1
,
$object
->
prop1
);
$this
->
assertTrue
(
$object
instanceof
$className
);
// check non-shared
$object2
=
$container
->
get
(
$className
);
$this
->
assertTrue
(
$object2
instanceof
$className
);
$this
->
assertTrue
(
$object
!==
$object2
);
}
public
function
testCallable
()
{
// anonymous function
$container
=
new
Container
;
$className
=
TestClass
::
className
();
$container
->
set
(
$className
,
function
(
$type
)
{
return
new
$type
([
'prop1'
=>
100
,
'prop2'
=>
200
,
]);
});
$object
=
$container
->
get
(
$className
);
$this
->
assertTrue
(
$object
instanceof
$className
);
$this
->
assertEquals
(
100
,
$object
->
prop1
);
$this
->
assertEquals
(
200
,
$object
->
prop2
);
// static method
$container
=
new
Container
;
$className
=
TestClass
::
className
();
$container
->
set
(
$className
,
[
__NAMESPACE__
.
"
\\
Creator"
,
'create'
]);
$object
=
$container
->
get
(
$className
);
$this
->
assertTrue
(
$object
instanceof
$className
);
$this
->
assertEquals
(
1
,
$object
->
prop1
);
$this
->
assertNull
(
$object
->
prop2
);
}
public
function
testObject
()
{
$object
=
new
TestClass
;
$className
=
TestClass
::
className
();
$container
=
new
Container
;
$container
->
set
(
$className
,
$object
);
$this
->
assertTrue
(
$container
->
get
(
$className
)
===
$object
);
}
public
function
testString
()
{
$object
=
new
TestClass
;
$className
=
TestClass
::
className
();
$container
=
new
Container
;
$container
->
set
(
'test'
,
$object
);
$container
->
set
(
$className
,
'test'
);
$this
->
assertTrue
(
$container
->
get
(
$className
)
===
$object
);
}
public
function
testShared
()
{
// with configuration: shared
$container
=
new
Container
;
$className
=
TestClass
::
className
();
$container
->
set
(
$className
,
[
'prop1'
=>
10
,
'prop2'
=>
20
,
]);
$object
=
$container
->
get
(
$className
);
$this
->
assertEquals
(
10
,
$object
->
prop1
);
$this
->
assertEquals
(
20
,
$object
->
prop2
);
$this
->
assertTrue
(
$object
instanceof
$className
);
// check shared
$object2
=
$container
->
get
(
$className
);
$this
->
assertTrue
(
$object2
instanceof
$className
);
$this
->
assertTrue
(
$object
===
$object2
);
// check leading slash is handled properly
$object3
=
$container
->
get
(
'\\'
.
$className
);
$this
->
assertTrue
(
$object3
instanceof
$className
);
$this
->
assertTrue
(
$object
===
$object3
);
}
public
function
testNonShared
()
{
// with configuration: non-shared
$container
=
new
Container
;
$className
=
TestClass
::
className
();
$container
->
set
(
'*'
.
$className
,
[
'prop1'
=>
10
,
'prop2'
=>
20
,
]);
$object
=
$container
->
get
(
$className
);
$this
->
assertEquals
(
10
,
$object
->
prop1
);
$this
->
assertEquals
(
20
,
$object
->
prop2
);
$this
->
assertTrue
(
$object
instanceof
$className
);
// check non-shared
$object2
=
$container
->
get
(
$className
);
$this
->
assertTrue
(
$object2
instanceof
$className
);
$this
->
assertTrue
(
$object
!==
$object2
);
// check leading slash is handled properly
$object3
=
$container
->
get
(
'\\'
.
$className
);
$this
->
assertTrue
(
$object3
instanceof
$className
);
$this
->
assertTrue
(
$object
!==
$object3
);
// shared as non-shared
$object
=
new
TestClass
;
$className
=
TestClass
::
className
();
$container
=
new
Container
;
$container
->
set
(
'*'
.
$className
,
$object
);
$this
->
assertTrue
(
$container
->
get
(
$className
)
===
$object
);
}
public
function
testRegisterByID
()
{
$className
=
TestClass
::
className
();
$container
=
new
Container
;
$container
->
set
(
'test'
,
[
'class'
=>
$className
,
'prop1'
=>
100
,
]);
$object
=
$container
->
get
(
'test'
);
$this
->
assertTrue
(
$object
instanceof
TestClass
);
$this
->
assertEquals
(
100
,
$object
->
prop1
);
}
public
function
testConstructorParams
()
{
// with configuration: shared
$container
=
new
Container
;
$className
=
TestClass2
::
className
();
$object
=
$container
->
get
(
$className
,
[
100
]);
$this
->
assertEquals
(
100
,
$object
->
prop1
);
}
}
tests/unit/framework/di/InstanceTest.php
0 → 100644
View file @
43fb937a
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yiiunit\framework\di
;
use
yii\base\Component
;
use
yii\base\Object
;
use
yii\di\Container
;
use
yii\di\Instance
;
use
yiiunit\TestCase
;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
InstanceTest
extends
TestCase
{
public
function
testOf
()
{
$container
=
new
Container
;
$className
=
Component
::
className
();
$instance
=
Instance
::
of
(
$className
,
$container
);
$this
->
assertTrue
(
$instance
instanceof
Instance
);
$this
->
assertTrue
(
$instance
->
get
()
instanceof
Component
);
$this
->
assertTrue
(
Instance
::
ensure
(
$instance
,
$className
)
instanceof
Component
);
$this
->
assertTrue
(
$instance
->
get
()
!==
Instance
::
ensure
(
$instance
,
$className
));
}
}
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