-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
208 additions
and
56 deletions.
There are no files selected for viewing
5 changes: 3 additions & 2 deletions
5
src/Scrapers/Distinct.php → src/Scrapers/Decorators/Distinct.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Vantoozz\ProxyScraper\Scrapers\Decorators; | ||
|
||
use Generator; | ||
use Vantoozz\ProxyScraper\Exceptions\ScraperException; | ||
use Vantoozz\ProxyScraper\Scrapers\ScraperInterface; | ||
|
||
/** | ||
* Class Timed | ||
* @package Vantoozz\ProxyScraper\Scrapers\Decorators | ||
*/ | ||
final class Timed implements ScraperInterface | ||
{ | ||
public const EVENT_DONE = 'done'; | ||
public const EVENT_PROXY_FOUND = 'proxy_found'; | ||
|
||
/** | ||
* @var ScraperInterface | ||
*/ | ||
private $scraper; | ||
|
||
/** | ||
* @var Generator | ||
*/ | ||
private $output; | ||
|
||
/** | ||
* Timed constructor. | ||
* @param ScraperInterface $scraper | ||
* @param Generator $output | ||
*/ | ||
public function __construct(ScraperInterface $scraper, Generator $output) | ||
{ | ||
$this->scraper = $scraper; | ||
$this->output = $output; | ||
} | ||
|
||
/** | ||
* @return Generator | ||
* @throws ScraperException | ||
*/ | ||
public function get(): Generator | ||
{ | ||
$startTime = microtime(true); | ||
|
||
foreach ($this->scraper->get() as $proxy) { | ||
$iterationStartTime = microtime(true); | ||
yield $proxy; | ||
$this->output->send([static::EVENT_PROXY_FOUND, microtime(true) - $iterationStartTime]); | ||
} | ||
|
||
$this->output->send([static::EVENT_DONE, microtime(true) - $startTime]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php declare(strict_types=1); | ||
|
||
|
||
namespace Vantoozz\ProxyScraper\UnitTests\Scrapers\Decorators; | ||
|
||
|
||
use Generator; | ||
use PHPUnit\Framework\TestCase; | ||
use Vantoozz\ProxyScraper\Ipv4; | ||
use Vantoozz\ProxyScraper\Port; | ||
use Vantoozz\ProxyScraper\Proxy; | ||
use Vantoozz\ProxyScraper\Scrapers\Decorators\Timed; | ||
use Vantoozz\ProxyScraper\Scrapers\ScraperInterface; | ||
|
||
/** | ||
* Class TimedTest | ||
* @package Vantoozz\ProxyScraper\UnitTests\Scrapers\Decorators | ||
*/ | ||
final class TimedTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_returns_proxy(): void | ||
{ | ||
$events = []; | ||
/** @var Proxy[] $proxies */ | ||
$proxies = iterator_to_array((new Timed($this->scraper(), $this->output($events)))->get()); | ||
|
||
static::assertCount(2, $proxies); | ||
static::assertInstanceOf(Proxy::class, $proxies[0]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_emits_events(): void | ||
{ | ||
$events = []; | ||
|
||
iterator_to_array((new Timed($this->scraper(), $this->output($events)))->get()); | ||
|
||
static::assertCount(3, $events); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_emits_done_events(): void | ||
{ | ||
$events = []; | ||
|
||
iterator_to_array((new Timed($this->scraper(), $this->output($events)))->get()); | ||
|
||
static::assertSame('done', $events[2][0]); | ||
static::assertIsFloat($events[2][1]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_emits_proxy_found_events(): void | ||
{ | ||
$events = []; | ||
|
||
iterator_to_array((new Timed($this->scraper(), $this->output($events)))->get()); | ||
|
||
static::assertSame('proxy_found', $events[0][0]); | ||
static::assertSame('proxy_found', $events[1][0]); | ||
static::assertIsFloat($events[0][1]); | ||
static::assertIsFloat($events[1][1]); | ||
static::assertNotSame($events[0][1], $events[1][1]); | ||
} | ||
|
||
/** | ||
* @return ScraperInterface | ||
*/ | ||
private function scraper(): ScraperInterface | ||
{ | ||
return new class implements ScraperInterface { | ||
/** | ||
* @return Generator | ||
*/ | ||
public function get(): Generator | ||
{ | ||
usleep(100000); | ||
yield new Proxy(new Ipv4('123.123.123.123'), new Port(1234)); | ||
usleep(150000); | ||
yield new Proxy(new Ipv4('234.234.234.234'), new Port(2345)); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* @param array $events | ||
* @return Generator | ||
*/ | ||
private function output(array &$events): Generator | ||
{ | ||
while ($event = yield) { | ||
$events[] = $event; | ||
} | ||
} | ||
} |