spatie / crawler

An easy to use, powerful crawler implemented in PHP. Can execute Javascript.
https://freek.dev/308-building-a-crawler-in-php
MIT License
2.51k stars 357 forks source link

Feat/convert phpunit tests to pest #401

Closed mansoorkhan96 closed 2 years ago

mansoorkhan96 commented 2 years ago

PHPUnit To Pest

This Pull Request mainly converts PHPUnit test suite to Pest. I am adding more description for other changes.

## New Log class This PR moves all the logic related to logging messages/content to a dedicated class under `tests/TestClasses/Log`.
```php namespace Spatie\Crawler\Test\TestClasses; class Log { private const path = __DIR__ . '/../temp/crawledUrls.txt'; public static function putContents(string $text): void { file_put_contents( static::path, $text . PHP_EOL, FILE_APPEND ); } public static function getContents(): string { return file_get_contents(static::path); } public static function reset(): void { file_put_contents(static::path, 'start log' . PHP_EOL); } } ```
## Custom `expecations` This PR replaces custom assertions to Pest's `expect()` syntax. These custom `expect()` helpers do not require Two Dimentional array instead they expect a simple One Dimentional array. But can also be used with Pest's `sequence()` and `each()` helper testing multiple URLs.
**Testing a single URL not crawled** ```php // Before $this->assertNotCrawled([ ['url' => 'http://example.com/'] ]); // After expect(['url' => 'http://example.com/'])->notToBeCrawled(); ``` **Testing multiple URLs not crawled** ```php // Before $this->assertNotCrawled([ ['url' => 'http://localhost:8080/'], [ 'url' => 'http://localhost:8080/link1', 'foundOn' => 'http://localhost:8080/', ], [ 'url' => 'http://localhost:8080/link2', 'foundOn' => 'http://localhost:8080/', ], ]); // After expect([ ['url' => 'http://localhost:8080/'], [ 'url' => 'http://localhost:8080/link1', 'foundOn' => 'http://localhost:8080/', ], [ 'url' => 'http://localhost:8080/link2', 'foundOn' => 'http://localhost:8080/', ], ])->each->notToBeCrawled(); ```

I have tested this PR heavily, would love for someone else to also double check this.

freekmurze commented 2 years ago

Thank you!