Skip to content

Commit

Permalink
Clean parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Nov 23, 2023
1 parent 7ccbbb1 commit 17202a5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 67 deletions.
40 changes: 17 additions & 23 deletions tests/Unit/Services/Parser/Parsers/CensoreTest.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
<?php

namespace Tests\Unit\Services\Parser\Parsers;

use Tests\TestCase;
use Coyote\Repositories\Eloquent\WordRepository;
use Coyote\Services\Parser\Parsers\Censore;

class CensoreTest extends TestCase
class CensoreTest extends \Tests\TestCase
{
/**
* @var \Coyote\Services\Parser\Parsers\Censore
*/
protected $parser;
protected Censore $parser;

protected function setUp(): void
{
parent::setUp();

$word = new \Coyote\Repositories\Eloquent\WordRepository(app());
$this->parser = new \Coyote\Services\Parser\Parsers\Censore($word);
$this->parser = new Censore(new WordRepository(app()));
}

/**
* @test
*/
public function testHashCodeTag()
{
$text = '<code></code><code>kurczak';

$result = $this->parser->parse($text);
$this->assertEquals($result, $text);

$text = '<code><code><code></code><code>kurczak';

$result = $this->parser->parse($text);
$this->assertEquals($result, $text);

$text = '</code></code><code>kurczak';
$this->assertIdentity('<code></code><code>kurczak');
$this->assertIdentity('<code><code><code></code><code>kurczak');
$this->assertIdentity('</code></code><code>kurczak');
}

$result = $this->parser->parse($text);
$this->assertEquals($result, $text);
private function assertIdentity(string $content): void
{
$this->assertSame(
$this->parser->parse($content),
$content);
}
}
38 changes: 11 additions & 27 deletions tests/Unit/Services/Parser/Parsers/PurifierTest.php
Original file line number Diff line number Diff line change
@@ -1,58 +1,42 @@
<?php

namespace Tests\Unit\Services\Parser\Parsers;

use Coyote\Services\Parser\Parsers\Purifier;
use Tests\TestCase;

class PurifierTest extends TestCase
class PurifierTest extends \Tests\TestCase
{
public function testParseLinks()
{
$parser = new Purifier();

$input = '<a href="http://www.wp.pl">http://www.wp.pl</a>';
$this->assertEquals($input, $parser->parse($input));
$this->assertIdentity('<a href="http://www.wp.pl">http://www.wp.pl</a>');
}

public function testParseBlockquotes()
{
$parser = new Purifier();

$input = '<blockquote>lorem ipsum<blockquote>lorem ipsum</blockquote></blockquote>';
$this->assertEquals($input, $parser->parse($input));
$this->assertIdentity('<blockquote>lorem ipsum<blockquote>lorem ipsum</blockquote></blockquote>');
}

public function testParseUnderscore()
{
$parser = new Purifier();

$input = '<u>foo</u>';
$this->assertEquals($input, $parser->parse($input));
$this->assertIdentity('<u>foo</u>');
}

public function testAllowKbd()
{
$parser = new Purifier();

$input = '<kbd>Ctrl</kbd>';
$this->assertEquals($input, $parser->parse($input));
$this->assertIdentity('<kbd>Ctrl</kbd>');
}

public function testAllowMark()
{
$parser = new Purifier();

$input = '<mark>Ctrl</mark>';
$this->assertEquals($input, $parser->parse($input));
$this->assertIdentity('<mark>Ctrl</mark>');
}

public function testAllowIframeInsideSpan()
{
$parser = new Purifier();

$input = '<p><span class="embed-responsive embed-responsive-16by9"><iframe src="https://youtube.com/embed/enOjqwOE1ec" class="embed-responsive-item"></iframe></span></p>';
$this->assertSame($input, $parser->parse($input));
$this->assertIdentity('<p><span class="embed-responsive embed-responsive-16by9"><iframe src="https://youtube.com/embed/enOjqwOE1ec" class="embed-responsive-item"></iframe></span></p>');
}

private function assertIdentity(string $input): void
{
$this->assertEquals($input, (new Purifier())->parse($input));
}
}
16 changes: 8 additions & 8 deletions tests/Unit/Services/Parser/Parsers/SimpleMarkdownTest.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?php

namespace Tests\Unit\Services\Parser\Parsers;

use Coyote\Repositories\Eloquent\PageRepository;
use Coyote\Repositories\Eloquent\UserRepository;
use Coyote\Services\Parser\Parsers\SimpleMarkdown;
use Tests\TestCase;

class SimpleMarkdownTest extends TestCase
class SimpleMarkdownTest extends \Tests\TestCase
{
public function testDoNotChangeLineBreaks()
{
$markdown = new SimpleMarkdown($this->app[UserRepository::class], $this->app[PageRepository::class], 'host');

$input = "one\ntwo\nthree";
$this->assertEquals($input, trim($markdown->parse($input)));
$this->assertIdentity($markdown, "one\ntwo\nthree\n");
}

public function testAutolinkExtension()
{
$markdown = new SimpleMarkdown($this->app[UserRepository::class], $this->app[PageRepository::class], 'host');

$link = 'https://docs.djangoproject.com/en/2.0/#first-steps';
$this->assertEquals("<a href=\"$link\">$link</a>", trim($markdown->parse($link)));
$this->assertEquals("<a href=\"$link\">$link</a>\n", $markdown->parse($link));
}

private function assertIdentity(SimpleMarkdown $markdown, string $input): void
{
$this->assertEquals($input, $markdown->parse($input));
}
}
44 changes: 35 additions & 9 deletions tests/Unit/Services/Parser/Parsers/SmiliesTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
<?php

namespace Tests\Unit\Services\Parser\Parsers;

use Coyote\Services\Parser\Parsers\Smilies;
use Tests\TestCase;

class SmiliesTest extends TestCase
class SmiliesTest extends \Tests\TestCase
{
// tests
public function testParseSmilies()
/**
* @test
*/
public function test()
{
$parser = new Smilies();
$this->assertSame('<img class="img-smile" alt=":)" title=":)" src="/img/smilies/smile.gif">', $parser->parse(':)'));
}

$this->assertMatchesRegularExpression('/<img class="img-smile" alt="\:\)" title="\:\)" src=\"\/img\/smilies\/smile\.gif\">/', $parser->parse(':)'));
$this->assertMatchesRegularExpression('/<p><img class="img-smile" alt="\:\)" title="\:\)" src="\/img\/smilies\/smile.gif"><\/p>/', $parser->parse('<p>:)</p>'));
$this->assertMatchesRegularExpression('/\(\:\)\)/', $parser->parse('(:))'));
$this->assertEquals('admin:)', $parser->parse('admin:)'));
/**
* @test
*/
public function smileInParenthesis()
{
$this->assertIdentity(new Smilies(), '(:))');
}

/**
* @test
*/
public function smileInParagraph()
{
$parser = new Smilies();
$this->assertSame('<p><img class="img-smile" alt=":)" title=":)" src="/img/smilies/smile.gif"></p>', $parser->parse('<p>:)</p>'));
}

/**
* @test
*/
public function smileAfterWord()
{
$this->assertIdentity(new Smilies(), 'admin:)');
}

private function assertIdentity(Smilies $parser, string $text): void
{
$this->assertEquals($text, $parser->parse($text));
}
}

0 comments on commit 17202a5

Please sign in to comment.