From 0b00a89677bc0931e088228983d5fefc8be987ee Mon Sep 17 00:00:00 2001 From: Daniel Bettles Date: Wed, 28 Sep 2022 17:00:18 +0100 Subject: [PATCH] Corrected 'Engine::create()'. --- src/TemplateEngine/Engine.php | 14 ++++++++++---- tests/src/TemplateEngine/EngineTest.php | 20 ++++++++++++-------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/TemplateEngine/Engine.php b/src/TemplateEngine/Engine.php index 5018b71..fddc9c7 100755 --- a/src/TemplateEngine/Engine.php +++ b/src/TemplateEngine/Engine.php @@ -24,7 +24,7 @@ class Engine public function __construct( Php $php, TemplateFileLoader $templateFileLoader, - ServiceFactory $globals = null + ?ServiceFactory $globals = null ) { $this ->setPhp($php) @@ -129,8 +129,14 @@ public function getGlobals(): ?ServiceFactory /** * Factory method, for convenience. */ - public static function create(TemplateFileLoader $loader): self - { - return new self(new Php(), $loader); + public static function create( + TemplateFileLoader $loader, + ServiceFactory $globals = null + ): self { + return new self( + new Php(), + $loader, + $globals + ); } } diff --git a/tests/src/TemplateEngine/EngineTest.php b/tests/src/TemplateEngine/EngineTest.php index 0a0cd99..020fdfb 100755 --- a/tests/src/TemplateEngine/EngineTest.php +++ b/tests/src/TemplateEngine/EngineTest.php @@ -234,19 +234,23 @@ public function testCreateReturnsANewInstance(): void $this->assertTrue($createMethod->isPublic()); $this->assertTrue($createMethod->isStatic()); - $loaderMock = $this - ->getMockBuilder(TemplateFileLoader::class) - ->disableOriginalConstructor() - ->getMock() - ; + $loaderMock = $this->createStub(TemplateFileLoader::class); + $globalsMock = $this->createStub(ServiceFactory::class); - /** @var TemplateFileLoader $loaderMock */ - $templateEngine = Engine::create($loaderMock); + $templateEngine = Engine::create($loaderMock, $globalsMock); $this->assertInstanceOf(Engine::class, $templateEngine); - $this->assertInstanceOf(Php::class, $templateEngine->getPhp()); $this->assertSame($loaderMock, $templateEngine->getTemplateFileLoader()); + $this->assertSame($globalsMock, $templateEngine->getGlobals()); + } + + public function testGlobalsNeedNotBePassedToCreate(): void + { + $createMethod = $this->getTestedClass()->getMethod('create'); + $globalsParam = $createMethod->getParameters()[1]; + + $this->assertTrue($globalsParam->isOptional()); } public function testTemplatesCanIncludeOtherFiles(): void