Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PeeHaa committed Dec 6, 2019
1 parent 30a1aaa commit 3c790b0
Show file tree
Hide file tree
Showing 12 changed files with 2,819 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Exception/UnexpectedHtmlFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ final class UnexpectedHtmlFormat extends Exception
{
public function __construct(string $element)
{
parent::__construct(sprintf('Could not find the "%s" element in the row', $element));
parent::__construct(sprintf('Could not find the "%s" element in the document', $element));
}
}
6 changes: 3 additions & 3 deletions src/Parser/FreeBsdDotOrg.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ private function isPartOfLongDescription(\DOMNode $currentNode): bool
private function getSynopsis(\DOMXPath $xpath): string
{
/** @var \DOMNodeList $nameNodes */
$synopsisNodes = $xpath->evaluate('//a[@name="SYNOPSIS"]/following-sibling::b');
$synopsisNodes = $xpath->evaluate('//a[@name="SYNOPSIS"]');

if (!$synopsisNodes->length) {
throw new UnexpectedHtmlFormat('synopsis');
}

$currentNode = $synopsisNodes->item(0);
$currentNode = $xpath->evaluate('//a[@name="SYNOPSIS"]/following-sibling::b')->item(0);

if ($currentNode === null) {
throw new UnexpectedHtmlFormat('synopsis start node');
Expand All @@ -120,7 +120,7 @@ private function getSynopsis(\DOMXPath $xpath): string
private function trimDescription(string $description): string
{
$description = str_replace(["\r\n", "\r", "\n"], ' ', $description);
$description = trim($description, ' -');
$description = trim($description, " \t\n\r\0\x0B-");
$description = preg_replace('/\s+/', ' ', $description);

return $description;
Expand Down
625 changes: 625 additions & 0 deletions tests/Data/ResponseHtml/FreeBsdDotOrg/command-not-found.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

660 changes: 660 additions & 0 deletions tests/Data/ResponseHtml/FreeBsdDotOrg/missing-name-element.html

Large diffs are not rendered by default.

660 changes: 660 additions & 0 deletions tests/Data/ResponseHtml/FreeBsdDotOrg/valid.html

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions tests/Fakes/HttpClient/MockResponseInterceptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace AsyncBot\Plugin\LinuxManualPagesTest\Fakes\HttpClient;

use Amp\ByteStream\InMemoryStream;
use Amp\CancellationToken;
use Amp\Http\Client\ApplicationInterceptor;
use Amp\Http\Client\DelegateHttpClient;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Promise;
use Amp\Success;

final class MockResponseInterceptor implements ApplicationInterceptor
{
private string $body;

public function __construct(string $body)
{
$this->body = $body;
}

/**
* phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
*
* @return Promise<Response>
*/
public function request(Request $request, CancellationToken $cancellation, DelegateHttpClient $client): Promise
{
// phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
$body = new InMemoryStream($this->body);

return new Success(new Response('2', 200, 'OK', [], $body, $request));
}
}
17 changes: 17 additions & 0 deletions tests/Unit/Exception/UnexpectedHtmlFormatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace AsyncBot\Plugin\LinuxManualPagesTest\Unit\Exception;

use AsyncBot\Plugin\LinuxManualPages\Exception\UnexpectedHtmlFormat;
use PHPUnit\Framework\TestCase;

class UnexpectedHtmlFormatTest extends TestCase
{
public function testConstructorFormatsMessageCorrectly(): void
{
$this->expectException(UnexpectedHtmlFormat::class);
$this->expectExceptionMessage('Could not find the "TEST" element in the document');

throw new UnexpectedHtmlFormat('TEST');
}
}
94 changes: 94 additions & 0 deletions tests/Unit/Parser/FreeBsdDotOrgTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php declare(strict_types=1);

namespace AsyncBot\Plugin\LinuxManualPagesTest\Unit\Parser;

use AsyncBot\Plugin\LinuxManualPages\Exception\UnexpectedHtmlFormat;
use AsyncBot\Plugin\LinuxManualPages\Parser\FreeBsdDotOrg;
use AsyncBot\Plugin\LinuxManualPages\ValueObject\ManualPage;
use PHPUnit\Framework\TestCase;
use function Room11\DOMUtils\domdocument_load_html;

class FreeBsdDotOrgTest extends TestCase
{
private function getDomFromFakeResponse(string $filename): \DOMDocument
{
return domdocument_load_html(
file_get_contents(TEST_DATA_DIR . '/ResponseHtml/FreeBsdDotOrg/' . $filename),
);
}

public function testParseReturnsNullWhenCommandCouldNotBeFound(): void
{
$this->assertNull((new FreeBsdDotOrg())->parse(
$this->getDomFromFakeResponse('command-not-found.html'),
));
}

public function testParseReturnsManualPageWhenValid(): void
{
$manualPage = (new FreeBsdDotOrg())->parse(
$this->getDomFromFakeResponse('valid.html'),
);

$this->assertInstanceOf(ManualPage::class, $manualPage);
}

public function testParseReturnsCorrectName(): void
{
$manualPage = (new FreeBsdDotOrg())->parse(
$this->getDomFromFakeResponse('valid.html'),
);

$this->assertSame('locate.updatedb', $manualPage->getName());
}

public function testParseReturnsCorrectShortDescription(): void
{
$manualPage = (new FreeBsdDotOrg())->parse(
$this->getDomFromFakeResponse('valid.html'),
);

$this->assertSame('update locate database', $manualPage->getShortDescription());
}

public function testParseReturnsCorrectLongDescription(): void
{
$manualPage = (new FreeBsdDotOrg())->parse(
$this->getDomFromFakeResponse('valid.html'),
);

$this->assertSame(
'The locate.updatedb utility updates the database used by locate(1) . It is typically run once a week by the /etc/periodic/weekly/310.locate script. The contents of the newly built database can be controlled by the /etc/locate.rc file.',
$manualPage->getLongDescription(),
);
}

public function testParseReturnsCorrectSynopsis(): void
{
$manualPage = (new FreeBsdDotOrg())->parse(
$this->getDomFromFakeResponse('valid.html'),
);

$this->assertSame('/usr/libexec/locate.updatedb', $manualPage->getSynopsis());
}

public function testParseThrowsOnMissingNameElement(): void
{
$this->expectException(UnexpectedHtmlFormat::class);
$this->expectExceptionMessage('Could not find the "name" element in the document');

(new FreeBsdDotOrg())->parse(
$this->getDomFromFakeResponse('missing-name-element.html'),
);
}

public function testParseThrowsOnMissingLongDescriptionElement(): void
{
$this->expectException(UnexpectedHtmlFormat::class);
$this->expectExceptionMessage('Could not find the "long description" element in the document');

(new FreeBsdDotOrg())->parse(
$this->getDomFromFakeResponse('missing-long-description-element.html'),
);
}
}
27 changes: 27 additions & 0 deletions tests/Unit/Retriever/SearchOnFreeBsdDotOrgTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace AsyncBot\Plugin\LinuxManualPagesTest\Unit\Retriever;

use Amp\Http\Client\HttpClientBuilder;
use AsyncBot\Core\Http\Client;
use AsyncBot\Plugin\LinuxManualPages\Retriever\SearchOnFreeBsdDotOrg;
use AsyncBot\Plugin\LinuxManualPages\ValueObject\ManualPage;
use AsyncBot\Plugin\LinuxManualPagesTest\Fakes\HttpClient\MockResponseInterceptor;
use PHPUnit\Framework\TestCase;
use function Amp\Promise\wait;

class SearchOnFreeBsdDotOrgTest extends TestCase
{
public function testRetrieveReturnsManualPage(): void
{
$httpClient = new Client(
(new HttpClientBuilder())->intercept(
new MockResponseInterceptor(file_get_contents(TEST_DATA_DIR . '/ResponseHtml/FreeBsdDotOrg/valid.html')),
)->build(),
);

$manualPage = wait((new SearchOnFreeBsdDotOrg($httpClient))->retrieve('updatedb'));

$this->assertInstanceOf(ManualPage::class, $manualPage);
}
}
36 changes: 36 additions & 0 deletions tests/Unit/ValueObject/ManualPageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

namespace AsyncBot\Plugin\LinuxManualPagesTest\Unit\ValueObject;

use AsyncBot\Plugin\LinuxManualPages\ValueObject\ManualPage;
use PHPUnit\Framework\TestCase;

class ManualPageTest extends TestCase
{
private ManualPage $manualPage;

protected function setUp(): void
{
$this->manualPage = new ManualPage('command', 'The short description', 'The long description', 'The synopsis');
}

public function testGetName(): void
{
$this->assertSame('command', $this->manualPage->getName());
}

public function testGetShortDescription(): void
{
$this->assertSame('The short description', $this->manualPage->getShortDescription());
}

public function testGetLongDescription(): void
{
$this->assertSame('The long description', $this->manualPage->getLongDescription());
}

public function testGetSynopsis(): void
{
$this->assertSame('The synopsis', $this->manualPage->getSynopsis());
}
}
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
namespace AsyncBot\Plugin\LinuxManualPagesTest;

require_once __DIR__ . '/../vendor/autoload.php';

define('TEST_DATA_DIR', __DIR__ . '/Data');

0 comments on commit 3c790b0

Please sign in to comment.