ContainerTest.php 2.99 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10
<?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\di\Container;
Qiang Xue committed
11 12 13 14
use yii\di\Instance;
use yiiunit\framework\di\stubs\Bar;
use yiiunit\framework\di\stubs\Foo;
use yiiunit\framework\di\stubs\Qux;
Qiang Xue committed
15 16 17 18 19 20 21 22 23 24 25
use yiiunit\TestCase;


/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class ContainerTest extends TestCase
{
    public function testDefault()
    {
Qiang Xue committed
26 27 28 29 30
        $namespace = __NAMESPACE__ . '\stubs';
        $QuxInterface = "$namespace\\QuxInterface";
        $Foo = Foo::className();
        $Bar = Bar::className();
        $Qux = Qux::className();
Qiang Xue committed
31

Qiang Xue committed
32
        // automatic wiring
Qiang Xue committed
33
        $container = new Container;
Qiang Xue committed
34 35 36 37 38
        $container->set($QuxInterface, $Qux);
        $foo = $container->get($Foo);
        $this->assertTrue($foo instanceof $Foo);
        $this->assertTrue($foo->bar instanceof $Bar);
        $this->assertTrue($foo->bar->qux instanceof $Qux);
39 40
        $foo2 = $container->get($Foo);
        $this->assertFalse($foo === $foo2);
Qiang Xue committed
41

Qiang Xue committed
42
        // full wiring
Qiang Xue committed
43
        $container = new Container;
Qiang Xue committed
44 45 46 47 48 49 50 51
        $container->set($QuxInterface, $Qux);
        $container->set($Bar);
        $container->set($Qux);
        $container->set($Foo);
        $foo = $container->get($Foo);
        $this->assertTrue($foo instanceof $Foo);
        $this->assertTrue($foo->bar instanceof $Bar);
        $this->assertTrue($foo->bar->qux instanceof $Qux);
Qiang Xue committed
52

Qiang Xue committed
53
        // wiring by closure
Qiang Xue committed
54
        $container = new Container;
Qiang Xue committed
55 56 57 58 59 60 61 62 63
        $container->set('foo', function () {
            $qux = new Qux;
            $bar = new Bar($qux);
            return new Foo($bar);
        });
        $foo = $container->get('foo');
        $this->assertTrue($foo instanceof $Foo);
        $this->assertTrue($foo->bar instanceof $Bar);
        $this->assertTrue($foo->bar->qux instanceof $Qux);
Qiang Xue committed
64

Qiang Xue committed
65
        // wiring by closure which uses container
Qiang Xue committed
66
        $container = new Container;
Qiang Xue committed
67
        $container->set($QuxInterface, $Qux);
68
        $container->set('foo', function (Container $c, $params, $config) {
Qiang Xue committed
69 70 71 72 73 74
            return $c->get(Foo::className());
        });
        $foo = $container->get('foo');
        $this->assertTrue($foo instanceof $Foo);
        $this->assertTrue($foo->bar instanceof $Bar);
        $this->assertTrue($foo->bar->qux instanceof $Qux);
Qiang Xue committed
75

Qiang Xue committed
76
        // predefined constructor parameters
Qiang Xue committed
77
        $container = new Container;
Qiang Xue committed
78 79 80 81 82 83 84
        $container->set('foo', $Foo, [Instance::of('bar')]);
        $container->set('bar', $Bar, [Instance::of('qux')]);
        $container->set('qux', $Qux);
        $foo = $container->get('foo');
        $this->assertTrue($foo instanceof $Foo);
        $this->assertTrue($foo->bar instanceof $Bar);
        $this->assertTrue($foo->bar->qux instanceof $Qux);
85 86 87 88 89 90 91

        // wiring by closure
        $container = new Container;
        $container->set('qux', new Qux);
        $qux1 = $container->get('qux');
        $qux2 = $container->get('qux');
        $this->assertTrue($qux1 === $qux2);
Qiang Xue committed
92 93
    }
}