Add a testing of TyposClientInterface with phpunit

parent a135d2e5
...@@ -23,5 +23,9 @@ ...@@ -23,5 +23,9 @@
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^7" "phpunit/phpunit": "^7"
},
"scripts": {
"run-all-tests": "./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/"
} }
} }
...@@ -61,25 +61,26 @@ abstract class TyposClientInterface ...@@ -61,25 +61,26 @@ abstract class TyposClientInterface
* @param string $context Context where the typo found * @param string $context Context where the typo found
* @param TyposArticle $article Article to fix the typo * @param TyposArticle $article Article to fix the typo
*/ */
private function replaceTypoInArticle(string $typo, string $corrected, string $context, TyposArticle $article) { public function replaceTypoInArticle(string $typo, string $corrected, string $context, TyposArticle $article) {
$article->text = "<p>Text article. <span>Contain</span> some tags and article<b>typo</b>. What to correct.</p>";
// Strip all tags from text // Strip all tags from text
$text = strip_tags($article->text); $text = strip_tags($article->text);
// Find all typos in text, capture an offset of each typo // Find all typos in text, capture an offset of each typo
$typos = []; $typos = [];
preg_match("#{$typo}#", $text, $typos, PREG_OFFSET_CAPTURE); preg_match_all("#{$typo}#", $text, $typos, PREG_OFFSET_CAPTURE);
$typos = $typos[0];
// Find a context in text, capture it offset // Find a context in text, capture it offset
$contextMatch = []; $contextMatch = [];
preg_match_all("#{$context}#", $text, $contextMatch, PREG_OFFSET_CAPTURE); preg_match_all("#{$context}#", $text, $contextMatch, PREG_OFFSET_CAPTURE);
$contextMatch = $contextMatch[0];
$contextOffset = $contextMatch[0][1]; $contextOffset = $contextMatch[0][1];
// Find a concrete typo that we want to fix // Find a concrete typo that we want to fix
$indexOfTypo = null; $indexOfTypo = null;
foreach ($typos as $index => $typo) { foreach ($typos as $index => $match) {
$typoOffset = $typo[1]; $typoOffset = $match[1];
if ($typoOffset >= $contextOffset) { if ($typoOffset >= $contextOffset) {
$indexOfTypo = $index; $indexOfTypo = $index;
break; break;
...@@ -88,14 +89,16 @@ abstract class TyposClientInterface ...@@ -88,14 +89,16 @@ abstract class TyposClientInterface
// Fix a match with index = $indexOfTypo // Fix a match with index = $indexOfTypo
$index = 0; $index = 0;
preg_replace_callback("#{$typo}#", function($match) use(&$index, $indexOfTypo, $corrected) { $article->text = preg_replace_callback("#{$typo}#",
if ($index == $indexOfTypo) { function($match) use(&$index, $indexOfTypo, $corrected) {
return $corrected; $index++;
} if (($index - 1) == $indexOfTypo) {
return $corrected;
}
$index++; return $match[0];
return $match[0]; },
}, $article->text); $article->text);
} }
} }
\ No newline at end of file
<?php
namespace Etersoft\Typos\Tests;
use Etersoft\Typos\TyposArticle;
use My\MyClientInterface;
use PHPUnit\Framework\TestCase;
final class ClientInterfaceTest extends TestCase
{
/**
* Tests a TypoClientInterface::replaceTypoInArticle method.
*/
public function testCorrectRightTypo() {
$typo = "tpo";
$corrected = "typo";
$context = "text contain one tpo. You should fix";
$text = "<p><b>How many tpo have this text?</b><br/> This text contain one tpo. You should fix them all. <span>Because this tpo is very very bad tpo.</span></p>";
$expectedText = "<p><b>How many tpo have this text?</b><br/> This text contain one typo. You should fix them all. <span>Because this tpo is very very bad tpo.</span></p>";
$article = new TyposArticle(0, $text);
$interface = new MyClientInterface();
// Test the method replaceTypoInArticle
$interface->replaceTypoInArticle($typo, $corrected, $context, $article);
$this->assertEquals($expectedText, $article->text);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment