Skip to content

Commit

Permalink
Feature/travis (#2)
Browse files Browse the repository at this point in the history
* Добавил @Covers для тестов парсера
Упростил парсер - удалив 2 лишних метода

* remove 7.4 and nightly version from travis
  • Loading branch information
somework committed Oct 24, 2019
1 parent 95e5b7e commit 7274757
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 137 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ language: php
php:
- 7.2
- 7.3
- 7.4
- nightly

before_script:
- composer self-update
- composer install --prefer-source --no-interaction --dev

script:
- phpunit --coverage-text --coverage-clover=coverage.clover
- php -dxdebug.coverage_enable=1 vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover --whitelist src --no-configuration tests

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
"email": "i.pinchuk.work@gmail.com"
}
],
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14",
"phpunit/phpunit": "^8.4",
"http-interop/http-factory-guzzle": "^1.0",
"divineomega/psr-18-guzzle-adapter": "^1.0"
},
"autoload": {
"psr-4": {
"SomeWork\\Minjust\\": "src/"
Expand All @@ -35,5 +29,11 @@
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"paquettg/php-html-parser": "^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14",
"phpunit/phpunit": "^8.4",
"http-interop/http-factory-guzzle": "^1.0",
"divineomega/psr-18-guzzle-adapter": "^1.0"
}
}
74 changes: 48 additions & 26 deletions src/Parser/DomParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,44 @@

class DomParser implements ParserInterface
{
/**
* @var string
*/
protected const CURRENT_PAGE_SELECTOR = 'span.currentStep';

/**
* @var string
*/
protected const PAGINATION_BLOCK_SELECTOR = 'div.pagination';

/**
* @var string
*/
protected const PAGINATION_STEP_SELECTOR = 'a.step';

/**
* @var string
*/
protected const LAWYERS_LIST_BLOCK_SELECTOR = 'table.persons > tbody > tr';

/**
* @var string
*/
protected const LAWYER_DETAIL_SELECTOR = '.floating > p.row';

public function list(string $body): FindResponse
{
$dom = (new Dom())->loadStr($body);

$findResponse = new FindResponse();
$findResponse
return (new FindResponse())
->setPage($this->getCurrentPage($dom))
->setTotalPage($this->getTotalPage($dom))
->setLawyers($this->getListLawyers($dom));

return $findResponse;
}

protected function getCurrentPage(Dom $dom): int
{
if ($span = $dom->find('span.currentStep', 0)) {
if ($span = $dom->find(static::CURRENT_PAGE_SELECTOR, 0)) {
return (int) $span->text();
}

Expand All @@ -41,7 +63,10 @@ protected function getTotalPage(Dom $dom): int
/**
* @var HtmlNode[] $collection
*/
$collection = $dom->find('div.pagination', 0)->find('a.step')->toArray();
$collection = $dom
->find(static::PAGINATION_BLOCK_SELECTOR, 0)
->find(static::PAGINATION_STEP_SELECTOR)
->toArray();
if (0 === count($collection)) {
return 1;
}
Expand All @@ -64,7 +89,7 @@ protected function getListLawyers(Dom $dom): array
/**
* @var Dom\HtmlNode[]|Collection $nodes
*/
$nodes = $dom->find('table.persons > tbody > tr');
$nodes = $dom->find(static::LAWYERS_LIST_BLOCK_SELECTOR);
foreach ($nodes as $node) {
/**
* @var Dom\HtmlNode[]|Collection $tds
Expand All @@ -89,27 +114,24 @@ public function detail(string $body): DetailLawyer
{
$dom = (new Dom())->loadStr($body);

return $this->getFullLawyer($dom);
}

protected function getFullLawyer(Dom $dom): DetailLawyer
{
$nodes = $dom->find('.floating > p.row')->toArray();
/**
* @var Dom\HtmlNode[] $nodes
*/
$nodes = $dom->find(static::LAWYER_DETAIL_SELECTOR)->toArray();

return (new DetailLawyer())
->setLawFormation($this->getLawFormation($nodes))
$lawyer = (new DetailLawyer())
->setChamberOfLaw(trim($nodes[5]->text()));
}
if (($organizationForm = trim($nodes[7]->text())) !== '') {
$lawyer->setLawFormation(
(new LawFormation())
->setOrganizationalForm($organizationForm)
->setName(trim($nodes[9]->text()))
->setAddress(trim($nodes[11]->text()))
->setPhone(trim($nodes[13]->text()))
->setEmail(trim($nodes[15]->text()))
);
}

protected function getLawFormation(array $nodes): ?LawFormation
{
$formation = (new LawFormation())
->setOrganizationalForm(trim($nodes[7]->text()))
->setName(trim($nodes[9]->text()))
->setAddress(trim($nodes[11]->text()))
->setPhone(trim($nodes[13]->text()))
->setEmail(trim($nodes[15]->text()));

return $formation->getOrganizationalForm() !== '' ? $formation : null;
return $lawyer;
}
}
25 changes: 6 additions & 19 deletions tests/Unit/AbstractParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,7 @@
abstract class AbstractParserTest extends TestCase
{
/**
* @var ParserInterface
*/
protected static $parser;

public static function setUpBeforeClass(): void
{
static::$parser = static::getNewParser();
}

abstract public static function getNewParser(): ParserInterface;

public static function tearDownAfterClass(): void
{
static::$parser = null;
}

/**
* @covers ::list
* @dataProvider listProvider
*
* @param string $resource
Expand All @@ -38,15 +22,18 @@ public static function tearDownAfterClass(): void
public function testList(string $resource, int $page, int $totalPages, int $count): void
{
$body = file_get_contents($resource);
$response = static::$parser->list($body);
$response = $this->getParser()->list($body);

$this->assertEquals($page, $response->getPage());
$this->assertEquals($totalPages, $response->getTotalPage());
$this->assertCount($count, $response->getLawyers());
$this->assertContainsOnlyInstancesOf(Lawyer::class, $response->getLawyers());
}

abstract public function getParser(): ParserInterface;

/**
* @covers ::detail
* @dataProvider detailProvider
*
* @param string $resource
Expand All @@ -56,7 +43,7 @@ public function testList(string $resource, int $page, int $totalPages, int $coun
public function testDetail(string $resource, string $chamberOfLaw, ?LawFormation $lawFormation): void
{
$body = file_get_contents($resource);
$lawyer = static::$parser->detail($body);
$lawyer = $this->getParser()->detail($body);

$this->assertIsString($lawyer->getChamberOfLaw());
$this->assertEquals($chamberOfLaw, $lawyer->getChamberOfLaw());
Expand Down
37 changes: 36 additions & 1 deletion tests/Unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,52 @@

namespace SomeWork\Minjust\Tests\Unit;

use Psr\Http\Client\ClientExceptionInterface;
use DivineOmega\Psr18GuzzleAdapter\Client as GuzzleClient;
use Http\Factory\Guzzle\RequestFactory;
use Http\Factory\Guzzle\StreamFactory;
use Iterator;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use ReflectionClass;
use SomeWork\Minjust\Client;
use SomeWork\Minjust\FindRequest;

/**
* @coversDefaultClass \SomeWork\Minjust\Client
*/
class ClientTest extends TestCase
{
/**
* @covers ::__construct
* @throws \ReflectionException
*/
public function testConstruct(): void
{
$httpClient = $this->createMock(ClientInterface::class);
$requestFactory = $this->createMock(RequestFactoryInterface::class);
$streamFactory = $this->createMock(StreamFactoryInterface::class);

$client = new Client($httpClient, $requestFactory, $streamFactory);

$ref = new ReflectionClass(Client::class);
$clientProperty = $ref->getProperty('client');
$requestFactoryProperty = $ref->getProperty('requestFactory');
$streamFactoryProperty = $ref->getProperty('streamFactory');

$clientProperty->setAccessible(true);
$requestFactoryProperty->setAccessible(true);
$streamFactoryProperty->setAccessible(true);

$this->assertEquals($httpClient, $clientProperty->getValue($client));
$this->assertEquals($requestFactory, $requestFactoryProperty->getValue($client));
$this->assertEquals($streamFactory, $streamFactoryProperty->getValue($client));
}

/**
* @covers ::list
* @dataProvider listProvider
*
* @param array $formData
Expand Down Expand Up @@ -56,6 +90,7 @@ public function listProvider(): Iterator
}

/**
* @covers ::detail
* @dataProvider detailProvider
*
* @param string $url
Expand Down

0 comments on commit 7274757

Please sign in to comment.