Skip to content

Commit 4705a82

Browse files
committed
more generator unit tests
1 parent 26ef398 commit 4705a82

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Safe\Generator;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Safe\PhpStanFunctions\PhpStanFunctionMapReader;
9+
use Safe\XmlDocParser\ErrorType;
10+
use Safe\XmlDocParser\DocPage;
11+
use Safe\XmlDocParser\Method;
12+
13+
class WritePhpFunctionTest extends TestCase
14+
{
15+
public function testGetPhpPrototypeFunctionRegular(): void
16+
{
17+
$docPage = new DocPage(DocPage::findReferenceDir() . '/pcre/functions/preg-match.xml');
18+
$xmlObject = $docPage->getMethodSynopsis();
19+
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader(), ErrorType::FALSY);
20+
21+
$writePhpFunction = new WritePhpFunction($method);
22+
$funcStr = $writePhpFunction->getPhpFunctionalFunction();
23+
$this->assertStringContainsString('function preg_match(string $pattern', $funcStr);
24+
}
25+
26+
public function testGetPhpPrototypeFunctionOverloaded(): void
27+
{
28+
$docPage = new DocPage(DocPage::findReferenceDir() . '/filesystem/functions/file-get-contents.xml');
29+
$xmlObject = $docPage->getMethodSynopsis();
30+
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader(), ErrorType::FALSY);
31+
32+
$writePhpFunction = new WritePhpFunction($method);
33+
$funcStr = $writePhpFunction->getPhpFunctionalFunction();
34+
$this->assertStringContainsString('} else {', $funcStr);
35+
}
36+
}

generator/tests/XmlDocParser/MethodTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public function testGetFunctionType(): void
2525
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader(), ErrorType::FALSY);
2626
$type = $method->getSignatureReturnType();
2727
$this->assertEquals('int', $type);
28+
$errorType = $method->getErrorType();
29+
$this->assertEquals(ErrorType::FALSY, $errorType);
2830
}
2931

3032
public function testGetFunctionParam(): void
@@ -117,6 +119,27 @@ public function testGetReturnDocBlock(): void
117119
$this->assertEquals('?bool', $method->getSignatureReturnType());
118120
}
119121

122+
public function testGetPhpDoc(): void
123+
{
124+
$docPage = new DocPage(DocPage::findReferenceDir() . '/array/functions/array-replace.xml');
125+
$xmlObject = $docPage->getMethodSynopsis();
126+
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader(), ErrorType::NULLSY);
127+
$this->assertStringContainsString('@param array $array', $method->getPhpDoc());
128+
}
129+
130+
public function testIsOverloaded(): void
131+
{
132+
$docPage = new DocPage(DocPage::findReferenceDir() . '/array/functions/array-all.xml');
133+
$xmlObject = $docPage->getMethodSynopsis();
134+
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader(), ErrorType::NULLSY);
135+
$this->assertFalse($method->isOverloaded());
136+
137+
$docPage = new DocPage(DocPage::findReferenceDir() . '/filesystem/functions/file-get-contents.xml');
138+
$xmlObject = $docPage->getMethodSynopsis();
139+
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader(), ErrorType::NULLSY);
140+
$this->assertTrue($method->isOverloaded());
141+
}
142+
120143
public function testOpensslCipherKeyLengthUnionTypeReturnDocBlocks(): void
121144
{
122145
$docPage = new DocPage(DocPage::findReferenceDir() . '/openssl/functions/openssl-cipher-key-length.xml');

generator/tests/XmlDocParser/ScannerTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Safe\XmlDocParser;
66

77
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Console\Output\OutputInterface;
89

910
class ScannerTest extends TestCase
1011
{
@@ -26,4 +27,23 @@ public function testGetFunctionsPaths(): void
2627
$this->assertArrayNotHasKey(DocPage::findReferenceDir() . '/filesystem/functions/chmod.xml', $paths);
2728
$this->assertArrayHasKey(DocPage::findReferenceDir() . '/spl/appenditerator/getarrayiterator.xml', $paths);
2829
}
30+
31+
public function testGetMethods(): void
32+
{
33+
$scanner = new Scanner(DocPage::findReferenceDir());
34+
$functions = $scanner->getFunctionsPaths();
35+
$testFunctions = [];
36+
foreach ($functions as $name => $info) {
37+
// check various branches:
38+
// - array = regular module
39+
// - bbcode = ignored module
40+
// - image = module with overloaded functions
41+
if (str_contains($name, "array") || str_contains($name, "bbcode") || str_contains($name, "image")) {
42+
$testFunctions[$name] = $info;
43+
}
44+
}
45+
$output = $this->createMock(OutputInterface::class);
46+
$response = $scanner->getMethods($testFunctions, $output);
47+
$this->assertNotEmpty($response->methods);
48+
}
2949
}

0 commit comments

Comments
 (0)