Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests to Pest #57

Merged
merged 6 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest

- name: Execute tests
run: vendor/bin/phpunit
run: vendor/bin/pest
14 changes: 10 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
}
],
"require": {
"php" : "^7.4|^8.0",
"php": "^7.4|^8.0",
"symfony/process": "^4.0|^5.0|^6.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"pestphp/pest-plugin-laravel": "^1.3"
},
"autoload": {
"psr-4": {
Expand All @@ -33,6 +34,11 @@
}
},
"scripts": {
"test": "phpunit"
"test": "pest"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
}
217 changes: 81 additions & 136 deletions tests/PdfToTextTest.php
Original file line number Diff line number Diff line change
@@ -1,150 +1,95 @@
<?php

namespace Spatie\PdfToText\Test;

use PHPUnit\Framework\TestCase;
use Spatie\PdfToText\Exceptions\CouldNotExtractText;
use Spatie\PdfToText\Exceptions\PdfNotFound;
use Spatie\PdfToText\Pdf;
use Symfony\Component\Process\Exception\InvalidArgumentException;

class PdfToTextTest extends TestCase
{
protected $dummyPdf = __DIR__.'/testfiles/dummy.pdf';
protected $dummyPdfText = 'This is a dummy PDF';

/**
* @var string
*/
private $pdftotextPath;

protected function setUp(): void
{
parent::setUp();

if (file_exists(__DIR__ . "/config.php")) {
$config = include __DIR__ . "/config.php";

$this->pdftotextPath = isset($config["pdftotextPath"])
? $config["pdftotextPath"]
: null;
}
}

/** @test */
public function it_can_extract_text_from_a_pdf()
{
$text = (new Pdf($this->pdftotextPath))
->setPdf($this->dummyPdf)
->text();

$this->assertSame($this->dummyPdfText, $text);
}

/** @test */
public function it_provides_a_static_method_to_extract_text()
{
$this->assertSame($this->dummyPdfText, Pdf::getText($this->dummyPdf, $this->pdftotextPath));
}

/** @test */
public function it_can_handle_paths_with_spaces()
{
$pdfPath = __DIR__.'/testfiles/dummy with spaces in its name.pdf';

$this->assertSame($this->dummyPdfText, Pdf::getText($pdfPath, $this->pdftotextPath));
}

/** @test */
public function it_can_handle_paths_with_single_quotes()
{
$pdfPath = __DIR__.'/testfiles/dummy\'s_file.pdf';

$this->assertSame($this->dummyPdfText, Pdf::getText($pdfPath, $this->pdftotextPath));
}

/** @test */
public function it_can_handle_pdftotext_options_without_starting_hyphen()
{
$text = (new Pdf($this->pdftotextPath))
->setPdf(__DIR__.'/testfiles/scoreboard.pdf')
->setOptions(['layout', 'f 1'])
->text();

$this->assertStringContainsString("Charleroi 50 28 13 11 4", $text);
}

/** @test */
public function it_can_handle_pdftotext_options_with_starting_hyphen()
{
$text = (new Pdf($this->pdftotextPath))
->setPdf(__DIR__.'/testfiles/scoreboard.pdf')
->setOptions(['-layout', '-f 1'])
->text();

$this->assertStringContainsString("Charleroi 50 28 13 11 4", $text);
}

/** @test */
public function it_can_handle_pdftotext_options_with_mixed_hyphen_status()
{
$text = (new Pdf($this->pdftotextPath))
->setPdf(__DIR__.'/testfiles/scoreboard.pdf')
->setOptions(['-layout', 'f 1'])
->text();

$this->assertStringContainsString("Charleroi 50 28 13 11 4", $text);
}

/** @test */
public function it_will_throw_an_exception_when_the_pdf_is_not_found()
{
$this->expectException(PdfNotFound::class);
uses(PHPUnit\Framework\TestCase::class);

(new Pdf($this->pdftotextPath))
->setPdf('/no/pdf/here/dummy.pdf')
->text();
}
beforeEach(function () {
$this->dummyPdf = __DIR__ . '/testfiles/dummy.pdf';
$this->dummyPdfText = 'This is a dummy PDF';

/** @test */
public function it_will_throw_an_exception_when_the_binary_is_not_found()
{
$this->expectException(CouldNotExtractText::class);
$this->pdftotextPath = PHP_OS === 'Linux'
? '/usr/bin/pdftotext'
: '/opt/homebrew/bin/pdftotext';

(new Pdf('/there/is/no/place/like/home/pdftotext'))
->setPdf($this->dummyPdf)
->text();
}
if (file_exists(__DIR__ . "/config.php")) {
$config = include __DIR__ . "/config.php";

/** @test */
public function it_will_throw_an_exception_when_the_option_is_unknown()
{
$this->expectException(CouldNotExtractText::class);
Pdf::getText($this->dummyPdf, $this->pdftotextPath, ['-foo']);
$this->pdftotextPath = isset($config["pdftotextPath"])
? $config["pdftotextPath"]
: null;
}

/** @test */
public function it_allows_for_options_to_be_added_programatically_without_overriding_previously_added_options()
{
$text = (new Pdf($this->pdftotextPath))
->setPdf(__DIR__.'/testfiles/multi_page.pdf')
->setOptions(['-layout', '-f 2'])
->addOptions(['-l 2'])
->text();

$this->assertStringContainsString("This is page 2", $text);
$this->assertStringNotContainsString("This is page 1", $text);
$this->assertStringNotContainsString("This is page 3", $text);
}

/** @test */
public function it_will_throw_an_exception_when_timeout_is_a_negative_number()
{
$this->expectException(InvalidArgumentException::class);

$text = (new Pdf($this->pdftotextPath))
});

it('can extract text from a pdf', function () {
$text = (new Pdf($this->pdftotextPath))
->setPdf($this->dummyPdf)
->text();

expect($text)->toBe($this->dummyPdfText);
});

it('provides a static method to extract text', function () {
expect(Pdf::getText($this->dummyPdf, $this->pdftotextPath))
->toBe($this->dummyPdfText);
});

it('can handle paths', function (string $path) {
$pdfPath = __DIR__ . $path;

expect(Pdf::getText($pdfPath, $this->pdftotextPath))->toBe($this->dummyPdfText);
})->with([
'with spaces' => '/testfiles/dummy with spaces in its name.pdf',
'with single quotes' => '/testfiles/dummy\'s_file.pdf'
]);

it('can handle pdftotext options', function (array $options) {
$text = (new Pdf($this->pdftotextPath))
->setPdf(__DIR__ . '/testfiles/scoreboard.pdf')
->setOptions($options)
->text();

expect($text)->toContain("Charleroi 50 28 13 11 4");
})->with([
'without starting hyphen' => fn () => ['layout', 'f 1'],
'with starting hyphen' => fn () => ['-layout', '-f 1'],
'with mixed hyphen status' => fn () => ['-layout', 'f 1']
]);

it('will throw an exception when the PDF is not found', function () {
(new Pdf($this->pdftotextPath))
->setPdf('/no/pdf/here/dummy.pdf')
->text();
})->throws(PdfNotFound::class);

it('will throw an exception when the binary is not found', function () {
(new Pdf('/there/is/no/place/like/home/pdftotext'))
->setPdf($this->dummyPdf)
->text();
})->throws(CouldNotExtractText::class);

it('will throw an exception when the option is unknown')
->tap(fn () => Pdf::getText($this->dummyPdf, $this->pdftotextPath, ['-foo']))
->throws(CouldNotExtractText::class);

it('allows for options to be added programatically without overriding previously added options', function () {
$text = (new Pdf($this->pdftotextPath))
->setPdf(__DIR__ . '/testfiles/multi_page.pdf')
->setOptions(['-layout', '-f 2'])
->addOptions(['-l 2'])
->text();

expect($text)->toContain("This is page 2")
->not->toContain("This is page 1", "This is page 3");
});

it('will throw an exception when timeout is a negative number')
->tap(
fn () => (new Pdf($this->pdftotextPath))
->setPdf($this->dummyPdf)
->setTimeout(-1)
->text();
}
}
->text()
)->throws(InvalidArgumentException::class);