develop #3
@ -17,7 +17,8 @@
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"View\\": "src/"
|
||||
"View\\": "src/",
|
||||
"Slim\\Views\\": "lib/Slim/Views"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
9
lib/Slim/Views/ViewInterface.php
Normal file
9
lib/Slim/Views/ViewInterface.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace Slim\Views;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
interface ViewInterface
|
||||
{
|
||||
public function render(ResponseInterface $response, $template, array $data = []);
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Alias;
|
||||
namespace View\Define;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
|
||||
|
6
src/Implement/Blade.php
Normal file
6
src/Implement/Blade.php
Normal 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
15
tests/BladeTest.php
Normal 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
68
tests/BladeTests.php
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,28 +1,15 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
require_once 'BladeTests.php';
|
||||
|
||||
class ViewTest extends TestCase
|
||||
class ViewTest extends BladeTests
|
||||
{
|
||||
public function testRender(): void
|
||||
{
|
||||
$response = $this->getMockBuilder(Psr\Http\Message\ResponseInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$templatesFolder = './templates';
|
||||
mkdir($templatesFolder);
|
||||
$cacheFolder = './cache';
|
||||
mkdir($cacheFolder);
|
||||
chmod($cacheFolder, 0o777);
|
||||
$response = $this->getResponse();
|
||||
|
||||
$templateName = 'test';
|
||||
$template = <<<TEMPLATE
|
||||
Test Template
|
||||
TEMPLATE;
|
||||
file_put_contents("{$templatesFolder}/{$templateName}.blade.php", $template);
|
||||
|
||||
$view = new View\Implement\View($templatesFolder, $cacheFolder);
|
||||
$view = new View\Implement\View($this->templatesFolder, $this->cacheFolder);
|
||||
|
||||
$result = $view->render($response, $templateName);
|
||||
$this->assertEquals($template, $result->getBody()->getContenst());
|
||||
$result = $view->render($response, $this->templateName);
|
||||
$this->assertEquals($this->template, $result->getBody()->getContents());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user