示例#1
0
// ->addParameters()
$t->diag('->addParameters()');
$sc = new Container(array('foo' => 'bar'));
$sc->addParameters(array('bar' => 'foo'));
$t->is($sc->getParameters(), array('foo' => 'bar', 'bar' => 'foo'), '->addParameters() adds parameters to the existing ones');
$sc->addParameters(array('Bar' => 'fooz'));
$t->is($sc->getParameters(), array('foo' => 'bar', 'bar' => 'fooz'), '->addParameters() converts keys to lowercase');
// ->setService() ->hasService() ->getService()
$t->diag('->setService() ->hasService() ->getService()');
$sc = new Container();
$sc->setService('foo', $obj = new stdClass());
$t->is(spl_object_hash($sc->getService('foo')), spl_object_hash($obj), '->setService() registers a service under a key name');
$sc->foo1 = $obj1 = new stdClass();
$t->is(spl_object_hash($sc->foo1), spl_object_hash($obj1), '->__set() sets a service');
$t->is(spl_object_hash($sc->foo), spl_object_hash($obj), '->__get() gets a service by name');
$t->ok($sc->hasService('foo'), '->hasService() returns true if the service is defined');
$t->ok(isset($sc->foo), '->__isset() returns true if the service is defined');
$t->ok(!$sc->hasService('bar'), '->hasService() returns false if the service is not defined');
$t->ok(!isset($sc->bar), '->__isset() returns false if the service is not defined');
// ->getServiceIds()
$t->diag('->getServiceIds()');
$sc = new Container();
$sc->setService('foo', $obj = new stdClass());
$sc->setService('bar', $obj = new stdClass());
$t->is($sc->getServiceIds(), array('service_container', 'foo', 'bar'), '->getServiceIds() returns all defined service ids');
class ProjectServiceContainer extends Container
{
    public $__bar, $__foo_bar, $__foo_baz;
    public function __construct()
    {
        parent::__construct();
 public function testServices()
 {
     $sc = new Container();
     $sc->setService('foo', $obj = new \stdClass());
     $this->assertEquals(spl_object_hash($obj), spl_object_hash($sc->getService('foo')), '->setService() registers a service under a key name');
     $sc->foo1 = $obj1 = new \stdClass();
     $this->assertEquals(spl_object_hash($obj1), spl_object_hash($sc->foo1), '->__set() sets a service');
     $this->assertEquals(spl_object_hash($obj), spl_object_hash($sc->foo), '->__get() gets a service by name');
     $this->assertTrue($sc->hasService('foo'), '->hasService() returns true if the service is defined');
     $this->assertTrue(isset($sc->foo), '->__isset() returns true if the service is defined');
     $this->assertFalse($sc->hasService('bar'), '->hasService() returns false if the service is not defined');
     $this->assertFalse(isset($sc->bar), '->__isset() returns false if the service is not defined');
 }