Added service providers
parent
fb05a38a96
commit
60fd72cd1a
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Config;
|
||||
|
||||
use Engelsystem\Container\ServiceProvider;
|
||||
|
||||
class ConfigServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register()
|
||||
{
|
||||
$defaultConfigFile = config_path('config.default.php');
|
||||
$configFile = config_path('config.php');
|
||||
|
||||
$config = $this->app->make(Config::class);
|
||||
$this->app->instance('config', $config);
|
||||
|
||||
$config->set(require $defaultConfigFile);
|
||||
|
||||
if (file_exists($configFile)) {
|
||||
$config->set(array_replace_recursive(
|
||||
$config->get(null),
|
||||
require $configFile
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Database;
|
||||
|
||||
use Engelsystem\Container\ServiceProvider;
|
||||
use Exception;
|
||||
use PDO;
|
||||
|
||||
class DatabaseServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register()
|
||||
{
|
||||
$config = $this->app->get('config');
|
||||
Db::connect(
|
||||
'mysql:host=' . $config->get('database')['host'] . ';dbname=' . $config->get('database')['db'] . ';charset=utf8',
|
||||
$config->get('database')['user'],
|
||||
$config->get('database')['pw']
|
||||
) || $this->exitOnError();
|
||||
|
||||
Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
Db::getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function exitOnError()
|
||||
{
|
||||
throw new Exception('Error: Unable to connect to database');
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Exceptions;
|
||||
|
||||
use Engelsystem\Container\ServiceProvider;
|
||||
use Engelsystem\Exceptions\Handler as ExceptionHandler;
|
||||
|
||||
class ExceptionsServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register()
|
||||
{
|
||||
$errorHandler = $this->app->make(ExceptionHandler::class);
|
||||
$this->app->instance('error.handler', $errorHandler);
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Logger;
|
||||
|
||||
use Engelsystem\Container\ServiceProvider;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class LoggerServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register()
|
||||
{
|
||||
$logger = $this->app->make(EngelsystemLogger::class);
|
||||
$this->app->instance('logger', $logger);
|
||||
|
||||
$this->app->bind(LoggerInterface::class, 'logger');
|
||||
$this->app->bind(EngelsystemLogger::class, 'logger');
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Renderer;
|
||||
|
||||
use Engelsystem\Container\ServiceProvider;
|
||||
|
||||
class RendererServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register()
|
||||
{
|
||||
$this->registerRenderer();
|
||||
$this->registerHtmlEngine();
|
||||
}
|
||||
|
||||
public function boot()
|
||||
{
|
||||
$renderer = $this->app->get('renderer');
|
||||
|
||||
foreach ($this->app->tagged('renderer.engine') as $engine) {
|
||||
$renderer->addRenderer($engine);
|
||||
}
|
||||
}
|
||||
|
||||
protected function registerRenderer()
|
||||
{
|
||||
$renderer = $this->app->make(Renderer::class);
|
||||
$this->app->instance('renderer', $renderer);
|
||||
}
|
||||
|
||||
protected function registerHtmlEngine()
|
||||
{
|
||||
$htmlEngine = $this->app->make(HtmlEngine::class);
|
||||
$this->app->instance('renderer.htmlEngine', $htmlEngine);
|
||||
$this->app->tag('renderer.htmlEngine', ['renderer.engine']);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Routing;
|
||||
|
||||
use Engelsystem\Container\ServiceProvider;
|
||||
|
||||
class RoutingServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register()
|
||||
{
|
||||
$urlGenerator = $this->app->make(UrlGenerator::class);
|
||||
$this->app->instance('routing.urlGenerator', $urlGenerator);
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Database;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Config\Config;
|
||||
use Engelsystem\Database\DatabaseServiceProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
|
||||
class DatabaseServiceProviderConnectionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Database\DatabaseServiceProvider::register()
|
||||
*/
|
||||
public function testRegister()
|
||||
{
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|Config $config */
|
||||
$config = $this->getMockBuilder(Config::class)
|
||||
->getMock();
|
||||
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
|
||||
$app = $this->getMockBuilder(Application::class)
|
||||
->setMethods(['get'])
|
||||
->getMock();
|
||||
Application::setInstance($app);
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('get')
|
||||
->with('config')
|
||||
->willReturn($config);
|
||||
|
||||
$config->expects($this->atLeastOnce())
|
||||
->method('get')
|
||||
->with('database')
|
||||
->willReturn($this->getDbConfig());
|
||||
|
||||
$serviceProvider = new DatabaseServiceProvider($app);
|
||||
$serviceProvider->register();
|
||||
}
|
||||
|
||||
private function getDbConfig()
|
||||
{
|
||||
$configValues = require __DIR__ . '/../../../config/config.default.php';
|
||||
$configFile = __DIR__ . '/../../../config/config.php';
|
||||
|
||||
if (file_exists($configFile)) {
|
||||
$configValues = array_replace_recursive($configValues, require $configFile);
|
||||
}
|
||||
|
||||
return $configValues['database'];
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Config;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Config\Config;
|
||||
use Engelsystem\Config\ConfigServiceProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
|
||||
class ConfigServiceProviderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Config\ConfigServiceProvider::register()
|
||||
*/
|
||||
public function testRegister()
|
||||
{
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|Config $config */
|
||||
$config = $this->getMockBuilder(Config::class)
|
||||
->getMock();
|
||||
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
|
||||
$app = $this->getMockBuilder(Application::class)
|
||||
->setMethods(['make', 'instance', 'get'])
|
||||
->getMock();
|
||||
Application::setInstance($app);
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('make')
|
||||
->with(Config::class)
|
||||
->willReturn($config);
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('instance')
|
||||
->with('config', $config);
|
||||
|
||||
$app->expects($this->atLeastOnce())
|
||||
->method('get')
|
||||
->with('path.config')
|
||||
->willReturn(__DIR__ . '/../../../config');
|
||||
|
||||
$config->expects($this->exactly(2))
|
||||
->method('set')
|
||||
->withAnyParameters();
|
||||
|
||||
$config->expects($this->once())
|
||||
->method('get')
|
||||
->with(null)
|
||||
->willReturn([]);
|
||||
|
||||
$serviceProvider = new ConfigServiceProvider($app);
|
||||
$serviceProvider->register();
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Database;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Config\Config;
|
||||
use Engelsystem\Database\DatabaseServiceProvider;
|
||||
use Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
|
||||
class DatabaseServiceProviderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Database\DatabaseServiceProvider::register()
|
||||
* @covers \Engelsystem\Database\DatabaseServiceProvider::exitOnError()
|
||||
*/
|
||||
public function testRegister()
|
||||
{
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|Config $config */
|
||||
$config = $this->getMockBuilder(Config::class)
|
||||
->getMock();
|
||||
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
|
||||
$app = $this->getMockBuilder(Application::class)
|
||||
->setMethods(['get'])
|
||||
->getMock();
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('get')
|
||||
->with('config')
|
||||
->willReturn($config);
|
||||
|
||||
$config->expects($this->atLeastOnce())
|
||||
->method('get')
|
||||
->with('database')
|
||||
->willReturn([
|
||||
'host' => 'localhost',
|
||||
'db' => 'database',
|
||||
'user' => 'user',
|
||||
'pw' => 'password',
|
||||
]);
|
||||
|
||||
$serviceProvider = new DatabaseServiceProvider($app);
|
||||
$this->expectException(Exception::class);
|
||||
|
||||
$serviceProvider->register();
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Exceptions;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Exceptions\ExceptionsServiceProvider;
|
||||
use Engelsystem\Exceptions\Handler as ExceptionHandler;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
|
||||
class ExceptionsServiceProviderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::register()
|
||||
*/
|
||||
public function testRegister()
|
||||
{
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|ExceptionHandler $exceptionHandler */
|
||||
$exceptionHandler = $this->getMockBuilder(ExceptionHandler::class)
|
||||
->getMock();
|
||||
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
|
||||
$app = $this->getMockBuilder(Application::class)
|
||||
->setMethods(['make', 'instance'])
|
||||
->getMock();
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('make')
|
||||
->with(ExceptionHandler::class)
|
||||
->willReturn($exceptionHandler);
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('instance')
|
||||
->with('error.handler', $exceptionHandler);
|
||||
|
||||
$serviceProvider = new ExceptionsServiceProvider($app);
|
||||
$serviceProvider->register();
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Logger;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Logger\EngelsystemLogger;
|
||||
use Engelsystem\Logger\LoggerServiceProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class LoggerServiceProviderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\LoggerServiceProvider::register()
|
||||
*/
|
||||
public function testRegister()
|
||||
{
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|EngelsystemLogger $logger */
|
||||
$logger = $this->getMockBuilder(EngelsystemLogger::class)
|
||||
->getMock();
|
||||
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
|
||||
$app = $this->getMockBuilder(Application::class)
|
||||
->setMethods(['make', 'instance', 'bind'])
|
||||
->getMock();
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('make')
|
||||
->with(EngelsystemLogger::class)
|
||||
->willReturn($logger);
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('instance')
|
||||
->with('logger', $logger);
|
||||
|
||||
$app->expects($this->atLeastOnce())
|
||||
->method('bind')
|
||||
->withConsecutive(
|
||||
[LoggerInterface::class, 'logger'],
|
||||
[EngelsystemLogger::class, 'logger']
|
||||
);
|
||||
|
||||
$serviceProvider = new LoggerServiceProvider($app);
|
||||
$serviceProvider->register();
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Logger;
|
||||
|
||||
use Engelsystem\Renderer\EngineInterface;
|
||||
use Engelsystem\Renderer\HtmlEngine;
|
||||
use Engelsystem\Renderer\Renderer;
|
||||
use Engelsystem\Renderer\RendererServiceProvider;
|
||||
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
|
||||
class RendererServiceProviderTest extends ServiceProviderTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Renderer\RendererServiceProvider::register()
|
||||
* @covers \Engelsystem\Renderer\RendererServiceProvider::registerRenderer()
|
||||
* @covers \Engelsystem\Renderer\RendererServiceProvider::registerHtmlEngine()
|
||||
*/
|
||||
public function testRegister()
|
||||
{
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|Renderer $renderer */
|
||||
$renderer = $this->getMockBuilder(Renderer::class)
|
||||
->getMock();
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|HtmlEngine $htmlEngine */
|
||||
$htmlEngine = $this->getMockBuilder(HtmlEngine::class)
|
||||
->getMock();
|
||||
|
||||
$app = $this->getApp(['make', 'instance', 'tag']);
|
||||
|
||||
$app->expects($this->exactly(2))
|
||||
->method('make')
|
||||
->withConsecutive(
|
||||
[Renderer::class],
|
||||
[HtmlEngine::class]
|
||||
)->willReturnOnConsecutiveCalls(
|
||||
$renderer,
|
||||
$htmlEngine
|
||||
);
|
||||
|
||||
$app->expects($this->exactly(2))
|
||||
->method('instance')
|
||||
->withConsecutive(
|
||||
['renderer', $renderer],
|
||||
['renderer.htmlEngine', $htmlEngine]
|
||||
);
|
||||
|
||||
$this->setExpects($app, 'tag', ['renderer.htmlEngine', ['renderer.engine']]);
|
||||
|
||||
$serviceProvider = new RendererServiceProvider($app);
|
||||
$serviceProvider->register();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Renderer\RendererServiceProvider::boot()
|
||||
*/
|
||||
public function testBoot()
|
||||
{
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|Renderer $renderer */
|
||||
$renderer = $this->getMockBuilder(Renderer::class)
|
||||
->getMock();
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|EngineInterface $engine1 */
|
||||
$engine1 = $this->getMockForAbstractClass(EngineInterface::class);
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|EngineInterface $engine2 */
|
||||
$engine2 = $this->getMockForAbstractClass(EngineInterface::class);
|
||||
|
||||
$app = $this->getApp(['get', 'tagged']);
|
||||
|
||||
$engines = [$engine1, $engine2];
|
||||
|
||||
$this->setExpects($app, 'get', ['renderer'], $renderer);
|
||||
$this->setExpects($app, 'tagged', ['renderer.engine'], $engines);
|
||||
|
||||
$invocation = $renderer
|
||||
->expects($this->exactly(count($engines)))
|
||||
->method('addRenderer');
|
||||
call_user_func_array([$invocation, 'withConsecutive'], $engines);
|
||||
|
||||
$serviceProvider = new RendererServiceProvider($app);
|
||||
$serviceProvider->boot();
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Routing;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Routing\RoutingServiceProvider;
|
||||
use Engelsystem\Routing\UrlGenerator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
|
||||
class RoutingServiceProviderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Routing\RoutingServiceProvider::register()
|
||||
*/
|
||||
public function testRegister()
|
||||
{
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|UrlGenerator $urlGenerator */
|
||||
$urlGenerator = $this->getMockBuilder(UrlGenerator::class)
|
||||
->getMock();
|
||||
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
|
||||
$app = $this->getMockBuilder(Application::class)
|
||||
->setMethods(['make', 'instance'])
|
||||
->getMock();
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('make')
|
||||
->with(UrlGenerator::class)
|
||||
->willReturn($urlGenerator);
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('instance')
|
||||
->with('routing.urlGenerator', $urlGenerator);
|
||||
|
||||
$serviceProvider = new RoutingServiceProvider($app);
|
||||
$serviceProvider->register();
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
|
||||
abstract class ServiceProviderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @param array $methods
|
||||
* @return Application|PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected function getApp($methods = ['make', 'instance'])
|
||||
{
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
|
||||
return $this->getMockBuilder(Application::class)
|
||||
->setMethods($methods)
|
||||
->getMock();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PHPUnit_Framework_MockObject_MockObject $object
|
||||
* @param string $method
|
||||
* @param array $arguments
|
||||
* @param mixed $return
|
||||
*/
|
||||
protected function setExpects($object, $method, $arguments, $return = null)
|
||||
{
|
||||
$invocation = $object->expects($this->once())
|
||||
->method($method);
|
||||
call_user_func_array([$invocation, 'with'], $arguments);
|
||||
|
||||
if (!is_null($return)) {
|
||||
$invocation->willReturn($return);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
use Composer\Autoload\ClassLoader;
|
||||
|
||||
require_once __DIR__ . '/../includes/autoload.php';
|
||||
|
||||
/** @var $loader ClassLoader */
|
||||
$loader->addPsr4('Engelsystem\\Test\\', __DIR__ . '/');
|
Loading…
Reference in New Issue