-
Notifications
You must be signed in to change notification settings - Fork 1
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
12 changed files
with
2,819 additions
and
4 deletions.
There are no files selected for viewing
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
625 changes: 625 additions & 0 deletions
625
tests/Data/ResponseHtml/FreeBsdDotOrg/command-not-found.html
Large diffs are not rendered by default.
Oops, something went wrong.
659 changes: 659 additions & 0 deletions
659
tests/Data/ResponseHtml/FreeBsdDotOrg/missing-long-description-element.html
Large diffs are not rendered by default.
Oops, something went wrong.
660 changes: 660 additions & 0 deletions
660
tests/Data/ResponseHtml/FreeBsdDotOrg/missing-name-element.html
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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
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)); | ||
} | ||
} |
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,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'); | ||
} | ||
} |
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,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'), | ||
); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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()); | ||
} | ||
} |
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