This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
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.
- Loading branch information
1 parent
a3ef3ca
commit 12d2f30
Showing
13 changed files
with
150 additions
and
127 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
/* | ||
* Copyright 2024 Code Inc. <https://www.codeinc.co> | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeInc\Pdf2TxtClient\Tests; | ||
|
||
use CodeInc\Pdf2TxtClient\ConvertOptions; | ||
use CodeInc\Pdf2TxtClient\Exception; | ||
use CodeInc\Pdf2TxtClient\Format; | ||
use CodeInc\Pdf2TxtClient\Pdf2TxtClient; | ||
use JsonException; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
final class Pdf2TxtClientTest extends TestCase | ||
{ | ||
private const string DEFAULT_PDF2TEXT_BASE_URL = 'http://localhost:3000'; | ||
private const string TEST_PDF_PATH = __DIR__.'/assets/file.pdf'; | ||
private const string TEST_PDF_RESULT_TXT = __DIR__.'/assets/file.txt'; | ||
private const string TEST_PDF_RESULT_JSON = __DIR__.'/assets/file.json'; | ||
|
||
/** | ||
* @throws Exception|JsonException | ||
*/ | ||
public function testConvertLocalFileToText(): void | ||
{ | ||
$stream = $this->getNewClient()->convertLocalFile(self::TEST_PDF_PATH); | ||
$this->assertInstanceOf(StreamInterface::class, $stream, "The stream is not valid"); | ||
|
||
$text = (string)$stream; | ||
$this->assertNotEmpty($text, "The stream is empty"); | ||
$this->assertStringEqualsFile(self::TEST_PDF_RESULT_TXT, $text, "The text is not valid"); | ||
} | ||
|
||
/** | ||
* @throws Exception|JsonException | ||
*/ | ||
public function testConvertLocalFileToRawJson(): void | ||
{ | ||
$client = $this->getNewClient(); | ||
$stream = $client->convertLocalFile(self::TEST_PDF_PATH, new ConvertOptions(format: Format::json)); | ||
$this->assertInstanceOf(StreamInterface::class, $stream, "The stream is not valid"); | ||
|
||
$rawJson = (string)$stream; | ||
$this->assertJson($rawJson, "The JSON is not valid"); | ||
$this->assertStringEqualsFile(self::TEST_PDF_RESULT_JSON, $rawJson, "The JSON is not valid"); | ||
} | ||
|
||
/** | ||
* @throws Exception|JsonException | ||
*/ | ||
public function testConvertLocalFileToProcessedJson(): void | ||
{ | ||
$client = $this->getNewClient(); | ||
$stream = $client->convertLocalFile(self::TEST_PDF_PATH, new ConvertOptions(format: Format::json)); | ||
$this->assertInstanceOf(StreamInterface::class, $stream, "The stream is not valid"); | ||
|
||
$json = $client->processJsonResponse($stream); | ||
$this->assertIsArray($json, "The processed JSON is not valid"); | ||
|
||
$expectedJson = json_decode(file_get_contents(self::TEST_PDF_RESULT_JSON), true); | ||
$this->assertArrayIsEqualToArrayOnlyConsideringListOfKeys( | ||
$json, | ||
$expectedJson, | ||
["meta", "pages"] | ||
); | ||
} | ||
|
||
private function getNewClient(): Pdf2TxtClient | ||
{ | ||
$apiBaseUrl = self::DEFAULT_PDF2TEXT_BASE_URL; | ||
if (defined('PDF2TEXT_BASE_URL')) { | ||
$apiBaseUrl = constant('PDF2TEXT_BASE_URL'); | ||
} | ||
return new Pdf2TxtClient($apiBaseUrl); | ||
} | ||
} |
Oops, something went wrong.