From e0c816c491881ec281aa8822f0778d51e5d540c3 Mon Sep 17 00:00:00 2001 From: Serhii Maistrenko Date: Tue, 10 Dec 2024 17:05:54 +0200 Subject: [PATCH 1/8] remove deprecated usage of function --- composer.json | 1 + src/ElementFinder.php | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index b29bd23..31b5dda 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "require": { "php": "^8.1", "ext-dom": "*", + "ext-iconv": "*", "ext-libxml": "*", "symfony/css-selector": "^7.1" }, diff --git a/src/ElementFinder.php b/src/ElementFinder.php index 80c022e..8859fbd 100644 --- a/src/ElementFinder.php +++ b/src/ElementFinder.php @@ -214,8 +214,19 @@ private function setData(string $data): self if (static::DOCUMENT_HTML === $this->type) { $data = StringHelper::safeEncodeStr($data); - $data = mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8'); - $this->dom->loadHTML($data, LIBXML_NOCDATA & LIBXML_NOERROR); + + $encodedData = mb_encode_numericentity( + htmlspecialchars_decode( + htmlentities($data, ENT_NOQUOTES, 'UTF-8', false) + , ENT_NOQUOTES + ), [0x80, 0x10FFFF, 0, ~0], + 'UTF-8' + ); + if (!$encodedData) { + $encodedData = iconv('UTF-8', 'UTF-8//IGNORE', $data); + } + + $this->dom->loadHTML($encodedData, LIBXML_NOCDATA & LIBXML_NOERROR); } elseif (static::DOCUMENT_XML === $this->type) { $this->dom->loadXML($data, LIBXML_NOCDATA & LIBXML_NOERROR); } else { From 9452823d66ff5b3a3c724d0130dedfa1579d1f54 Mon Sep 17 00:00:00 2001 From: Serhii Maistrenko Date: Fri, 13 Dec 2024 15:04:41 +0200 Subject: [PATCH 2/8] remove iconv extension, add comments and tests --- composer.json | 1 - src/ElementFinder.php | 12 ++++++------ tests/ElementFinderTest.php | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 31b5dda..b29bd23 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,6 @@ "require": { "php": "^8.1", "ext-dom": "*", - "ext-iconv": "*", "ext-libxml": "*", "symfony/css-selector": "^7.1" }, diff --git a/src/ElementFinder.php b/src/ElementFinder.php index 8859fbd..4c0b135 100644 --- a/src/ElementFinder.php +++ b/src/ElementFinder.php @@ -215,18 +215,18 @@ private function setData(string $data): self if (static::DOCUMENT_HTML === $this->type) { $data = StringHelper::safeEncodeStr($data); - $encodedData = mb_encode_numericentity( + //Analogue of mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8') + //Usage of mb_convert_encoding with encoding to HTML_ENTITIES is deprecated since php version 8.2 + //When passing data to ElementFinder in an encoding other than UTF-8, any unrecognized characters will be ignored + $data = mb_encode_numericentity( htmlspecialchars_decode( - htmlentities($data, ENT_NOQUOTES, 'UTF-8', false) + htmlentities($data, ENT_NOQUOTES | ENT_IGNORE, 'UTF-8', false) , ENT_NOQUOTES ), [0x80, 0x10FFFF, 0, ~0], 'UTF-8' ); - if (!$encodedData) { - $encodedData = iconv('UTF-8', 'UTF-8//IGNORE', $data); - } - $this->dom->loadHTML($encodedData, LIBXML_NOCDATA & LIBXML_NOERROR); + $this->dom->loadHTML($data, LIBXML_NOCDATA & LIBXML_NOERROR); } elseif (static::DOCUMENT_XML === $this->type) { $this->dom->loadXML($data, LIBXML_NOCDATA & LIBXML_NOERROR); } else { diff --git a/tests/ElementFinderTest.php b/tests/ElementFinderTest.php index 44b4b93..7f3a1d6 100644 --- a/tests/ElementFinderTest.php +++ b/tests/ElementFinderTest.php @@ -5,6 +5,7 @@ namespace Test\Xparse\ElementFinder; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use RuntimeException; use Test\Xparse\ElementFinder\Dummy\ItemsByClassExpressionTranslator; @@ -519,4 +520,36 @@ private function getValidXml(): string '; } + + + /** + * @return string[][] + */ + public static function getDifferentEncodingsSupportDataProvider(): array + { + return [ + [ + 'Текст текст text', + 'Текст текст text', + ], + [ + (string)mb_convert_encoding('Текст текст text', 'WINDOWS-1251', 'UTF-8'), + ' text', + ], + [ + (string)mb_convert_encoding('Текст текст text', 'ISO-8859-5', 'UTF-8'), + ' text', + ], + ]; + } + + + #[DataProvider('getDifferentEncodingsSupportDataProvider')] + public function testDifferentEncodingsSupport(string $html, string $bodyText): void + { + $page = new ElementFinder($html); + $pageBodyText = $page->content('//body')->first(); + self::assertInstanceOf(ElementFinder::class, $page); + self::assertEquals($bodyText, $pageBodyText); + } } From d7342eff442fae1f80b47149f2e96b85fa2bbf8c Mon Sep 17 00:00:00 2001 From: Ivan Shcherbak Date: Sun, 15 Dec 2024 12:36:38 +0200 Subject: [PATCH 3/8] Remove php 8.1 (#308) * Fix tests * Drop composer dev flag * Support css-selector 6 * Drop php 8.1 * Update changelog --- .gitattributes | 2 +- .github/workflows/test.yaml | 5 ++- .idea/ElementFinder.iml | 34 +++++++------------ .idea/php.xml | 11 ++++++ CHANGELOG.md | 2 ++ composer.json | 4 +-- ecs.php | 7 +++- phpunit.xml | 16 ++++++--- .../StringFilter/RegexStringFilterTest.php | 6 ++-- .../Modify/StringModify/RegexReplaceTest.php | 6 ++-- tests/Collection/StringCollectionTest.php | 20 ++++++----- .../CssExpressionTranslatorTest.php | 9 +++-- .../CssOrXpathExpressionTranslatorTest.php | 7 ++-- .../ItemsByClassExpressionTranslator.php | 2 ++ tests/Helper/FormHelperTest.php | 2 +- tests/Helper/NodeHelperTest.php | 2 +- tests/Helper/StringHelperTest.php | 4 +-- 17 files changed, 80 insertions(+), 59 deletions(-) diff --git a/.gitattributes b/.gitattributes index 62bc288..1b422fb 100644 --- a/.gitattributes +++ b/.gitattributes @@ -9,4 +9,4 @@ phpunit.xml.dist export-ignore tests export-ignore docs export-ignore - +.phpunit.result.cache diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index f77afc4..3d951c7 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -16,9 +16,8 @@ jobs: strategy: matrix: include: - - php-version: '8.1' - main: true - php-version: '8.2' + main: true - php-version: '8.3' - php-version: '8.4' nightly: true @@ -44,7 +43,7 @@ jobs: restore-keys: ${{ runner.os }}-composer- - name: Install dependencies - run: composer install --prefer-dist --dev + run: composer install --prefer-dist - name: Run tests continue-on-error: ${{ matrix.nightly }} diff --git a/.idea/ElementFinder.iml b/.idea/ElementFinder.iml index e0fcf0b..c7bb0ea 100644 --- a/.idea/ElementFinder.iml +++ b/.idea/ElementFinder.iml @@ -3,28 +3,20 @@ + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + diff --git a/.idea/php.xml b/.idea/php.xml index 5bbce04..be54721 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -75,6 +75,17 @@ + + + + + + + + + + + diff --git a/CHANGELOG.md b/CHANGELOG.md index 6159742..22c0122 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog All Notable changes to `ElementFinder` will be documented in this file +## 3.0 [Unreleased] +- Move to php 8.2 ## 2.0.0 [2023-01-11] - Move to php 8.1 diff --git a/composer.json b/composer.json index b29bd23..87ebca7 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ } ], "require": { - "php": "^8.1", + "php": "^8.2", "ext-dom": "*", "ext-libxml": "*", "symfony/css-selector": "^7.1" @@ -35,7 +35,7 @@ }, "autoload-dev": { "psr-4": { - "Test\\Xparse\\ElementFinder\\": "tests" + "Test\\Xparse\\ElementFinder\\": "./tests" } }, "config": { diff --git a/ecs.php b/ecs.php index b3c703e..3b67e58 100644 --- a/ecs.php +++ b/ecs.php @@ -4,6 +4,7 @@ use PhpCsFixer\Fixer\FunctionNotation\VoidReturnFixer; use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer; +use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer; use Symplify\EasyCodingStandard\Config\ECSConfig; use Symplify\EasyCodingStandard\ValueObject\Set\SetList; @@ -14,7 +15,11 @@ __FILE__, ]); - $ecsConfig->rules([NoUnusedImportsFixer::class, VoidReturnFixer::class]); + $ecsConfig->rules([ + NoUnusedImportsFixer::class, + VoidReturnFixer::class, + DeclareStrictTypesFixer::class, + ]); // this way you can add sets - group of rules $ecsConfig->sets([SetList::SPACES, SetList::ARRAY, SetList::DOCBLOCK, SetList::NAMESPACES, SetList::COMMENTS, SetList::PSR_12]); diff --git a/phpunit.xml b/phpunit.xml index 00edbe1..0eef20b 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -2,19 +2,25 @@ - - src/ - - tests + ./tests + + + ./src + + diff --git a/tests/Collection/Filters/StringFilter/RegexStringFilterTest.php b/tests/Collection/Filters/StringFilter/RegexStringFilterTest.php index d3e100c..c28cc54 100644 --- a/tests/Collection/Filters/StringFilter/RegexStringFilterTest.php +++ b/tests/Collection/Filters/StringFilter/RegexStringFilterTest.php @@ -1,11 +1,13 @@ */ -class StringCollectionTest extends TestCase +final class StringCollectionTest extends TestCase { public function testInvalidObjectIndex(): void { @@ -87,10 +88,12 @@ public function testMergeWithPartialItems(): void { $collection = (new StringCollection([ 1 => 'a', - ]))->merge(new StringCollection([ - 1 => 'b', - 'c', - ])); + ]))->merge( + new StringCollection([ + 1 => 'b', + 'c', + ]) + ); self::assertSame(['a', 'b', 'c'], $collection->all()); } @@ -128,7 +131,8 @@ public function testFilter(): void self::assertSame( [ - 'bar', 'baz', + 'bar', + 'baz', ], $collection->all() ); @@ -148,9 +152,7 @@ public function testMap(): void ); } - /** - * @dataProvider lastDataProvider - */ + #[DataProvider('lastDataProvider')] public function testLast(array $items, mixed $expected): void { $collection = new StringCollection($items); diff --git a/tests/CssExpressionTranslator/CssExpressionTranslatorTest.php b/tests/CssExpressionTranslator/CssExpressionTranslatorTest.php index 32f651a..6204fc8 100644 --- a/tests/CssExpressionTranslator/CssExpressionTranslatorTest.php +++ b/tests/CssExpressionTranslator/CssExpressionTranslatorTest.php @@ -2,8 +2,9 @@ declare(strict_types=1); -namespace Xparse\ElementFinder\CssExpressionTranslator\Test; +namespace Test\Xparse\ElementFinder\CssExpressionTranslator; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Xparse\ElementFinder\CssExpressionTranslator\CssExpressionTranslator; @@ -15,7 +16,7 @@ class CssExpressionTranslatorTest extends TestCase /** * @return string[][] */ - final public function getConvertWithAttributesDataProvider(): array + final public static function getConvertWithAttributesDataProvider(): array { return [ ['a', 'descendant-or-self::a'], @@ -26,9 +27,7 @@ final public function getConvertWithAttributesDataProvider(): array ]; } - /** - * @dataProvider getConvertWithAttributesDataProvider - */ + #[DataProvider('getConvertWithAttributesDataProvider')] final public function testConvertWithAttributes(string $input, string $expect): void { self::assertSame( diff --git a/tests/CssExpressionTranslator/CssOrXpathExpressionTranslatorTest.php b/tests/CssExpressionTranslator/CssOrXpathExpressionTranslatorTest.php index 7b82ab2..87619f3 100644 --- a/tests/CssExpressionTranslator/CssOrXpathExpressionTranslatorTest.php +++ b/tests/CssExpressionTranslator/CssOrXpathExpressionTranslatorTest.php @@ -5,6 +5,7 @@ namespace Test\Xparse\ElementFinder\CssExpressionTranslator; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Xparse\ElementFinder\CssExpressionTranslator\CssOrXpathExpressionTranslator; @@ -16,7 +17,7 @@ final class CssOrXpathExpressionTranslatorTest extends TestCase /** * @return string[][] */ - public function getQueriesDataProvider(): array + public static function getQueriesDataProvider(): array { return [ [ @@ -90,9 +91,7 @@ public function getQueriesDataProvider(): array ]; } - /** - * @dataProvider getQueriesDataProvider - */ + #[DataProvider('getQueriesDataProvider')] public function testQueries(string $input, string $expect): void { $output = (new CssOrXpathExpressionTranslator()) diff --git a/tests/Dummy/ItemsByClassExpressionTranslator.php b/tests/Dummy/ItemsByClassExpressionTranslator.php index e29230d..d94a449 100644 --- a/tests/Dummy/ItemsByClassExpressionTranslator.php +++ b/tests/Dummy/ItemsByClassExpressionTranslator.php @@ -1,5 +1,7 @@ */ -class StringHelperTest extends TestCase +final class StringHelperTest extends TestCase { public function testEncode(): void { From 672a1f9062f9675b759f682b77bff32b0d057a3f Mon Sep 17 00:00:00 2001 From: Serhii Maistrenko Date: Mon, 16 Dec 2024 15:50:39 +0200 Subject: [PATCH 4/8] refactor tests --- tests/ElementFinderTest.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/ElementFinderTest.php b/tests/ElementFinderTest.php index 7f3a1d6..0ef5a02 100644 --- a/tests/ElementFinderTest.php +++ b/tests/ElementFinderTest.php @@ -533,11 +533,7 @@ public static function getDifferentEncodingsSupportDataProvider(): array 'Текст текст text', ], [ - (string)mb_convert_encoding('Текст текст text', 'WINDOWS-1251', 'UTF-8'), - ' text', - ], - [ - (string)mb_convert_encoding('Текст текст text', 'ISO-8859-5', 'UTF-8'), + '����� ����� text', ' text', ], ]; From bdb6568bae0d142e951189bcca925859f44e4b3f Mon Sep 17 00:00:00 2001 From: Serhii Maistrenko Date: Tue, 10 Dec 2024 17:05:54 +0200 Subject: [PATCH 5/8] remove deprecated usage of function --- composer.json | 1 + src/ElementFinder.php | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 87ebca7..a0e5e90 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "require": { "php": "^8.2", "ext-dom": "*", + "ext-iconv": "*", "ext-libxml": "*", "symfony/css-selector": "^7.1" }, diff --git a/src/ElementFinder.php b/src/ElementFinder.php index 80c022e..8859fbd 100644 --- a/src/ElementFinder.php +++ b/src/ElementFinder.php @@ -214,8 +214,19 @@ private function setData(string $data): self if (static::DOCUMENT_HTML === $this->type) { $data = StringHelper::safeEncodeStr($data); - $data = mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8'); - $this->dom->loadHTML($data, LIBXML_NOCDATA & LIBXML_NOERROR); + + $encodedData = mb_encode_numericentity( + htmlspecialchars_decode( + htmlentities($data, ENT_NOQUOTES, 'UTF-8', false) + , ENT_NOQUOTES + ), [0x80, 0x10FFFF, 0, ~0], + 'UTF-8' + ); + if (!$encodedData) { + $encodedData = iconv('UTF-8', 'UTF-8//IGNORE', $data); + } + + $this->dom->loadHTML($encodedData, LIBXML_NOCDATA & LIBXML_NOERROR); } elseif (static::DOCUMENT_XML === $this->type) { $this->dom->loadXML($data, LIBXML_NOCDATA & LIBXML_NOERROR); } else { From 17556e05bfadbffa25c2a54c3174af5ce730240f Mon Sep 17 00:00:00 2001 From: Serhii Maistrenko Date: Fri, 13 Dec 2024 15:04:41 +0200 Subject: [PATCH 6/8] remove iconv extension, add comments and tests --- composer.json | 1 - src/ElementFinder.php | 12 ++++++------ tests/ElementFinderTest.php | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index a0e5e90..87ebca7 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,6 @@ "require": { "php": "^8.2", "ext-dom": "*", - "ext-iconv": "*", "ext-libxml": "*", "symfony/css-selector": "^7.1" }, diff --git a/src/ElementFinder.php b/src/ElementFinder.php index 8859fbd..4c0b135 100644 --- a/src/ElementFinder.php +++ b/src/ElementFinder.php @@ -215,18 +215,18 @@ private function setData(string $data): self if (static::DOCUMENT_HTML === $this->type) { $data = StringHelper::safeEncodeStr($data); - $encodedData = mb_encode_numericentity( + //Analogue of mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8') + //Usage of mb_convert_encoding with encoding to HTML_ENTITIES is deprecated since php version 8.2 + //When passing data to ElementFinder in an encoding other than UTF-8, any unrecognized characters will be ignored + $data = mb_encode_numericentity( htmlspecialchars_decode( - htmlentities($data, ENT_NOQUOTES, 'UTF-8', false) + htmlentities($data, ENT_NOQUOTES | ENT_IGNORE, 'UTF-8', false) , ENT_NOQUOTES ), [0x80, 0x10FFFF, 0, ~0], 'UTF-8' ); - if (!$encodedData) { - $encodedData = iconv('UTF-8', 'UTF-8//IGNORE', $data); - } - $this->dom->loadHTML($encodedData, LIBXML_NOCDATA & LIBXML_NOERROR); + $this->dom->loadHTML($data, LIBXML_NOCDATA & LIBXML_NOERROR); } elseif (static::DOCUMENT_XML === $this->type) { $this->dom->loadXML($data, LIBXML_NOCDATA & LIBXML_NOERROR); } else { diff --git a/tests/ElementFinderTest.php b/tests/ElementFinderTest.php index 44b4b93..7f3a1d6 100644 --- a/tests/ElementFinderTest.php +++ b/tests/ElementFinderTest.php @@ -5,6 +5,7 @@ namespace Test\Xparse\ElementFinder; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use RuntimeException; use Test\Xparse\ElementFinder\Dummy\ItemsByClassExpressionTranslator; @@ -519,4 +520,36 @@ private function getValidXml(): string '; } + + + /** + * @return string[][] + */ + public static function getDifferentEncodingsSupportDataProvider(): array + { + return [ + [ + 'Текст текст text', + 'Текст текст text', + ], + [ + (string)mb_convert_encoding('Текст текст text', 'WINDOWS-1251', 'UTF-8'), + ' text', + ], + [ + (string)mb_convert_encoding('Текст текст text', 'ISO-8859-5', 'UTF-8'), + ' text', + ], + ]; + } + + + #[DataProvider('getDifferentEncodingsSupportDataProvider')] + public function testDifferentEncodingsSupport(string $html, string $bodyText): void + { + $page = new ElementFinder($html); + $pageBodyText = $page->content('//body')->first(); + self::assertInstanceOf(ElementFinder::class, $page); + self::assertEquals($bodyText, $pageBodyText); + } } From 4e740356e1a28c2014c91e464227fab8c6bada97 Mon Sep 17 00:00:00 2001 From: Serhii Maistrenko Date: Mon, 16 Dec 2024 15:50:39 +0200 Subject: [PATCH 7/8] refactor tests --- tests/ElementFinderTest.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/ElementFinderTest.php b/tests/ElementFinderTest.php index 7f3a1d6..0ef5a02 100644 --- a/tests/ElementFinderTest.php +++ b/tests/ElementFinderTest.php @@ -533,11 +533,7 @@ public static function getDifferentEncodingsSupportDataProvider(): array 'Текст текст text', ], [ - (string)mb_convert_encoding('Текст текст text', 'WINDOWS-1251', 'UTF-8'), - ' text', - ], - [ - (string)mb_convert_encoding('Текст текст text', 'ISO-8859-5', 'UTF-8'), + '����� ����� text', ' text', ], ]; From ff734095dc562605bfa98185d77bdc082108e4eb Mon Sep 17 00:00:00 2001 From: Serhii Maistrenko Date: Wed, 18 Dec 2024 15:55:00 +0200 Subject: [PATCH 8/8] fix code style --- src/ElementFinder.php | 7 ++++--- tests/ElementFinderTest.php | 2 -- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/ElementFinder.php b/src/ElementFinder.php index 4c0b135..41f91c7 100644 --- a/src/ElementFinder.php +++ b/src/ElementFinder.php @@ -220,9 +220,10 @@ private function setData(string $data): self //When passing data to ElementFinder in an encoding other than UTF-8, any unrecognized characters will be ignored $data = mb_encode_numericentity( htmlspecialchars_decode( - htmlentities($data, ENT_NOQUOTES | ENT_IGNORE, 'UTF-8', false) - , ENT_NOQUOTES - ), [0x80, 0x10FFFF, 0, ~0], + htmlentities($data, ENT_NOQUOTES | ENT_IGNORE, 'UTF-8', false), + ENT_NOQUOTES + ), + [0x80, 0x10FFFF, 0, ~0], 'UTF-8' ); diff --git a/tests/ElementFinderTest.php b/tests/ElementFinderTest.php index 0ef5a02..38f3f19 100644 --- a/tests/ElementFinderTest.php +++ b/tests/ElementFinderTest.php @@ -521,7 +521,6 @@ private function getValidXml(): string '; } - /** * @return string[][] */ @@ -539,7 +538,6 @@ public static function getDifferentEncodingsSupportDataProvider(): array ]; } - #[DataProvider('getDifferentEncodingsSupportDataProvider')] public function testDifferentEncodingsSupport(string $html, string $bodyText): void {