Skip to content

Commit

Permalink
Add tests to cover AbstractUnicode fixing undefined offset error
Browse files Browse the repository at this point in the history
This test case has been added to document that `AbstractUnicode` lower cases it's encoding option. Also discovered that an undefined index would be caused by accessing the encoding option when no default had been set.

Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Sep 28, 2022
1 parent fa2ef11 commit d14c37b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/AbstractUnicode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Laminas\Filter;

use function array_map;
use function assert;
use function function_exists;
use function in_array;
use function is_string;
use function mb_internal_encoding;
use function mb_list_encodings;
use function sprintf;
Expand Down Expand Up @@ -53,7 +55,9 @@ public function setEncoding($encoding = null)
*/
public function getEncoding()
{
if ($this->options['encoding'] === null && function_exists('mb_internal_encoding')) {
$encoding = $this->options['encoding'] ?? null;
assert($encoding === null || is_string($encoding));
if ($encoding === null && function_exists('mb_internal_encoding')) {
$this->options['encoding'] = mb_internal_encoding();
}

Expand Down
72 changes: 72 additions & 0 deletions test/AbstractUnicodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Filter;

use Laminas\Filter\AbstractUnicode;
use Laminas\Filter\Exception\InvalidArgumentException;
use PHPUnit\Framework\TestCase;

use function assert;
use function is_string;
use function mb_internal_encoding;
use function strtolower;

class AbstractUnicodeTest extends TestCase
{
/** @var AbstractUnicode */
private $filter;

protected function setUp(): void
{
parent::setUp();

$this->filter = new class extends AbstractUnicode {
/** @param mixed $value */
public function filter($value): string
{
assert(is_string($value));
return strtolower($value);
}
};
}

/** @return list<array{0: string, 1: string}> */
public function encodingProvider(): array
{
return [
['ISO-8859-16', 'iso-8859-16'],
['UTF-8', 'utf-8'],
['Windows-1251', 'windows-1251'],
];
}

/** @dataProvider encodingProvider */
public function testThatEncodingOptionIsLowerCased(string $encoding, string $expectedEncoding): void
{
$this->filter->setEncoding($encoding);
self::assertNotSame($encoding, $this->filter->getEncoding());
self::assertSame($expectedEncoding, $this->filter->getEncoding());
}

public function testThatAnUnSupportedEncodingCausesAnException(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Encoding \'goats\' is not supported by mbstring extension');

$this->filter->setEncoding('Goats');
}

public function testThatMbStringInternalEncodingIsReturnedWhenEncodingHasNotBeenSpecified(): void
{
$expect = mb_internal_encoding();
self::assertSame($expect, $this->filter->getEncoding());
}

public function testThatExplicitlySettingEncodingToNullWillYieldDefaultEncoding(): void
{
$this->filter->setEncoding(null);
self::assertSame(mb_internal_encoding(), $this->filter->getEncoding());
}
}

0 comments on commit d14c37b

Please sign in to comment.