Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated usage of function #310

Merged
merged 9 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
phpunit.xml.dist export-ignore
tests export-ignore
docs export-ignore

.phpunit.result.cache
5 changes: 2 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
Expand Down
34 changes: 13 additions & 21 deletions .idea/ElementFinder.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}
],
"require": {
"php": "^8.1",
"php": "^8.2",
"ext-dom": "*",
"ext-libxml": "*",
"symfony/css-selector": "^7.1"
Expand All @@ -35,7 +35,7 @@
},
"autoload-dev": {
"psr-4": {
"Test\\Xparse\\ElementFinder\\": "tests"
"Test\\Xparse\\ElementFinder\\": "./tests"
}
},
"config": {
Expand Down
7 changes: 6 additions & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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]);
Expand Down
16 changes: 11 additions & 5 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
>
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<clover outputFile=".tmp/clover.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
<directory>./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
14 changes: 13 additions & 1 deletion src/ElementFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +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');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use htmlentities
image
Have you tried it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using only htmlentities, some special characters will remain encoded, as shown in the screenshot above. To decode them, you need to use htmlspecialchars_decode, and also use mb_encode_numericentity to properly handle characters in UTF-8 encoding.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you give an example when special symbols are not encoded? I want to check this strings on both converters current and a new one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that if you use only htmlentities, then the characters "'&<> will remain encoded and you will need to additionally use html_entity_decode to convert them.

In addition, if you use only htmlentities and html_entity_decode, then in the tests I added you will see that in the second case, when "broken" characters are used, htmlentities successfully removes them, but in the first test case, instead of "Текст текст" it will output ТекÑ... and so on.
image

//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 | ENT_IGNORE, 'UTF-8', false),
ENT_NOQUOTES
),
[0x80, 0x10FFFF, 0, ~0],
'UTF-8'
);

$this->dom->loadHTML($data, LIBXML_NOCDATA & LIBXML_NOERROR);
} elseif (static::DOCUMENT_XML === $this->type) {
$this->dom->loadXML($data, LIBXML_NOCDATA & LIBXML_NOERROR);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

namespace Tests\Xparse\ElementFinder\Collection\Filters\StringFilter;
declare(strict_types=1);

namespace Test\Xparse\ElementFinder\Collection\Filters\StringFilter;

use PHPUnit\Framework\TestCase;
use Xparse\ElementFinder\Collection\Filters\StringFilter\RegexStringFilter;

class RegexStringFilterTest extends TestCase
final class RegexStringFilterTest extends TestCase
{
public function testRegexSuccess(): void
{
Expand Down
6 changes: 4 additions & 2 deletions tests/Collection/Modify/StringModify/RegexReplaceTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

namespace Tests\Xparse\ElementFinder\Collection\Modify\StringModify;
declare(strict_types=1);

namespace Test\Xparse\ElementFinder\Collection\Modify\StringModify;

use PHPUnit\Framework\TestCase;
use Xparse\ElementFinder\Collection\Modify\StringModify\RegexReplace;
use Xparse\ElementFinder\Collection\StringCollection;

class RegexReplaceTest extends TestCase
final class RegexReplaceTest extends TestCase
{
public function testReplace(): void
{
Expand Down
20 changes: 11 additions & 9 deletions tests/Collection/StringCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Test\Xparse\ElementFinder\Collection;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Test\Xparse\ElementFinder\Collection\Dummy\JoinedBy;
use Test\Xparse\ElementFinder\Collection\Dummy\WithLetterFilter;
Expand All @@ -12,7 +13,7 @@
/**
* @author Ivan Shcherbak <[email protected]>
*/
class StringCollectionTest extends TestCase
final class StringCollectionTest extends TestCase
{
public function testInvalidObjectIndex(): void
{
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -128,7 +131,8 @@ public function testFilter(): void

self::assertSame(
[
'bar', 'baz',
'bar',
'baz',
],
$collection->all()
);
Expand 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);
Expand Down
9 changes: 4 additions & 5 deletions tests/CssExpressionTranslator/CssExpressionTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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'],
Expand All @@ -26,9 +27,7 @@ final public function getConvertWithAttributesDataProvider(): array
];
}

/**
* @dataProvider getConvertWithAttributesDataProvider
*/
#[DataProvider('getConvertWithAttributesDataProvider')]
final public function testConvertWithAttributes(string $input, string $expect): void
{
self::assertSame(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,7 +17,7 @@ final class CssOrXpathExpressionTranslatorTest extends TestCase
/**
* @return string[][]
*/
public function getQueriesDataProvider(): array
public static function getQueriesDataProvider(): array
{
return [
[
Expand Down Expand Up @@ -90,9 +91,7 @@ public function getQueriesDataProvider(): array
];
}

/**
* @dataProvider getQueriesDataProvider
*/
#[DataProvider('getQueriesDataProvider')]
public function testQueries(string $input, string $expect): void
{
$output = (new CssOrXpathExpressionTranslator())
Expand Down
2 changes: 2 additions & 0 deletions tests/Dummy/ItemsByClassExpressionTranslator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Test\Xparse\ElementFinder\Dummy;

use Xparse\ElementFinder\ExpressionTranslator\ExpressionTranslatorInterface;
Expand Down
27 changes: 27 additions & 0 deletions tests/ElementFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -519,4 +520,30 @@ private function getValidXml(): string
</breakfast_menu>
';
}

/**
* @return string[][]
*/
public static function getDifferentEncodingsSupportDataProvider(): array
{
return [
[
'<body>Текст текст text</body>',
'Текст текст text',
],
[
'<body>&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; text</body>',
' 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);
}
}
2 changes: 1 addition & 1 deletion tests/Helper/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Xparse\ElementFinder\Helper;
namespace Test\Xparse\ElementFinder\Helper;

use Exception;
use PHPUnit\Framework\TestCase;
Expand Down
2 changes: 1 addition & 1 deletion tests/Helper/NodeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Xparse\ElementFinder\Helper;
namespace Test\Xparse\ElementFinder\Helper;

use DOMDocument;
use PHPUnit\Framework\TestCase;
Expand Down
Loading
Loading