commit
801c17aa6c
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem;
|
||||
|
||||
use Engelsystem\Container\Container;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class Application extends Container
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Container;
|
||||
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class Container 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;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Container;
|
||||
|
||||
use Exception;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
|
||||
class ContainerException extends Exception implements ContainerExceptionInterface
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Container;
|
||||
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class NotFoundException extends ContainerException implements NotFoundExceptionInterface
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Logger;
|
||||
|
||||
use Psr\Log\AbstractLogger;
|
||||
use Psr\Log\InvalidArgumentException;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
class EngelsystemLogger extends AbstractLogger
|
||||
{
|
||||
protected $allowedLevels = [
|
||||
LogLevel::ALERT,
|
||||
LogLevel::CRITICAL,
|
||||
LogLevel::DEBUG,
|
||||
LogLevel::EMERGENCY,
|
||||
LogLevel::ERROR,
|
||||
LogLevel::INFO,
|
||||
LogLevel::NOTICE,
|
||||
LogLevel::WARNING,
|
||||
];
|
||||
|
||||
/**
|
||||
* Logs with an arbitrary level.
|
||||
*
|
||||
* @TODO: Implement $context['exception']
|
||||
*
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function log($level, $message, array $context = [])
|
||||
{
|
||||
if (!$this->checkLevel($level)) {
|
||||
throw new InvalidArgumentException();
|
||||
}
|
||||
|
||||
$message = $this->interpolate($message, $context);
|
||||
|
||||
LogEntry_create($level, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Interpolates context values into the message placeholders.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @return string
|
||||
*/
|
||||
protected function interpolate($message, array $context = [])
|
||||
{
|
||||
foreach ($context as $key => $val) {
|
||||
// check that the value can be casted to string
|
||||
if (is_array($val) || (is_object($val) && !method_exists($val, '__toString'))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// replace the values of the message
|
||||
$message = str_replace('{' . $key . '}', $val, $message);
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $level
|
||||
* @return bool
|
||||
*/
|
||||
protected function checkLevel($level)
|
||||
{
|
||||
return in_array($level, $this->allowedLevels);
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
<?php
|
||||
namespace Engelsystem\Test;
|
||||
|
||||
use \PHPUnit\Framework\TestCase;
|
||||
|
||||
class LogEntriesModelTest extends TestCase
|
||||
{
|
||||
|
||||
public function create_LogEntry()
|
||||
{
|
||||
LogEntry_create('test', 'test');
|
||||
}
|
||||
|
||||
public function test_LogEntry_create()
|
||||
{
|
||||
$count = count(LogEntries());
|
||||
$this->assertNotFalse(LogEntry_create('test', 'test_LogEntry_create'));
|
||||
|
||||
// There should be one more log entry now
|
||||
$this->assertEquals(count(LogEntries()), $count + 1);
|
||||
}
|
||||
|
||||
public function test_LogEntries_clear_all()
|
||||
{
|
||||
$this->create_LogEntry();
|
||||
$this->assertTrue(count(LogEntries()) > 0);
|
||||
$this->assertNotFalse(LogEntries_clear_all());
|
||||
$this->assertEquals(count(LogEntries()), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @after
|
||||
*/
|
||||
public function teardown()
|
||||
{
|
||||
LogEntries_clear_all();
|
||||
}
|
||||
}
|
@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Logger;
|
||||
|
||||
use Engelsystem\Logger\EngelsystemLogger;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\InvalidArgumentException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
class EngelsystemLoggerTest extends TestCase
|
||||
{
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
require_once __DIR__ . '/../../../includes/engelsystem_provider.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return LoggerInterface
|
||||
*/
|
||||
public function getLogger()
|
||||
{
|
||||
return new EngelsystemLogger();
|
||||
}
|
||||
|
||||
public function testImplements()
|
||||
{
|
||||
$this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function provideLogLevels()
|
||||
{
|
||||
return [
|
||||
[LogLevel::ALERT],
|
||||
[LogLevel::CRITICAL],
|
||||
[LogLevel::DEBUG],
|
||||
[LogLevel::EMERGENCY],
|
||||
[LogLevel::ERROR],
|
||||
[LogLevel::INFO],
|
||||
[LogLevel::NOTICE],
|
||||
[LogLevel::WARNING],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideLogLevels
|
||||
* @param string $level
|
||||
*/
|
||||
public function testAllLevels($level)
|
||||
{
|
||||
$logger = $this->getLogger();
|
||||
|
||||
LogEntries_clear_all();
|
||||
|
||||
$logger->log($level, 'First log message');
|
||||
$logger->{$level}('Second log message');
|
||||
|
||||
$entries = LogEntries();
|
||||
$this->assertCount(2, $entries);
|
||||
}
|
||||
|
||||
public function testContextReplacement()
|
||||
{
|
||||
$logger = $this->getLogger();
|
||||
LogEntries_clear_all();
|
||||
|
||||
$logger->log(LogLevel::INFO, 'My username is {username}', ['username' => 'Foo']);
|
||||
|
||||
$entry = $this->getLastEntry();
|
||||
$this->assertEquals('My username is Foo', $entry['message']);
|
||||
$this->assertEquals(LogLevel::INFO, $entry['level']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function provideContextReplaceValues()
|
||||
{
|
||||
return [
|
||||
['Data and {context}', [], 'Data and {context}'],
|
||||
['Data and {context}', ['context' => null], 'Data and '],
|
||||
['Data and {context}', ['context' => new \stdClass()], 'Data and {context}'],
|
||||
['Some user asked: {question}', ['question' => 'Foo?'], 'Some user asked: Foo?'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideContextReplaceValues
|
||||
*
|
||||
* @param string $message
|
||||
* @param string[] $context
|
||||
* @param string $expected
|
||||
*/
|
||||
public function testContextReplaceValues($message, $context, $expected)
|
||||
{
|
||||
$logger = $this->getLogger();
|
||||
$logger->log(LogLevel::INFO, $message, $context);
|
||||
|
||||
$entry = $this->getLastEntry();
|
||||
$this->assertEquals($expected, $entry['message']);
|
||||
}
|
||||
|
||||
public function testContextToString()
|
||||
{
|
||||
$logger = $this->getLogger();
|
||||
LogEntries_clear_all();
|
||||
|
||||
$mock = $this->getMockBuilder('someDataProvider')
|
||||
->setMethods(['__toString'])
|
||||
->getMock();
|
||||
|
||||
$mock->expects($this->atLeastOnce())
|
||||
->method('__toString')
|
||||
->will($this->returnValue('FooBar'));
|
||||
|
||||
$logger->log(LogLevel::INFO, 'Some data and {context}', ['context' => $mock]);
|
||||
|
||||
$entry = $this->getLastEntry();
|
||||
$this->assertEquals('Some data and FooBar', $entry['message']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testThrowExceptionOnInvalidLevel()
|
||||
{
|
||||
$logger = $this->getLogger();
|
||||
|
||||
$logger->log('This log level should never be defined', 'Some message');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getLastEntry()
|
||||
{
|
||||
$entries = LogEntries();
|
||||
$entry = array_pop($entries);
|
||||
|
||||
return $entry;
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
LogEntries_clear_all();
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
class LogEntriesModelTest extends TestCase
|
||||
{
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
require_once __DIR__ . '/../../../includes/engelsystem_provider.php';
|
||||
}
|
||||
|
||||
public function testCreateLogEntry()
|
||||
{
|
||||
LogEntries_clear_all();
|
||||
$count = count(LogEntries());
|
||||
$this->assertNotFalse(LogEntry_create(LogLevel::WARNING, 'test_LogEntry_create'));
|
||||
|
||||
// There should be one more log entry now
|
||||
$this->assertEquals(count(LogEntries()), $count + 1);
|
||||
}
|
||||
|
||||
public function testClearAllLogEntries()
|
||||
{
|
||||
LogEntry_create(LogLevel::WARNING, 'test');
|
||||
$this->assertTrue(count(LogEntries()) > 0);
|
||||
|
||||
$this->assertNotFalse(LogEntries_clear_all());
|
||||
$this->assertCount(0, LogEntries());
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
LogEntries_clear_all();
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Config;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Container\Container;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class ApplicationTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Application::__construct
|
||||
* @covers \Engelsystem\Application::registerBaseBindings
|
||||
*/
|
||||
public function testConstructor()
|
||||
{
|
||||
$app = new Application();
|
||||
|
||||
$this->assertInstanceOf(Container::class, $app);
|
||||
$this->assertInstanceOf(ContainerInterface::class, $app);
|
||||
$this->assertSame($app, $app->get('app'));
|
||||
$this->assertSame($app, $app->get('container'));
|
||||
$this->assertSame($app, $app->get(Container::class));
|
||||
$this->assertSame($app, $app->get(Application::class));
|
||||
$this->assertSame($app, $app->get(ContainerInterface::class));
|
||||
$this->assertSame($app, Container::getInstance());
|
||||
}
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Config;
|
||||
|
||||
use Engelsystem\Config\Config;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ConfigTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Config\Config::get
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
$config = new Config();
|
||||
|
||||
$config->set('test', 'FooBar');
|
||||
$this->assertEquals(['test' => 'FooBar'], $config->get(null));
|
||||
$this->assertEquals('FooBar', $config->get('test'));
|
||||
|
||||
$this->assertEquals('defaultValue', $config->get('notExisting', 'defaultValue'));
|
||||
|
||||
$this->assertNull($config->get('notExisting'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Config\Config::set
|
||||
*/
|
||||
public function testSet()
|
||||
{
|
||||
$config = new Config();
|
||||
|
||||
$config->set('test', 'FooBar');
|
||||
$this->assertEquals('FooBar', $config->get('test'));
|
||||
|
||||
$config->set([
|
||||
'name' => 'Engelsystem',
|
||||
'mail' => ['user' => 'test'],
|
||||
]);
|
||||
$this->assertEquals('Engelsystem', $config->get('name'));
|
||||
$this->assertEquals(['user' => 'test'], $config->get('mail'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Config\Config::has
|
||||
*/
|
||||
public function testHas()
|
||||
{
|
||||
$config = new Config();
|
||||
|
||||
$this->assertFalse($config->has('test'));
|
||||
|
||||
$config->set('test', 'FooBar');
|
||||
$this->assertTrue($config->has('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Config\Config::remove
|
||||
*/
|
||||
public function testRemove()
|
||||
{
|
||||
$config = new Config();
|
||||
$config->set(['foo' => 'bar', 'test' => '123']);
|
||||
|
||||
$config->remove('foo');
|
||||
$this->assertEquals(['test' => '123'], $config->get(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Config\Config::__get
|
||||
*/
|
||||
public function testMagicGet()
|
||||
{
|
||||
$config = new Config();
|
||||
|
||||
$config->set('test', 'FooBar');
|
||||
$this->assertEquals('FooBar', $config->test);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Config\Config::__set
|
||||
*/
|
||||
public function testMagicSet()
|
||||
{
|
||||
$config = new Config();
|
||||
|
||||
$config->test = 'FooBar';
|
||||
$this->assertEquals('FooBar', $config->get('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Config\Config::__isset
|
||||
*/
|
||||
public function testMagicIsset()
|
||||
{
|
||||
$config = new Config();
|
||||
|
||||
$this->assertFalse(isset($config->test));
|
||||
|
||||
$config->set('test', 'FooBar');
|
||||
$this->assertTrue(isset($config->test));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Config\Config::__unset
|
||||
*/
|
||||
public function testMagicUnset()
|
||||
{
|
||||
$config = new Config();
|
||||
$config->set(['foo' => 'bar', 'test' => '123']);
|
||||
|
||||
unset($config->foo);
|
||||
$this->assertEquals(['test' => '123'], $config->get(null));
|
||||
}
|
||||
}
|
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Config;
|
||||
|
||||
use Engelsystem\Container\Container;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ContainerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Container\Container::get
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
$container = new Container();
|
||||
$class = new class
|
||||
{
|
||||
};
|
||||
|
||||
$container->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());
|
||||
}
|
||||
}
|
@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Config;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Config\Config;
|
||||
use Engelsystem\Container\Container;
|
||||
use Engelsystem\Http\Request;
|
||||
use Engelsystem\Renderer\Renderer;
|
||||
use Engelsystem\Routing\UrlGenerator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
|
||||
class HelpersTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \app
|
||||
*/
|
||||
public function testApp()
|
||||
{
|
||||
$class = new class
|
||||
{
|
||||
};
|
||||
|
||||
$appMock = $this->getAppMock('some.name', $class);
|
||||
|
||||
$this->assertEquals($appMock, app());
|
||||
$this->assertEquals($class, app('some.name'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \config
|
||||
*/
|
||||
public function testConfig()
|
||||
{
|
||||
$configMock = $this->getMockBuilder(Config::class)
|
||||
->getMock();
|
||||
|
||||
$this->getAppMock('config', $configMock);
|
||||
$this->assertEquals($configMock, config());
|
||||
|
||||
$configMock->expects($this->once())
|
||||
->method('set')
|
||||
->with(['foo' => 'bar']);
|
||||
|
||||
$this->assertTrue(config(['foo' => 'bar']));
|
||||
|
||||
$configMock->expects($this->once())
|
||||
->method('get')
|
||||
->with('mail')
|
||||
->willReturn(['user' => 'FooBar']);
|
||||
|
||||
$this->assertEquals(['user' => 'FooBar'], config('mail'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \request
|
||||
*/
|
||||
public function testRequest()
|
||||
{
|
||||
$requestMock = $this->getMockBuilder(Request::class)
|
||||
->getMock();
|
||||
|
||||
$this->getAppMock('request', $requestMock);
|
||||
$this->assertEquals($requestMock, request());
|
||||
|
||||
$requestMock->expects($this->once())
|
||||
->method('input')
|
||||
->with('requestKey')
|
||||
->willReturn('requestValue');
|
||||
|
||||
$this->assertEquals('requestValue', request('requestKey'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \session
|
||||
*/
|
||||
public function testSession()
|
||||
{
|
||||
$sessionMock = $this->getMockBuilder(Session::class)
|
||||
->getMock();
|
||||
|
||||
$this->getAppMock('session', $sessionMock);
|
||||
$this->assertEquals($sessionMock, session());
|
||||
|
||||
$sessionMock->expects($this->once())
|
||||
->method('get')
|
||||
->with('someKey')
|
||||
->willReturn('someValue');
|
||||
|
||||
$this->assertEquals('someValue', session('someKey'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \view
|
||||
*/
|
||||
public function testView()
|
||||
{
|
||||
$rendererMock = $this->getMockBuilder(Renderer::class)
|
||||
->getMock();
|
||||
|
||||
$this->getAppMock('renderer', $rendererMock);
|
||||
$this->assertEquals($rendererMock, view());
|
||||
|
||||
$rendererMock->expects($this->once())
|
||||
->method('render')
|
||||
->with('template.name', ['template' => 'data'])
|
||||
->willReturn('rendered template');
|
||||
|
||||
$this->assertEquals('rendered template', view('template.name', ['template' => 'data']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \url
|
||||
*/
|
||||
public function testUrl()
|
||||
{
|
||||
$urlGeneratorMock = $this->getMockBuilder(UrlGenerator::class)
|
||||
->getMock();
|
||||
|
||||
$this->getAppMock('routing.urlGenerator', $urlGeneratorMock);
|
||||
$this->assertEquals($urlGeneratorMock, url());
|
||||
|
||||
$urlGeneratorMock->expects($this->once())
|
||||
->method('to')
|
||||
->with('foo/bar', ['param' => 'value'])
|
||||
->willReturn('http://lorem.ipsum/foo/bar?param=value');
|
||||
|
||||
$this->assertEquals('http://lorem.ipsum/foo/bar?param=value', url('foo/bar', ['param' => 'value']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $alias
|
||||
* @param object $object
|
||||
* @return Application|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected function getAppMock($alias, $object)
|
||||
{
|
||||
$appMock = $this->getMockBuilder(Container::class)
|
||||
->getMock();
|
||||
|
||||
$appMock->expects($this->atLeastOnce())
|
||||
->method('get')
|
||||
->with($alias)
|
||||
->willReturn($object);
|
||||
|
||||
/** @var $appMock Application */
|
||||
Application::setInstance($appMock);
|
||||
|
||||
return $appMock;
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Config;
|
||||
|
||||
use Engelsystem\Renderer\HtmlEngine;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class HtmlEngineTest extends TestCase
|
||||
{
|
||||
/** @var string[] */
|
||||
protected $tmpFileNames = [];
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Renderer\HtmlEngine::get
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
$engine = new HtmlEngine();
|
||||
|
||||
$file = $this->createTempFile('<div>%main_content%</div>');
|
||||
|
||||
$data = $engine->get($file, ['main_content' => 'Lorem ipsum dolor sit']);
|
||||
$this->assertEquals('<div>Lorem ipsum dolor sit</div>', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Renderer\HtmlEngine::canRender
|
||||
*/
|
||||
public function testCanRender()
|
||||
{
|
||||
$engine = new HtmlEngine();
|
||||
|
||||
$this->assertFalse($engine->canRender('/dev/null'));
|
||||
|
||||
$file = $this->createTempFile();
|
||||
$this->assertTrue($engine->canRender($file));
|
||||
|
||||
$htmFile = $this->createTempFile('', '.htm');
|
||||
$this->assertTrue($engine->canRender($htmFile));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @param string $extension
|
||||
* @return string
|
||||
*/
|
||||
protected function createTempFile($content = '', $extension = '.html')
|
||||
{
|
||||
$tmpFileName = tempnam(sys_get_temp_dir(), 'EngelsystemUnitTest');
|
||||
|
||||
$fileName = $tmpFileName . $extension;
|
||||
rename($tmpFileName, $fileName);
|
||||
|
||||
file_put_contents($fileName, $content);
|
||||
|
||||
$this->tmpFileNames[] = $fileName;
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
foreach ($this->tmpFileNames as $fileName) {
|
||||
unlink($fileName);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Config;
|
||||
|
||||
use Engelsystem\Renderer\EngineInterface;
|
||||
use Engelsystem\Renderer\Renderer;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class RendererTest extends TestCase
|
||||
{
|
||||
public function testGet()
|
||||
{
|
||||
$renderer = new Renderer();
|
||||
|
||||
$nullRenderer = $this->getMockForAbstractClass(EngineInterface::class);
|
||||
|
||||
$nullRenderer->expects($this->atLeastOnce())
|
||||
->method('canRender')
|
||||
->willReturn(false);
|
||||
$renderer->addRenderer($nullRenderer);
|
||||
|
||||
$mockRenderer = $this->getMockForAbstractClass(EngineInterface::class);
|
||||
|
||||
$mockRenderer->expects($this->atLeastOnce())
|
||||
->method('canRender')
|
||||
->with('foo.template')
|
||||
->willReturn(true);
|
||||
|
||||
$mockRenderer->expects($this->atLeastOnce())
|
||||
->method('get')
|
||||
->with('foo.template', ['lorem' => 'ipsum'])
|
||||
->willReturn('Rendered content');
|
||||
|
||||
$renderer->addRenderer($mockRenderer);
|
||||
$data = $renderer->render('foo.template', ['lorem' => 'ipsum']);
|
||||
|
||||
$this->assertEquals('Rendered content', $data);
|
||||
}
|
||||
|
||||
public function testError()
|
||||
{
|
||||
$renderer = new Renderer();
|
||||
|
||||
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
|
||||
$loggerMock
|
||||
->expects($this->once())
|
||||
->method('error');
|
||||
|
||||
$renderer->setLogger($loggerMock);
|
||||
|
||||
$data = $renderer->render('testing.template');
|
||||
$this->assertEquals('', $data);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Config;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Container\Container;
|
||||
use Engelsystem\Http\Request;
|
||||
use Engelsystem\Routing\UrlGenerator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class UrlGeneratorTest extends TestCase
|
||||
{
|
||||
public function provideLinksTo()
|
||||
{
|
||||
return [
|
||||
['/foo/path', '/foo/path', 'http://foo.bar/foo/path', [], 'http://foo.bar/foo/path'],
|
||||
['foo', '/foo', 'https://foo.bar/foo', [], 'https://foo.bar/foo'],
|
||||
['foo', '/foo', 'http://f.b/foo', ['test' => 'abc', 'bla' => 'foo'], 'http://f.b/foo?test=abc&bla=foo'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideLinksTo
|
||||
* @covers \Engelsystem\Routing\UrlGenerator::to
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $willReturn
|
||||
* @param string $urlToPath
|
||||
* @param string[] $arguments
|
||||
* @param string $expectedUrl
|
||||
*/
|
||||
public function testTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl)
|
||||
{
|
||||
$app = new Container();
|
||||
$urlGenerator = new UrlGenerator();
|
||||
Application::setInstance($app);
|
||||
|
||||
$request = $this->getMockBuilder(Request::class)
|
||||
->getMock();
|
||||
|
||||
$request->expects($this->once())
|
||||
->method('getUriForPath')
|
||||
->with($path)
|
||||
->willReturn($willReturn);
|
||||
|
||||
$app->instance('request', $request);
|
||||
|
||||
$url = $urlGenerator->to($urlToPath, $arguments);
|
||||
$this->assertEquals($expectedUrl, $url);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue