2017-09-20 00:04:59 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-10-31 14:23:23 +01:00
|
|
|
namespace Engelsystem\Test\Unit;
|
2017-09-20 00:04:59 +02:00
|
|
|
|
|
|
|
use Engelsystem\Application;
|
2017-09-22 14:02:02 +02:00
|
|
|
use Engelsystem\Config\Config;
|
2017-09-20 00:04:59 +02:00
|
|
|
use Engelsystem\Container\Container;
|
2017-09-22 14:02:02 +02:00
|
|
|
use Engelsystem\Container\ServiceProvider;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-09-20 00:04:59 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Container\ContainerInterface;
|
2018-08-12 00:08:52 +02:00
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
2017-09-22 14:02:02 +02:00
|
|
|
use ReflectionClass;
|
2017-09-20 00:04:59 +02:00
|
|
|
|
|
|
|
class ApplicationTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2017-09-22 14:02:02 +02:00
|
|
|
* @covers \Engelsystem\Application::__construct
|
|
|
|
* @covers \Engelsystem\Application::registerBaseBindings
|
2017-09-20 00:04:59 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testConstructor(): void
|
2017-09-20 00:04:59 +02:00
|
|
|
{
|
2017-09-22 14:02:02 +02:00
|
|
|
$app = new Application('.');
|
2017-09-20 00:04:59 +02:00
|
|
|
|
|
|
|
$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));
|
2017-09-21 18:37:37 +02:00
|
|
|
$this->assertSame($app, Application::getInstance());
|
2017-09-20 00:04:59 +02:00
|
|
|
$this->assertSame($app, Container::getInstance());
|
|
|
|
}
|
2017-09-22 14:02:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Application::path
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Application::registerPaths
|
|
|
|
* @covers \Engelsystem\Application::setAppPath
|
2017-09-22 14:02:02 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testAppPath(): void
|
2017-09-22 14:02:02 +02:00
|
|
|
{
|
|
|
|
$app = new Application();
|
|
|
|
|
|
|
|
$this->assertFalse($app->has('path'));
|
|
|
|
|
|
|
|
$app->setAppPath('.');
|
|
|
|
$this->assertTrue($app->has('path'));
|
2018-09-10 17:22:05 +02:00
|
|
|
$this->assertTrue($app->has('path.assets'));
|
2017-09-22 14:02:02 +02:00
|
|
|
$this->assertTrue($app->has('path.config'));
|
|
|
|
$this->assertTrue($app->has('path.lang'));
|
2018-09-10 17:22:05 +02:00
|
|
|
$this->assertTrue($app->has('path.resources'));
|
2018-08-26 02:54:52 +02:00
|
|
|
$this->assertTrue($app->has('path.views'));
|
2018-10-28 12:59:49 +01:00
|
|
|
$this->assertTrue($app->has('path.storage'));
|
|
|
|
$this->assertTrue($app->has('path.cache'));
|
|
|
|
$this->assertTrue($app->has('path.cache.routes'));
|
|
|
|
$this->assertTrue($app->has('path.cache.views'));
|
2021-08-10 11:19:58 +02:00
|
|
|
$this->assertTrue($app->has('path.public'));
|
|
|
|
$this->assertTrue($app->has('path.assets.public'));
|
2017-09-22 14:02:02 +02:00
|
|
|
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRegister(): void
|
2017-09-22 14:02:02 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRegisterBoot(): void
|
2017-09-22 14:02:02 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRegisterClassName(): void
|
2017-09-22 14:02:02 +02:00
|
|
|
{
|
|
|
|
$app = new Application();
|
|
|
|
|
|
|
|
$mockClassName = $this->getMockClass(ServiceProvider::class);
|
|
|
|
$serviceProvider = $this->getMockBuilder($mockClassName)
|
|
|
|
->setConstructorArgs([$app])
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['register'])
|
2017-09-22 14:02:02 +02:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$serviceProvider->expects($this->once())
|
|
|
|
->method('register');
|
|
|
|
|
|
|
|
$app->instance($mockClassName, $serviceProvider);
|
|
|
|
$app->register($mockClassName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Application::bootstrap
|
2018-08-12 00:08:52 +02:00
|
|
|
* @covers \Engelsystem\Application::getMiddleware
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Application::isBooted
|
2017-09-22 14:02:02 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testBootstrap(): void
|
2017-09-22 14:02:02 +02:00
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Application|MockObject $app */
|
2017-09-22 14:02:02 +02:00
|
|
|
$app = $this->getMockBuilder(Application::class)
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['register'])
|
2017-09-22 14:02:02 +02:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$serviceProvider = $this->mockServiceProvider($app, ['boot']);
|
|
|
|
$serviceProvider->expects($this->once())
|
|
|
|
->method('boot');
|
|
|
|
|
|
|
|
$app->expects($this->once())
|
|
|
|
->method('register')
|
|
|
|
->with($serviceProvider);
|
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Config|MockObject $config */
|
2017-09-22 14:02:02 +02:00
|
|
|
$config = $this->getMockBuilder(Config::class)
|
|
|
|
->getMock();
|
|
|
|
|
2018-08-12 00:08:52 +02:00
|
|
|
$middleware = [MiddlewareInterface::class];
|
|
|
|
$config->expects($this->exactly(2))
|
2017-09-22 14:02:02 +02:00
|
|
|
->method('get')
|
2018-08-12 00:08:52 +02:00
|
|
|
->withConsecutive(['providers'], ['middleware'])
|
|
|
|
->willReturnOnConsecutiveCalls([$serviceProvider], $middleware);
|
2017-09-22 14:02:02 +02:00
|
|
|
|
|
|
|
$property = (new ReflectionClass($app))->getProperty('serviceProviders');
|
|
|
|
$property->setAccessible(true);
|
|
|
|
$property->setValue($app, [$serviceProvider]);
|
|
|
|
|
|
|
|
$app->bootstrap($config);
|
|
|
|
|
|
|
|
$this->assertTrue($app->isBooted());
|
2018-08-12 00:08:52 +02:00
|
|
|
$this->assertEquals($middleware, $app->getMiddleware());
|
2017-09-22 14:02:02 +02:00
|
|
|
|
|
|
|
// Run bootstrap another time to ensure that providers are registered only once
|
|
|
|
$app->bootstrap($config);
|
|
|
|
}
|
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function mockServiceProvider(Application $app, array $methods = []): ServiceProvider|MockObject
|
2017-09-22 14:02:02 +02:00
|
|
|
{
|
2020-04-19 20:41:38 +02:00
|
|
|
return $this->getMockBuilder(ServiceProvider::class)
|
2017-09-22 14:02:02 +02:00
|
|
|
->setConstructorArgs([$app])
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods($methods)
|
2017-09-22 14:02:02 +02:00
|
|
|
->getMockForAbstractClass();
|
|
|
|
}
|
2017-09-20 00:04:59 +02:00
|
|
|
}
|