Skip to content

Commit

Permalink
Merge pull request #39 from gsteel/test-uncovered
Browse files Browse the repository at this point in the history
General Maintenance
  • Loading branch information
Ocramius authored Oct 10, 2022
2 parents ab1f99c + c4fd955 commit ee7a4c3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
11 changes: 7 additions & 4 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/.github/ export-ignore
/docs/ export-ignore
/test/ export-ignore

/.gitignore export-ignore
/.laminas-ci.json export-ignore
/composer.lock export-ignore
/composer-require-checker.json export-ignore
/renovate.json export-ignore
/.gitattributes export-ignore
/infection.json.dist export-ignore
/phpcs.xml.dist export-ignore
/.gitattributes export-ignore
/phpunit.xml.dist export-ignore
/psalm.xml export-ignore
/psalm-baseline.xml export-ignore
/mkdocs.yml export-ignore
/.github/ export-ignore
/docs/ export-ignore
/test/ export-ignore
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/.phpcs-cache
/.phpunit.result.cache
/clover.xml
/coveralls-upload.json
/docs/html/
/laminas-mkdoc-theme.tgz
/laminas-mkdoc-theme/
Expand Down
6 changes: 1 addition & 5 deletions .laminas-ci.json
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
{
"ignore_php_platform_requirements": {
"8.1": true
}
}
{}
55 changes: 31 additions & 24 deletions test/EscaperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

class EscaperTest extends TestCase
{
/** @var Escaper */
protected $escaper;
private Escaper $escaper;

protected function setUp(): void
{
Expand Down Expand Up @@ -76,7 +75,7 @@ public function supportedEncodingsProvider(): array
public function testSettingValidEncodingShouldNotThrowExceptions(string $encoding): void
{
$escaper = new Escaper($encoding);
$this->assertSame($encoding, $escaper->getEncoding());
self::assertSame($encoding, $escaper->getEncoding());
}

public function testSettingEncodingToInvalidValueShouldThrowException(): void
Expand All @@ -87,7 +86,7 @@ public function testSettingEncodingToInvalidValueShouldThrowException(): void

public function testReturnsEncodingFromGetter(): void
{
$this->assertEquals('utf-8', $this->escaper->getEncoding());
self::assertEquals('utf-8', $this->escaper->getEncoding());
}

/** @return array<array-key, array{0: string, 1: string}> */
Expand All @@ -107,7 +106,7 @@ public function htmlSpecialCharsProvider(): array
*/
public function testHtmlEscapingConvertsSpecialChars(string $string, string $encoded): void
{
$this->assertEquals($encoded, $this->escaper->escapeHtml($string), 'Failed to escape: ' . $string);
self::assertEquals($encoded, $this->escaper->escapeHtml($string), 'Failed to escape: ' . $string);
}

/** @return array<array-key, array{0: string, 1: string}> */
Expand Down Expand Up @@ -156,7 +155,7 @@ public function htmlAttrSpecialCharsProvider(): array
*/
public function testHtmlAttributeEscapingConvertsSpecialChars(string $string, string $encoded): void
{
$this->assertEquals($encoded, $this->escaper->escapeHtmlAttr($string), 'Failed to escape: ' . $string);
self::assertEquals($encoded, $this->escaper->escapeHtmlAttr($string), 'Failed to escape: ' . $string);
}

/** @return array<array-key, array{0: string, 1: string}> */
Expand Down Expand Up @@ -199,17 +198,17 @@ public function jsSpecialCharsProvider(): array
*/
public function testJavascriptEscapingConvertsSpecialChars(string $string, string $encoded): void
{
$this->assertEquals($encoded, $this->escaper->escapeJs($string), 'Failed to escape: ' . $string);
self::assertEquals($encoded, $this->escaper->escapeJs($string), 'Failed to escape: ' . $string);
}

public function testJavascriptEscapingReturnsStringIfZeroLength(): void
{
$this->assertEquals('', $this->escaper->escapeJs(''));
self::assertEquals('', $this->escaper->escapeJs(''));
}

public function testJavascriptEscapingReturnsStringIfContainsOnlyDigits(): void
{
$this->assertEquals('123', $this->escaper->escapeJs('123'));
self::assertEquals('123', $this->escaper->escapeJs('123'));
}

/** @return array<array-key, array{0: string, 1: string}> */
Expand Down Expand Up @@ -252,17 +251,17 @@ public function cssSpecialCharsProvider(): array
*/
public function testCssEscapingConvertsSpecialChars(string $string, string $encoded): void
{
$this->assertEquals($encoded, $this->escaper->escapeCss($string), 'Failed to escape: ' . $string);
self::assertEquals($encoded, $this->escaper->escapeCss($string), 'Failed to escape: ' . $string);
}

public function testCssEscapingReturnsStringIfZeroLength(): void
{
$this->assertEquals('', $this->escaper->escapeCss(''));
self::assertEquals('', $this->escaper->escapeCss(''));
}

public function testCssEscapingReturnsStringIfContainsOnlyDigits(): void
{
$this->assertEquals('123', $this->escaper->escapeCss('123'));
self::assertEquals('123', $this->escaper->escapeCss('123'));
}

/** @return array<array-key, array{0: string, 1: string}> */
Expand Down Expand Up @@ -309,7 +308,7 @@ public function urlSpecialCharsProvider(): array
*/
public function testUrlEscapingConvertsSpecialChars(string $string, string $encoded): void
{
$this->assertEquals($encoded, $this->escaper->escapeUrl($string), 'Failed to escape: ' . $string);
self::assertEquals($encoded, $this->escaper->escapeUrl($string), 'Failed to escape: ' . $string);
}

/**
Expand All @@ -326,9 +325,9 @@ public function testUnicodeCodepointConversionToUtf8(): void
$codepoints = [0x20, 0x7e, 0x799];
$result = '';
foreach ($codepoints as $value) {
$result .= $this->codepointToUtf8($value);
$result .= self::codepointToUtf8($value);
}
$this->assertEquals($expected, $result);
self::assertEquals($expected, $result);
}

/**
Expand All @@ -338,7 +337,7 @@ public function testUnicodeCodepointConversionToUtf8(): void
* @return string UTF-8 literal string
* @throws Exception When codepoint requested is outside Unicode range.
*/
protected function codepointToUtf8(int $codepoint): string
private static function codepointToUtf8(int $codepoint): string
{
if ($codepoint < 0x80) {
return chr($codepoint);
Expand Down Expand Up @@ -375,7 +374,7 @@ public function owaspJSRecommendedEscapeRangeProvider(): Generator
continue;
}

$literal = $this->codepointToUtf8($chr);
$literal = self::codepointToUtf8($chr);
if (in_array($literal, $immune)) {
yield $chr => [$chr, 'assertEquals'];
continue;
Expand All @@ -390,7 +389,7 @@ public function owaspJSRecommendedEscapeRangeProvider(): Generator
*/
public function testJavascriptEscapingEscapesOwaspRecommendedRanges(int $codepoint, string $assertion): void
{
$literal = $this->codepointToUtf8($codepoint);
$literal = self::codepointToUtf8($codepoint);

$this->$assertion($literal, $this->escaper->escapeJs($literal));
}
Expand All @@ -404,14 +403,14 @@ public function testHtmlAttributeEscapingEscapesOwaspRecommendedRanges(): void
|| $chr >= 0x41 && $chr <= 0x5A
|| $chr >= 0x61 && $chr <= 0x7A
) {
$literal = $this->codepointToUtf8($chr);
$this->assertEquals($literal, $this->escaper->escapeHtmlAttr($literal));
$literal = self::codepointToUtf8($chr);
self::assertEquals($literal, $this->escaper->escapeHtmlAttr($literal));
} else {
$literal = $this->codepointToUtf8($chr);
$literal = self::codepointToUtf8($chr);
if (in_array($literal, $immune)) {
$this->assertEquals($literal, $this->escaper->escapeHtmlAttr($literal));
self::assertEquals($literal, $this->escaper->escapeHtmlAttr($literal));
} else {
$this->assertNotEquals(
self::assertNotEquals(
$literal,
$this->escaper->escapeHtmlAttr($literal),
$literal . ' should be escaped!'
Expand Down Expand Up @@ -447,8 +446,16 @@ public function owaspCSSRecommendedEscapeRangeProvider(): array
*/
public function testCssEscapingEscapesOwaspRecommendedRanges(int $codePoint, string $assertion): void
{
$literal = $this->codepointToUtf8($codePoint);
$literal = self::codepointToUtf8($codePoint);

$this->$assertion($literal, $this->escaper->escapeCss($literal));
}

public function testCanEscapeTextInAlternativeEncodings(): void
{
$westernLatin1String = chr(0x43) . chr(0x61) . chr(0x66) . chr(0xE9);
$expect = 'Caf&#xE9;';
$escaper = new Escaper('iso-8859-1');
self::assertSame($expect, $escaper->escapeHtmlAttr($westernLatin1String));
}
}

0 comments on commit ee7a4c3

Please sign in to comment.