Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,26 @@ jobs:
composer require --dev -W 'vimeo/psalm=>=5.0' 'nikic/php-parser=>=4.0'
php vendor/bin/psalm --shepherd --php-version=${{ matrix.php }}
continue-on-error: '${{ matrix.failure }}'

infection:
runs-on: 'ubuntu-latest'
steps:
- name: 'Checkout'
uses: 'actions/checkout@v4'
with:
fetch-depth: 0
- name: 'Install PHP'
uses: 'shivammathur/setup-php@v2'
with:
php-version: '8.5'
tools: 'composer:v2'
coverage: 'xdebug'
- name: 'Infection'
run: |
composer remove --dev -W 'vimeo/psalm'
composer require --dev -W phpunit/phpunit infection/infection
if php -r 'exit((int)(version_compare(PHP_VERSION, "8.3", ">=") === false));'; then
docker create --name mago ghcr.io/carthage-software/mago:1.59.0
docker cp mago:/usr/local/bin/mago vendor/bin/mago
php vendor/bin/infection --static-analysis-tool=mago
else
php vendor/bin/infection
fi
continue-on-error: '${{ matrix.failure }}'
composer require --dev -W infection/infection
DIFF="${BASE_REF:+--git-diff-lines --git-diff-base=origin/$BASE_REF}"
docker create --name mago ghcr.io/carthage-software/mago:1.59.0
docker cp mago:/usr/local/bin/mago vendor/bin/mago
php vendor/bin/infection --static-analysis-tool=mago $DIFF
env:
BASE_REF: '${{ github.base_ref }}'
21 changes: 21 additions & 0 deletions tests/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ public function testRaw()
$this->assertEquals(2, $times);
}

public function testEventContainerKeepsAllListeners()
{
$events = new EventContainer();
$events->addListener(Events::FILTER_SHORTCODES, function() {});
$events->addListener(Events::FILTER_SHORTCODES, function() {});

$this->assertCount(2, $events->getListeners(Events::FILTER_SHORTCODES));
}

public function testExceptionOnInvalidFilterShortcodesParent()
{
$this->willThrowException('LogicException');
new \Thunder\Shortcode\Event\FilterShortcodesEvent(array(), new \stdClass());
}

public function testExceptionOnInvalidReplaceShortcodesShortcode()
{
$this->willThrowException('LogicException');
new ReplaceShortcodesEvent('text', array(), new \stdClass());
}

public function testStripContentOutsideShortcodes()
{
$handlers = new HandlerContainer();
Expand Down
13 changes: 13 additions & 0 deletions tests/HandlerContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ public function testDefaultHandler()
static::assertNotNull($handlers->get('missing'));
}

public function testSetDefaultReturnsContainer()
{
$handlers = new HandlerContainer();
static::assertSame($handlers, $handlers->setDefault(function () {}));
}

public function testExceptionOnInvalidDefaultHandler()
{
$handlers = new HandlerContainer();
$this->willThrowException('RuntimeException');
$handlers->setDefault('not callable at all');
}

public function testExceptionIfAliasingNonExistentHandler()
{
$handlers = new HandlerContainer();
Expand Down
79 changes: 79 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,65 @@
*/
final class ParserTest extends AbstractTestCase
{
public function testParsersCountOffsetsInCharacters()
{
foreach(array(new RegularParser(), new HybridParser()) as $parser) {
$this->assertShortcodes($parser->parse('żż[a]x[/a]'), array(
new ParsedShortcode(new Shortcode('a', array(), 'x', null), '[a]x[/a]', 2),
));
}
}

public function testParsersWithRegexSpecialCharactersInSyntax()
{
$syntax = new Syntax('[', ']', '.', '*', '|');
foreach(array(new RegularParser($syntax), new HybridParser($syntax), new RegexParser($syntax)) as $parser) {
$this->assertShortcodes($parser->parse('[a b*|x y|]c[.a]'), array(
new ParsedShortcode(new Shortcode('a', array('b' => 'x y'), 'c', null), '[a b*|x y|]c[.a]', 0),
));
}
}

public function testWordpressParserLowercasesParameterNames()
{
$parser = new WordpressParser();
$shortcodes = $parser->parse('[a FOO="x" Bar=y]');

$this->assertSame(array('foo' => 'x', 'bar' => 'y'), $shortcodes[0]->getParameters());
}

public function testWordpressParserQuotesNames()
{
$parser = WordpressParser::createFromNames(array('a.b'));

$this->assertCount(1, $parser->parse('[a.b]'));
$this->assertCount(0, $parser->parse('[axb]'));
}

public function testExceptionOnInvalidParserSyntax()
{
$classes = array(
'Thunder\Shortcode\Parser\RegularParser',
'Thunder\Shortcode\Parser\HybridParser',
'Thunder\Shortcode\Parser\RegexParser',
);
foreach($classes as $class) {
try {
new $class(new \stdClass());
$this->fail('Expected LogicException.');
} catch(\LogicException $e) {
$this->assertSame('Parameter $syntax must be an instance of SyntaxInterface.', $e->getMessage());
}
}
}

public function testHybridParserExceptionOnInvalidUtf8()
{
$parser = new HybridParser();
$this->willThrowException('RuntimeException');
$parser->parse("[a]\xff[/a]");
}

/**
* @param ParserInterface $parser
* @param string $code
Expand Down Expand Up @@ -431,4 +490,24 @@ public function testParsersEndNamesAtSyntaxTokens()
));
}
}

public function testRegexParserCountsCharacterOffsetsWithCustomSyntax()
{
$parser = new RegexParser(new Syntax('(', ')', '/', '=', '"'));
$this->assertShortcodes($parser->parse('ż (a)'), array(
new ParsedShortcode(new Shortcode('a', array(), null, null), '(a)', 2),
));
}

public function testRegexParserParameterValues()
{
$parser = new RegexParser();
$this->assertShortcodes($parser->parse('[a k=a/b]'), array(
new ParsedShortcode(new Shortcode('a', array('k' => 'a/b'), null, null), '[a k=a/b]', 0),
));
$this->assertShortcodes($parser->parse('[a k="x\"y"] [b k="z"]'), array(
new ParsedShortcode(new Shortcode('a', array('k' => 'x\"y'), null, null), '[a k="x\"y"]', 0),
new ParsedShortcode(new Shortcode('b', array('k' => 'z'), null, null), '[b k="z"]', 13),
));
}
}
66 changes: 66 additions & 0 deletions tests/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,34 @@ public static function provideBuiltInTests()
);
}

public function testProcessorKeepsMultibyteTextAroundShortcodes()
{
$parsers = array(
new RegularParser(),
new \Thunder\Shortcode\Parser\HybridParser(),
new RegexParser(),
new \Thunder\Shortcode\Parser\WordpressParser(),
);
foreach($parsers as $parser) {
$processor = new Processor($parser, $this->getHandlers());

static::assertSame("\xF0\x9F\x98\x80name", $processor->process("\xF0\x9F\x98\x80[name]"), get_class($parser));
static::assertSame('name ab name', $processor->process('[name x="ż"] ab [name]'), get_class($parser));
static::assertSame('[u x="ż"]ó name ż[/u]', $processor->process('[u x="ż"]ó [name /] ż[/u]'), get_class($parser));
}
}

public function testBaseOffsetCountsCharacters()
{
$handlers = $this->getHandlers();
$handlers->add('offset', function (ProcessedShortcode $s) { return (string)$s->getBaseOffset(); });

foreach(array(new RegularParser(), new \Thunder\Shortcode\Parser\HybridParser()) as $parser) {
$processor = new Processor($parser, $handlers);
static::assertSame('9', $processor->process('[c x="ż"][offset /][/c]'), get_class($parser));
}
}

public function testProcessorDeclare()
{
$handlers = new HandlerContainer();
Expand Down Expand Up @@ -288,6 +316,35 @@ public function testExceptionOnInvalidAutoProcessFlag()
$processor->withAutoProcessContent(new \stdClass());
}

public function testExceptionOnZeroMaxIterations()
{
$processor = new Processor(new RegularParser(), new HandlerContainer());
$this->willThrowException('InvalidArgumentException');
$processor->withMaxIterations(0);
}

public function testProcessorWithersReturnNewInstance()
{
$processor = new Processor(new RegularParser(), new HandlerContainer());

static::assertNotSame($processor, $processor->withMaxIterations(2));
static::assertNotSame($processor, $processor->withRecursionDepth(1));
static::assertNotSame($processor, $processor->withAutoProcessContent(false));
static::assertNotSame($processor, $processor->withEventContainer(new \Thunder\Shortcode\EventContainer\EventContainer()));
}

public function testIterationsStopWhenTextDoesNotChange()
{
$calls = 0;
$handlers = new HandlerContainer();
$handlers->add('same', function () use (&$calls) { $calls++; return '[same]'; });
$processor = new Processor(new RegularParser(), $handlers);
$processor = $processor->withMaxIterations(5);

static::assertSame('[same]', $processor->process('[same]'));
static::assertSame(1, $calls);
}

public function testDefaultHandler()
{
$handlers = new HandlerContainer();
Expand Down Expand Up @@ -367,4 +424,13 @@ public function testValidProcessAfterHandlerRemoval()
static::assertSame('n', $processor->process('[n]'));
static::assertSame('[name]', $processor->process('[name]'));
}

public function testProcessorStartsIterationNumberAtOne()
{
$handlers = new HandlerContainer();
$handlers->add('n', function(ShortcodeInterface $s) { return (string)$s->getIterationNumber(); });
$processor = new Processor(new RegularParser(), $handlers);

static::assertSame('1', $processor->process('[n]'));
}
}
70 changes: 70 additions & 0 deletions tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,74 @@ public static function provideExceptions()
array($xml, '', 'InvalidArgumentException'),
);
}

public function testTextSerializerParametersAndBbCode()
{
$serializer = new TextSerializer();

$this->assertSame('[n=bb a=1 b=2 /]', $serializer->serialize(new Shortcode('n', array('a' => '1', 'b' => '2'), null, 'bb')));
$this->assertSame('[n a=żółć /]', $serializer->serialize(new Shortcode('n', array('a' => 'żółć'), null)));
}

public function testExceptionOnInvalidTextSerializerSyntax()
{
$this->willThrowException('LogicException');
new TextSerializer(new \stdClass());
}

public function testXmlSerializerRestoresLibxmlErrorMode()
{
$serializer = new XmlSerializer();
$previous = libxml_use_internal_errors();

foreach(array(true, false) as $mode) {
libxml_use_internal_errors($mode);
try {
$serializer->unserialize('<shortcode');
} catch(\InvalidArgumentException $e) {
}
$this->assertSame($mode, libxml_use_internal_errors());

$serializer->unserialize('<shortcode name="a"><parameters/><content/></shortcode>');
$this->assertSame($mode, libxml_use_internal_errors());
}

libxml_use_internal_errors($previous);
}

public function testYamlSerializerMalformedException()
{
$serializer = new YamlSerializer();

try {
$serializer->unserialize("foo: bar\n");
$this->fail('Expected InvalidArgumentException.');
} catch(\InvalidArgumentException $e) {
$this->assertSame('Malformed shortcode YAML, expected name, parameters, and content!', $e->getMessage());
}
}

public function testJsonSerializerMalformedException()
{
$serializer = new JsonSerializer();

try {
$serializer->unserialize('{}');
$this->fail('Expected InvalidArgumentException.');
} catch(\InvalidArgumentException $e) {
$this->assertSame('Malformed Shortcode JSON, expected name, parameters, and content!', $e->getMessage());
}
}

public function testXmlSerializerInvalidXmlException()
{
$serializer = new XmlSerializer();

try {
$serializer->unserialize('<invalid');
$this->fail('Expected InvalidArgumentException.');
} catch(\InvalidArgumentException $e) {
$this->assertSame('Failed to parse provided XML!', $e->getMessage());
}
}
}
Loading