-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improve Module::class + test. * Refactoring internals.
- Loading branch information
Showing
16 changed files
with
443 additions
and
20 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
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
6 changes: 5 additions & 1 deletion
6
src/Finder/PackagistModuleFinder.php → ...PackagistFinder/PackagistModuleFinder.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
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
2 changes: 1 addition & 1 deletion
2
src/Finder/ModuleItemCollection.php → src/Application/ModuleItemCollection.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
2 changes: 1 addition & 1 deletion
2
src/Finder/PackagistItem.php → src/Application/PackagistItem.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
2 changes: 1 addition & 1 deletion
2
src/Finder/PackagistItemCollection.php → src/Application/PackagistItemCollection.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
Empty file.
72 changes: 72 additions & 0 deletions
72
tests/Unit/Adapter/PackagistFinder/PackagistHttpResponseTrait.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace OpenEMR\Modules\Marketplace\Tests\Unit\Adapter\PackagistFinder; | ||
|
||
trait PackagistHttpResponseTrait | ||
{ | ||
private function packagistEmptyResponseContent(): string | ||
{ | ||
return <<<JSON | ||
{ | ||
"results": [], | ||
"total": 0 | ||
} | ||
JSON; | ||
} | ||
|
||
private function packagistDefaultSingleResultResponseContent(): string | ||
{ | ||
return <<<JSON | ||
{ | ||
"results": [ | ||
{ | ||
"name": "openemr/oe-module-faxsms", | ||
"description": "OpenEMR Fax and SMS module", | ||
"url": "https://packagist.org/packages/openemr/oe-module-faxsms", | ||
"repository": "https://github.com/openemr/oe-module-faxsms", | ||
"downloads": 36, | ||
"favers": 1 | ||
} | ||
], | ||
"total": 1 | ||
} | ||
JSON; | ||
} | ||
|
||
private function packagistDefaultMultipleResultResponseContent(): string | ||
{ | ||
return <<<JSON | ||
{ | ||
"results": [ | ||
{ | ||
"name": "zerai/oe-module-demo-farm-add-ons", | ||
"description": "OpenEMR Demo Farm Add-ons module", | ||
"url": "https://packagist.org/packages/zerai/oe-module-demo-farm-add-ons", | ||
"repository": "https://github.com/zerai/oe-module-demo-farm-add-ons", | ||
"downloads": 7, | ||
"favers": 0 | ||
}, | ||
{ | ||
"name": "openemr/oe-module-faxsms", | ||
"description": "OpenEMR Fax and SMS module", | ||
"url": "https://packagist.org/packages/openemr/oe-module-faxsms", | ||
"repository": "https://github.com/openemr/oe-module-faxsms", | ||
"downloads": 36, | ||
"favers": 1 | ||
} | ||
], | ||
"total": 2 | ||
} | ||
JSON; | ||
} | ||
|
||
private function packagistEmptyResultResponseContent(): string | ||
{ | ||
return <<<JSON | ||
{ | ||
"results": [], | ||
"total": 0 | ||
} | ||
JSON; | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
tests/Unit/Adapter/PackagistFinder/PackagistModuleFinderTest.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace OpenEMR\Modules\Marketplace\Tests\Unit\Adapter\PackagistFinder; | ||
|
||
use Exception; | ||
use Http\Mock\Client; | ||
use OpenEMR\Modules\Marketplace\Adapter\PackagistFinder\PackagistModuleFinder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PackagistModuleFinderTest extends TestCase | ||
{ | ||
use PackagistHttpResponseTrait; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_find_one_module(): void | ||
{ | ||
$client = $this->createHttpClientWithDefaultResponse($this->packagistDefaultSingleResultResponseContent()); | ||
$packagistModuleFinder = new PackagistModuleFinder($client); | ||
|
||
$foundedModulesCollection = $packagistModuleFinder->searchModule(); | ||
|
||
self::assertEquals(1, $foundedModulesCollection->count()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_find_multiple_module(): void | ||
{ | ||
$client = $this->createHttpClientWithDefaultResponse($this->packagistDefaultMultipleResultResponseContent()); | ||
$packagistModuleFinder = new PackagistModuleFinder($client); | ||
|
||
$foundedModulesCollection = $packagistModuleFinder->searchModule(); | ||
|
||
self::assertEquals(2, $foundedModulesCollection->count()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_handle_http_exception(): void | ||
{ | ||
$this->expectException(\RuntimeException::class); | ||
|
||
$clientException = new \RuntimeException('Whoops! The server is down.'); | ||
$client = $this->createHttpClientWithExceptionResponse($clientException, 500); | ||
$packagistModuleFinder = new PackagistModuleFinder($client); | ||
|
||
$packagistModuleFinder->searchModule(); | ||
} | ||
|
||
/** | ||
* This is an edge case, the module system is based on "openemr/oe-module-installer-plugin" | ||
* it should always appear in the search results. | ||
* 0 result should never happen. | ||
* | ||
* @test | ||
*/ | ||
public function can_handle_a_empty_respose_content(): void | ||
{ | ||
$client = $this->createHttpClientWithDefaultResponse($this->packagistEmptyResponseContent()); | ||
$packagistModuleFinder = new PackagistModuleFinder($client); | ||
|
||
$foundedModulesCollection = $packagistModuleFinder->searchModule(); | ||
|
||
self::assertEquals(0, $foundedModulesCollection->count()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_generate_an_valid_http_endpoint_without_an_input_queryString_parameter(): void | ||
{ | ||
$expectedEndpoint = 'https://packagist.org/search.json?q=&type=openemr-module'; | ||
|
||
$packagistModuleFinder = new PackagistModuleFinder($client = new Client()); | ||
|
||
$endpoint = $packagistModuleFinder->endpoint(); | ||
|
||
self::assertStringContainsString('type=openemr-module', $endpoint); | ||
self::assertSame($expectedEndpoint, $endpoint); | ||
self::assertTrue((bool) filter_var($endpoint, FILTER_VALIDATE_URL)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_generate_an_valid_http_endpoint_from_an_input_queryString_parameter(): void | ||
{ | ||
$queryString = 'vendor_1'; | ||
|
||
$expectedEndpoint = 'https://packagist.org/search.json?q=' . $queryString . '&type=openemr-module'; | ||
|
||
$packagistModuleFinder = new PackagistModuleFinder($client = new Client()); | ||
|
||
$endpoint = $packagistModuleFinder->endpoint($queryString); | ||
|
||
self::assertStringContainsString('type=openemr-module', $endpoint); | ||
self::assertSame($expectedEndpoint, $endpoint); | ||
self::assertTrue((bool) filter_var($endpoint, FILTER_VALIDATE_URL)); | ||
} | ||
|
||
private function createHttpClientWithDefaultResponse(string $contentResponse, int $httpStatusCode = 200): Client | ||
{ | ||
$client = new Client(); | ||
|
||
$stream = $this->createMock('Psr\Http\Message\StreamInterface'); | ||
$stream->expects(self::any())->method('getContents')->willReturn($contentResponse); | ||
|
||
$response = $this->createMock('Psr\Http\Message\ResponseInterface'); | ||
$response->expects(self::any())->method('getStatusCode')->willReturn($httpStatusCode); | ||
$response->expects(self::any())->method('getBody')->willReturn($stream); | ||
|
||
$client->setDefaultResponse($response); | ||
|
||
return $client; | ||
} | ||
|
||
private function createHttpClientWithExceptionResponse(Exception $exception, int $httpStatusCode): Client | ||
{ | ||
$client = new Client(); | ||
$client->addException($exception); | ||
|
||
return $client; | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function httpClientTest(): void | ||
{ | ||
$stream = $this->createMock('Psr\Http\Message\StreamInterface'); | ||
$stream->method('getContents')->willReturn($this->packagistDefaultSingleResultResponseContent()); | ||
$response = $this->createMock('Psr\Http\Message\ResponseInterface'); | ||
$response->method('getBody')->willReturn($stream); | ||
$client = $this->createHttpClientWithDefaultResponse($this->packagistDefaultSingleResultResponseContent()); | ||
|
||
$returnedResponse = $client->sendRequest($this->createMock('Psr\Http\Message\RequestInterface')); | ||
|
||
$this->assertEquals($response, $returnedResponse); | ||
$this->assertEquals(200, $returnedResponse->getStatusCode()); | ||
} | ||
} |
Oops, something went wrong.