Skip to content

Commit

Permalink
Update PEST
Browse files Browse the repository at this point in the history
Therefor also fix set_error_handler() / restore_error_handler()
occurences, because the old version lead to phpunit warnings with the
new version. And also add a rule to phpstan config.
  • Loading branch information
otsch committed Oct 15, 2024
1 parent 81aac99 commit bad52eb
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 20 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.0.0] - 2024-10-11
## [2.0.0] - 2024-10-15
### Changed
* __BREAKING__: Removed methods `BaseStep::addToResult()`, `BaseStep::addLaterToResult()`, `BaseStep::addsToOrCreatesResult()`, `BaseStep::createsResult()`, and `BaseStep::keepInputData()`. These methods were deprecated in v1.8.0 and should be replaced with `Step::keep()`, `Step::keepAs()`, `Step::keepFromInput()`, and `Step::keepInputAs()`.
* __BREAKING__: Added the following keep methods to the `StepInterface`: `StepInterface::keep()`, `StepInterface::keepAs()`, `StepInterface::keepFromInput()`, `StepInterface::keepInputAs()`, as well as `StepInterface::keepsAnything()`, `StepInterface::keepsAnythingFromInputData()` and `StepInterface::keepsAnythingFromOutputData()`. If you have a class that implements this interface without extending `Step` (or `BaseStep`), you will need to implement these methods yourself. However, it is strongly recommended to extend `Step` instead.
Expand All @@ -24,7 +24,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* __BREAKING__: Removed method `RespondedRequest::cacheKeyFromRequest()`. Use `RequestKey::from()` instead.
* __BREAKING__: The `HttpLoader::retryCachedErrorResponses()` method now returns an instance of the new `Crwlr\Crawler\Loader\Http\Cache\RetryManager` class. This class provides the methods `only()` and `except()` to restrict retries to specific HTTP response status codes. Previously, this method returned the `HttpLoader` itself (`$this`), so if you're using it in a chain and calling other loader methods after it, you will need to refactor your code.
* __BREAKING__: Removed the `Microseconds` class from this package. It has been moved to the `crwlr/utils` package, which you can use instead.
* __BREAKING__: When a

### Added
* New methods `FileCache::prolong()` and `FileCache::prolongAll()` to allow prolonging the time to live for cached responses.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"crwlr/html-2-text": "^0.1.0"
},
"require-dev": {
"pestphp/pest": "^2.3",
"pestphp/pest": "^3.0",
"mockery/mockery": "^1.5",
"phpstan/phpstan": "^1.4",
"phpstan/phpstan-mockery": "^1.0",
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ parameters:
- "#^Call to an undefined method Pest\\\\PendingCalls\\\\TestCall\\|Pest\\\\Support\\\\HigherOrderTapProxy\\:\\:(with|throws)\\(\\).$#"
- "#^Access to an undefined property Spatie\\\\Invade\\\\Invader#"
- "#^Call to an undefined method Spatie\\\\Invade\\\\Invader#"
- "#^Call to protected method [a-zA-Z]{5,30}\\(\\) of class PHPUnit\\\\Framework\\\\TestCase.#"
4 changes: 2 additions & 2 deletions src/Cache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ protected function saveCacheItem(CacheItem $item): bool
protected function unserialize(string $content): mixed
{
// Temporarily set a new error handler, so unserializing a compressed string does not result in a PHP warning.
$previousHandler = set_error_handler(function ($errno, $errstr) {
set_error_handler(function ($errno, $errstr) {
return $errno === E_WARNING && str_starts_with($errstr, 'unserialize(): Error at offset 0 of ');
});

Expand All @@ -228,7 +228,7 @@ protected function unserialize(string $content): mixed
}
}

set_error_handler($previousHandler);
restore_error_handler();

return $unserialized;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/Http/HttpLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function load(mixed $subject): ?RespondedRequest

return null;
} finally {
$this->callHook('afterLoad', $request);
//$this->callHook('afterLoad', $request);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Steps/Html/XPathQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ public function filter(Crawler $domCrawler): Crawler
private function validateQuery(string $query): void
{
// Temporarily set a new error handler, so checking an invalid XPath query does not generate a PHP warning.
$previousHandler = set_error_handler(function ($errno, $errstr) {
set_error_handler(function ($errno, $errstr) {
if ($errno === E_WARNING && $errstr === 'DOMXPath::evaluate(): Invalid expression') {
return true;
}

return false;
});

if ((new DOMXPath(new DOMDocument()))->evaluate($query) === false) {
set_error_handler($previousHandler);
$evaluation = (new DOMXPath(new DOMDocument()))->evaluate($query);

restore_error_handler();

if ($evaluation === false) {
throw InvalidDomQueryException::make('Invalid XPath query', $query);
} else {
set_error_handler($previousHandler);
}
}
}
8 changes: 3 additions & 5 deletions tests/Loader/Http/HttpLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,11 +826,9 @@ function (string $loadingMethod) {

/** @var RespondedRequestChild $response */

expect($response)
->toBeInstanceOf(RespondedRequestChild::class)
->toHaveMethods(['itseme'])
->and($response->itseme())
->toBe('mario');
expect($response)->toBeInstanceOf(RespondedRequestChild::class)
->and(method_exists($response, 'itseme'))->toBeTrue()
->and($response->itseme())->toBe('mario');
});

test('By default it uses the cookie jar and passes on cookies', function () {
Expand Down
4 changes: 2 additions & 2 deletions tests/Steps/Loading/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ function ($key) {
function () {
$warnings = [];

$previousHandler = set_error_handler(function ($errno, $errstr) use (&$warnings) {
set_error_handler(function ($errno, $errstr) use (&$warnings) {
if ($errno === E_WARNING) {
$warnings[] = $errstr;
}
Expand All @@ -420,7 +420,7 @@ function () {

$string = Http::getBodyString($response);

set_error_handler($previousHandler);
restore_error_handler();

expect($warnings)->toBeEmpty()
->and($string)->toBe('Servas!');
Expand Down
4 changes: 2 additions & 2 deletions tests/Utils/GzipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
it('does not generate a warning, when string to decode actually isn\'t encoded', function () {
$warnings = [];

$previousHandler = set_error_handler(function ($errno, $errstr) use (&$warnings) {
set_error_handler(function ($errno, $errstr) use (&$warnings) {
if ($errno === E_WARNING) {
$warnings[] = $errstr;
}
Expand All @@ -33,7 +33,7 @@

$decoded = Gzip::decode('Hello World!');

set_error_handler($previousHandler);
restore_error_handler();

expect($decoded)->toBe('Hello World!')
->and($warnings)->toBeEmpty();
Expand Down

0 comments on commit bad52eb

Please sign in to comment.