예제 #1
0
 public function testServices()
 {
     $sc = new Container();
     $sc->setService('foo', $obj = new \stdClass());
     $this->assertEquals(spl_object_hash($sc->getService('foo')), spl_object_hash($obj), '->setService() registers a service under a key name');
     $sc->foo1 = $obj1 = new \stdClass();
     $this->assertEquals(spl_object_hash($sc->foo1), spl_object_hash($obj1), '->__set() sets a service');
     $this->assertEquals(spl_object_hash($sc->foo), spl_object_hash($obj), '->__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->assertTrue(!$sc->hasService('bar'), '->hasService() returns false if the service is not defined');
     $this->assertTrue(!isset($sc->bar), '->__isset() returns false if the service is not defined');
 }
예제 #2
0
$t->ok(isset($sc['Foo']), '->offsetExists() converts the key to lowercase');
$t->ok(!$sc->hasParameter('bar'), '->hasParameter() returns false if a parameter is not defined');
$t->ok(isset($sc['foo']), '->offsetExists() returns true if a parameter is defined');
$t->ok(!isset($sc['bar']), '->offsetExists() returns false if a parameter is not defined');
// ->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
{
예제 #3
0
 public function testGetService()
 {
     $sc = new Container();
     try {
         $sc->getService('');
         $this->fail('->getService() throws a \\InvalidArgumentException exception if the service is empty');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->getService() throws a \\InvalidArgumentException exception if the service is empty');
         $this->assertEquals('The service "" does not exist.', $e->getMessage(), '->getService() throws a \\InvalidArgumentException exception if the service is empty');
     }
 }