This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Loader\CacheLoader;
use Symfony\Component\Templating\Loader\Loader;
use Symfony\Component\Templating\Storage\StringStorage;
use Symfony\Component\Templating\TemplateReference;
use Symfony\Component\Templating\TemplateReferenceInterface;
class CacheLoaderTest extends TestCase
{
public function testConstructor()
{
$loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(), sys_get_temp_dir());
$this->assertSame($loader->getLoader(), $varLoader, '__construct() takes a template loader as its first argument');
$this->assertEquals(sys_get_temp_dir(), $loader->getDir(), '__construct() takes a directory where to store the cache as its second argument');
}
public function testLoad()
{
$dir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.mt_rand(111111, 999999);
mkdir($dir, 0777, true);
$loader = new ProjectTemplateLoader($varLoader = new ProjectTemplateLoaderVar(), $dir);
$this->assertFalse($loader->load(new TemplateReference('foo', 'php')), '->load() returns false if the embed loader is not able to load the template');
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$logger
->expects($this->once())
->method('debug')
->with('Storing template in cache.', ['name' => 'index']);
$loader->setLogger($logger);
$loader->load(new TemplateReference('index'));
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$logger
->expects($this->once())
->method('debug')
->with('Fetching template from cache.', ['name' => 'index']);
$loader->setLogger($logger);
$loader->load(new TemplateReference('index'));
}
}
class ProjectTemplateLoader extends CacheLoader
{
public function getDir()
{
return $this->dir;
}
public function getLoader()
{
return $this->loader;
}
}
class ProjectTemplateLoaderVar extends Loader
{
public function getIndexTemplate()
{
return 'Hello World';
}
public function getSpecialTemplate()
{
return 'Hello {{ name }}';
}
public function load(TemplateReferenceInterface $template)
{
if (method_exists($this, $method = 'get'.ucfirst($template->get('name')).'Template')) {
return new StringStorage($this->$method());
}
return false;
}
public function isFresh(TemplateReferenceInterface $template, $time)
{
return false;
}
}

View File

@@ -0,0 +1,63 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Loader\ChainLoader;
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Component\Templating\TemplateReference;
class ChainLoaderTest extends TestCase
{
protected $loader1;
protected $loader2;
protected function setUp()
{
$fixturesPath = realpath(__DIR__.'/../Fixtures/');
$this->loader1 = new FilesystemLoader($fixturesPath.'/null/%name%');
$this->loader2 = new FilesystemLoader($fixturesPath.'/templates/%name%');
}
public function testConstructor()
{
$loader = new ProjectTemplateLoader1([$this->loader1, $this->loader2]);
$this->assertEquals([$this->loader1, $this->loader2], $loader->getLoaders(), '__construct() takes an array of template loaders as its second argument');
}
public function testAddLoader()
{
$loader = new ProjectTemplateLoader1([$this->loader1]);
$loader->addLoader($this->loader2);
$this->assertEquals([$this->loader1, $this->loader2], $loader->getLoaders(), '->addLoader() adds a template loader at the end of the loaders');
}
public function testLoad()
{
$loader = new ProjectTemplateLoader1([$this->loader1, $this->loader2]);
$this->assertFalse($loader->load(new TemplateReference('bar', 'php')), '->load() returns false if the template is not found');
$this->assertFalse($loader->load(new TemplateReference('foo', 'php')), '->load() returns false if the template does not exist for the given renderer');
$this->assertInstanceOf(
'Symfony\Component\Templating\Storage\FileStorage',
$loader->load(new TemplateReference('foo.php', 'php')),
'->load() returns a FileStorage if the template exists'
);
}
}
class ProjectTemplateLoader1 extends ChainLoader
{
public function getLoaders()
{
return $this->loaders;
}
}

View File

@@ -0,0 +1,85 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Component\Templating\TemplateReference;
class FilesystemLoaderTest extends TestCase
{
protected static $fixturesPath;
public static function setUpBeforeClass()
{
self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
}
public function testConstructor()
{
$pathPattern = self::$fixturesPath.'/templates/%name%.%engine%';
$loader = new ProjectTemplateLoader2($pathPattern);
$this->assertEquals([$pathPattern], $loader->getTemplatePathPatterns(), '__construct() takes a path as its second argument');
$loader = new ProjectTemplateLoader2([$pathPattern]);
$this->assertEquals([$pathPattern], $loader->getTemplatePathPatterns(), '__construct() takes an array of paths as its second argument');
}
public function testIsAbsolutePath()
{
$this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
$this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('c:\\\\foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
$this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('c:/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
$this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('\\server\\foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
$this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('https://server/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
$this->assertTrue(ProjectTemplateLoader2::isAbsolutePath('phar://server/foo.xml'), '->isAbsolutePath() returns true if the path is an absolute path');
}
public function testLoad()
{
$pathPattern = self::$fixturesPath.'/templates/%name%';
$path = self::$fixturesPath.'/templates';
$loader = new ProjectTemplateLoader2($pathPattern);
$storage = $loader->load(new TemplateReference($path.'/foo.php', 'php'));
$this->assertInstanceOf('Symfony\Component\Templating\Storage\FileStorage', $storage, '->load() returns a FileStorage if you pass an absolute path');
$this->assertEquals($path.'/foo.php', (string) $storage, '->load() returns a FileStorage pointing to the passed absolute path');
$this->assertFalse($loader->load(new TemplateReference('bar', 'php')), '->load() returns false if the template is not found');
$storage = $loader->load(new TemplateReference('foo.php', 'php'));
$this->assertInstanceOf('Symfony\Component\Templating\Storage\FileStorage', $storage, '->load() returns a FileStorage if you pass a relative template that exists');
$this->assertEquals($path.'/foo.php', (string) $storage, '->load() returns a FileStorage pointing to the absolute path of the template');
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$logger->expects($this->exactly(2))->method('debug');
$loader = new ProjectTemplateLoader2($pathPattern);
$loader->setLogger($logger);
$this->assertFalse($loader->load(new TemplateReference('foo.xml', 'php')), '->load() returns false if the template does not exist for the given engine');
$loader = new ProjectTemplateLoader2([self::$fixturesPath.'/null/%name%', $pathPattern]);
$loader->setLogger($logger);
$loader->load(new TemplateReference('foo.php', 'php'));
}
}
class ProjectTemplateLoader2 extends FilesystemLoader
{
public function getTemplatePathPatterns()
{
return $this->templatePathPatterns;
}
public static function isAbsolutePath($path)
{
return parent::isAbsolutePath($path);
}
}

View File

@@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Templating\Loader\Loader;
use Symfony\Component\Templating\TemplateReferenceInterface;
class LoaderTest extends TestCase
{
public function testGetSetLogger()
{
$loader = new ProjectTemplateLoader4();
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$loader->setLogger($logger);
$this->assertSame($logger, $loader->getLogger(), '->setLogger() sets the logger instance');
}
}
class ProjectTemplateLoader4 extends Loader
{
public function load(TemplateReferenceInterface $template)
{
}
public function getLogger()
{
return $this->logger;
}
public function isFresh(TemplateReferenceInterface $template, $time)
{
return false;
}
}