Merge pull request 'feature/slim-views' (#2) from feature/slim-views into develop

Reviewed-on: #2
This commit is contained in:
2025-09-29 13:39:37 -03:00
9 changed files with 133 additions and 21 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
# Composer # Composer
**/vendor/ **/vendor/
composer.lock composer.lock
**/.phpunit.cache/

View File

@ -17,7 +17,8 @@
], ],
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"View\\": "src/" "View\\": "src/",
"Slim\\Views\\": "lib/Slim/Views"
} }
} }
} }

View File

@ -0,0 +1,9 @@
<?php
namespace Slim\Views;
use Psr\Http\Message\ResponseInterface;
interface ViewInterface
{
public function render(ResponseInterface $response, $template, array $data = []);
}

25
phpunit.xml Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="false"
beStrictAboutCoverageMetadata="false"
beStrictAboutOutputDuringTests="true"
displayDetailsOnPhpunitDeprecations="true"
failOnPhpunitDeprecation="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>

View File

@ -1,5 +1,5 @@
<?php <?php
namespace ProVM\Common\Alias; namespace View\Define;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;

6
src/Implement/Blade.php Normal file
View File

@ -0,0 +1,6 @@
<?php
namespace View\Implement;
use Slim\Views;
class Blade extends Views\Blade implements Views\ViewInterface {}

15
tests/BladeTest.php Normal file
View File

@ -0,0 +1,15 @@
<?php
require_once 'BladeTests.php';
class BladeTest extends BladeTests
{
public function testRender(): void
{
$response = $this->getResponse();
$view = new View\Implement\Blade($this->templatesFolder, $this->cacheFolder);
$result = $view->render($response, $this->templateName);
$this->assertEquals($this->template, $result->getBody()->getContents());
}
}

68
tests/BladeTests.php Normal file
View File

@ -0,0 +1,68 @@
<?php
use PHPUnit\Framework\TestCase;
abstract class BladeTests extends TestCase
{
protected string $templatesFolder = './templates';
protected string $cacheFolder = './cache';
protected string $templateName;
protected string $template;
protected function setUp(): void
{
$this->setFolders();
$this->setTemplate();
}
protected function tearDown(): void
{
$this->removeFiles();
$this->removeFolders();
}
protected function getResponse(): Psr\Http\Message\ResponseInterface
{
$response = $this->getMockBuilder(Psr\Http\Message\ResponseInterface::class)
->disableOriginalConstructor()
->getMock();
$body = $this->getMockBuilder(Psr\Http\Message\StreamInterface::class)
->disableOriginalConstructor()
->getMock();
$body->method('getContents')->willReturn($this->template);
$body->method('write')->willReturn($body);
$response->method('getBody')->willReturn($body);
return $response;
}
protected function setFolders(): void
{
mkdir($this->templatesFolder);
mkdir($this->cacheFolder);
chmod($this->cacheFolder, 0o777);
}
protected function removeFolders(): void
{
rmdir($this->cacheFolder);
rmdir($this->templatesFolder);
}
protected function setTemplate(): void
{
$this->templateName = 'test';
$this->template = <<<TEMPLATE
Test Template
TEMPLATE;
file_put_contents("{$this->templatesFolder}/{$this->templateName}.blade.php", $this->template);
}
protected function removeFiles(): void
{
$files = new FilesystemIterator($this->cacheFolder);
foreach ($files as $file) {
unlink($file->getRealPath());
}
$files = new FilesystemIterator($this->templatesFolder);
foreach ($files as $file) {
unlink($file->getRealPath());
}
}
}

View File

@ -1,28 +1,15 @@
<?php <?php
use PHPUnit\Framework\TestCase; require_once 'BladeTests.php';
class ViewTest extends TestCase class ViewTest extends BladeTests
{ {
public function testRender(): void public function testRender(): void
{ {
$response = $this->getMockBuilder(Psr\Http\Message\ResponseInterface::class) $response = $this->getResponse();
->disableOriginalConstructor()
->getMock();
$templatesFolder = './templates';
mkdir($templatesFolder);
$cacheFolder = './cache';
mkdir($cacheFolder);
chmod($cacheFolder, 0o777);
$templateName = 'test'; $view = new View\Implement\View($this->templatesFolder, $this->cacheFolder);
$template = <<<TEMPLATE
Test Template
TEMPLATE;
file_put_contents("{$templatesFolder}/{$templateName}.blade.php", $template);
$view = new View\Implement\View($templatesFolder, $cacheFolder); $result = $view->render($response, $this->templateName);
$this->assertEquals($this->template, $result->getBody()->getContents());
$result = $view->render($response, $templateName);
$this->assertEquals($template, $result->getBody()->getContenst());
} }
} }