-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a factory for the Doctype view helper.
This factory and default configuration preserves the historic method of configuring the doctype for an MVC application
- Loading branch information
Showing
4 changed files
with
150 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Mvc\View\Helper\Factory; | ||
|
||
use ArrayAccess; | ||
use Laminas\View\Helper\Doctype; | ||
use Psr\Container\ContainerInterface; | ||
|
||
use function assert; | ||
use function is_array; | ||
use function is_string; | ||
|
||
final class DoctypeFactory | ||
{ | ||
public function __invoke(ContainerInterface $container): Doctype | ||
{ | ||
$helper = new Doctype(); | ||
|
||
if (! $container->has('config')) { | ||
return $helper; | ||
} | ||
|
||
$config = $container->get('config'); | ||
assert(is_array($config) || $config instanceof ArrayAccess); | ||
|
||
$options = $config['view_manager'] ?? []; | ||
assert(is_array($options)); | ||
|
||
/** @var mixed $doctype */ | ||
$doctype = $options['doctype'] ?? null; | ||
if (is_string($doctype)) { | ||
$helper->setDoctype($doctype); | ||
} | ||
|
||
return $helper; | ||
} | ||
} |
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,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\Mvc\View\Helper\Factory; | ||
|
||
use Laminas\Mvc\View\Helper\Factory\DoctypeFactory; | ||
use Laminas\View\Helper\Doctype; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\ContainerInterface; | ||
|
||
final class DoctypeFactoryTest extends TestCase | ||
{ | ||
private DoctypeFactory $factory; | ||
/** @var MockObject&ContainerInterface */ | ||
private ContainerInterface $container; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->factory = new DoctypeFactory(); | ||
$this->container = $this->createMock(ContainerInterface::class); | ||
Doctype::unsetDoctypeRegistry(); // Overcome nasty static state | ||
} | ||
|
||
public function testAHelperWillBeReturnedWhenNoConfigIsFound(): void | ||
{ | ||
$this->container->expects(self::once()) | ||
->method('has') | ||
->with('config') | ||
->willReturn(false); | ||
|
||
$this->container->expects(self::never()) | ||
->method('get'); | ||
|
||
$this->factory->__invoke($this->container); | ||
} | ||
|
||
public function testThatTheDoctypeWillBeAsConfiguredWhenAppropriateConfigCanBeFound(): void | ||
{ | ||
$config = [ | ||
'view_manager' => [ | ||
'doctype' => Doctype::HTML4_FRAMESET, | ||
], | ||
]; | ||
|
||
$this->container->expects(self::once()) | ||
->method('has') | ||
->with('config') | ||
->willReturn(true); | ||
|
||
$this->container->expects(self::once()) | ||
->method('get') | ||
->with('config') | ||
->willReturn($config); | ||
|
||
$helper = $this->factory->__invoke($this->container); | ||
|
||
self::assertEquals(Doctype::HTML4_FRAMESET, $helper->getDoctype()); | ||
} | ||
|
||
/** @return array<string, array{0: iterable<mixed>, 1: string}> */ | ||
public function configProvider(): array | ||
{ | ||
$helper = new Doctype(); | ||
$default = $helper->getDoctype(); | ||
|
||
return [ | ||
'Empty Config' => [[], $default], | ||
'Missing Doctype Key' => [['view_manager' => []], $default], | ||
'Doctype Configured' => [['view_manager' => ['doctype' => Doctype::XHTML1_RDFA]], Doctype::XHTML1_RDFA], | ||
'Doctype Explicit Null' => [['view_manager' => ['doctype' => null]], $default], | ||
]; | ||
} | ||
|
||
/** @dataProvider configProvider */ | ||
public function testConfigScenarios(iterable $config, string $expectedDoctype): void | ||
{ | ||
$this->container->expects(self::once()) | ||
->method('has') | ||
->with('config') | ||
->willReturn(true); | ||
|
||
$this->container->expects(self::once()) | ||
->method('get') | ||
->with('config') | ||
->willReturn($config); | ||
|
||
$helper = $this->factory->__invoke($this->container); | ||
|
||
self::assertEquals($expectedDoctype, $helper->getDoctype()); | ||
} | ||
} |