-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
121 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
.idea | ||
.php_cs.cache | ||
composer.lock | ||
.phpunit.result.cache |
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Slepic\Templating\Template; | ||
|
||
/** | ||
* An implementation of TemplateInterface which wraps another implementation and keeps some default data | ||
* which it then supplies to the inner template upon rendering. | ||
* | ||
* Overriding default values is allowed. | ||
* A simple array_merge is used and so nested arrays are not merged recursively. | ||
* However, this may change in future. | ||
*/ | ||
class DefaultDataTemplate implements TemplateInterface | ||
{ | ||
private TemplateInterface $template; | ||
private array $data; | ||
|
||
/** | ||
* @param TemplateInterface $template | ||
* @param array<string,mixed> $data | ||
*/ | ||
public function __construct(TemplateInterface $template, array $data) | ||
{ | ||
$this->template = $template; | ||
$this->data = $data; | ||
} | ||
|
||
public function render(array $data): string | ||
{ | ||
$allData = \array_merge($this->data, $data); | ||
return $this->template->render($allData); | ||
} | ||
} |
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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Slepic\Tests\Templating\Template; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Slepic\Templating\Template\DefaultDataTemplate; | ||
use Slepic\Templating\Template\TemplateInterface; | ||
|
||
final class DefaultDataTemplateTest extends TestCase | ||
{ | ||
public function testImplements(): void | ||
{ | ||
$innerTemplate = self::createMock(TemplateInterface::class); | ||
$template = new DefaultDataTemplate($innerTemplate, []); | ||
$this->assertInstanceOf(TemplateInterface::class, $template); | ||
} | ||
|
||
/** | ||
* @param array $defaultData | ||
* @param array $data | ||
* @param array $allData | ||
* | ||
* @dataProvider provideRenderData | ||
*/ | ||
public function testRender(array $defaultData, array $data, array $allData): void | ||
{ | ||
$hash = \md5((string) \time()); | ||
$innerTemplate = self::createMock(TemplateInterface::class); | ||
$innerTemplate->expects(self::once()) | ||
->method('render') | ||
->with($allData) | ||
->willReturn($hash); | ||
$template = new DefaultDataTemplate($innerTemplate, $defaultData); | ||
$output = $template->render($data); | ||
self::assertSame($hash, $output); | ||
} | ||
|
||
public function provideRenderData(): array | ||
{ | ||
$hash = \md5((string) \time()); | ||
return [ | ||
[[], [], []], | ||
[ | ||
['default' => 'value'], | ||
['test' => $hash], | ||
['test' => $hash, 'default' => 'value'] | ||
], | ||
[ | ||
['override' => 'default'], | ||
['keep' => $hash, 'override' => 'overridden'], | ||
['keep' => $hash, 'override' => 'overridden'] | ||
], | ||
]; | ||
} | ||
} |