From dbe37bcb0e02d45903f3cce3b27919b5c50856c1 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 10 Feb 2019 22:38:34 +0000 Subject: [PATCH] Type check tests --- psalm.xml | 8 ++--- tests/CommonMarkTest.php | 24 +++++++++---- tests/CommonMarkTestStrict.php | 61 +++++++++++++++++++++++----------- tests/CommonMarkTestWeak.php | 44 +++++++++++++++++------- tests/ParsedownTest.php | 30 +++++++++++++---- 5 files changed, 116 insertions(+), 51 deletions(-) diff --git a/psalm.xml b/psalm.xml index 5c152d9..e7c5ea3 100644 --- a/psalm.xml +++ b/psalm.xml @@ -6,11 +6,13 @@ > + + @@ -18,14 +20,8 @@ - - - - - - diff --git a/tests/CommonMarkTest.php b/tests/CommonMarkTest.php index 1245954..3d3e492 100644 --- a/tests/CommonMarkTest.php +++ b/tests/CommonMarkTest.php @@ -25,7 +25,8 @@ namespace Erusev\Parsedown\Tests; class CommonMarkTest extends CommonMarkTestStrict { /** - * @return array + * @return array + * @throws \PHPUnit\Framework\AssertionFailedError */ public function data() { @@ -68,10 +69,13 @@ class CommonMarkTest extends CommonMarkTestStrict /** * @group update * @dataProvider dataUpdate - * @param $id - * @param $section - * @param $markdown - * @param $expectedHtml + * @param int $id + * @param string $section + * @param string $markdown + * @param string $expectedHtml + * @return void + * @throws \PHPUnit\Framework\AssertionFailedError + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException */ public function testUpdateDatabase($id, $section, $markdown, $expectedHtml) { @@ -79,19 +83,25 @@ class CommonMarkTest extends CommonMarkTestStrict // you can only get here when the test passes $dir = static::getDataDir(true); - $basename = $id . '-' . \preg_replace('/[^\w.-]/', '_', $section); + $basename = \strval($id) . '-' . \preg_replace('/[^\w.-]/', '_', $section); \file_put_contents($dir . $basename . '.md', $markdown); \file_put_contents($dir . $basename . '.html', $expectedHtml); } /** - * @return array + * @return array + * @throws \PHPUnit\Framework\AssertionFailedError */ public function dataUpdate() { return parent::data(); } + /** + * @param bool $mkdir + * @return string + * @throws \PHPUnit\Framework\AssertionFailedError + */ public static function getDataDir($mkdir = false) { $dir = __DIR__ . '/commonmark/'; diff --git a/tests/CommonMarkTestStrict.php b/tests/CommonMarkTestStrict.php index 6687081..f83a058 100644 --- a/tests/CommonMarkTestStrict.php +++ b/tests/CommonMarkTestStrict.php @@ -20,30 +20,49 @@ class CommonMarkTestStrict extends TestCase const SPEC_LOCAL_CACHE = 'spec_cache.txt'; const SPEC_CACHE_SECONDS = 300; - protected $parsedown; + /** @var Parsedown */ + protected $Parsedown; - protected function setUp() + /** + * @param string|null $name + * @param array $data + * @param string $dataName + */ + public function __construct($name = null, array $data = [], $dataName = '') { - $this->parsedown = new Parsedown(new State([ + $this->Parsedown = new Parsedown(new State([ StrictMode::enabled(), InlineTypes::initial()->removing([Url::class]), ])); + + $this->backupGlobals = false; + $this->backupStaticAttributes = false; + $this->runTestInSeparateProcess = false; + + parent::__construct($name, $data, $dataName); } /** * @dataProvider data - * @param $id - * @param $section - * @param $markdown - * @param $expectedHtml + * @param int $_ + * @param string $__ + * @param string $markdown + * @param string $expectedHtml + * @return void + * @throws \PHPUnit\Framework\AssertionFailedError + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException */ - public function testExample($id, $section, $markdown, $expectedHtml) + public function testExample($_, $__, $markdown, $expectedHtml) { - $actualHtml = $this->parsedown->text($markdown); + $actualHtml = $this->Parsedown->text($markdown); $this->assertEquals($expectedHtml, $actualHtml); } - public static function getSpec() + /** + * @return string + * @throws \PHPUnit\Framework\AssertionFailedError + */ + public function getSpec() { $specPath = __DIR__ .'/'.self::SPEC_LOCAL_CACHE; @@ -57,20 +76,23 @@ class CommonMarkTestStrict extends TestCase \file_put_contents($specPath, $spec); } - return $spec; - } - - /** - * @return array - */ - public function data() - { - $spec = self::getSpec(); if ($spec === false) { $this->fail('Unable to load CommonMark spec from ' . self::SPEC_URL); } + return $spec; + } + + /** + * @return array + * @throws \PHPUnit\Framework\AssertionFailedError + */ + public function data() + { + $spec = $this->getSpec(); + $spec = \str_replace("\r\n", "\n", $spec); + /** @var string */ $spec = \strstr($spec, '', true); $matches = []; @@ -79,6 +101,7 @@ class CommonMarkTestStrict extends TestCase $data = []; $currentId = 0; $currentSection = ''; + /** @var array{0: string, 1: string, 2?: string, 3?: string} $match */ foreach ($matches as $match) { if (isset($match[3])) { $currentSection = $match[3]; diff --git a/tests/CommonMarkTestWeak.php b/tests/CommonMarkTestWeak.php index 19832ed..22a4470 100644 --- a/tests/CommonMarkTestWeak.php +++ b/tests/CommonMarkTestWeak.php @@ -18,37 +18,57 @@ use Erusev\Parsedown\Html\Renderables\Element; */ class CommonMarkTestWeak extends CommonMarkTestStrict { + /** @var string */ protected $textLevelElementRegex; - protected function setUp() + /** + * @param string|null $name + * @param array $data + * @param string $dataName + */ + public function __construct($name = null, array $data = [], $dataName = '') { - parent::setUp(); - $textLevelElements = \array_keys(Element::$TEXT_LEVEL_ELEMENTS); - \array_walk($textLevelElements, function (&$element) { - $element = \preg_quote($element, '/'); - }); + \array_walk( + $textLevelElements, + /** + * @param string &$element + * @return void + */ + function (&$element) { + $element = \preg_quote($element, '/'); + } + ); $this->textLevelElementRegex = '\b(?:' . \implode('|', $textLevelElements) . ')\b'; + + parent::__construct($name, $data, $dataName); } /** * @dataProvider data - * @param $id - * @param $section - * @param $markdown - * @param $expectedHtml + * @param int $_ + * @param string $__ + * @param string $markdown + * @param string $expectedHtml + * @return void + * @throws \PHPUnit\Framework\AssertionFailedError + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException */ - public function testExample($id, $section, $markdown, $expectedHtml) + public function testExample($_, $__, $markdown, $expectedHtml) { $expectedHtml = $this->cleanupHtml($expectedHtml); - $actualHtml = $this->parsedown->text($markdown); + $actualHtml = $this->Parsedown->text($markdown); $actualHtml = $this->cleanupHtml($actualHtml); $this->assertEquals($expectedHtml, $actualHtml); } + /** + * @param string $markup + * @return string + */ protected function cleanupHtml($markup) { // invisible whitespaces at the beginning and end of block elements diff --git a/tests/ParsedownTest.php b/tests/ParsedownTest.php index 187a29f..03bf8aa 100755 --- a/tests/ParsedownTest.php +++ b/tests/ParsedownTest.php @@ -15,30 +15,40 @@ use PHPUnit\Framework\TestCase; class ParsedownTest extends TestCase { + /** + * @param string|null $name + * @param array $data + * @param string $dataName + */ final public function __construct($name = null, array $data = [], $dataName = '') { $this->dirs = $this->initDirs(); + $this->backupGlobals = false; + $this->backupStaticAttributes = false; + $this->runTestInSeparateProcess = false; + parent::__construct($name, $data, $dataName); } + /** @var string[] */ private $dirs; - protected $Parsedown; /** - * @return array + * @return string[] */ protected function initDirs() { - $dirs []= \dirname(__FILE__).'/data/'; - - return $dirs; + return [\dirname(__FILE__).'/data/']; } /** * @dataProvider data - * @param $test - * @param $dir + * @param string $test + * @param string $dir + * @return void + * @throws \PHPUnit\Framework\ExpectationFailedException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException */ public function test_($test, $dir) { @@ -60,6 +70,7 @@ class ParsedownTest extends TestCase $this->assertEquals($expectedMarkup, $actualMarkup); } + /** @return array