Skip to content

Commit

Permalink
test Template Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinDev committed Jan 9, 2021
1 parent 19d809b commit 7ea1da8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 12 deletions.
1 change: 0 additions & 1 deletion packages/skeleton/templates/test

This file was deleted.

1 change: 1 addition & 0 deletions packages/skeleton/templatesnewTemplateFile.html.twig
@@ -0,0 +1 @@
<p>test</p>
1 change: 1 addition & 0 deletions packages/skeleton/templatesnewTemplateFile2.html.twig
@@ -0,0 +1 @@
<p>test</p>
30 changes: 20 additions & 10 deletions packages/template-editor/src/Element.php
Expand Up @@ -30,11 +30,13 @@ class Element
public function __construct($templateDir, $path = null)
{
$this->templateDir = realpath($templateDir);
$this->path = substr($path, \strlen($this->templateDir));
if (null !== $path) {
$this->path = self::normalizePath($path);
}
$this->code = $this->loadCode();
}

protected function loadCode()
protected function loadCode(): string
{
if ($this->path && file_exists($this->getTemplateDir().$this->getPath())) {
return file_get_contents($this->getTemplateDir().$this->getPath());
Expand All @@ -43,7 +45,7 @@ protected function loadCode()
return '';
}

protected function getTemplateDir()
protected function getTemplateDir(): string
{
return $this->templateDir;
}
Expand All @@ -58,16 +60,22 @@ public function getEncodedPath(): ?string
return md5($this->path);
}

public function setPath(string $path)
private static function normalizePath($path): string
{
return '/'.ltrim($path, '/');
}

public function setPath(string $path): self
{
if (false !== strpos($path, '..')) { // avoiding to store in an other folder than templates.
throw new \Exception('You can\'t do that...');
}

$path = self::normalizePath($path);

if (null === $this->path) {
$this->path = $path;
} else {
$path = '/'.ltrim($path, '/');
if ($this->path != $path) {
if (file_exists($this->getTemplateDir().$path)) { // check if we don't erase an other file
throw new \Exception('file ever exist'); // todo move it to assert to avoid error 500..
Expand All @@ -84,26 +92,28 @@ public function setPath(string $path)
return $this;
}

public function getCode()
public function getCode(): ?string
{
return $this->code;
}

public function setCode(string $code)
public function setCode(string $code): self
{
$this->code = $code;

return $this;
}

public function storeElement()
public function storeElement(): bool
{
if ($this->unlink) { // for rename
unlink($this->unlink);
}

return file_put_contents($this->getTemplateDir().$this->path, $this->code);
return false !== file_put_contents($this->getTemplateDir().$this->path, $this->code) ? true : false;
}

public function deleteElement()
public function deleteElement(): bool
{
return unlink($this->getTemplateDir().$this->path);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/template-editor/src/ElementRepository.php
Expand Up @@ -20,7 +20,7 @@ public function getAll(): array
$elements = [];

foreach ($finder as $file) {
$elements[] = new Element($this->templateDir, $file);
$elements[] = new Element($this->templateDir, substr($file, \strlen($this->templateDir)));
}

return $elements;
Expand Down
66 changes: 66 additions & 0 deletions packages/template-editor/tests/ElementTest.php
@@ -0,0 +1,66 @@
<?php

namespace Pushword\TemplateEditor;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class ElementTest extends KernelTestCase
{
public function testIt()
{
$templateDir = __DIR__.'/../../skeleton/templates';
$path = 'newTemplateFile.html.twig';
$newPath = 'newTemplateFile2.html.twig';

$element = new Element($templateDir);

$element->setPath($path);
$element->setCode('<p>test</p>');

$element->storeElement();

$this->assertTrue(file_exists($templateDir.'/'.$path));

$element->setPath($newPath);

$element->storeElement();

$this->assertTrue(! file_exists($templateDir.'/'.$path));
$this->assertTrue(file_exists($templateDir.'/'.$newPath));

$element->deleteElement();

$this->assertTrue(! file_exists($templateDir.'/'.$path));
$this->assertTrue(! file_exists($templateDir.'/'.$newPath));

$this->assertSame($element->getCode(), '<p>test</p>');
}

public function testLoadCode()
{
$templateDir = __DIR__.'/../../skeleton/templates';
$newPath = 'newTemplateFile2.html.twig';

$element = new Element($templateDir);
$element->setPath($newPath);
$element->setCode('<p>test</p>');
$element->storeElement();
unset($element);

$element = new Element($templateDir, $newPath);
$this->assertSame('<p>test</p>', $element->getCode());

$element->deleteElement();
}

public function testRepository()
{
$templateDir = __DIR__.'/../../skeleton/templates';
$repo = new ElementRepository($templateDir);

$templates = $repo->getAll();
$this->assertTrue(\count($templates) > 0);

$this->assertSame($templates[0]->getPath(), $repo->getOneByEncodedPath($templates[0]->getEncodedPath())->getPath());
}
}

0 comments on commit 7ea1da8

Please sign in to comment.