You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.5 KiB
50 lines
1.5 KiB
11 years ago
|
<?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;
|
||
10 years ago
|
use yii\db\Connection;
|
||
11 years ago
|
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();
|
||
11 years ago
|
$instance = Instance::of($className);
|
||
|
|
||
11 years ago
|
$this->assertTrue($instance instanceof Instance);
|
||
11 years ago
|
$this->assertTrue($instance->get($container) instanceof Component);
|
||
|
$this->assertTrue(Instance::ensure($instance, $className, $container) instanceof Component);
|
||
|
$this->assertTrue($instance->get($container) !== Instance::ensure($instance, $className, $container));
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
public function testEnsure()
|
||
|
{
|
||
|
$container = new Container;
|
||
|
$container->set('db', [
|
||
|
'class' => 'yii\db\Connection',
|
||
|
'dsn' => 'test',
|
||
|
]);
|
||
|
|
||
|
$this->assertTrue(Instance::ensure('db', 'yii\db\Connection', $container) instanceof Connection);
|
||
|
$this->assertTrue(Instance::ensure(new Connection, 'yii\db\Connection', $container) instanceof Connection);
|
||
|
$this->assertTrue(Instance::ensure([
|
||
|
'class' => 'yii\db\Connection',
|
||
|
'dsn' => 'test',
|
||
|
], 'yii\db\Connection', $container) instanceof Connection);
|
||
|
}
|
||
11 years ago
|
}
|