-
Notifications
You must be signed in to change notification settings - Fork 4
Ignore expanded entity nodes in source-backed sniffs #32
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,61 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace DocbookCS\Runner; | ||
|
|
||
| final class EntityExpansionMarker | ||
| { | ||
| private const string START = 'docbook-cs:entity-expansion:start'; | ||
| private const string END = 'docbook-cs:entity-expansion:end'; | ||
|
|
||
| public static function wrap(string $content): string | ||
| { | ||
| return sprintf('<!--%s-->%s<!--%s-->', self::START, $content, self::END); | ||
| } | ||
|
|
||
| public static function contains(\DOMNode $node): bool | ||
| { | ||
| for ($current = $node; $current->parentNode !== null; $current = $current->parentNode) { | ||
| if (self::isBetweenMarkers($current)) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static function isBetweenMarkers(\DOMNode $node): bool | ||
| { | ||
| $nestedMarkers = 0; | ||
|
|
||
| for ($sibling = $node->previousSibling; $sibling !== null; $sibling = $sibling->previousSibling) { | ||
| if (self::isEnd($sibling)) { | ||
| $nestedMarkers++; | ||
| continue; | ||
| } | ||
|
|
||
| if (!self::isStart($sibling)) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($nestedMarkers === 0) { | ||
| return true; | ||
| } | ||
|
|
||
| $nestedMarkers--; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static function isStart(\DOMNode $node): bool | ||
| { | ||
| return $node instanceof \DOMComment && $node->textContent === self::START; | ||
| } | ||
|
|
||
| private static function isEnd(\DOMNode $node): bool | ||
| { | ||
| return $node instanceof \DOMComment && $node->textContent === self::END; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,72 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace DocbookCS\Tests\Integration\Runner; | ||
|
|
||
| use DocbookCS\Runner\EntityExpansionMarker; | ||
| use DocbookCS\Runner\EntityPreprocessor; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| #[ | ||
| CoversClass(EntityExpansionMarker::class), | ||
| CoversClass(EntityPreprocessor::class), | ||
| ] | ||
| final class EntityExpansionMarkerTest extends TestCase | ||
| { | ||
| #[Test] | ||
| public function itMarksXmlExpansionsForParsingOnly(): void | ||
| { | ||
| $preprocessor = new EntityPreprocessor([ | ||
| 'expanded' => '<para>Expanded</para>', | ||
| ]); | ||
| $source = '<root>&expanded;</root>'; | ||
| $document = $this->parse($preprocessor->processForParsing($source)); | ||
| $para = $document->getElementsByTagName('para')->item(0); | ||
| $root = $document->documentElement; | ||
|
|
||
| self::assertSame('<root><para>Expanded</para></root>', $preprocessor->process($source)); | ||
| self::assertInstanceOf(\DOMElement::class, $para); | ||
| self::assertInstanceOf(\DOMElement::class, $root); | ||
| self::assertTrue(EntityExpansionMarker::contains($para)); | ||
| self::assertFalse(EntityExpansionMarker::contains($root)); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function itRecognizesNestedExpansionMarkers(): void | ||
| { | ||
| $preprocessor = new EntityPreprocessor([ | ||
| 'outer' => '<wrapper>&inner;<after/></wrapper>', | ||
| 'inner' => '<para/>', | ||
| ]); | ||
| $document = $this->parse($preprocessor->processForParsing('<root>&outer;</root>')); | ||
|
|
||
| foreach (['wrapper', 'para', 'after'] as $elementName) { | ||
| $element = $document->getElementsByTagName($elementName)->item(0); | ||
| self::assertInstanceOf(\DOMElement::class, $element); | ||
| self::assertTrue(EntityExpansionMarker::contains($element)); | ||
| } | ||
| } | ||
|
|
||
| #[Test] | ||
| public function itDoesNotExpandLiteralEntityText(): void | ||
| { | ||
| $preprocessor = new EntityPreprocessor(['value' => 'expanded']); | ||
| $source = '<root><!-- &value; --><![CDATA[&value;]]><?test &value;?>&value;</root>'; | ||
|
|
||
| self::assertSame( | ||
| '<root><!-- &value; --><![CDATA[&value;]]><?test &value;?>expanded</root>', | ||
| $preprocessor->process($source), | ||
| ); | ||
| } | ||
|
|
||
| private function parse(string $xml): \DOMDocument | ||
| { | ||
| $document = new \DOMDocument(); | ||
| $document->loadXML($xml); | ||
|
|
||
| return $document; | ||
| } | ||
| } |
This file contains hidden or 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,62 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace DocbookCS\Tests\Integration\Sniff; | ||
|
|
||
| use DocbookCS\Report\Violation; | ||
| use DocbookCS\Runner\EntityExpansionMarker; | ||
| use DocbookCS\Runner\EntityPreprocessor; | ||
| use DocbookCS\Sniff\AbstractSniff; | ||
| use DocbookCS\Sniff\ExceptionNameSniff; | ||
| use DocbookCS\Sniff\SimparaSniff; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| #[ | ||
| CoversClass(AbstractSniff::class), | ||
| CoversClass(EntityExpansionMarker::class), | ||
| CoversClass(EntityPreprocessor::class), | ||
| CoversClass(ExceptionNameSniff::class), | ||
| CoversClass(SimparaSniff::class), | ||
| CoversClass(Violation::class), | ||
| ] | ||
| final class EntityExpandedSniffTest extends TestCase | ||
| { | ||
| #[Test] | ||
| public function simparaIgnoresExpandedElements(): void | ||
| { | ||
| $source = '<root><para>Source</para>&expanded;</root>'; | ||
| $document = $this->processedDocument($source, '<para>Expanded</para>'); | ||
|
|
||
| $violations = new SimparaSniff()->process($document, $source, 'file.xml'); | ||
|
|
||
| self::assertCount(1, $violations); | ||
| self::assertSame(1, $violations[0]->line); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function exceptionNameIgnoresExpandedElements(): void | ||
| { | ||
| $source = '<root><classname>RuntimeException</classname>&expanded;</root>'; | ||
| $document = $this->processedDocument( | ||
| $source, | ||
| '<classname>ExpandedException</classname>', | ||
| ); | ||
|
|
||
| $violations = new ExceptionNameSniff()->process($document, $source, 'file.xml'); | ||
|
|
||
| self::assertCount(1, $violations); | ||
| self::assertSame(1, $violations[0]->line); | ||
| } | ||
|
|
||
| private function processedDocument(string $source, string $expanded): \DOMDocument | ||
| { | ||
| $content = new EntityPreprocessor(['expanded' => $expanded])->processForParsing($source); | ||
| $document = new \DOMDocument(); | ||
| $document->loadXML($content); | ||
|
|
||
| return $document; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it's debatable if they are unit tests. Could you merge them into
tests/Unitinstead?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, can follow your preference! Before I move them back: the end result in later branches is having Unit, Feature, and Integration. That is because there will be tests that run actual git commands for e2e testing. Given that, still all to Unit?
Ref: https://github.com/NickSdot/php__docbook-cs/tree/stack-09-performance/tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, yes. As it would also be more easy to distinguish between tests and non-tests (fixtures & Support).