-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to cover
AbstractUnicode
fixing undefined offset error
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
Showing
2 changed files
with
77 additions
and
1 deletion.
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,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()); | ||
} | ||
} |