From 8c81adc8e83969e90b4c54daf4a396b1094134ff Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Thu, 31 Aug 2017 17:30:54 +0200 Subject: Implemented container --- src/Container/Container.php | 105 +++++++++++++++++++++++++++++++++++ src/Container/ContainerException.php | 11 ++++ src/Container/NotFoundException.php | 10 ++++ 3 files changed, 126 insertions(+) create mode 100644 src/Container/Container.php create mode 100644 src/Container/ContainerException.php create mode 100644 src/Container/NotFoundException.php (limited to 'src/Container') diff --git a/src/Container/Container.php b/src/Container/Container.php new file mode 100644 index 00000000..df2f92fe --- /dev/null +++ b/src/Container/Container.php @@ -0,0 +1,105 @@ +has($id)) { + return $this->resolve($id); + } + + throw new NotFoundException(sprintf('The entry with the id "%s" could not be found')); + } + + /** + * Register a shared entry in the container + * + * @param string $abstract Identifier of the entry to set + * @param mixed $instance Entry + */ + public function instance($abstract, $instance) + { + $this->instances[$abstract] = $instance; + } + + /** + * Returns true if the container can return an entry for the given identifier + * Returns false otherwise + * + * `has($id)` returning true does not mean that `get($id)` will not throw an exception + * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface` + * + * @param string $id Identifier of the entry to look for + * + * @return bool + */ + public function has($id) + { + return isset($this->instances[$id]); + } + + /** + * Get the globally available instance of the container + * + * @return Container + */ + public static function getInstance() + { + if (is_null(static::$instance)) { + static::$instance = new static; + } + + return static::$instance; + } + + /** + * Set the globally available instance of the container + * + * @param Container $container + */ + public static function setInstance(Container $container) + { + static::$instance = $container; + } + + /** + * Resolve the requested object + * + * @param string $abstract + * @return mixed + */ + protected function resolve($abstract) + { + return $this->instances[$abstract]; + } +} diff --git a/src/Container/ContainerException.php b/src/Container/ContainerException.php new file mode 100644 index 00000000..3cdde506 --- /dev/null +++ b/src/Container/ContainerException.php @@ -0,0 +1,11 @@ + Date: Tue, 19 Sep 2017 14:50:20 +0200 Subject: Added Application --- includes/engelsystem_provider.php | 19 ++++++++----------- src/Application.php | 25 +++++++++++++++++++++++++ src/Container/Container.php | 35 +++++++++++++++++++++++------------ src/helpers.php | 8 ++++---- 4 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 src/Application.php (limited to 'src/Container') diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index f3c161a6..e10fdba0 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -1,13 +1,12 @@ instance('container', $container); -$container->instance(ContainerInterface::class, $container); +$app = Application::getInstance(); /** * Load configuration */ $config = new Config(); -$container->instance('config', $config); +$app->instance('config', $config); $config->set(require __DIR__ . '/../config/config.default.php'); if (file_exists(__DIR__ . '/../config/config.php')) { @@ -48,7 +45,7 @@ date_default_timezone_set($config->get('timezone')); * @var Request $request */ $request = Request::createFromGlobals(); -$container->instance('request', $request); +$app->instance('request', $request); /** @@ -64,7 +61,7 @@ if ($config->get('maintenance')) { * Initialize renderer */ $renderer = new Renderer(); -$container->instance('renderer', $renderer); +$app->instance('renderer', $renderer); $renderer->addRenderer(new HtmlEngine()); @@ -72,7 +69,7 @@ $renderer->addRenderer(new HtmlEngine()); * Register error handler */ $errorHandler = new ExceptionHandler(); -$container->instance('error.handler', $errorHandler); +$app->instance('error.handler', $errorHandler); if (config('environment') == 'development') { $errorHandler->setEnvironment(ExceptionHandler::ENV_DEVELOPMENT); ini_set('display_errors', true); @@ -184,7 +181,7 @@ foreach ($includeFiles as $file) { * Init application */ $session = new Session(); -$container->instance('session', $session); +$app->instance('session', $session); $session->start(); $request->setSession($session); diff --git a/src/Application.php b/src/Application.php new file mode 100644 index 00000000..674b3869 --- /dev/null +++ b/src/Application.php @@ -0,0 +1,25 @@ +registerBaseBindings(); + } + + protected function registerBaseBindings() + { + self::setInstance($this); + Container::setInstance($this); + $this->instance('app', $this); + $this->instance('container', $this); + $this->instance(Container::class, $this); + $this->instance(Application::class, $this); + $this->instance(ContainerInterface::class, $this); + } +} diff --git a/src/Container/Container.php b/src/Container/Container.php index df2f92fe..9af5c1e6 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -48,6 +48,17 @@ class Container implements ContainerInterface * @param mixed $instance Entry */ public function instance($abstract, $instance) + { + $this->singleton($abstract, $instance); + } + + /** + * Register a shared entry as singleton in the container + * + * @param string $abstract + * @param mixed $instance + */ + public function singleton($abstract, $instance) { $this->instances[$abstract] = $instance; } @@ -68,10 +79,21 @@ class Container implements ContainerInterface return isset($this->instances[$id]); } + /** + * Resolve the requested object + * + * @param string $abstract + * @return mixed + */ + protected function resolve($abstract) + { + return $this->instances[$abstract]; + } + /** * Get the globally available instance of the container * - * @return Container + * @return self */ public static function getInstance() { @@ -91,15 +113,4 @@ class Container implements ContainerInterface { static::$instance = $container; } - - /** - * Resolve the requested object - * - * @param string $abstract - * @return mixed - */ - protected function resolve($abstract) - { - return $this->instances[$abstract]; - } } diff --git a/src/helpers.php b/src/helpers.php index 733b902d..b942068f 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,15 +1,15 @@ get($id); + return Application::getInstance()->get($id); } /** -- cgit v1.2.3-54-g00ecf From 2cb636b651c889243919d99eda8fa724d5c08392 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 19 Sep 2017 21:50:22 +0200 Subject: Added Container unit test --- src/Container/Container.php | 2 +- tests/Unit/Container/ContainerTest.php | 98 ++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Container/ContainerTest.php (limited to 'src/Container') diff --git a/src/Container/Container.php b/src/Container/Container.php index 9af5c1e6..59a17a04 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -38,7 +38,7 @@ class Container implements ContainerInterface return $this->resolve($id); } - throw new NotFoundException(sprintf('The entry with the id "%s" could not be found')); + throw new NotFoundException(sprintf('The entry with the id "%s" could not be found', $id)); } /** diff --git a/tests/Unit/Container/ContainerTest.php b/tests/Unit/Container/ContainerTest.php new file mode 100644 index 00000000..f0ba24e7 --- /dev/null +++ b/tests/Unit/Container/ContainerTest.php @@ -0,0 +1,98 @@ +instance('foo', $class); + $this->assertSame($class, $container->get('foo')); + } + + /** + * @covers \Engelsystem\Container\Container::get + * @expectedException \Engelsystem\Container\NotFoundException + */ + public function testGetException() + { + $container = new Container(); + + $container->get('not.registered.service'); + } + + /** + * @covers \Engelsystem\Container\Container::instance + * @covers \Engelsystem\Container\Container::resolve + */ + public function testInstance() + { + $container = new Container(); + $class = new class + { + }; + + $container->instance('foo', $class); + $this->assertSame($class, $container->get('foo')); + } + + /** + * @covers \Engelsystem\Container\Container::has + */ + public function testHas() + { + $container = new Container(); + + $this->assertFalse($container->has('test')); + + $class = new class + { + }; + + $container->instance('test', $class); + $this->assertTrue($container->has('test')); + } + + /** + * @covers \Engelsystem\Container\Container::singleton + */ + public function testSingleton() + { + $container = new Container(); + $class = new class + { + }; + + $container->singleton('foo', $class); + $this->assertSame($class, $container->get('foo')); + $this->assertSame($class, $container->get('foo')); + } + + /** + * @covers \Engelsystem\Container\Container::setInstance + * @covers \Engelsystem\Container\Container::getInstance + */ + public function testContainerSingleton() + { + $container0 = new Container(); + $container = Container::getInstance(); + + $this->assertNotSame($container0, $container); + + $container1 = new Container; + Container::setInstance($container1); + + $this->assertSame($container1, Container::getInstance()); + } +} -- cgit v1.2.3-54-g00ecf From 212760d4c93ce14e9ae34ef207bbb8f48a7dd9a7 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Thu, 21 Sep 2017 18:37:37 +0200 Subject: Changed Container to Illuminate/Container @see https://laravel.com/docs/5.5/container @see https://davejamesmiller.com/2017/06/15/laravel-illuminate-container-in-depth --- composer.json | 9 +-- includes/engelsystem_provider.php | 4 +- src/Application.php | 4 +- src/Container/Container.php | 110 +-------------------------------- src/Container/ContainerException.php | 11 ---- src/Container/NotFoundException.php | 10 --- tests/Unit/ApplicationTest.php | 1 + tests/Unit/Container/ContainerTest.php | 104 ------------------------------- 8 files changed, 12 insertions(+), 241 deletions(-) delete mode 100644 src/Container/ContainerException.php delete mode 100644 src/Container/NotFoundException.php delete mode 100644 tests/Unit/Container/ContainerTest.php (limited to 'src/Container') diff --git a/composer.json b/composer.json index 35956e20..6467fc1c 100644 --- a/composer.json +++ b/composer.json @@ -15,11 +15,12 @@ ], "require": { "php": ">=7.0.0", - "erusev/parsedown": "1.6.*", - "twbs/bootstrap": "^3.3", - "symfony/http-foundation": "^3.3", + "erusev/parsedown": "^1.6", + "illuminate/container": "^5.*", "psr/container": "^1.0", - "psr/log": "^1.0" + "psr/log": "^1.0", + "symfony/http-foundation": "^3.3", + "twbs/bootstrap": "^3.3" }, "require-dev": { "phpunit/phpunit": "^6.3" diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index 0de5e0f5..33422bfc 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -106,8 +106,8 @@ Db::getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); */ $logger = new EngelsystemLogger(); $app->instance('logger', $logger); -$app->instance(LoggerInterface::class, $logger); -$app->instance(EngelsystemLogger::class, $logger); +$app->bind(LoggerInterface::class, 'logger'); +$app->bind(EngelsystemLogger::class, 'logger'); /** diff --git a/src/Application.php b/src/Application.php index 674b3869..fa895d77 100644 --- a/src/Application.php +++ b/src/Application.php @@ -14,12 +14,12 @@ class Application extends Container protected function registerBaseBindings() { - self::setInstance($this); + static::setInstance($this); Container::setInstance($this); $this->instance('app', $this); $this->instance('container', $this); $this->instance(Container::class, $this); $this->instance(Application::class, $this); - $this->instance(ContainerInterface::class, $this); + $this->bind(ContainerInterface::class, Application::class); } } diff --git a/src/Container/Container.php b/src/Container/Container.php index 59a17a04..44c57b6f 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -2,115 +2,9 @@ namespace Engelsystem\Container; -use Psr\Container\ContainerExceptionInterface; +use Illuminate\Container\Container as IlluminateContainer; use Psr\Container\ContainerInterface; -use Psr\Container\NotFoundExceptionInterface; -class Container implements ContainerInterface +class Container extends IlluminateContainer implements ContainerInterface { - /** - * The globally available container - * - * @var static - */ - protected static $instance; - - /** - * Contains the shared instances - * - * @var mixed[] - */ - protected $instances = []; - - /** - * Finds an entry of the container by its identifier and returns it - * - * @param string $id Identifier of the entry to look for - * - * @throws NotFoundExceptionInterface No entry was found for **this** identifier - * @throws ContainerExceptionInterface Error while retrieving the entry - * - * @return mixed Entry - */ - public function get($id) - { - if ($this->has($id)) { - return $this->resolve($id); - } - - throw new NotFoundException(sprintf('The entry with the id "%s" could not be found', $id)); - } - - /** - * Register a shared entry in the container - * - * @param string $abstract Identifier of the entry to set - * @param mixed $instance Entry - */ - public function instance($abstract, $instance) - { - $this->singleton($abstract, $instance); - } - - /** - * Register a shared entry as singleton in the container - * - * @param string $abstract - * @param mixed $instance - */ - public function singleton($abstract, $instance) - { - $this->instances[$abstract] = $instance; - } - - /** - * Returns true if the container can return an entry for the given identifier - * Returns false otherwise - * - * `has($id)` returning true does not mean that `get($id)` will not throw an exception - * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface` - * - * @param string $id Identifier of the entry to look for - * - * @return bool - */ - public function has($id) - { - return isset($this->instances[$id]); - } - - /** - * Resolve the requested object - * - * @param string $abstract - * @return mixed - */ - protected function resolve($abstract) - { - return $this->instances[$abstract]; - } - - /** - * Get the globally available instance of the container - * - * @return self - */ - public static function getInstance() - { - if (is_null(static::$instance)) { - static::$instance = new static; - } - - return static::$instance; - } - - /** - * Set the globally available instance of the container - * - * @param Container $container - */ - public static function setInstance(Container $container) - { - static::$instance = $container; - } } diff --git a/src/Container/ContainerException.php b/src/Container/ContainerException.php deleted file mode 100644 index 3cdde506..00000000 --- a/src/Container/ContainerException.php +++ /dev/null @@ -1,11 +0,0 @@ -assertSame($app, $app->get(Container::class)); $this->assertSame($app, $app->get(Application::class)); $this->assertSame($app, $app->get(ContainerInterface::class)); + $this->assertSame($app, Application::getInstance()); $this->assertSame($app, Container::getInstance()); } } diff --git a/tests/Unit/Container/ContainerTest.php b/tests/Unit/Container/ContainerTest.php deleted file mode 100644 index 89c34209..00000000 --- a/tests/Unit/Container/ContainerTest.php +++ /dev/null @@ -1,104 +0,0 @@ -instance('foo', $class); - $this->assertSame($class, $container->get('foo')); - } - - /** - * @covers \Engelsystem\Container\Container::get - * @expectedException \Engelsystem\Container\NotFoundException - */ - public function testGetException() - { - $container = new Container(); - - $container->get('not.registered.service'); - } - - /** - * @covers \Engelsystem\Container\Container::instance - * @covers \Engelsystem\Container\Container::resolve - */ - public function testInstance() - { - $container = new Container(); - $class = new class - { - }; - - $container->instance('foo', $class); - $this->assertSame($class, $container->get('foo')); - } - - /** - * @covers \Engelsystem\Container\Container::has - */ - public function testHas() - { - $container = new Container(); - - $this->assertFalse($container->has('test')); - - $class = new class - { - }; - - $container->instance('test', $class); - $this->assertTrue($container->has('test')); - } - - /** - * @covers \Engelsystem\Container\Container::singleton - */ - public function testSingleton() - { - $container = new Container(); - $class = new class - { - }; - - $container->singleton('foo', $class); - $this->assertSame($class, $container->get('foo')); - $this->assertSame($class, $container->get('foo')); - } - - /** - * @covers \Engelsystem\Container\Container::setInstance - * @covers \Engelsystem\Container\Container::getInstance - */ - public function testContainerSingleton() - { - // Ensure that no container has been initialized - $reflection = new \ReflectionProperty(Container::class, 'instance'); - $reflection->setAccessible(true); - $reflection->setValue(null, null); - $reflection->setAccessible(false); - - $container0 = new Container(); - $container = Container::getInstance(); - - $this->assertNotSame($container0, $container); - - $container1 = new Container; - Container::setInstance($container1); - - $this->assertSame($container1, Container::getInstance()); - } -} -- cgit v1.2.3-54-g00ecf From d49e49c364c1b73e4e4e3b52dc10ee9d0150e447 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Fri, 22 Sep 2017 14:02:02 +0200 Subject: Implemented service provider functionality --- config/app.php | 9 ++ includes/engelsystem_provider.php | 13 ++- includes/helper/internationalization_helper.php | 2 +- src/Application.php | 80 ++++++++++++- src/Container/ServiceProvider.php | 31 +++++ src/helpers.php | 50 +++++--- tests/Unit/ApplicationTest.php | 147 +++++++++++++++++++++++- 7 files changed, 310 insertions(+), 22 deletions(-) create mode 100644 config/app.php create mode 100644 src/Container/ServiceProvider.php (limited to 'src/Container') diff --git a/config/app.php b/config/app.php new file mode 100644 index 00000000..fe0a97c1 --- /dev/null +++ b/config/app.php @@ -0,0 +1,9 @@ + [ + ], +]; diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index e1669c57..3067ab62 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -26,6 +26,13 @@ require_once __DIR__ . '/autoload.php'; $app = new Application(realpath(__DIR__ . DIRECTORY_SEPARATOR . '..')); +/** + * Bootstrap application + */ +$appConfig = $app->make(Config::class); +$appConfig->set(app('path.config') . '/app.php'); +$app->bootstrap($appConfig); + /** * Load configuration */ @@ -40,6 +47,10 @@ if (file_exists(__DIR__ . '/../config/config.php')) { )); } + +/** + * Configure application + */ date_default_timezone_set($config->get('timezone')); @@ -55,7 +66,7 @@ $app->instance('request', $request); /** * Check for maintenance */ -if ($config->get('maintenance')) { +if ($app->get('config')->get('maintenance')) { echo file_get_contents(__DIR__ . '/../templates/maintenance.html'); die(); } diff --git a/includes/helper/internationalization_helper.php b/includes/helper/internationalization_helper.php index efbe5db5..7fa6518b 100644 --- a/includes/helper/internationalization_helper.php +++ b/includes/helper/internationalization_helper.php @@ -36,7 +36,7 @@ function gettext_init() } gettext_locale(); - bindtextdomain('default', realpath(__DIR__ . '/../../locale')); + bindtextdomain('default', app('path.lang')); bind_textdomain_codeset('default', 'UTF-8'); textdomain('default'); } diff --git a/src/Application.php b/src/Application.php index 80538396..b62b28a9 100644 --- a/src/Application.php +++ b/src/Application.php @@ -2,7 +2,9 @@ namespace Engelsystem; +use Engelsystem\Config\Config; use Engelsystem\Container\Container; +use Engelsystem\Container\ServiceProvider; use Psr\Container\ContainerInterface; class Application extends Container @@ -10,6 +12,16 @@ class Application extends Container /** @var string|null */ protected $appPath = null; + /** @var bool */ + protected $isBootstrapped = false; + + /** + * Registered service providers + * + * @var array + */ + protected $serviceProviders = []; + /** * Application constructor. * @@ -36,15 +48,73 @@ class Application extends Container } /** + * @param string|ServiceProvider $provider + * @return ServiceProvider + */ + public function register($provider) + { + if (is_string($provider)) { + $provider = $this->get($provider); + } + + $this->serviceProviders[] = $provider; + + $provider->register(); + + if ($this->isBootstrapped) { + $this->call([$provider, 'boot']); + } + + return $provider; + } + + /** + * Boot service providers + * + * @param Config|null $config + */ + public function bootstrap(Config $config = null) + { + if ($this->isBootstrapped) { + return; + } + + if ($config instanceof Config) { + foreach ($config->get('providers', []) as $provider) { + $this->register($provider); + } + } + + foreach ($this->serviceProviders as $provider) { + $this->call([$provider, 'boot']); + } + + $this->isBootstrapped = true; + } + + protected function registerPaths() + { + $appPath = $this->appPath; + + $this->instance('path', $appPath); + $this->instance('path.config', $appPath . DIRECTORY_SEPARATOR . 'config'); + $this->instance('path.lang', $appPath . DIRECTORY_SEPARATOR . 'locale'); + } + + /** + * Set app base path + * * @param string $appPath * @return static */ public function setAppPath($appPath) { + $appPath = realpath($appPath); $appPath = rtrim($appPath, DIRECTORY_SEPARATOR); $this->appPath = $appPath; - $this->instance('path', $appPath); + + $this->registerPaths(); return $this; } @@ -56,4 +126,12 @@ class Application extends Container { return $this->appPath; } + + /** + * @return bool + */ + public function isBooted() + { + return $this->isBootstrapped; + } } diff --git a/src/Container/ServiceProvider.php b/src/Container/ServiceProvider.php new file mode 100644 index 00000000..2a1bbebf --- /dev/null +++ b/src/Container/ServiceProvider.php @@ -0,0 +1,31 @@ +app = $app; + } + + /** + * Register container bindings + */ + public function register() { } + + /** + * Called after other services had been registered + */ + public function boot() { } +} diff --git a/src/helpers.php b/src/helpers.php index de303963..c3c727ec 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -23,6 +23,15 @@ function app($id = null) return Application::getInstance()->get($id); } +/** + * @param string $path + * @return string + */ +function base_path($path = '') +{ + return app('path') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path); +} + /** * Get or set config values * @@ -46,6 +55,15 @@ function config($key = null, $default = null) return $config->get($key, $default); } +/** + * @param string $path + * @return string + */ +function config_path($path = '') +{ + return app('path.config') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path); +} + /** * @param string $key * @param mixed $default @@ -78,22 +96,6 @@ function session($key = null, $default = null) return $session->get($key, $default); } -/** - * @param string $template - * @param mixed[] $data - * @return Renderer|string - */ -function view($template = null, $data = null) -{ - $renderer = app('renderer'); - - if (is_null($template)) { - return $renderer; - } - - return $renderer->render($template, $data); -} - /** * @param string $path * @param array $parameters @@ -109,3 +111,19 @@ function url($path = null, $parameters = []) return $urlGenerator->to($path, $parameters); } + +/** + * @param string $template + * @param mixed[] $data + * @return Renderer|string + */ +function view($template = null, $data = null) +{ + $renderer = app('renderer'); + + if (is_null($template)) { + return $renderer; + } + + return $renderer->render($template, $data); +} diff --git a/tests/Unit/ApplicationTest.php b/tests/Unit/ApplicationTest.php index 53fe3109..78310134 100644 --- a/tests/Unit/ApplicationTest.php +++ b/tests/Unit/ApplicationTest.php @@ -3,19 +3,23 @@ namespace Engelsystem\Test\Config; use Engelsystem\Application; +use Engelsystem\Config\Config; use Engelsystem\Container\Container; +use Engelsystem\Container\ServiceProvider; use PHPUnit\Framework\TestCase; +use PHPUnit_Framework_MockObject_MockObject; use Psr\Container\ContainerInterface; +use ReflectionClass; class ApplicationTest extends TestCase { /** - * @covers \Engelsystem\Application::__construct - * @covers \Engelsystem\Application::registerBaseBindings + * @covers \Engelsystem\Application::__construct + * @covers \Engelsystem\Application::registerBaseBindings */ public function testConstructor() { - $app = new Application(); + $app = new Application('.'); $this->assertInstanceOf(Container::class, $app); $this->assertInstanceOf(ContainerInterface::class, $app); @@ -27,4 +31,141 @@ class ApplicationTest extends TestCase $this->assertSame($app, Application::getInstance()); $this->assertSame($app, Container::getInstance()); } + + /** + * @covers \Engelsystem\Application::setAppPath + * @covers \Engelsystem\Application::registerPaths + * @covers \Engelsystem\Application::path + */ + public function testAppPath() + { + $app = new Application(); + + $this->assertFalse($app->has('path')); + + $app->setAppPath('.'); + $this->assertTrue($app->has('path')); + $this->assertTrue($app->has('path.config')); + $this->assertTrue($app->has('path.lang')); + + $this->assertEquals(realpath('.'), $app->path()); + $this->assertEquals(realpath('.') . '/config', $app->get('path.config')); + + $app->setAppPath('./../'); + $this->assertEquals(realpath('../') . '/config', $app->get('path.config')); + } + + /** + * @covers \Engelsystem\Application::register + */ + public function testRegister() + { + $app = new Application(); + + $serviceProvider = $this->mockServiceProvider($app, ['register']); + $serviceProvider->expects($this->once()) + ->method('register'); + + $app->register($serviceProvider); + + $anotherServiceProvider = $this->mockServiceProvider($app, ['register', 'boot']); + $anotherServiceProvider->expects($this->once()) + ->method('register'); + $anotherServiceProvider->expects($this->once()) + ->method('boot'); + + $app->bootstrap(); + $app->register($anotherServiceProvider); + } + + /** + * @covers \Engelsystem\Application::register + */ + public function testRegisterBoot() + { + $app = new Application(); + $app->bootstrap(); + + $serviceProvider = $this->mockServiceProvider($app, ['register', 'boot']); + $serviceProvider->expects($this->once()) + ->method('register'); + $serviceProvider->expects($this->once()) + ->method('boot'); + + $app->register($serviceProvider); + } + + /** + * @covers \Engelsystem\Application::register + */ + public function testRegisterClassName() + { + $app = new Application(); + + $mockClassName = $this->getMockClass(ServiceProvider::class); + $serviceProvider = $this->getMockBuilder($mockClassName) + ->setConstructorArgs([$app]) + ->setMethods(['register']) + ->getMock(); + + $serviceProvider->expects($this->once()) + ->method('register'); + + $app->instance($mockClassName, $serviceProvider); + $app->register($mockClassName); + } + + /** + * @covers \Engelsystem\Application::bootstrap + * @covers \Engelsystem\Application::isBooted + */ + public function testBootstrap() + { + /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ + $app = $this->getMockBuilder(Application::class) + ->setMethods(['register']) + ->getMock(); + + $serviceProvider = $this->mockServiceProvider($app, ['boot']); + $serviceProvider->expects($this->once()) + ->method('boot'); + + $app->expects($this->once()) + ->method('register') + ->with($serviceProvider); + + $config = $this->getMockBuilder(Config::class) + ->getMock(); + + $config->expects($this->once()) + ->method('get') + ->with('providers') + ->willReturn([$serviceProvider]); + + $property = (new ReflectionClass($app))->getProperty('serviceProviders'); + $property->setAccessible(true); + $property->setValue($app, [$serviceProvider]); + + $app->bootstrap($config); + + $this->assertTrue($app->isBooted()); + + // Run bootstrap another time to ensure that providers are registered only once + $app->bootstrap($config); + } + + /** + * @param Application $app + * @param array $methods + * @return PHPUnit_Framework_MockObject_MockObject|ServiceProvider + */ + protected function mockServiceProvider(Application $app, $methods = []) + { + $serviceProvider = $this->getMockBuilder(ServiceProvider::class) + ->setConstructorArgs([$app]) + ->setMethods($methods) + ->getMockForAbstractClass(); + + return $serviceProvider; + } } -- cgit v1.2.3-54-g00ecf