Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace substr in getCode() to cover more cases (#61)
- Loading branch information
1 parent
53f7fe1
commit 69e7735
Showing
3 changed files
with
72 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
use ClassPreloader\ClassPreloader; | ||
use ClassPreloader\Parser\NodeTraverser; | ||
use PhpParser\Parser; | ||
use PhpParser\PrettyPrinter\Standard as PrettyPrinter; | ||
|
||
class ClassPreloaderTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* This tests the correct detection and stripping of strict_types declarations while preloading files. | ||
*/ | ||
public function testStripStrictTypeDeclaration() | ||
{ | ||
$printer = $this->getMockBuilder(PrettyPrinter::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['prettyPrint']); | ||
$parser = $this->getMockBuilder(Parser::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['parse', 'getErrors']); | ||
$traverser = $this->getMockBuilder(NodeTraverser::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['traverseFile']); | ||
|
||
$parserMock = $parser->getMock(); | ||
$parserMock->expects($this->once()) | ||
->method('parse') | ||
->willReturn([]); | ||
$traverserMock = $traverser->getMock(); | ||
$traverserMock->expects($this->once()) | ||
->method('traverseFile') | ||
->willReturn([]); | ||
$printerMock = $printer->getMock(); | ||
$printerMock->expects($this->once()) | ||
->method('prettyPrint') | ||
->willReturn( | ||
file_get_contents(__DIR__.'/stubs/StrictClassWithComments.php') | ||
); | ||
|
||
$classPreloader = new ClassPreloader( | ||
$printerMock, | ||
$parserMock, | ||
$traverserMock | ||
); | ||
|
||
$code = $classPreloader->getCode(__DIR__.'/stubs/StrictClassWithComments.php'); | ||
|
||
// $code should not have 'declare(strict_types=1)' declarations. | ||
$this->assertNotRegExp( | ||
'/(.*?)declare\s*\(strict_types\s*=\s*1\)(.*?)/mi', | ||
$code, | ||
'Generated ClassPreloader output should correctly detect and strip strict_type declare statements.' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* This is some copyright or file comment, popular in some libraries. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Not some Class comment. | ||
*/ | ||
class StrictClassWithComments | ||
{ | ||
} |