Skip to content

Commit 0386f10

Browse files
authored
Merge pull request #78 from thunderer/issue-77
Faulty parameter value empties previous shortcodes
2 parents 8f3a65f + b036ab8 commit 0386f10

13 files changed

Lines changed: 71 additions & 39 deletions

.symfony.insight.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
rules:
2+
# RegularParser::parse() disables xdebug.max_nesting_level to avoid errors when XDebug is enabled
3+
php.dynamically_change_configuration:
4+
enabled: false

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ after_script:
2525
matrix:
2626
allow_failures:
2727
- php: nightly
28+
- php: hhvm

composer.json

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@
1313
"php": ">=5.3"
1414
},
1515
"require-dev": {
16-
"phpunit/phpunit": "^4.1|^5.0|^6.0",
17-
"symfony/yaml": "^2.0|^3.0"
18-
},
19-
"suggest": {
20-
"symfony/yaml": "if you want to use YAML serializer",
21-
"ext-dom": "if you want to use XML serializer",
22-
"ext-json": "if you want to use JSON serializer"
16+
"phpunit/phpunit": ">=4.1",
17+
"symfony/yaml": ">=2.0"
2318
},
2419
"autoload": {
2520
"psr-4": {
26-
"Thunder\\Shortcode\\": "src/",
21+
"Thunder\\Shortcode\\": "src/"
22+
}
23+
},
24+
"autoload-dev": {
25+
"psr-4": {
2726
"Thunder\\Shortcode\\Tests\\": "tests/"
2827
}
28+
},
29+
"suggest": {
30+
"symfony/yaml": "if you want to use YAML serializer",
31+
"ext-dom": "if you want to use XML serializer",
32+
"ext-json": "if you want to use JSON serializer"
2933
}
3034
}

phpunit.xml.dist

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
convertWarningsToExceptions = "true"
99
processIsolation = "false"
1010
stopOnFailure = "false"
11-
syntaxCheck = "false"
1211
bootstrap = "vendor/autoload.php"
1312
>
1413

@@ -19,8 +18,7 @@
1918
</testsuites>
2019

2120
<logging>
22-
<log type="coverage-html" target="coverage" charset="UTF-8"
23-
yui="true" highlight="false" lowUpperBound="50" highLowerBound="90"/>
21+
<log type="coverage-html" target="coverage" lowUpperBound="50" highLowerBound="90"/>
2422
<log type="coverage-clover" target="coverage.xml"/>
2523
</logging>
2624

src/Parser/RegularParser.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public function parse($text)
5757
$names = array();
5858
$this->beginBacktrack();
5959
$matches = $this->shortcode($names);
60+
if(false === $matches) {
61+
$this->backtrack();
62+
$this->match(null, true);
63+
continue;
64+
}
6065
if(\is_array($matches)) {
6166
foreach($matches as $shortcode) {
6267
$shortcodes[] = $shortcode;
@@ -151,11 +156,13 @@ private function shortcode(array &$names)
151156
if(false === $content || $closingName !== $name) {
152157
$this->backtrack(false);
153158
$text = $this->backtrack(false);
159+
array_pop($names);
154160

155161
return array_merge(array($this->getObject($name, $parameters, $bbCode, $offset, null, $text)), $shortcodes);
156162
}
157163
$content = $this->getBacktrack();
158164
if(!$this->close($names)) { return false; }
165+
array_pop($names);
159166

160167
return array($this->getObject($name, $parameters, $bbCode, $offset, $content, $this->getBacktrack()));
161168
}

tests/AbstractTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
*/
99
abstract class AbstractTestCase extends TestCase
1010
{
11-
public function expectException($exception)
11+
public function willThrowException($exception)
1212
{
1313
version_compare(phpversion(), '7.0.0') > 0
14-
? parent::expectException($exception)
14+
? $this->expectException($exception)
1515
: $this->setExpectedException($exception);
1616
}
1717
}

tests/EventsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ public function testDefaultApplier()
7979
public function testExceptionOnHandlerForUnknownEvent()
8080
{
8181
$events = new EventContainer();
82-
$this->expectException('InvalidArgumentException');
82+
$this->willThrowException('InvalidArgumentException');
8383
$events->addListener('invalid', function() {});
8484
}
8585

8686
public function testInvalidFilterRawShortcodesNames()
8787
{
88-
$this->expectException('InvalidArgumentException');
88+
$this->willThrowException('InvalidArgumentException');
8989
new FilterRawEventHandler(array(new \stdClass()));
9090
}
9191

9292
public function testInvalidReplaceJoinNames()
9393
{
94-
$this->expectException('InvalidArgumentException');
94+
$this->willThrowException('InvalidArgumentException');
9595
new ReplaceJoinEventHandler(array(new \stdClass()));
9696
}
9797
}

tests/FacadeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ public function testSerialization()
8484

8585
public function testInvalidSerializationFormatException()
8686
{
87-
$this->expectException('InvalidArgumentException');
87+
$this->willThrowException('InvalidArgumentException');
8888
$facade = new ShortcodeFacade();
8989
$facade->serialize(new Shortcode('name', array(), null), 'invalid');
9090
}
9191

9292
public function testInvalidUnserializationFormatException()
9393
{
94-
$this->expectException('InvalidArgumentException');
94+
$this->willThrowException('InvalidArgumentException');
9595
$facade = new ShortcodeFacade();
9696
$facade->unserialize('[c]', 'invalid');
9797
}

tests/HandlerContainerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testExceptionOnDuplicateHandler()
1414
{
1515
$handlers = new HandlerContainer();
1616
$handlers->add('name', function () {});
17-
$this->expectException('RuntimeException');
17+
$this->willThrowException('RuntimeException');
1818
$handlers->add('name', function () {});
1919
}
2020

@@ -31,7 +31,7 @@ public function testRemove()
3131
public function testRemoveException()
3232
{
3333
$handlers = new HandlerContainer();
34-
$this->expectException('RuntimeException');
34+
$this->willThrowException('RuntimeException');
3535
$handlers->remove('code');
3636
}
3737

@@ -59,7 +59,7 @@ public function testHandlerContainer()
5959
public function testInvalidHandler()
6060
{
6161
$handlers = new HandlerContainer();
62-
$this->expectException('RuntimeException');
62+
$this->willThrowException('RuntimeException');
6363
$handlers->add('invalid', new \stdClass());
6464
}
6565

@@ -75,7 +75,7 @@ public function testDefaultHandler()
7575
public function testExceptionIfAliasingNonExistentHandler()
7676
{
7777
$handlers = new HandlerContainer();
78-
$this->expectException('RuntimeException');
78+
$this->willThrowException('RuntimeException');
7979
$handlers->addAlias('m', 'missing');
8080
}
8181

tests/ParserTest.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,26 @@ final class ParserTest extends AbstractTestCase
1919
/**
2020
* @param ParserInterface $parser
2121
* @param string $code
22-
* @param ParsedShortcode[] $tested
22+
* @param ParsedShortcode[] $expected
2323
*
2424
* @dataProvider provideShortcodes
2525
*/
26-
public function testParser(ParserInterface $parser, $code, array $tested)
26+
public function testParser(ParserInterface $parser, $code, array $expected)
2727
{
28-
$parsed = $parser->parse($code);
28+
$this->assertShortcodes($parser->parse($code), $expected);
29+
}
2930

30-
$count = count($tested);
31-
static::assertCount($count, $parsed, 'counts');
31+
private function assertShortcodes(array $actual, array $expected)
32+
{
33+
$count = count($actual);
34+
static::assertCount($count, $expected, 'counts');
3235
for ($i = 0; $i < $count; $i++) {
33-
static::assertSame($tested[$i]->getName(), $parsed[$i]->getName(), 'name');
34-
static::assertSame($tested[$i]->getParameters(), $parsed[$i]->getParameters(), 'parameters');
35-
static::assertSame($tested[$i]->getContent(), $parsed[$i]->getContent(), 'content');
36-
static::assertSame($tested[$i]->getText(), $parsed[$i]->getText(), 'text');
37-
static::assertSame($tested[$i]->getOffset(), $parsed[$i]->getOffset(), 'offset');
38-
static::assertSame($tested[$i]->getBbCode(), $parsed[$i]->getBbCode(), 'bbCode');
36+
static::assertSame($actual[$i]->getName(), $expected[$i]->getName(), 'name');
37+
static::assertSame($actual[$i]->getParameters(), $expected[$i]->getParameters(), 'parameters');
38+
static::assertSame($actual[$i]->getContent(), $expected[$i]->getContent(), 'content');
39+
static::assertSame($actual[$i]->getText(), $expected[$i]->getText(), 'text');
40+
static::assertSame($actual[$i]->getOffset(), $expected[$i]->getOffset(), 'offset');
41+
static::assertSame($actual[$i]->getBbCode(), $expected[$i]->getBbCode(), 'bbCode');
3942
}
4043
}
4144

@@ -258,6 +261,21 @@ public function provideShortcodes()
258261
return $result;
259262
}
260263

264+
public function testIssue77()
265+
{
266+
$parser = new RegularParser();
267+
268+
$this->assertShortcodes($parser->parse('[a][x][/x][x k="v][/x][y]x[/y]'), array(
269+
new ParsedShortcode(new Shortcode('a', array(), null, null), '[a]', 0),
270+
new ParsedShortcode(new Shortcode('x', array(), '', null), '[x][/x]', 3),
271+
new ParsedShortcode(new Shortcode('y', array(), 'x', null), '[y]x[/y]', 22),
272+
));
273+
274+
$this->assertShortcodes($parser->parse('[a k="v][x][/x]'), array(
275+
new ParsedShortcode(new Shortcode('x', array(), '', null), '[x][/x]', 8),
276+
));
277+
}
278+
261279
public function testWordPress()
262280
{
263281
$parser = new WordpressParser();
@@ -291,7 +309,7 @@ public function testWordPress()
291309

292310
public function testWordpressInvalidNamesException()
293311
{
294-
$this->expectException('InvalidArgumentException');
312+
$this->willThrowException('InvalidArgumentException');
295313
WordpressParser::createFromNames(array('string', new \stdClass()));
296314
}
297315

0 commit comments

Comments
 (0)