-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
233 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) 2021 Heimrich & Hannot GmbH | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace HeimrichHannot\EncoreBundle\Helper; | ||
|
||
use Contao\PageModel; | ||
use HeimrichHannot\UtilsBundle\Container\ContainerUtil; | ||
use HeimrichHannot\UtilsBundle\Model\ModelUtil; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Webmozart\PathUtil\Path; | ||
|
||
class ConfigurationHelper | ||
{ | ||
/** | ||
* @var ContainerUtil | ||
*/ | ||
protected $containerUtil; | ||
/** | ||
* @var RequestStack | ||
*/ | ||
protected $requestStack; | ||
/** | ||
* @var ModelUtil | ||
*/ | ||
protected $modelUtil; | ||
/** | ||
* @var array | ||
*/ | ||
protected $bundleConfig; | ||
/** | ||
* @var string | ||
*/ | ||
protected $webDir; | ||
|
||
public function __construct(ContainerUtil $containerUtil, RequestStack $requestStack, ModelUtil $modelUtil, array $bundleConfig, string $webDir) | ||
{ | ||
$this->containerUtil = $containerUtil; | ||
$this->requestStack = $requestStack; | ||
$this->modelUtil = $modelUtil; | ||
$this->bundleConfig = $bundleConfig; | ||
$this->webDir = $webDir; | ||
} | ||
|
||
/** | ||
* Check if encore is enabled on the current page. | ||
*/ | ||
public function isEnabledOnCurrentPage(?PageModel $pageModel = null): bool | ||
{ | ||
if (!$this->containerUtil->isFrontend()) { | ||
return false; | ||
} | ||
|
||
if (null !== $this->requestStack->getParentRequest()) { | ||
return false; | ||
} | ||
|
||
if (null === $pageModel) { | ||
global $objPage; | ||
$pageModel = $objPage; | ||
} | ||
|
||
if (!$pageModel) { | ||
return false; | ||
} | ||
|
||
$layout = $this->modelUtil->findModelInstanceByPk('tl_layout', $pageModel->layoutId); | ||
if (!$layout || !$layout->addEncore) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Return the relative path to the encore output folder. | ||
*/ | ||
public function getRelativeOutputPath(): string | ||
{ | ||
return Path::makeRelative($this->bundleConfig['outputPath'], $this->webDir); | ||
} | ||
|
||
/** | ||
* Return the absolute path to the encore output folder. | ||
*/ | ||
public function getAbsoluteOutputPath(): string | ||
{ | ||
return $this->bundleConfig['outputPath']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) 2021 Heimrich & Hannot GmbH | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace HeimrichHannot\EncoreBundle\Test\Helper; | ||
|
||
use Contao\LayoutModel; | ||
use Contao\PageModel; | ||
use Contao\TestCase\ContaoTestCase; | ||
use HeimrichHannot\EncoreBundle\Helper\ConfigurationHelper; | ||
use HeimrichHannot\UtilsBundle\Container\ContainerUtil; | ||
use HeimrichHannot\UtilsBundle\Model\ModelUtil; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
class ConfigurationHelperTest extends ContaoTestCase | ||
{ | ||
public function createTestInstance(array $parameters = []) | ||
{ | ||
$containerUtil = $parameters['containerUtil'] ?? $this->createMock(ContainerUtil::class); | ||
$requestStack = $parameters['requestStack'] ?? $this->createMock(RequestStack::class); | ||
$modelUtil = $parameters['modelUtil'] ?? $this->createMock(ModelUtil::class); | ||
$bundleConfig = $parameters['bundleConfig'] ?? []; | ||
$webDir = $parameters['webDir'] ?? ''; | ||
|
||
$instance = new ConfigurationHelper( | ||
$containerUtil, | ||
$requestStack, | ||
$modelUtil, | ||
$bundleConfig, | ||
$webDir | ||
); | ||
|
||
return $instance; | ||
} | ||
|
||
public function testIsEnabledOnCurrentPage() | ||
{ | ||
$containerUtil = $this->createMock(ContainerUtil::class); | ||
$containerUtil->method('isFrontend')->willReturn(false); | ||
$instance = $this->createTestInstance([ | ||
'containerUtil' => $containerUtil, | ||
]); | ||
$this->assertFalse($instance->isEnabledOnCurrentPage()); | ||
|
||
$containerUtil = $this->createMock(ContainerUtil::class); | ||
$containerUtil->method('isFrontend')->willReturn(true); | ||
$requestStack = $this->createMock(RequestStack::class); | ||
$requestStack->method('getParentRequest')->willReturn(new Request()); | ||
$instance = $this->createTestInstance([ | ||
'containerUtil' => $containerUtil, | ||
'requestStack' => $requestStack, | ||
]); | ||
$this->assertFalse($instance->isEnabledOnCurrentPage()); | ||
|
||
$containerUtil = $this->createMock(ContainerUtil::class); | ||
$containerUtil->method('isFrontend')->willReturn(true); | ||
$requestStack = $this->createMock(RequestStack::class); | ||
$requestStack->method('getParentRequest')->willReturn(null); | ||
$instance = $this->createTestInstance([ | ||
'containerUtil' => $containerUtil, | ||
'requestStack' => $requestStack, | ||
]); | ||
$pageModel = $this->mockClassWithProperties(PageModel::class, []); | ||
$this->assertFalse($instance->isEnabledOnCurrentPage($pageModel)); | ||
|
||
$modelUtil = $this->createMock(ModelUtil::class); | ||
$modelUtil->method('findModelInstanceByPk')->willReturnCallback(function ($table, $id) { | ||
switch ($id) { | ||
case '1': | ||
return null; | ||
case '2': | ||
return $this->mockClassWithProperties(LayoutModel::class, ['addEncore' => '']); | ||
case '3': | ||
return $this->mockClassWithProperties(LayoutModel::class, ['addEncore' => '1']); | ||
} | ||
}); | ||
$instance = $this->createTestInstance([ | ||
'containerUtil' => $containerUtil, | ||
'requestStack' => $requestStack, | ||
'modelUtil' => $modelUtil, | ||
]); | ||
$pageModel = $this->mockClassWithProperties(PageModel::class, ['layoutId' => 2]); | ||
$this->assertFalse($instance->isEnabledOnCurrentPage($pageModel)); | ||
|
||
$pageModel = $this->mockClassWithProperties(PageModel::class, ['layoutId' => 3]); | ||
$this->assertTrue($instance->isEnabledOnCurrentPage($pageModel)); | ||
|
||
$this->assertFalse($instance->isEnabledOnCurrentPage()); | ||
|
||
$GLOBALS['objPage'] = $this->mockClassWithProperties(PageModel::class, ['layoutId' => 3]); | ||
$this->assertTrue($instance->isEnabledOnCurrentPage()); | ||
|
||
unset($GLOBALS['objPage']); | ||
} | ||
|
||
public function testGetRelativeOutputPath() | ||
{ | ||
$instance = $this->createTestInstance([ | ||
'bundleConfig' => ['outputPath' => ''], | ||
]); | ||
$this->assertEmpty($instance->getRelativeOutputPath()); | ||
|
||
$instance = $this->createTestInstance([ | ||
'bundleConfig' => ['outputPath' => '/a/b/c/d'], | ||
'webDir' => '/a/b/c', | ||
]); | ||
$this->assertSame('d', $instance->getRelativeOutputPath()); | ||
} | ||
|
||
public function testGetAbsoluteOutputPath() | ||
{ | ||
$instance = $this->createTestInstance([ | ||
'bundleConfig' => ['outputPath' => ''], | ||
]); | ||
$this->assertEmpty($instance->getAbsoluteOutputPath()); | ||
|
||
$instance = $this->createTestInstance([ | ||
'bundleConfig' => ['outputPath' => '/a/b/c/d'], | ||
'webDir' => '/a/b/c', | ||
]); | ||
$this->assertSame('/a/b/c/d', $instance->getAbsoluteOutputPath()); | ||
} | ||
} |