You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.5 KiB

<?php namespace Sensory5\Shortcode;
use Lang;
use PluginTestCase;
use Sensory5\Shortcode\Classes\Shortcode;
use Sensory5\Shortcode\Classes\ShortcodeInterface;
class ShortcodeTest extends PluginTestCase
{
/**
* @dataProvider provideTexts
*/
public function testParse($text, $expect)
{
$this->assertSame($expect, $this->getShortcode()->parse($text));
}
public function provideTexts()
{
return [
['[name]', 'name'],
['[content]', ''],
['[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(6, count($this->getShortcode()->getShortCodes()));
}
public function testNames()
{
$this->assertSame(['name', 'content', 'nc', 'nested', 'params', 'param'], $this->getShortcode()->names());
}
public function testRemove()
{
$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 y', $this->getShortcode()->strip('x [name] a [content /] a [/name] y'));
}
public function testHas()
{
$shortcode = $this->getShortcode();
$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->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->add('name', function(ShortcodeInterface $s) {
return $s->getName();
});
$shortcode->add('content', function(ShortcodeInterface $s) {
return $s->getContent();
});
$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;
}
}