From 8fa87df0d5cc05d8c4218c14c2989cc81541eca9 Mon Sep 17 00:00:00 2001 From: George Steel Date: Sat, 29 Jun 2024 21:32:34 +0100 Subject: [PATCH] Refactor Lower Case and Upper Case Filters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Removes `AbstractUnicode` from the inheritance tree and deletes it - Clean up `File\LowerCase`, `File\UpperCase`, `StringToLower`, `StringToUpper` and `UpperCaseWords` so that they all implement `FilterInterface` directly without any inheritance. All the filters support an `encoding` option which is re-implemented in each class. Boilerplate is reduced by adding a new internal class `EncodingOption` for consistent handling of encoding options… Extracted some duplicated file handling code into an internal class `FilterFileContents` that applies a filter to the contents of a file. This removes the need for `File\LowerCase` to extend `StringToLower` for example. Signed-off-by: George Steel --- docs/book/v3/file.md | 9 ++-- docs/book/v3/standard-filters.md | 49 ++++++++----------- psalm-baseline.xml | 51 -------------------- src/AbstractUnicode.php | 60 ----------------------- src/EncodingOption.php | 45 ++++++++++++++++++ src/File/FilterFileContents.php | 65 +++++++++++++++++++++++++ src/File/LowerCase.php | 82 +++++++++++++++----------------- src/File/UpperCase.php | 75 +++++++++++++++-------------- src/StringToLower.php | 31 ++++++------ src/StringToUpper.php | 31 ++++++------ src/UpperCaseWords.php | 38 +++++---------- test/AbstractUnicodeTest.php | 71 --------------------------- test/File/LowerCaseTest.php | 32 +++---------- test/File/UpperCaseTest.php | 35 ++++---------- test/StringToLowerTest.php | 54 +++++---------------- test/StringToUpperTest.php | 38 ++------------- test/UpperCaseWordsTest.php | 44 +++-------------- 17 files changed, 288 insertions(+), 522 deletions(-) delete mode 100644 src/AbstractUnicode.php create mode 100644 src/EncodingOption.php create mode 100644 src/File/FilterFileContents.php delete mode 100644 test/AbstractUnicodeTest.php diff --git a/docs/book/v3/file.md b/docs/book/v3/file.md index 5df36d49..8216c35b 100644 --- a/docs/book/v3/file.md +++ b/docs/book/v3/file.md @@ -11,8 +11,7 @@ performing file operations such as renaming. ## Lowercase -`Laminas\Filter\File\Lowercase` can be used to convert all file contents to -lowercase. +`Laminas\Filter\File\Lowercase` can be used to convert file contents to lowercase. ### Supported Options @@ -314,8 +313,7 @@ foreach ($request->getUploadedFiles() as $uploadedFile) { ## Uppercase -`Laminas\Filter\File\Uppercase` can be used to convert all file contents to -uppercase. +`Laminas\Filter\File\Uppercase` can be used to convert file contents to uppercase. ### Supported Options @@ -337,5 +335,4 @@ $filter = new UpperCase(); $filter->filter($files['my-upload']); ``` -See the documentation on the [`LowerCase`](#lowercase) filter, above, for more -information. +See the documentation on the [`LowerCase`](#lowercase) filter, above, for more information. diff --git a/docs/book/v3/standard-filters.md b/docs/book/v3/standard-filters.md index 1f97f7f9..d4d2d981 100644 --- a/docs/book/v3/standard-filters.md +++ b/docs/book/v3/standard-filters.md @@ -1221,13 +1221,13 @@ The above results in the string `MidCentral-PHP`. ## StringToLower -This filter converts any input to lowercase. +This filter converts string input to lowercase. ### Supported Options The following options are supported for `Laminas\Filter\StringToLower`: -- `encoding`: This option can be used to set an encoding to use. +- `encoding`: This option can be used to set the expected character encoding of the input. ### Basic Usage @@ -1238,41 +1238,31 @@ print $filter->filter('SAMPLE'); // returns "sample" ``` -### Handling alternate Encoding +### Handling Alternate Encodings -By default, `StringToLower` will only handle characters from the locale of your -server; characters from other charsets will be ignored. If you have the mbstring -extension, however, you can use the filter with other encodings. Pass the -desired encoding when initiating the `StringToLower` filter, or use the -`setEncoding()` method to change it. +By default, `StringToLower` will only handle characters from the locale of your server; characters from other charsets will be ignored. +To correctly filter input in encodings other than the default detected encoding for your environment, pass the +desired encoding when initiating the `StringToLower` filter. ```php -// using UTF-8 -$filter = new Laminas\Filter\StringToLower('UTF-8'); - -// or give an array which can be useful when using a configuration -$filter = new Laminas\Filter\StringToLower(['encoding' => 'UTF-8']); - -// or do this afterwards -$filter->setEncoding('ISO-8859-1'); +$filter = new Laminas\Filter\StringToLower([ + 'encoding' => 'UTF-8', +]); ``` > ### Setting invalid Encodings > -> Be aware that you will get an exception when: -> -> - you attempt to set an encoding and the mbstring extension is unavailable; or -> - you attempt to set an encoding unsupported by the mbstring extension. +> Be aware that you will get an exception when you provide an encoding that is not supported by the `mbstring` extension. ## StringToUpper -This filter converts any input to UPPERCASE. +This filter converts string input to UPPERCASE. ### Supported Options The following options are supported for `Laminas\Filter\StringToUpper`: -- `encoding`: This option can be used to set the encoding to use. +- `encoding`: This option can be used to set the expected character encoding of the input. ### Basic Usage @@ -1283,17 +1273,16 @@ print $filter->filter('Sample'); // returns "SAMPLE" ``` -### Different encoded Strings +### Handling Alternate Encodings -Like the `StringToLower` filter, this filter will only handle characters -supported by your server locale, unless you have the mbstring extension enabled. -Using different character sets works the same as with `StringToLower`. +By default, `StringToUpper` will only handle characters from the locale of your server; characters from other charsets will be ignored. +To correctly filter input in encodings other than the default detected encoding for your environment, pass the +desired encoding when initiating the `StringToUpper` filter. ```php -$filter = new Laminas\Filter\StringToUpper(['encoding' => 'UTF-8']); - -// or do this afterwards -$filter->setEncoding('ISO-8859-1'); +$filter = new Laminas\Filter\StringToUpper([ + 'encoding' => 'UTF-8', +]); ``` ## StringTrim diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 28847e81..1250101d 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -283,14 +283,6 @@ - - - - - - - - @@ -443,14 +435,6 @@ - - - - - - - - @@ -634,18 +618,6 @@ - - - - - - - - - - - - options['charlist']]]> @@ -687,12 +659,6 @@ - - - - - - @@ -757,14 +723,6 @@ - - - - - - - - @@ -870,9 +828,6 @@ - - - @@ -905,9 +860,6 @@ - - - @@ -1028,9 +980,6 @@ - - getMessage()]]> - diff --git a/src/AbstractUnicode.php b/src/AbstractUnicode.php deleted file mode 100644 index 11995022..00000000 --- a/src/AbstractUnicode.php +++ /dev/null @@ -1,60 +0,0 @@ - - */ -abstract class AbstractUnicode extends AbstractFilter -{ - /** - * Set the input encoding for the given string - * - * @throws Exception\InvalidArgumentException - * @throws Exception\ExtensionNotLoadedException - * @return $this - */ - public function setEncoding(?string $encoding = null): self - { - if ($encoding !== null) { - $encoding = strtolower($encoding); - $mbEncodings = array_map('strtolower', mb_list_encodings()); - if (! in_array($encoding, $mbEncodings, true)) { - throw new Exception\InvalidArgumentException(sprintf( - "Encoding '%s' is not supported by mbstring extension", - $encoding - )); - } - } - - $this->options['encoding'] = $encoding; - return $this; - } - - /** - * Returns the set encoding - */ - public function getEncoding(): string - { - $encoding = $this->options['encoding'] ?? null; - if ($encoding === null) { - $encoding = mb_internal_encoding(); - $this->options['encoding'] = $encoding; - } - - return $encoding; - } -} diff --git a/src/EncodingOption.php b/src/EncodingOption.php new file mode 100644 index 00000000..38d45753 --- /dev/null +++ b/src/EncodingOption.php @@ -0,0 +1,45 @@ + $filter */ + public function __construct(private readonly FilterInterface $filter) + { + } + + /** + * @throws InvalidArgumentException If no file exists. + * @throws RuntimeException If the file cannot be written to or read from. + */ + public function __invoke(string $filePath): void + { + if (! file_exists($filePath)) { + throw new InvalidArgumentException(sprintf( + 'File %s not found', + $filePath, + )); + } + + if (! is_writable($filePath)) { + throw new RuntimeException(sprintf( + 'File "%s" is not writable', + $filePath + )); + } + + $content = file_get_contents($filePath); + if ($content === false) { + throw new RuntimeException(sprintf( + 'The contents of "%s" could not be read', + $filePath, + )); + } + + $result = file_put_contents( + $filePath, + $this->filter->filter($content), + ); + + if ($result === false) { + throw new RuntimeException(sprintf( + 'The file "%s" could not be written to', + $filePath, + )); + } + } +} diff --git a/src/File/LowerCase.php b/src/File/LowerCase.php index 1490b56e..5486f970 100644 --- a/src/File/LowerCase.php +++ b/src/File/LowerCase.php @@ -4,73 +4,69 @@ namespace Laminas\Filter\File; -use Laminas\Filter\Exception; +use Laminas\Filter\EncodingOption; +use Laminas\Filter\Exception\InvalidArgumentException; +use Laminas\Filter\Exception\RuntimeException; +use Laminas\Filter\FilterInterface; use Laminas\Filter\StringToLower; -use function assert; -use function file_exists; -use function file_get_contents; -use function file_put_contents; use function is_array; -use function is_scalar; use function is_string; -use function is_writable; -final class LowerCase extends StringToLower +/** + * @psalm-type Options = array{encoding?: string} + * @implements FilterInterface + */ +final class LowerCase implements FilterInterface { + private readonly string $encoding; + /** - * Defined by Laminas\Filter\Filter - * - * Does a lowercase on the content of the given file + * @param Options $options + */ + public function __construct(array $options = []) + { + $this->encoding = EncodingOption::assertWithDefault($options['encoding'] ?? null); + } + + /** + * Lowercases the contents of the given file path * * @param mixed $value Full path of file to change or $_FILES data array - * @return mixed The given $value - * @throws Exception\InvalidArgumentException - * @throws Exception\RuntimeException + * @throws InvalidArgumentException + * @throws RuntimeException */ public function filter(mixed $value): mixed { - if (! is_scalar($value) && ! is_array($value)) { - return $value; + $filePath = null; + + if (is_string($value)) { + $filePath = $value; } // An uploaded file? Retrieve the 'tmp_name' - $isFileUpload = false; if (is_array($value)) { - if (! isset($value['tmp_name'])) { + if (! isset($value['tmp_name']) || ! is_string($value['tmp_name'])) { return $value; } - $isFileUpload = true; - $uploadData = $value; - $value = $value['tmp_name']; + $filePath = $value['tmp_name']; } - assert(is_string($value)); - - if (! file_exists($value)) { - throw new Exception\InvalidArgumentException("File '$value' not found"); - } - - if (! is_writable($value)) { - throw new Exception\RuntimeException("File '$value' is not writable"); - } - - $content = file_get_contents($value); - if ($content === false) { - throw new Exception\RuntimeException("Problem while reading file '$value'"); + if ($filePath === null) { + return $value; } - $content = parent::filter($content); - $result = file_put_contents($value, $content); - - if ($result === false) { - throw new Exception\RuntimeException("Problem while writing file '$value'"); - } + (new FilterFileContents( + new StringToLower(['encoding' => $this->encoding]), + ))($filePath); - if ($isFileUpload) { - return $uploadData; - } return $value; } + + /** @inheritDoc */ + public function __invoke(mixed $value): mixed + { + return $this->filter($value); + } } diff --git a/src/File/UpperCase.php b/src/File/UpperCase.php index 6aad9fc3..3651b869 100644 --- a/src/File/UpperCase.php +++ b/src/File/UpperCase.php @@ -4,70 +4,71 @@ namespace Laminas\Filter\File; -use Laminas\Filter\Exception; +use Laminas\Filter\EncodingOption; +use Laminas\Filter\Exception\InvalidArgumentException; +use Laminas\Filter\Exception\RuntimeException; +use Laminas\Filter\FilterInterface; use Laminas\Filter\StringToUpper; -use function file_exists; -use function file_get_contents; -use function file_put_contents; use function is_array; -use function is_scalar; use function is_string; -use function is_writable; -final class UpperCase extends StringToUpper +/** + * @psalm-type Options = array{encoding?: string} + * @implements FilterInterface + */ +final class UpperCase implements FilterInterface { + private readonly string $encoding; + + /** + * @param Options $options + */ + public function __construct(array $options = []) + { + $this->encoding = EncodingOption::assertWithDefault($options['encoding'] ?? null); + } + /** * Defined by Laminas\Filter\FilterInterface * * Does a lowercase on the content of the given file * * @param mixed $value Full path of file to change or $_FILES data array - * @return mixed|string|array The given $value - * @throws Exception\RuntimeException - * @throws Exception\InvalidArgumentException + * @throws RuntimeException + * @throws InvalidArgumentException */ public function filter(mixed $value): mixed { - if (! is_scalar($value) && ! is_array($value)) { - return $value; + $filePath = null; + + if (is_string($value)) { + $filePath = $value; } // An uploaded file? Retrieve the 'tmp_name' - $isFileUpload = false; if (is_array($value)) { - if (! isset($value['tmp_name'])) { + if (! isset($value['tmp_name']) || ! is_string($value['tmp_name'])) { return $value; } - $isFileUpload = true; - $uploadData = $value; - $value = $value['tmp_name']; - } - - if (! is_string($value) || ! file_exists($value)) { - throw new Exception\InvalidArgumentException("File '$value' not found"); + $filePath = $value['tmp_name']; } - if (! is_writable($value)) { - throw new Exception\InvalidArgumentException("File '$value' is not writable"); - } - - $content = file_get_contents($value); - if ($content === false) { - throw new Exception\RuntimeException("Problem while reading file '$value'"); + if ($filePath === null) { + return $value; } - $content = parent::filter($content); - $result = file_put_contents($value, $content); - - if ($result === false) { - throw new Exception\RuntimeException("Problem while writing file '$value'"); - } + (new FilterFileContents( + new StringToUpper(['encoding' => $this->encoding]), + ))($filePath); - if ($isFileUpload) { - return $uploadData; - } return $value; } + + /** @inheritDoc */ + public function __invoke(mixed $value): mixed + { + return $this->filter($value); + } } diff --git a/src/StringToLower.php b/src/StringToLower.php index cf9981be..41a06eac 100644 --- a/src/StringToLower.php +++ b/src/StringToLower.php @@ -8,33 +8,25 @@ use function mb_strtolower; /** - * @psalm-import-type UnicodeOptions from AbstractUnicode - * @extends AbstractUnicode + * @psalm-type Options = array{encoding?: string} + * @implements FilterInterface */ -class StringToLower extends AbstractUnicode +final class StringToLower implements FilterInterface { + private readonly string $encoding; + /** - * @param string|UnicodeOptions|iterable|null $encodingOrOptions + * @param Options $options */ - public function __construct($encodingOrOptions = null) + public function __construct(array $options = []) { - if ($encodingOrOptions !== null) { - if (! static::isOptions($encodingOrOptions)) { - $this->setEncoding($encodingOrOptions); - } else { - $this->setOptions($encodingOrOptions); - } - } + $this->encoding = EncodingOption::assertWithDefault($options['encoding'] ?? null); } /** - * Defined by Laminas\Filter\FilterInterface - * * Returns the string $value, converting characters to lowercase as necessary * * If the value provided is non-scalar, the value will remain unfiltered - * - * @psalm-return ($value is string ? string : $value) */ public function filter(mixed $value): mixed { @@ -42,6 +34,11 @@ public function filter(mixed $value): mixed return $value; } - return mb_strtolower((string) $value, $this->getEncoding()); + return mb_strtolower((string) $value, $this->encoding); + } + + public function __invoke(mixed $value): mixed + { + return $this->filter($value); } } diff --git a/src/StringToUpper.php b/src/StringToUpper.php index 9c7a2a84..c255b9c5 100644 --- a/src/StringToUpper.php +++ b/src/StringToUpper.php @@ -8,33 +8,25 @@ use function mb_strtoupper; /** - * @psalm-import-type UnicodeOptions from AbstractUnicode - * @extends AbstractUnicode + * @psalm-type Options = array{encoding?: string} + * @implements FilterInterface */ -class StringToUpper extends AbstractUnicode +final class StringToUpper implements FilterInterface { + private readonly string $encoding; + /** - * @param string|UnicodeOptions|iterable|null $encodingOrOptions + * @param Options $options */ - public function __construct($encodingOrOptions = null) + public function __construct(array $options = []) { - if ($encodingOrOptions !== null) { - if (! static::isOptions($encodingOrOptions)) { - $this->setEncoding($encodingOrOptions); - } else { - $this->setOptions($encodingOrOptions); - } - } + $this->encoding = EncodingOption::assertWithDefault($options['encoding'] ?? null); } /** - * Defined by Laminas\Filter\FilterInterface - * * Returns the string $value, converting characters to uppercase as necessary * * If the value provided is non-scalar, the value will remain unfiltered - * - * @psalm-return ($value is scalar ? string : mixed) */ public function filter(mixed $value): mixed { @@ -42,6 +34,11 @@ public function filter(mixed $value): mixed return $value; } - return mb_strtoupper((string) $value, $this->getEncoding()); + return mb_strtoupper((string) $value, $this->encoding); + } + + public function __invoke(mixed $value): mixed + { + return $this->filter($value); } } diff --git a/src/UpperCaseWords.php b/src/UpperCaseWords.php index 36d7b978..7835b696 100644 --- a/src/UpperCaseWords.php +++ b/src/UpperCaseWords.php @@ -10,42 +10,25 @@ use const MB_CASE_TITLE; /** - * @psalm-import-type UnicodeOptions from AbstractUnicode - * @extends AbstractUnicode + * @psalm-type Options = array{encoding?: string} + * @implements FilterInterface */ -final class UpperCaseWords extends AbstractUnicode +final class UpperCaseWords implements FilterInterface { - /** - * {@inheritDoc} - */ - protected $options = [ - 'encoding' => null, - ]; + private readonly string $encoding; /** - * @param string|UnicodeOptions|iterable|null $encodingOrOptions OPTIONAL + * @param Options $options */ - public function __construct($encodingOrOptions = null) + public function __construct(array $options = []) { - if ($encodingOrOptions !== null) { - if (self::isOptions($encodingOrOptions)) { - $this->setOptions($encodingOrOptions); - } else { - $this->setEncoding($encodingOrOptions); - } - } + $this->encoding = EncodingOption::assertWithDefault($options['encoding'] ?? null); } /** - * {@inheritDoc} - * * Returns the string $value, converting words to have an uppercase first character as necessary * * If the value provided is not a string, the value will remain unfiltered - * - * @param string|mixed $value - * @return mixed - * @psalm-return ($value is string ? string : mixed) */ public function filter(mixed $value): mixed { @@ -53,6 +36,11 @@ public function filter(mixed $value): mixed return $value; } - return mb_convert_case((string) $value, MB_CASE_TITLE, $this->getEncoding()); + return mb_convert_case($value, MB_CASE_TITLE, $this->encoding); + } + + public function __invoke(mixed $value): mixed + { + return $this->filter($value); } } diff --git a/test/AbstractUnicodeTest.php b/test/AbstractUnicodeTest.php deleted file mode 100644 index bfd862b6..00000000 --- a/test/AbstractUnicodeTest.php +++ /dev/null @@ -1,71 +0,0 @@ -filter = new class extends AbstractUnicode { - public function filter(mixed $value): mixed - { - assert(is_string($value)); - return strtolower($value); - } - }; - } - - /** @return list */ - public static 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()); - } -} diff --git a/test/File/LowerCaseTest.php b/test/File/LowerCaseTest.php index 74f0a55f..dbc7ac66 100644 --- a/test/File/LowerCaseTest.php +++ b/test/File/LowerCaseTest.php @@ -4,8 +4,7 @@ namespace LaminasTest\Filter\File; -use Laminas\Filter\Exception; -use Laminas\Filter\Exception\ExtensionNotLoadedException; +use Laminas\Filter\Exception\InvalidArgumentException; use Laminas\Filter\File\LowerCase as FileLowerCase; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -62,35 +61,18 @@ public function testNormalWorkflowWithFilesArray(): void public function testFileNotFoundException(): void { - $this->expectException(Exception\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('not found'); $filter = new FileLowerCase(); $filter($this->testFile . 'unknown'); } - public function testCheckSettingOfEncodingInIstance(): void + public function testCheckSettingOfEncodingInInstance(): void { self::assertStringContainsString('This is a File', file_get_contents($this->testFile)); - try { - $filter = new FileLowerCase('ISO-8859-1'); - $filter($this->testFile); - self::assertStringContainsString('this is a file', file_get_contents($this->testFile)); - } catch (ExtensionNotLoadedException $e) { - self::assertStringContainsString('mbstring is required', $e->getMessage()); - } - } - - public function testCheckSettingOfEncodingWithMethod(): void - { - self::assertStringContainsString('This is a File', file_get_contents($this->testFile)); - try { - $filter = new FileLowerCase(); - $filter->setEncoding('ISO-8859-1'); - $filter($this->testFile); - self::assertStringContainsString('this is a file', file_get_contents($this->testFile)); - } catch (ExtensionNotLoadedException $e) { - self::assertStringContainsString('mbstring is required', $e->getMessage()); - } + $filter = new FileLowerCase(['encoding' => 'ISO-8859-1']); + $filter($this->testFile); + self::assertStringContainsString('this is a file', file_get_contents($this->testFile)); } public static function returnUnfilteredDataProvider(): array @@ -108,7 +90,7 @@ public static function returnUnfilteredDataProvider(): array } #[DataProvider('returnUnfilteredDataProvider')] - public function testReturnUnfiltered($input): void + public function testReturnUnfiltered(mixed $input): void { $filter = new FileLowerCase(); diff --git a/test/File/UpperCaseTest.php b/test/File/UpperCaseTest.php index 42f86d0f..8329057e 100644 --- a/test/File/UpperCaseTest.php +++ b/test/File/UpperCaseTest.php @@ -4,8 +4,7 @@ namespace LaminasTest\Filter\File; -use Laminas\Filter\Exception; -use Laminas\Filter\Exception\ExtensionNotLoadedException; +use Laminas\Filter\Exception\InvalidArgumentException; use Laminas\Filter\File\UpperCase as FileUpperCase; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -76,34 +75,17 @@ public function testNormalWorkflowWithFilesArray(): void public function testFileNotFoundException(): void { $filter = new FileUpperCase(); - $this->expectException(Exception\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('not found'); $filter($this->testFile . 'unknown'); } - public function testCheckSettingOfEncodingInIstance(): void + public function testCheckSettingOfEncodingInInstance(): void { self::assertStringContainsString('This is a File', file_get_contents($this->testFile)); - try { - $filter = new FileUpperCase('ISO-8859-1'); - $filter($this->testFile); - self::assertStringContainsString('THIS IS A FILE', file_get_contents($this->testFile)); - } catch (ExtensionNotLoadedException $e) { - self::assertStringContainsString('mbstring is required', $e->getMessage()); - } - } - - public function testCheckSettingOfEncodingWithMethod(): void - { - self::assertStringContainsString('This is a File', file_get_contents($this->testFile)); - try { - $filter = new FileUpperCase(); - $filter->setEncoding('ISO-8859-1'); - $filter($this->testFile); - self::assertStringContainsString('THIS IS A FILE', file_get_contents($this->testFile)); - } catch (ExtensionNotLoadedException $e) { - self::assertStringContainsString('mbstring is required', $e->getMessage()); - } + $filter = new FileUpperCase(['encoding' => 'ISO-8859-1']); + $filter($this->testFile); + self::assertStringContainsString('THIS IS A FILE', file_get_contents($this->testFile)); } public static function returnUnfilteredDataProvider(): array @@ -121,10 +103,9 @@ public static function returnUnfilteredDataProvider(): array } #[DataProvider('returnUnfilteredDataProvider')] - public function testReturnUnfiltered($input): void + public function testReturnUnfiltered(mixed $input): void { - $filter = new FileUpperCase(); - $filter->setEncoding('ISO-8859-1'); + $filter = new FileUpperCase(['encoding' => 'ISO-8859-1']); self::assertSame($input, $filter($input)); } diff --git a/test/StringToLowerTest.php b/test/StringToLowerTest.php index 26a62c4c..9e011984 100644 --- a/test/StringToLowerTest.php +++ b/test/StringToLowerTest.php @@ -7,12 +7,9 @@ use Laminas\Filter\Exception; use Laminas\Filter\StringToLower as StringToLowerFilter; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; use stdClass; -use function mb_internal_encoding; - class StringToLowerTest extends TestCase { private StringToLowerFilter $filter; @@ -45,14 +42,13 @@ public function testBasic(): void */ public function testWithEncoding(): void { - $filter = $this->filter; + $filter = new StringToLowerFilter(['encoding' => 'utf-8']); $valuesExpected = [ 'Ü' => 'ü', 'Ñ' => 'ñ', 'ÜÑ123' => 'üñ123', ]; - $filter->setEncoding('UTF-8'); foreach ($valuesExpected as $input => $output) { self::assertSame($output, $filter($input)); } @@ -62,13 +58,13 @@ public function testFalseEncoding(): void { $this->expectException(Exception\InvalidArgumentException::class); $this->expectExceptionMessage('is not supported'); - $this->filter->setEncoding('aaaaa'); + new StringToLowerFilter(['encoding' => 'aaaaa']); } /** - * @Laminas-8989 + * @Laminas-9058 */ - public function testInitiationWithEncoding(): void + public function testCaseInsensitiveEncoding(): void { $valuesExpected = [ 'Ü' => 'ü', @@ -80,44 +76,16 @@ public function testInitiationWithEncoding(): void foreach ($valuesExpected as $input => $output) { self::assertSame($output, $filter($input)); } - } - - /** - * @Laminas-9058 - */ - public function testCaseInsensitiveEncoding(): void - { - $filter = $this->filter; - $valuesExpected = [ - 'Ü' => 'ü', - 'Ñ' => 'ñ', - 'ÜÑ123' => 'üñ123', - ]; - try { - $filter->setEncoding('UTF-8'); - foreach ($valuesExpected as $input => $output) { - self::assertSame($output, $filter($input)); - } - - $this->filter->setEncoding('utf-8'); - foreach ($valuesExpected as $input => $output) { - self::assertSame($output, $filter($input)); - } - - $this->filter->setEncoding('UtF-8'); - foreach ($valuesExpected as $input => $output) { - self::assertSame($output, $filter($input)); - } - } catch (Exception\ExtensionNotLoadedException $e) { - self::assertContains('mbstring is required', $e->getMessage()); + $filter = new StringToLowerFilter(['encoding' => 'utf-8']); + foreach ($valuesExpected as $input => $output) { + self::assertSame($output, $filter($input)); } - } - #[Group('Laminas-9854')] - public function testDetectMbInternalEncoding(): void - { - self::assertSame(mb_internal_encoding(), $this->filter->getEncoding()); + $filter = new StringToLowerFilter(['encoding' => 'UtF-8']); + foreach ($valuesExpected as $input => $output) { + self::assertSame($output, $filter($input)); + } } /** @return list */ diff --git a/test/StringToUpperTest.php b/test/StringToUpperTest.php index 6bc9e01d..9a079ba8 100644 --- a/test/StringToUpperTest.php +++ b/test/StringToUpperTest.php @@ -7,12 +7,9 @@ use Laminas\Filter\Exception; use Laminas\Filter\StringToUpper as StringToUpperFilter; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; use stdClass; -use function mb_internal_encoding; - class StringToUpperTest extends TestCase { private StringToUpperFilter $filter; @@ -45,14 +42,13 @@ public function testBasic(): void */ public function testWithEncoding(): void { - $filter = $this->filter; + $filter = new StringToUpperFilter(['encoding' => 'utf-8']); $valuesExpected = [ 'ü' => 'Ü', 'ñ' => 'Ñ', 'üñ123' => 'ÜÑ123', ]; - $filter->setEncoding('UTF-8'); foreach ($valuesExpected as $input => $output) { self::assertSame($output, $filter($input)); } @@ -62,24 +58,7 @@ public function testFalseEncoding(): void { $this->expectException(Exception\InvalidArgumentException::class); $this->expectExceptionMessage('is not supported'); - $this->filter->setEncoding('aaaaa'); - } - - /** - * @Laminas-8989 - */ - public function testInitiationWithEncoding(): void - { - $valuesExpected = [ - 'ü' => 'Ü', - 'ñ' => 'Ñ', - 'üñ123' => 'ÜÑ123', - ]; - - $filter = new StringToUpperFilter(['encoding' => 'UTF-8']); - foreach ($valuesExpected as $input => $output) { - self::assertSame($output, $filter($input)); - } + new StringToUpperFilter(['encoding' => 'aaaaa']); } /** @@ -87,35 +66,28 @@ public function testInitiationWithEncoding(): void */ public function testCaseInsensitiveEncoding(): void { - $filter = $this->filter; $valuesExpected = [ 'ü' => 'Ü', 'ñ' => 'Ñ', 'üñ123' => 'ÜÑ123', ]; - $filter->setEncoding('UTF-8'); + $filter = new StringToUpperFilter(['encoding' => 'UTF-8']); foreach ($valuesExpected as $input => $output) { self::assertSame($output, $filter($input)); } - $this->filter->setEncoding('utf-8'); + $filter = new StringToUpperFilter(['encoding' => 'utf-8']); foreach ($valuesExpected as $input => $output) { self::assertSame($output, $filter($input)); } - $this->filter->setEncoding('UtF-8'); + new StringToUpperFilter(['encoding' => 'UtF-8']); foreach ($valuesExpected as $input => $output) { self::assertSame($output, $filter($input)); } } - #[Group('Laminas-9854')] - public function testDetectMbInternalEncoding(): void - { - self::assertSame(mb_internal_encoding(), $this->filter->getEncoding()); - } - /** @return list */ public static function returnUnfilteredDataProvider(): array { diff --git a/test/UpperCaseWordsTest.php b/test/UpperCaseWordsTest.php index 16111cd5..bc1812c8 100644 --- a/test/UpperCaseWordsTest.php +++ b/test/UpperCaseWordsTest.php @@ -4,15 +4,12 @@ namespace LaminasTest\Filter; -use Laminas\Filter\Exception; +use Laminas\Filter\Exception\InvalidArgumentException; use Laminas\Filter\UpperCaseWords as UpperCaseWordsFilter; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; use stdClass; -use function mb_internal_encoding; - class UpperCaseWordsTest extends TestCase { private UpperCaseWordsFilter $filter; @@ -45,14 +42,13 @@ public function testBasic(): void */ public function testWithEncoding(): void { - $filter = $this->filter; $valuesExpected = [ '√º' => '√º', '√±' => '√±', '√º√±123' => '√º√±123', ]; - $filter->setEncoding('UTF-8'); + $filter = new UpperCaseWordsFilter(['encoding' => 'utf-8']); foreach ($valuesExpected as $input => $output) { self::assertSame($output, $filter($input)); } @@ -60,28 +56,9 @@ public function testWithEncoding(): void public function testFalseEncoding(): void { - $this->expectException(Exception\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is not supported'); - $this->filter->setEncoding('aaaaa'); - } - - /** - * @Laminas-8989 - */ - public function testInitiationWithEncoding(): void - { - $valuesExpected = [ - '√º' => '√º', - '√±' => '√±', - '√º√±123' => '√º√±123', - ]; - - $filter = new UpperCaseWordsFilter([ - 'encoding' => 'UTF-8', - ]); - foreach ($valuesExpected as $input => $output) { - self::assertSame($output, $filter($input)); - } + new UpperCaseWordsFilter(['encoding' => 'aaaaa']); } /** @@ -89,35 +66,28 @@ public function testInitiationWithEncoding(): void */ public function testCaseInsensitiveEncoding(): void { - $filter = $this->filter; $valuesExpected = [ '√º' => '√º', '√±' => '√±', '√º√±123' => '√º√±123', ]; - $filter->setEncoding('UTF-8'); + $filter = new UpperCaseWordsFilter(['encoding' => 'UTF-8']); foreach ($valuesExpected as $input => $output) { self::assertSame($output, $filter($input)); } - $this->filter->setEncoding('utf-8'); + $filter = new UpperCaseWordsFilter(['encoding' => 'utf-8']); foreach ($valuesExpected as $input => $output) { self::assertSame($output, $filter($input)); } - $this->filter->setEncoding('UtF-8'); + $filter = new UpperCaseWordsFilter(['encoding' => 'UtF-8']); foreach ($valuesExpected as $input => $output) { self::assertSame($output, $filter($input)); } } - #[Group('Laminas-9854')] - public function testDetectMbInternalEncoding(): void - { - self::assertSame(mb_internal_encoding(), $this->filter->getEncoding()); - } - /** @return list */ public static function returnUnfilteredDataProvider(): array {