public function testOverrideServiceWhenUsingADumpedContainer() { require_once self::$fixturesPath . '/php/services9.php'; require_once self::$fixturesPath . '/includes/foo.php'; $container = new \ProjectServiceContainer(); $container->setService('bar', $bar = new \stdClass()); $container->setParameter('foo_bar', 'foo_bar'); $this->assertEquals($bar, $container->getBarService(), '->setService() overrides an already defined service'); $this->assertEquals($bar, $container->getService('bar'), '->setService() overrides an already defined service'); }
{ return $this->__bar; } protected function getFooBarService() { return $this->__foo_bar; } protected function getFoo_BazService() { return $this->__foo_baz; } } $sc = new ProjectServiceContainer(); $t->is(spl_object_hash($sc->getService('bar')), spl_object_hash($sc->__bar), '->getService() looks for a getXXXService() method'); $t->ok($sc->hasService('bar'), '->hasService() returns true if the service has been defined as a getXXXService() method'); $sc->setService('bar', $bar = new stdClass()); $t->isnt(spl_object_hash($sc->getService('bar')), spl_object_hash($bar), '->getService() prefers to return a service defined with a getXXXService() method than one defined with setService()'); try { $sc->getService('baba'); $t->fail('->getService() thrown an \\InvalidArgumentException if the service does not exist'); } catch (\InvalidArgumentException $e) { $t->pass('->getService() thrown an \\InvalidArgumentException if the service does not exist'); } try { $sc->baba; $t->fail('->__get() thrown an \\InvalidArgumentException if the service does not exist'); } catch (\InvalidArgumentException $e) { $t->pass('->__get() thrown an \\InvalidArgumentException if the service does not exist'); } try { unset($sc->baba);
{ unset($sc->baba); $t->fail('->__unset() thrown an LogicException if you try to remove a service'); } catch (LogicException $e) { $t->pass('->__unset() thrown an LogicException if you try to remove a service'); } $t->is(spl_object_hash($sc->getService('foo_bar')), spl_object_hash($sc->__foo_bar), '->getService() camelizes the service id when looking for a method'); $t->is(spl_object_hash($sc->getService('foo.baz')), spl_object_hash($sc->__foo_baz), '->getService() camelizes the service id when looking for a method'); // Iterator $t->diag('implements Iterator'); $sc = new ProjectServiceContainer(); $sc->setService('foo', $foo = new stdClass()); $services = array(); foreach ($sc as $id => $service) { $services[$id] = spl_object_hash($service); } $t->is($services, array( 'service_container' => spl_object_hash($sc), 'bar' => spl_object_hash($sc->__bar), 'foo_bar' => spl_object_hash($sc->__foo_bar), 'foo.baz' => spl_object_hash($sc->__foo_baz), 'foo' => spl_object_hash($foo)), 'Container implements the Iterator interface'); // __call() $t->diag('__call()');