Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Testing:Developer] Adding php unit tests #6

Merged
merged 21 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Test

on: [push, pull_request]

jobs:
run:
runs-on: 'ubuntu-latest'
strategy:
matrix:
php-versions: ['7.4', '8.0']

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: xdebug

- run: mkdir -p build/logs

- name: Install composer dependencies
uses: ramsey/composer-install@v1

- name: Run Tests
run: vendor/bin/phpunit
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"require-dev": {
"php": "^7.2.5|^8.0",
"phpunit/phpunit": "~6.0|~5.0|~8.0"
},
"autoload": {
"psr-0": { "Submitty": "src/" }
}
}
22 changes: 22 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Aptoma Markdown Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src/</directory>
</whitelist>
</filter>
</phpunit>
2 changes: 1 addition & 1 deletion src/Submitty/Twig/Extension/PHPLeagueMarkdownEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(MarkdownConverter $converter = null)
*/
public function transform($content)
{
return $this->converter->convertToHtml($content);
return $this->converter->convert($content)->getContent();
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/Submitty/Twig/Extension/MarkdownExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Submitty\Twig\Extension;

use Submitty\Twig\Extension\PHPLeagueMarkdownEngine;
use PHPUnit\Framework\TestCase;

/**
* @author Gunnar Liun <[email protected]>
*/
class MarkdownExtensionTest extends TestCase
{
/**
* @dataProvider getParseMarkdownTests
*/
public function testParseMarkdown($template, $expected, $context = array())
{
$this->assertEquals($expected, $this->getTemplate($template)->render($context));
}

public function getParseMarkdownTests()
{
return array(
array('{{ "# Main Title"|markdown }}', '<h1>Main Title</h1>' . PHP_EOL),
array('{{ content|markdown }}', '<h1>Main Title</h1>' . PHP_EOL, array('content' => '# Main Title'))
);
}

protected function getEngine()
{
return new PHPLeagueMarkdownEngine();
}

protected function getTemplate($template)
{
$loader = new \Twig\Loader\ArrayLoader(array('index' => $template));
$twig = new \Twig\Environment($loader, array('debug' => true, 'cache' => false));
$twig->addExtension(new MarkdownExtension($this->getEngine()));

return $twig->load('index');
}
}
139 changes: 139 additions & 0 deletions tests/Submitty/Twig/TokenParser/MarkdownTokenParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

namespace Submitty\Twig\TokenParser;

use Submitty\Twig\Extension\PHPLeagueMarkdownEngine;
use Submitty\Twig\Node\MarkdownNode;
use PHPUnit\Framework\TestCase;
use Twig\Compiler;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Node\Node;
use Twig\Node\TextNode;

/**
* @author Gunnar Lium <[email protected]>
*/
class MarkdownTokenParserTest extends TestCase
{
public function testConstructor()
{
$body = new Node(array(new TextNode("#Title\n\nparagraph\n", 1)));
$node = new MarkdownNode($body, 1);

$this->assertEquals($body, $node->getNode('body'));
}

/**
* Test that the generated code actually do what we expect
*
* The contents of this test is the same that we write in the compile method.
* This requires manual synchronization, which we should probably not rely on.
*/
public function testMarkdownPrepareBehavior()
{
$body = " #Title\n\n paragraph\n\n code";
$bodyPrepared = "#Title\n\nparagraph\n\n code";

ob_start();
echo $body;
$content = ob_get_clean();
preg_match("/^\s*/", $content, $matches);
$lines = explode("\n", $content);
$content = preg_replace('/^' . $matches[0]. '/', "", $lines);
$content = join("\n", $content);

// Assert prepared content looks right
$this->assertEquals($bodyPrepared, $content);

// Assert Markdown output
$expectedOutput = "<h1>Title</h1>\n\n<p>paragraph</p>\n\n<pre><code>code\n</code></pre>\n";
$output = $this->getEngine()->transform($content);
$this->assertIsString($output,$expectedOutput);
$this->assertEquals($output, $this->getEngine()->transform($content));
}

/**
* Test that the generated code looks as expected
*
* @dataProvider getTests
*/
public function testCompile($node, $source, $environment = null, $isPattern = false)
{
$this->assertNodeCompilation($source, $node, $environment, $isPattern = false);
}

protected function getEngine()
{
return new PHPLeagueMarkdownEngine();
}

public function getTests()
{
$tests = array();

$body = new Node(array(new TextNode("#Title\n\nparagraph\n", 1)));
$node = new MarkdownNode($body, 1);

$tests['simple text'] = array($node, <<<EOF
// line 1
ob_start();
echo "#Title

paragraph
";
\$content = ob_get_clean();
preg_match("/^\s*/", \$content, \$matches);
\$lines = explode("\\n", \$content);
\$content = preg_replace('/^' . \$matches[0]. '/', "", \$lines);
\$content = join("\\n", \$content);
echo \$this->env->getExtension('Submitty\Twig\Extension\MarkdownExtension')->parseMarkdown(\$content);
EOF
);

$body = new Node(array(new TextNode(" #Title\n\n paragraph\n\n code\n", 1)));
$node = new MarkdownNode($body, 1);

$tests['text with leading indent'] = array($node, <<<EOF
// line 1
ob_start();
echo " #Title

paragraph

code
";
\$content = ob_get_clean();
preg_match("/^\s*/", \$content, \$matches);
\$lines = explode("\\n", \$content);
\$content = preg_replace('/^' . \$matches[0]. '/', "", \$lines);
\$content = join("\\n", \$content);
echo \$this->env->getExtension('Submitty\Twig\Extension\MarkdownExtension')->parseMarkdown(\$content);
EOF
);

return $tests;
}

public function assertNodeCompilation($source, Node $node, Environment $environment = null, $isPattern = false)
{
$compiler = $this->getCompiler($environment);
$compiler->compile($node);

if ($isPattern) {
$this->assertStringMatchesFormat($source, trim($compiler->getSource()));
} else {
$this->assertEquals($source, trim($compiler->getSource()));
}
}

protected function getCompiler(Environment $environment = null)
{
return new Compiler(null === $environment ? $this->getEnvironment() : $environment);
}

protected function getEnvironment()
{
return new Environment(new ArrayLoader(array()));
}
}
Loading