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.
89 lines
2.4 KiB
89 lines
2.4 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\Object;
|
||
11 years ago
|
use yii\di\ServiceLocator;
|
||
11 years ago
|
use yiiunit\TestCase;
|
||
|
|
||
|
class Creator
|
||
|
{
|
||
11 years ago
|
public static function create()
|
||
11 years ago
|
{
|
||
11 years ago
|
return new TestClass;
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
class TestClass extends Object
|
||
|
{
|
||
|
public $prop1 = 1;
|
||
|
public $prop2;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class ServiceLocatorTest extends TestCase
|
||
|
{
|
||
|
public function testCallable()
|
||
|
{
|
||
|
// anonymous function
|
||
11 years ago
|
$container = new ServiceLocator;
|
||
11 years ago
|
$className = TestClass::className();
|
||
11 years ago
|
$container->set($className, function () {
|
||
|
return new TestClass([
|
||
11 years ago
|
'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
|
||
11 years ago
|
$container = new ServiceLocator;
|
||
11 years ago
|
$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();
|
||
11 years ago
|
$container = new ServiceLocator;
|
||
11 years ago
|
$container->set($className, $object);
|
||
|
$this->assertTrue($container->get($className) === $object);
|
||
|
}
|
||
|
|
||
|
public function testShared()
|
||
|
{
|
||
|
// with configuration: shared
|
||
11 years ago
|
$container = new ServiceLocator;
|
||
11 years ago
|
$className = TestClass::className();
|
||
|
$container->set($className, [
|
||
11 years ago
|
'class' => $className,
|
||
11 years ago
|
'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);
|
||
|
}
|
||
|
}
|