Initial commit: apps.prometeysev.ru

This commit is contained in:
root committed 2026-10-22 00:22:57 +03:00
commit 96ccabcd8b
6561 files changed
+1270349

No files matched your search

@@ -0,0 +1,89 @@
<?php
namespace Jasny\Twig;
use Jasny\Twig\ArrayExtension;
use Jasny\Twig\TestHelper;
/**
* @covers Jasny\Twig\ArrayExtension
*/
class ArrayExtensionTest extends \PHPUnit_Framework_TestCase
{
use TestHelper;
protected function getExtension()
{
return new ArrayExtension();
}
public function testSum()
{
$data = [1, 2, 3, 4];
$this->assertRender('10', '{{ data|sum }}', compact('data'));
}
public function testProduct()
{
$data = [1, 2, 3, 4];
$this->assertRender('24', '{{ data|product }}', compact('data'));
}
public function testValues()
{
$data = (object)['foo' => 1, 'bar' => 2, 'zoo' => 3];
$this->assertRender('1-2-3', '{{ data|values|join("-") }}', compact('data'));
}
public function testAsArrayWithObject()
{
$data = (object)['foo' => 1, 'bar' => 2, 'zoo' => 3];
$this->assertRender('foo-bar-zoo', '{{ data|as_array|keys|join("-") }}', compact('data'));
}
public function testAsArrayWithString()
{
$data = 'foo';
$this->assertRender('foo', '{{ data|as_array|join("-") }}', compact('data'));
}
public function testHtmlAttr()
{
$data = ['href' => 'foo.html', 'class' => 'big small', 'checked' => true, 'disabled' => false];
$this->assertRender(
'href="foo.html" class="big small" checked="checked"',
'{{ data|html_attr|raw }}',
compact('data')
);
}
public function filterProvider()
{
return [
['sum'],
['product'],
['values'],
['as_array'],
['html_attr']
];
}
/**
* @dataProvider filterProvider
*
* @param string $filter
*/
public function testWithNull($filter)
{
$this->assertRender('-', '{{ null|' . $filter . '("//")|default("-") }}');
}
}
+168
View File
@@ -0,0 +1,168 @@
<?php
namespace Jasny\Twig;
use Jasny\Twig\DateExtension;
use Jasny\Twig\TestHelper;
/**
* @covers Jasny\Twig\DateExtension
*/
class DateExtensionTest extends \PHPUnit_Framework_TestCase
{
use TestHelper;
public function setUp()
{
date_default_timezone_set('UTC');
\Locale::setDefault("en_EN");
}
protected function getExtension()
{
return new DateExtension();
}
public function localDateTimeProvider()
{
return [
['9/20/2015', '20-09-2015', "{{ '20-09-2015'|localdate }}"],
['September 20, 2015', '20 september 2015', "{{ '20-09-2015'|localdate('long') }}"],
['9/20/15', "20-09-15", "{{ '20-09-2015'|localdate('short') }}"],
['Sunday, September 20, 2015', "zondag 20 september 2015", "{{ '20-09-2015'|localdate('full') }}"],
['20|09|2015', "20|09|2015", "{{ '20-09-2015'|localdate('dd|MM|yyyy') }}"],
['11:14 PM', "23:14", "{{ '23:14:12'|localtime }}"],
['11:14:12 PM GMT', "23:14:12 GMT", "{{ '23:14:12'|localtime('long') }}"],
['11:14 PM', "23:14", "{{ '23:14:12'|localtime('short') }}"],
['11:14:12 PM GMT', "23:14:12 GMT", "{{ '23:14:12'|localtime('full') }}"],
['23|14|12', "23|14|12", "{{ '23:14:12'|localtime('HH|mm|ss') }}"],
// NOTE: a `replace` is used to remove the comma, which seems to be inconsistant accross environments.
['9/20/2015 11:14 PM', '20-09-2015 23:14', "{{ '20-09-2015 23:14:12'|localdatetime|replace({',': ''}) }}"],
['20|23', '20|23', "{{ '20-09-2015 23:14:12'|localdatetime('dd|HH') }}"],
[
'9/20/15 11:14:12 PM GMT',
'20-09-15 23:14:12 GMT',
"{{ '20-09-2015 23:14:12'|localdatetime({date: 'short', time: 'full'})|replace({',': ''}) }}"
],
[
'20150920 11:14:12 PM GMT',
'20150920 23:14:12 GMT',
"{{ '20-09-2015 23:14:12'|localdatetime({date: 'yyyyMMdd', time: 'full'}) }}"
]
];
}
/**
* @dataProvider localDateTimeProvider
*
* @param string $en
* @param string $nl
* @param string $template
*/
public function testLocalDateTimeEn($en, $nl, $template)
{
if (!\Locale::setDefault("en_EN")) {
return $this->markAsSkipped("Unable to set locale to 'en_EN'");
}
$this->assertRender($en, $template);
}
/**
* @dataProvider localDateTimeProvider
*
* @param string $en
* @param string $nl
* @param string $template
*/
public function testLocalDateTimeNL($en, $nl, $template)
{
if (!\Locale::setDefault("nl_NL")) {
return $this->markAsSkipped("Unable to set locale to 'nl_NL'");
}
$this->assertRender($nl, $template);
}
public function durationProvider()
{
return [
['31s', "{{ 31|duration }}"],
['17m 31s', "{{ 1051|duration }}"],
['3h 17m 31s', "{{ 11851|duration }}"],
['2d 3h 17m 31s', "{{ 184651|duration }}"],
['3w 2d 3h 17m 31s', "{{ 1999051|duration }}"],
['1y 3w 2d 3h 17m 31s', "{{ 33448651|duration }}"],
['17 minute(s)', "{{ 1051|duration([null, ' minute(s)', ' hour(s)', ' day(s)']) }}"],
['3 hour(s)', "{{ 11851|duration([null, null, ' hour(s)']) }}"],
['2 day(s)', "{{ 184651|duration([null, null, null, ' day(s)']) }}"],
['3 week(s)', "{{ 1999051|duration([null, null, null, null, ' week(s)']) }}"],
['1 year(s)', "{{ 33448651|duration([null, null, null, null, null, ' year(s)']) }}"],
['3u:17m', "{{ 11851|duration([null, 'm', 'u'], ':') }}"],
['3:17h', "{{ 11851|duration([null, '', ''], ':') }}h"],
];
}
/**
* @dataProvider durationProvider
*
* @param string $expect
* @param string $template
*/
public function testDuration($expect, $template)
{
$this->assertRender($expect, $template);
}
public function ageProvider()
{
$time = time() - (((32 * 365) + 100) * 24 * 3600);
$date = date('Y-m-d', $time);
return [
['32', "{{ $time|age }}"],
['32', "{{ '$date'|age }}"]
];
}
/**
* @dataProvider ageProvider
*
* @param string $expect
* @param string $template
*/
public function testAge($expect, $template)
{
$this->assertRender($expect, $template);
}
public function filterProvider()
{
return [
['localdate'],
['localtime'],
['localdatetime'],
['duration'],
['age']
];
}
/**
* @dataProvider filterProvider
*
* @param string $filter
*/
public function testWithNull($filter)
{
$this->assertRender('-', '{{ null|' . $filter . '("//")|default("-") }}');
}
}
+177
View File
@@ -0,0 +1,177 @@
<?php
namespace Jasny\Twig;
use Jasny\Twig\PcreExtension;
use Jasny\Twig\TestHelper;
/**
* @covers Jasny\Twig\PcreExtension
*/
class PcreExtensionTest extends \PHPUnit_Framework_TestCase
{
use TestHelper;
protected function getExtension()
{
return new PcreExtension();
}
public function testGetName()
{
$this->assertEquals('jasny/pcre', $this->getExtension()->getName());
}
public function testQuote()
{
$this->assertRender('foo\(\)', '{{ "foo()"|preg_quote }}');
}
public function testQuoteDelimiter()
{
$this->assertRender('foo\@bar', '{{ "foo@bar"|preg_quote("@") }}');
}
public function testPregMatch()
{
$this->assertRender('YES', '{% if "foo"|preg_match("/oo/") %}YES{% else %}NO{% endif %}');
}
public function testPregMatchNo()
{
$this->assertRender('NO', '{% if "fod"|preg_match("/oo/") %}YES{% else %}NO{% endif %}');
}
/**
* @expectedException \Twig_Error_Runtime
*/
public function testPregMatchError()
{
$this->render('{% if "fod"|preg_match("/o//o/") %}YES{% else %}NO{% endif %}');
}
public function testPregGet()
{
$this->assertRender('d', '{{ "food"|preg_get("/oo(.)/", 1) }}');
}
public function testPregGetDefault()
{
$this->assertRender('ood', '{{ "food"|preg_get("/oo(.)/") }}');
}
public function testPregGetAll()
{
$this->assertRender('d|t|m', '{{ "food woot should doom"|preg_get_all("/oo(.)/", 1)|join("|") }}');
}
public function testPregGetAllDefault()
{
$this->assertRender('ood|oot|oom', '{{ "food woot doom"|preg_get_all("/oo(.)/")|join("|") }}');
}
public function testPregGrep()
{
$this->assertRender(
'world|how|you',
'{{ ["hello", "sweet", "world", "how", "are", "you"]|preg_grep("/o./")|join("|") }}'
);
}
public function testPregGrepInvert()
{
$this->assertRender(
'hello|sweet|are',
'{{ ["hello", "sweet", "world", "how", "are", "you"]|preg_grep("/o./", "invert")|join("|") }}'
);
}
public function testReplace()
{
$this->assertRender(
'the quick brawen faxe jumped aveer the lazy dage',
'{{ "the quick brown fox jumped over the lazy dog"|preg_replace("/o(\\\\w)/", "a$1e") }}'
);
}
public function testReplaceLimit()
{
$this->assertRender(
'the quick brawen faxe jumped over the lazy dog',
'{{ "the quick brown fox jumped over the lazy dog"|preg_replace("/o(\\\\w)/", "a$1e", 2) }}'
);
}
public function testReplaceWithArray()
{
$this->assertRender(
'hello|sweet|wareld|hawe|are|yaue',
'{{ ["hello", "sweet", "world", "how", "are", "you"]|preg_replace("/o(.)/", "a$1e")|join("|") }}'
);
}
/**
* @expectedException Twig_Error_Runtime
*/
public function testReplaceAssertNoEval()
{
$this->render('{{ "foo"|preg_replace("/o/ei", "strtoupper") }}');
}
public function testFilter()
{
$this->assertRender(
'wareld|hawe|yaue',
'{{ ["hello", "sweet", "world", "how", "are", "you"]|preg_filter("/o(.)/", "a$1e")|join("|") }}'
);
}
/**
* @expectedException Twig_Error_Runtime
*/
public function testFilterAssertNoEval()
{
$this->render('{{ "foo"|preg_filter("/o/ei", "strtoupper") }}');
}
public function testSplit()
{
$this->assertRender(
'the quick br|n f| jumped |er the lazy d|',
'{{ "the quick brown fox jumped over the lazy dog"|preg_split("/o(\\\\w)/", "a$1e")|join("|") }}'
);
}
public function filterProvider()
{
return [
['preg_quote'],
['preg_match'],
['preg_get'],
['preg_get_all'],
['preg_grep'],
['preg_replace'],
['preg_filter'],
['preg_split']
];
}
/**
* @dataProvider filterProvider
*
* @param string $filter
*/
public function testWithNull($filter)
{
$this->assertRender('-', '{{ null|' . $filter . '("//")|default("-") }}');
}
}
+181
View File
@@ -0,0 +1,181 @@
<?php
namespace Jasny\Twig;
use Jasny\Twig\TextExtension;
use Jasny\Twig\TestHelper;
/**
* @covers Jasny\Twig\TextExtension
*/
class TextExtensionTest extends \PHPUnit_Framework_TestCase
{
use TestHelper;
protected function getExtension()
{
return new TextExtension();
}
public function testParagraph()
{
$this->assertRender("<p>foo<br>\nbar</p>\n<p>monkey</p>", "{{ 'foo\nbar\n\nmonkey'|paragraph() }}");
}
public function testLine()
{
$this->assertRender("foo", "{{ 'foo\nbar\nbaz'|line() }}");
}
public function testLineTwo()
{
$this->assertRender("bar", "{{ 'foo\nbar\nbaz'|line(2) }}");
}
public function testLineToHigh()
{
$this->assertRender("", "{{ 'foo\nbar\nbaz'|line(100) }}");
}
public function testLess()
{
$this->assertRender("foo...", "{{ 'foo<!-- pagebreak -->baz'|less() }}");
}
public function testLessCustom()
{
$this->assertRender("foo..", "{{ 'fooXbarXbaz'|less('..', 'X') }}");
}
public function testLessNoPageBreak()
{
$this->assertRender("foo bar", "{{ 'foo bar'|less }}");
}
public function testTruncate()
{
$this->assertRender("foo...", "{{ 'foo bar baz'|truncate(6) }}");
}
public function testTruncateCustom()
{
$this->assertRender("foo ..", "{{ 'foo bar baz'|truncate(6, '..') }}");
}
public function testTruncateToHigh()
{
$this->assertRender("foo bar baz", "{{ 'foo bar baz'|truncate(100) }}");
}
public function testLinkify()
{
$this->assertRender(
'<a href="http://www.example.com">www.example.com</a>, color.bar and '
. '<a href="mailto:john@example.com">john@example.com</a>',
'{{ "www.example.com, color.bar and john@example.com"|linkify }}'
);
}
public function testLinkifyAll()
{
$this->assertRender(
'<a href="http://www.example.com">www.example.com</a>, <a href="http://color.bar">color.bar</a> and '
. '<a href="mailto:john@example.com">john@example.com</a>',
'{{ "www.example.com, color.bar and john@example.com"|linkify(["http", "mail"], [], "all") }}'
);
}
public function testLinkifyHttps()
{
$this->assertRender(
'<a href="https://www.example.com">www.example.com</a>',
'{{ "www.example.com"|linkify("https") }}'
);
}
public function testLinkifyMail()
{
$this->assertRender(
'<a href="mailto:john@example.com">john@example.com</a> and '
. '<a href="mailto:jeff@example.com">jeff@example.com</a>',
'{{ "john@example.com and jeff@example.com"|linkify }}'
);
}
public function testLinkifyFtp()
{
$this->assertRender(
'<a href="ftp://www.example.com">www.example.com</a>',
'{{ "ftp://www.example.com"|linkify("ftp") }}'
);
}
public function testLinkifyFtpAll()
{
$this->assertRender(
'<a href="ftp://www.example.com">www.example.com</a>',
'{{ "www.example.com"|linkify("ftp", [], "all") }}'
);
}
public function testLinkifyOther()
{
$this->assertRender(
'<a href="foo:abc.def.hif">abc.def.hif</a>',
'{{ "foo:abc.def.hif"|linkify("foo") }}'
);
}
public function testLinkifyOtherAll()
{
$this->assertRender(
'<a href="foo:abc.def.hif">abc.def.hif</a>',
'{{ "abc.def.hif"|linkify("foo", [], "all") }}'
);
}
public function testLinkifyWithAttributes()
{
$this->assertRender(
'<a foo="bar" color="blue" href="http://www.example.com">www.example.com</a> and '
. '<a foo="bar" color="blue" href="mailto:john@example.com">john@example.com</a>',
'{{ "www.example.com and john@example.com"|linkify(["http", "mail"], {foo: "bar", color: "blue"}) }}'
);
}
public function testLinkifyWithExistingLink()
{
$this->assertRender(
'<a href="http://www.example.com">www.example.com</a> and '
. '<a href="http://www.example.net">www.example.net</a>',
'{{ "<a href=\\"http://www.example.com\\">www.example.com</a> and www.example.net"|linkify }}'
);
}
public function filterProvider()
{
return [
['paragraph'],
['line'],
['less'],
['truncate'],
['linkify']
];
}
/**
* @dataProvider filterProvider
*
* @param string $filter
*/
public function testWithNull($filter)
{
$this->assertRender('-', '{{ null|' . $filter . '("//")|default("-") }}');
}
}
@@ -0,0 +1,63 @@
<?php
namespace Jasny\Twig;
/**
* Helper methods for unit tests
*/
trait TestHelper
{
/**
* Get the tested extension
*
* @return \Twig_Extension
*/
abstract protected function getExtension();
/**
* Build the Twig environment for the template
*
* @param string $template
* @return \Twig_Environment
*/
protected function buildEnv($template)
{
$loader = new \Twig_Loader_Array([
'template' => $template,
]);
$twig = new \Twig_Environment($loader);
$twig->addExtension($this->getExtension());
return $twig;
}
/**
* Render template
*
* @param string $template
* @param array $data
* @return string
*/
protected function render($template, array $data = [])
{
$twig = $this->buildEnv($template);
$result = $twig->render('template', $data);
return $result;
}
/**
* Render template and assert equals
*
* @param string $expected
* @param string $template
* @param array $data
*/
protected function assertRender($expected, $template, array $data = [])
{
$result = $this->render($template, $data);
$this->assertEquals($expected, (string)$result);
}
}