require __DIR__ . '/header.php'; // TESTS: test('Container: can create components', function () { $c = new Container(); $c->register('a', function () { return 'A'; }); $c->register('b', function () { return 'B'; }); ok($c->has('a'), 'first component defined'); ok($c->has('b'), 'second component defined'); ok(!$c->has('c'), 'no third component defined'); $c->set('c', 'C'); ok($c->has('c'), 'third component initialized directly'); ok(!$c->isActive('a'), 'first component not yet active'); ok(!$c->isActive('b'), 'first component not yet active'); eq($c->get('a'), 'A', 'returns the first component'); ok($c->isActive('a'), 'first component activated'); ok(!$c->isActive('b'), 'second component still not active'); eq($c->get('b'), 'B', 'returns the second component'); ok($c->isActive('b'), 'second component activated'); $c->register('x', Foo::class); ok($c->get('x') instanceof Foo, 'registers a default factory function when $func is a name'); $c->register(Foo::class); ok($c->get(Foo::class) instanceof Foo, 'registers a default factory function when $func is NULL'); $c->register(FileCache::class, ['/tmp/foo']); ok($c->get(FileCache::class) instanceof FileCache, 'registers a default factory function when $func is a map'); expect(NotFoundException::class, 'should throw on attempted to get undefined component', function () use($c) { $c->get('nope'); });