Skip to content

Commit

Permalink
Upgrade goutte from v3 to v4 (#10)
Browse files Browse the repository at this point in the history
Remove support for Laravel 7.x because it is too outdated.
Moved from goutte v3 to v4 to be able to use Guzzle 7.x
  • Loading branch information
Serginyu authored Feb 28, 2023
1 parent 72fe8ad commit d44d6d6
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 28 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
},
"require": {
"php": ">= 8.0",
"fabpot/goutte": "^3.2",
"fabpot/goutte": "^4.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"illuminate/database": "^7.0 || ^8.0 || ^9.0",
"illuminate/events": "^7.0 || ^8.0 || ^9.0",
"illuminate/database": "^8.0 || ^9.0",
"illuminate/events": "^8.0 || ^9.0",
"ext-dom": "*",
"ext-json": "*"
},
Expand Down
8 changes: 4 additions & 4 deletions src/Scraper/Application/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Joskfg\LaravelIntelligentScraper\Scraper\Application;

use Goutte\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Joskfg\LaravelIntelligentScraper\Scraper\Entities\Field;
Expand All @@ -17,6 +15,8 @@
use Joskfg\LaravelIntelligentScraper\Scraper\Repositories\Configuration as ConfigurationRepository;
use JsonException;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use UnexpectedValueException;

class Configurator
Expand Down Expand Up @@ -68,13 +68,13 @@ private function getCrawler(ScrapedDataset $scrapedData): ?Crawler
Log::info("Request {$scrapedData['url']}");

return $this->client->request('GET', $scrapedData['url']);
} catch (ConnectException $e) {
} catch (TransportException $e) {
Log::notice(
"Connection error: {$e->getMessage()}",
compact('scrapedData')
);
$scrapedData->delete();
} catch (RequestException $e) {
} catch (HttpExceptionInterface $e) {
$httpCode = $e->getResponse()->getStatusCode();
Log::notice(
"Response status ($httpCode) invalid, so proceeding to delete the scraped data.",
Expand Down
8 changes: 4 additions & 4 deletions src/Scraper/Application/XpathFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace Joskfg\LaravelIntelligentScraper\Scraper\Application;

use Goutte\Client as GoutteClient;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Joskfg\LaravelIntelligentScraper\Scraper\Entities\Field;
use Joskfg\LaravelIntelligentScraper\Scraper\Entities\ScrapedData;
use Joskfg\LaravelIntelligentScraper\Scraper\Exceptions\MissingXpathValueException;
use Joskfg\LaravelIntelligentScraper\Scraper\Models\Configuration;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use UnexpectedValueException;

class XpathFinder
Expand Down Expand Up @@ -72,10 +72,10 @@ private function getCrawler(string $url): ?Crawler
Log::info("Requesting $url");

return $this->client->request('GET', $url);
} catch (ConnectException $e) {
} catch (TransportException $e) {
Log::info("Unavailable url '$url'", ['message' => $e->getMessage()]);
throw new UnexpectedValueException("Unavailable url '$url'");
} catch (RequestException $e) {
} catch (HttpExceptionInterface $e) {
$httpCode = $e->getResponse()->getStatusCode();
Log::info('Invalid response http status', ['status' => $httpCode]);
throw new UnexpectedValueException("Response error from '$url' with '$httpCode' http code");
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Fakes/FakeHttpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Tests\Unit\Fakes;

use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;

abstract class FakeHttpException extends \Exception implements HttpExceptionInterface
{
}
16 changes: 9 additions & 7 deletions tests/Unit/Scraper/Application/ConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use DOMElement;
use Goutte\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Log;
use Joskfg\LaravelIntelligentScraper\Scraper\Events\ConfigurationScraped;
Expand All @@ -16,7 +14,9 @@
use Mockery;
use Mockery\LegacyMockInterface;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpClient\Exception\TransportException;
use Tests\TestCase;
use Tests\Unit\Fakes\FakeHttpException;
use UnexpectedValueException;

class ConfiguratorTest extends TestCase
Expand Down Expand Up @@ -76,7 +76,7 @@ public function whenTryToFindNewXpathButUrlFromDatasetIsNotFoundThrowAnException
]),
]);

$requestException = Mockery::mock(RequestException::class);
$requestException = Mockery::mock(FakeHttpException::class);
$requestException->shouldReceive('getResponse->getStatusCode')
->once()
->andReturn(404);
Expand All @@ -86,7 +86,7 @@ public function whenTryToFindNewXpathButUrlFromDatasetIsNotFoundThrowAnException
'GET',
':scrape-url:'
)
->andThrows($requestException);
->andThrow($requestException);

$this->configuration->shouldReceive('findByType')
->once()
Expand Down Expand Up @@ -125,7 +125,7 @@ public function whenTryToFindNewXpathButUrlFromDatasetIsNotAvailableThrowAnExcep
]),
]);

$connectException = Mockery::mock(ConnectException::class);
$connectException = Mockery::mock(TransportException::class);
$this->client->shouldReceive('request')
->once()
->with(
Expand Down Expand Up @@ -319,10 +319,12 @@ public function whenUseSomeOldXpathButNotFoundNewsItShouldLogItAndResetVariant()
$crawler->shouldReceive('getNode')
->with(0)
->andReturn($rootElement);
$crawler->shouldReceive('filterXpath->count')
$filter = $crawler->shouldReceive('filterXpath')
->andReturnSelf()
->getMock()
->shouldReceive('count')
->once()
->andReturn(1);

$this->xpathBuilder->shouldReceive('find')
->never()
->with($rootElement, ':value-1:');
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Scraper/Application/XpathFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Joskfg\LaravelIntelligentScraper\Scraper\Application;

use Goutte\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Log;
use Joskfg\LaravelIntelligentScraper\Scraper\Exceptions\MissingXpathValueException;
use Joskfg\LaravelIntelligentScraper\Scraper\Models\Configuration;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpClient\Exception\TransportException;
use Tests\TestCase;
use Tests\Unit\Fakes\FakeHttpException;

class XpathFinderTest extends TestCase
{
Expand Down Expand Up @@ -38,7 +38,7 @@ public function whenExtractUsingAnInvalidUrlStatusItShouldThrowAnException(): vo

$variantGenerator = \Mockery::mock(VariantGenerator::class);

$requestException = \Mockery::mock(RequestException::class);
$requestException = \Mockery::mock(FakeHttpException::class);
$requestException->shouldReceive('getResponse->getStatusCode')
->once()
->andReturn(404);
Expand Down Expand Up @@ -74,7 +74,7 @@ public function whenExtractUsingAnUnavailableUrlItShouldThrowAnException(): void

$variantGenerator = \Mockery::mock(VariantGenerator::class);

$connectException = \Mockery::mock(ConnectException::class);
$connectException = \Mockery::mock(TransportException::class);
$client = \Mockery::mock(Client::class);
$client->shouldReceive('request')
->once()
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Scraper/Listeners/ConfigureScraperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Joskfg\LaravelIntelligentScraper\Scraper\Listeners;

use GuzzleHttp\Exception\ConnectException;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Log;
use Joskfg\LaravelIntelligentScraper\Scraper\Application\XpathFinder;
Expand All @@ -16,6 +15,7 @@
use Joskfg\LaravelIntelligentScraper\Scraper\Repositories\Configuration;
use Mockery;
use Mockery\LegacyMockInterface;
use Symfony\Component\HttpClient\Exception\TransportException;
use Tests\TestCase;
use UnexpectedValueException;

Expand Down Expand Up @@ -167,9 +167,9 @@ public function whenScrappingConnectionFailsItShouldThrowAConnectionException():
$this->xpathFinder->shouldReceive('extract')
->once()
->with(':scrape-url:', $xpathConfig)
->andThrowExceptions([Mockery::mock(ConnectException::class)]);
->andThrowExceptions([Mockery::mock(TransportException::class)]);

$this->expectException(ConnectException::class);
$this->expectException(TransportException::class);

$configureScraper = new ConfigureScraper(
$this->config,
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Scraper/Listeners/ScrapeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Joskfg\LaravelIntelligentScraper\Scraper\Listeners;

use GuzzleHttp\Exception\ConnectException;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Log;
use Joskfg\LaravelIntelligentScraper\Scraper\Application\XpathFinder;
Expand All @@ -14,6 +13,7 @@
use Joskfg\LaravelIntelligentScraper\Scraper\Repositories\Configuration;
use Mockery;
use Mockery\LegacyMockInterface;
use Symfony\Component\HttpClient\Exception\TransportException;
use Tests\TestCase;
use UnexpectedValueException;

Expand Down Expand Up @@ -79,9 +79,9 @@ public function whenScrappingConnectionFailsItShouldThrowAConnectionException():
$this->xpathFinder->shouldReceive('extract')
->once()
->with(':scrape-url:', $xpathConfig)
->andThrowExceptions([Mockery::mock(ConnectException::class)]);
->andThrowExceptions([Mockery::mock(TransportException::class)]);

$this->expectException(ConnectException::class);
$this->expectException(TransportException::class);

$scrape = new Scrape(
$this->config,
Expand Down

0 comments on commit d44d6d6

Please sign in to comment.