/** * @testdox The modified command bus has an execute method that takes a Query as a parameter */ public function testTheCommandBusHasAnExecuteMethodThatTakesAQueryAsAParameter() { $this->expectOutputString(sprintf("LOG: Starting %1\$s\nLOG: Ending %1\$s\n", "Joomla\\Tests\\Unit\\Service\\Stubs\\SimpleQuery")); $container = new Container(); $container->set('EventDispatcher', $this->getMockBuilder(DispatcherInterface::class)->getMock()); $container->set('CommandBusMiddleware', [new LoggingMiddleware(new Logger())]); $container->registerServiceProvider(new CommandBusServiceProvider()); $commandBus = $container->get('CommandBus'); $this->assertEquals('XSome contentY', $commandBus->handle(new SimpleQuery('Some content'))); }
/** * @testdox The ConfigServiceProvider adds an config to a container with variables from the environment */ public function testConfigServiceProviderCreatesConfigFromEnv() { $container = new Container(); $container->set('ConfigDirectory', __DIR__ . '/data'); $container->set('ConfigFileName', 'env.txt'); $service = new ConfigServiceProvider(); $service->register($container); /** @var Registry $config * */ $config = $container->get('config'); $this->assertEquals('bar', $config->get('foo')); }
/** * @testdox The existence of a resource can be checked */ public function testExists() { $container = new Container(); $container->set('foo', 'bar'); $this->assertTrue($container->has('foo'), "'foo' should be present"); $this->assertFalse($container->has('baz'), "'baz' should not be present"); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns itself to support chaining. * * @since 1.0 */ public function register(Container $container) { $app = $this->app; $container->set('app', function () use($app) { return $app; }, true, true); }
/** * Add the plugin factory to a container * * @param Container $container The container * @param string $alias An optional alias * * @return void */ public function register(Container $container, $alias = null) { $container->set($this->key, [$this, 'createExtensionFactory'], true, true); if (!empty($alias)) { $container->alias($alias, $this->key); } }
/** * @param Container $container The DI container * @param string $alias An optional alias * * @return void */ public function register(Container $container, $alias = null) { $container->set('Request', [$this, 'createRequest'], true, true); if ($alias) { $container->alias($alias, 'Request'); } }
/** * @param Container $container The DI container * @param string $alias An optional alias * * @return void */ public function register(Container $container, $alias = null) { $container->set('Session', [$this, 'createSession'], true, true); if ($alias) { $container->alias($alias, 'Session'); } }
/** * @param Container $container The DI container * @param string $alias An optional alias * * @return void */ public function register(Container $container, $alias = null) { $container->set('CommandBus', [$this, 'createCommandBus'], true, true); if ($alias) { $container->alias($alias, 'CommandBus'); } }
/** * Registers a RepositoryFactory with the container * * @param Container $container The DI container * @param string $alias An optional alias * * @return void */ public function register(Container $container, $alias = null) { $container->set('Repository', [$this, 'createRepositoryFactory'], true, true); if (!empty($alias)) { $container->alias($alias, 'Repository'); } }
/** * Tests the getNew method which will always return a * new instance, even if the $key was set to be shared. * * @return void * * @since 1.0 */ public function testGetNewInstance() { $this->fixture->set('foo', function () { return new \stdClass(); }); $this->assertNotSame($this->fixture->getNewInstance('foo'), $this->fixture->getNewInstance('foo')); }
/** * Add the dispatcher to a container * * @param Container $container The container * @param string $alias An optional alias * * @return void */ public function register(Container $container, $alias = null) { $container->set($this->key, [$this, 'createDispatcher'], true, true); if (!empty($alias)) { $container->alias($alias, $this->key); } }
/** * Registers the service provider within a DI container. * * @param Container $container The DI container. * * @return void * * @since 2.0 */ public function register(Container $container) { $that = $this; $container->set('mustache', function ($c) use($that) { return $that->getMustache($c); }); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns itself to support chaining. * * @since 1.0 */ public function register(Container $container) { $container->set('Joomla\\Database\\DatabaseDriver', function () { return (new DatabaseDriver())->create($this->test, 'mysqli'); }, true, true); // Alias the database $container->alias('db', 'Joomla\\Database\\DatabaseDriver'); }
/** * @testdox The EventDispatcherServiceProvider adds an EventDispatcher to a container with an alias */ public function testEventDispatcherServiceProviderCreatesDispatcherWithAlias() { $container = new Container(); $container->set('extension_factory', $this->getMockBuilder(ExtensionFactoryInterface::class)->getMock()); $service = new EventDispatcherServiceProvider(); $service->register($container, 'unit'); $this->assertInstanceOf(DispatcherInterface::class, $container->get('unit')); }
/** * @testdox The ExtensionFactoryServiceProvider adds an ExtensionFactory to a container with an alias */ public function testExtensionFactoryServiceProviderCreatesExtensionFactoryWithAlias() { $container = new Container(); $container->set('ConfigDirectory', __DIR__); $service = new ExtensionFactoryServiceProvider(); $service->register($container, 'unit'); $this->assertInstanceOf(ExtensionFactoryInterface::class, $container->get('unit')); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns itself to support chaining. * * @since 1.0 * @throws \RuntimeException */ public function register(Container $container) { $container->set('App\\Debug\\TrackerDebugger', function () use($container) { return new TrackerDebugger($container); }, true, true); // Alias the object $container->alias('debugger', 'App\\Debug\\TrackerDebugger'); }
/** * @testdox has() also resolves the alias if set. */ public function testExistsResolvesAlias() { $container = new Container(); $container->set('foo', function () { return new \stdClass(); }, true, true)->alias('bar', 'foo'); $this->assertTrue($container->has('foo'), "Original 'foo' was not resolved"); $this->assertTrue($container->has('bar'), "Alias 'bar' was not resolved"); }
/** * Add the configuration from the environment to a container * * @param Container $container The container * @param string $alias An optional alias, defaults to 'config' * * @return void */ public function register(Container $container, $alias = 'config') { $file = '.env'; if ($container->has('ConfigFileName')) { $file = $container->get('ConfigFileName'); } $dotenv = new Dotenv($container->get('ConfigDirectory'), $file); $dotenv->overload(); $container->set($alias, new Registry($_ENV), true); }
/** * @testdox The SessionServiceProvider adds an SessionInterface to a container with an alias */ public function testSessionServiceProviderCreatesDispatcherWithAlias() { $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); $request->method('getCookieParams')->willReturn(['unit' => 'test']); $container = new Container(); $container->set('Request', $request); $service = new SessionServiceProvider(); $service->register($container, 'unit'); $this->assertInstanceOf(SessionInterface::class, $container->get('unit')); }
/** * @testdox Child container resolves parent's alias to parent's resource */ public function testChildResolveAlias() { $container = new Container(); $container->set('Joomla\\Tests\\Unit\\DI\\StubInterface', function () { return new Stub1(); }); $container->alias('stub', 'Joomla\\Tests\\Unit\\DI\\StubInterface'); $child = $container->createChild(); $this->assertInstanceOf('Joomla\\Tests\\Unit\\DI\\Stub1', $child->get('stub')); }
/** * @testdox Scalar resources can be extended */ public function testExtendScalar() { $container = new Container(); $container->set('foo', 'bar'); $this->assertEquals('bar', $container->get('foo')); $container->extend('foo', function ($originalResult, Container $c) { return $originalResult . 'baz'; }); $this->assertEquals('barbaz', $container->get('foo')); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns itself to support chaining. */ public function register(Container $container) { $server = new ApiServer($this->element, $this->uri, $this->option); $container->set('api.server', $server); $server->register(); // Listener $dispatcher = $container->get('event.dispatcher'); /** @var $dispatcher \JEventDispatcher */ $dispatcher->attach(new ApiListener($this->element, $dispatcher)); }
/** * Prepare and return currency object. * * <code> * $this->prepareCurrency($container, $params); * </code> * * @param Container $container * @param Registry $params * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \OutOfBoundsException */ protected function prepareCurrency($container, $params) { $currencyId = $params->get('project_currency'); $currencyHash = StringHelper::generateMd5Hash(Constants::CONTAINER_CURRENCY, $currencyId); // Get the currency from the container. if (!$container->exists($currencyHash)) { $currency = new Currency(\JFactory::getDbo()); $currency->load($currencyId); $container->set($currencyHash, $currency); } }
public function setUp() { $this->dataDirectory = __DIR__ . '/tmp'; $this->mkdir($this->dataDirectory . '/entities'); $container = new Container(); $container->set('ConfigDirectory', JPATH_ROOT); $container->registerServiceProvider(new StorageServiceProvider(), 'repository'); $container->registerServiceProvider(new EventDispatcherServiceProvider(), 'dispatcher'); $container->registerServiceProvider(new ExtensionFactoryServiceProvider(), 'extension_factory'); $this->installer = new Installer($this->dataDirectory, $container); }
/** * Prepare profile object and inject it in the container. * * <code> * $userId = 1; * * $this->prepareProfile($container, $params, $userId); * </code> * * @param Container $container * @param Registry $params * @param int $userId * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \UnexpectedValueException * @throws \OutOfBoundsException * * @return Project */ protected function prepareProfile($container, $params, $userId) { $userId = (int) abs($userId); $options = array('platform' => $params->get('integration_social_platform'), 'user_id' => $userId); $hash = StringHelper::generateMd5Hash(Constants::CONTAINER_PROFILE, $options); if (!$container->exists($hash) and $userId > 0) { $config = new Registry($options); $socialProfileBuilder = new Factory($config); $socialProfile = $socialProfileBuilder->create(); $container->set($hash, $socialProfile); } }
/** * {@inheritdoc} */ public function register(Container $container) { $container->set('Joomla\\Database\\DatabaseDriver', function () use($container) { $config = $container->get('config'); $options = array('driver' => $config->get('database.driver'), 'host' => $config->get('database.host'), 'user' => $config->get('database.user'), 'password' => $config->get('database.password'), 'database' => $config->get('database.name'), 'prefix' => $config->get('database.prefix')); $db = DatabaseDriver::getInstance($options); $db->setDebug($config->get('debug.database', false)); return $db; }, true, true); // Alias the database $container->alias('db', 'Joomla\\Database\\DatabaseDriver'); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns the container to support chaining. * * @since 1.0 * @throws \RuntimeException */ public function register(Container $container) { return $container->set('BabDev\\Transifex\\Transifex', function () use($container) { $options = new Registry(); /* @var \JTracker\Application $app */ $app = $container->get('app'); $options->set('api.username', $app->get('transifex.username')); $options->set('api.password', $app->get('transifex.password')); // Instantiate Transifex return new Transifex($options); })->alias('transifex', 'BabDev\\Transifex\\Transifex'); }
/** * Prepare profile object from Proof of Identity and inject it in the container. * * <code> * $userId = 1; * * $this->prepareProofProfile($container, $userId); * </code> * * @param Container $container * @param int $userId * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \UnexpectedValueException * @throws \OutOfBoundsException * * @return User */ protected function prepareProofProfile($container, $userId) { $userId = (int) abs($userId); $hash = StringHelper::generateMd5Hash(Constants::CONTAINER_PROOF_PROFILE, $userId); if (!$container->exists($hash) and $userId > 0) { $proof = new User(\JFactory::getDbo()); $proof->load($userId); if (!$proof->getId()) { $proof = null; } $container->set($hash, $proof); } }
/** * @testdox Loading an invalid string */ public function testLoadInvalidString() { $this->setExpectedException(ParseException::class); $content = <<<EOF providers unit test foo: class = \\SimpleServiceProvider EOF; $container = new Container(); $container->set('unit-test', 'called from case'); $loader = new YamlLoader($container); $loader->load($content); }
/** * Prepare reward object and inject it in the container. * * <code> * $rewardId = 1; * $projectId = 2; * * $this->prepareReward($container, $rewardId, $projectId); * </code> * * @param Container $container * @param int $rewardId * @param int $projectId * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \UnexpectedValueException * @throws \OutOfBoundsException * * @return Reward */ protected function prepareReward($container, $rewardId, $projectId) { $rewardId = (int) abs($rewardId); $hash = StringHelper::generateMd5Hash(Constants::CONTAINER_REWARD, array($rewardId, $projectId)); if (!$container->exists($hash) and $rewardId > 0) { $reward = new Reward(\JFactory::getDbo()); $reward->load(array('id' => $rewardId, 'project_id' => $projectId)); if (!$reward->getId()) { $reward = null; } $container->set($hash, $reward); } }