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

  1. <?php namespace Sensory5\Shortcode;
  2. use Lang;
  3. use PluginTestCase;
  4. use Sensory5\Shortcode\Classes\Shortcode;
  5. use Sensory5\Shortcode\Classes\ShortcodeInterface;
  6. class ShortcodeTest extends PluginTestCase
  7. {
  8. /**
  9. * @dataProvider provideTexts
  10. */
  11. public function testParse($text, $expect)
  12. {
  13. $this->assertSame($expect, $this->getShortcode()->parse($text));
  14. }
  15. public function provideTexts()
  16. {
  17. return [
  18. ['[name]', 'name'],
  19. ['[content]', ''],
  20. ['[content]sensory5[/content]', 'sensory5'],
  21. ['[nested][name][/nested]', 'name'],
  22. ['[content]no1[/content], [content]no2[/content], [nested]no3, [content]no4[/content][/nested]', 'no1, no2, no3, no4'],
  23. /* ['[nc][name][/nc]', 'nc: name'], */
  24. ];
  25. }
  26. public function testCount()
  27. {
  28. $this->assertSame(6, count($this->getShortcode()->getShortCodes()));
  29. }
  30. public function testNames()
  31. {
  32. $this->assertSame(['name', 'content', 'nc', 'nested', 'params', 'param'], $this->getShortcode()->names());
  33. }
  34. public function testRemove()
  35. {
  36. $this->assertSame('[name]', $this->getShortcode()->remove('name')->parse('[name]'));
  37. }
  38. public function testStrip()
  39. {
  40. $this->assertSame('', $this->getShortcode()->strip('[name]'));
  41. $this->assertSame('x y', $this->getShortcode()->strip('x [name]y'));
  42. // $this->assertSame('x a a y', $this->getShortcode()->strip('x [name] a [content /] a [/name] y'));
  43. $this->assertSame('x y', $this->getShortcode()->strip('x [name] a [content /] a [/name] y'));
  44. }
  45. public function testHas()
  46. {
  47. $shortcode = $this->getShortcode();
  48. $this->assertTrue($shortcode->has('name'));
  49. $this->assertTrue($shortcode->has('content'));
  50. $this->assertTrue($shortcode->has('nc'));
  51. $this->assertFalse($shortcode->has('invalid'));
  52. }
  53. public function testContains()
  54. {
  55. $shortcode = $this->getShortcode();
  56. $this->assertTrue($shortcode->contentContains('[name]', 'name'));
  57. $this->assertFalse($shortcode->contentContains('[x]', 'name'));
  58. }
  59. public function testGetParameters()
  60. {
  61. $shortcode = $this->getShortcode();
  62. $this->assertSame('param1,one,param2,two,', $shortcode->parse('[params param1=one param2=two]'));
  63. }
  64. public function testgetParameter()
  65. {
  66. $shortcode = $this->getShortcode();
  67. $this->assertSame('numero uno', $shortcode->parse('[param param1="numero uno"]'));
  68. }
  69. private function getShortcode()
  70. {
  71. $shortcode = new Shortcode();
  72. $shortcode->add('name', function(ShortcodeInterface $s) {
  73. return $s->getName();
  74. });
  75. $shortcode->add('content', function(ShortcodeInterface $s) {
  76. return $s->getContent();
  77. });
  78. $shortcode->add('nc', function(ShortcodeInterface $s) {
  79. return $s->getName().': '.$s->getContent();
  80. });
  81. $shortcode->add('nested', function(ShortcodeInterface $s) use ($shortcode) {
  82. return $shortcode->parse($s->getContent());
  83. });
  84. $shortcode->add('params', function(ShortcodeInterface $s) use ($shortcode) {
  85. $params = '';
  86. foreach($s->getParameters() as $key => $value) {
  87. $params .= $key.','.$value.',';
  88. }
  89. return $params;
  90. });
  91. $shortcode->add('param', function(ShortcodeInterface $s) use ($shortcode) {
  92. return $s->getParameter('param1');
  93. });
  94. return $shortcode;
  95. }
  96. }