Replacing shortcode implementation

This commit is contained in:
Patrick Ward
2015-11-25 15:29:27 -05:00
parent a31d79a984
commit bbe024ce14
10 changed files with 471 additions and 202 deletions

View File

@ -1,12 +1,11 @@
<?php namespace Sensory5\Shortcode;
use Lang;
use PluginTestCase;
use Sensory5\Shortcode\Classes\Shortcode;
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
use Thunder\Shortcode\Parser\RegexParser;
use Thunder\Shortcode\Processor\Processor;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use Sensory5\Shortcode\Classes\ShortcodeInterface;
class ShortcodeTest extends \PHPUnit_Framework_TestCase
class ShortcodeTest extends PluginTestCase
{
/**
* @dataProvider provideTexts
@ -21,71 +20,95 @@ class ShortcodeTest extends \PHPUnit_Framework_TestCase
return [
['[name]', 'name'],
['[content]', ''],
['[content]thunder[/content]', 'thunder'],
['[content][name][/content]', 'name'],
['[nc][name][/nc]', 'nc: name'],
['[content]sensory5[/content]', 'sensory5'],
['[nested][name][/nested]', 'name'],
['[content]no1[/content], [content]no2[/content], [nested]no3, [content]no4[/content][/nested]', 'no1, no2, no3, no4'],
/* ['[nc][name][/nc]', 'nc: name'], */
];
}
public function testCount()
{
$this->assertSame(3, $this->getShortcode()->count());
$this->assertSame(6, count($this->getShortcode()->getShortCodes()));
}
public function testAll()
public function testNames()
{
$this->assertSame(['name', 'content', 'nc'], $this->getShortcode()->all());
$this->assertSame(['name', 'content', 'nc', 'nested', 'params', 'param'], $this->getShortcode()->names());
}
public function testUnregister()
public function testRemove()
{
$this->assertSame('[name]', $this->getShortcode()->unregister('name')->parse('[name]'));
}
public function testDestroy()
{
$this->assertSame('[name]', $this->getShortcode()->destroy()->parse('[name]'));
$this->assertSame('[name]', $this->getShortcode()->remove('name')->parse('[name]'));
}
public function testStrip()
{
$this->assertSame('', $this->getShortcode()->strip('[name]'));
$this->assertSame('x y', $this->getShortcode()->strip('x [name]y'));
$this->assertSame('x a a y', $this->getShortcode()->strip('x [name] a [content /] a [/name] y'));
// $this->assertSame('x a a y', $this->getShortcode()->strip('x [name] a [content /] a [/name] y'));
$this->assertSame('x y', $this->getShortcode()->strip('x [name] a [content /] a [/name] y'));
}
public function testExists()
public function testHas()
{
$shortcode = $this->getShortcode();
$this->assertTrue($shortcode->exists('name'));
$this->assertTrue($shortcode->exists('content'));
$this->assertTrue($shortcode->exists('nc'));
$this->assertFalse($shortcode->exists('invalid'));
$this->assertTrue($shortcode->has('name'));
$this->assertTrue($shortcode->has('content'));
$this->assertTrue($shortcode->has('nc'));
$this->assertFalse($shortcode->has('invalid'));
}
public function testContains()
{
$shortcode = $this->getShortcode();
$this->assertTrue($shortcode->contains('[name]', 'name'));
$this->assertFalse($shortcode->contains('[x]', 'name'));
$this->assertTrue($shortcode->contentContains('[name]', 'name'));
$this->assertFalse($shortcode->contentContains('[x]', 'name'));
}
public function testGetParameters()
{
$shortcode = $this->getShortcode();
$this->assertSame('param1,one,param2,two,', $shortcode->parse('[params param1=one param2=two]'));
}
public function testgetParameter()
{
$shortcode = $this->getShortcode();
$this->assertSame('numero uno', $shortcode->parse('[param param1="numero uno"]'));
}
private function getShortcode()
{
$shortcode = new Shortcode();
$shortcode->register('name', function(ShortcodeInterface $s) {
$shortcode->add('name', function(ShortcodeInterface $s) {
return $s->getName();
});
$shortcode->register('content', function(ShortcodeInterface $s) {
$shortcode->add('content', function(ShortcodeInterface $s) {
return $s->getContent();
});
$shortcode->register('nc', function(ShortcodeInterface $s) {
$shortcode->add('nc', function(ShortcodeInterface $s) {
return $s->getName().': '.$s->getContent();
});
$shortcode->add('nested', function(ShortcodeInterface $s) use ($shortcode) {
return $shortcode->parse($s->getContent());
});
$shortcode->add('params', function(ShortcodeInterface $s) use ($shortcode) {
$params = '';
foreach($s->getParameters() as $key => $value) {
$params .= $key.','.$value.',';
}
return $params;
});
$shortcode->add('param', function(ShortcodeInterface $s) use ($shortcode) {
return $s->getParameter('param1');
});
return $shortcode;
}
}