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

Converting PHPUnit to Pest tests #189

Merged
merged 1 commit into from
Mar 8, 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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"ext-imagick" : "*"
},
"require-dev": {
"phpunit/phpunit" : "^9.4"
"pestphp/pest": "^1.21"
},
"autoload": {
"psr-4": {
Expand All @@ -36,6 +36,6 @@
}
},
"scripts": {
"test": "phpunit"
"test": "vendor/bin/pest"
}
}
199 changes: 75 additions & 124 deletions tests/PdfTest.php
Original file line number Diff line number Diff line change
@@ -1,129 +1,80 @@
<?php

namespace Spatie\PdfToImage\Test;

use Imagick;
use PHPUnit\Framework\TestCase;
use Spatie\PdfToImage\Pdf;
use Spatie\PdfToImage\Exceptions\InvalidFormat;
use Spatie\PdfToImage\Exceptions\PageDoesNotExist;
use Spatie\PdfToImage\Exceptions\PdfDoesNotExist;
use Spatie\PdfToImage\Pdf;
use Spatie\PdfToImage\Exceptions\PageDoesNotExist;

beforeEach(function () {
$this->testFile = __DIR__.'/files/test.pdf';
$this->multipageTestFile = __DIR__.'/files/multipage-test.pdf';
$this->remoteFileUrl = 'https://tcd.blackboard.com/webapps/dur-browserCheck-BBLEARN/samples/sample.pdf';
});


it('will throw an exception when try to convert a non existing file', function () {
new Pdf('pdfdoesnotexists.pdf');
})->throws(PdfDoesNotExist::class);

it('will throw an exception when trying to convert an invalid file type', function () {
(new Pdf($this->testFile))->setOutputFormat('bla');
})->throws(InvalidFormat::class);

it('will throw an exception when passed an invalid page number', function ($invalidPage) {
(new Pdf($this->testFile))->setPage(100);
})
->throws(PageDoesNotExist::class)
->with([5, 0, -1]);

it('will correctly return the number of pages in pdf file', function () {
$pdf = new Pdf($this->multipageTestFile);

expect($pdf->getNumberOfPages())->toEqual(3);
});

it('will accept a custom specified resolution', function () {
$image = (new Pdf($this->testFile))
->setResolution(150)
->getImageData('test.jpg')
->getImageResolution();

expect($image['x'])->toEqual(150);
expect($image['y'])->toEqual(150);
});

it('will convert a specified page', function () {
$imagick = (new Pdf($this->multipageTestFile))
->setPage(2)
->getImageData('page-2.jpg');

expect($imagick)->toBeInstanceOf(Imagick::class);
});

it('will accpect a specified file type and convert to it', function () {
$imagick = (new Pdf($this->testFile))
->setOutputFormat('png')
->getImageData('test.png');

expect($imagick->getFormat())->toEqual('png');
expect($imagick->getFormat())->not->toEqual('jpg');
});

it('can accepct a layer', function () {
$image = (new Pdf($this->testFile))
->setLayerMethod(Imagick::LAYERMETHOD_FLATTEN)
->setResolution(72)
->getImageData('test.jpg')
->getImageResolution();

expect($image['x'])->toEqual(72);
expect($image['y'])->toEqual(72);
});

it('will set compression quality', function () {
$imagick = (new Pdf($this->testFile))
->setCompressionQuality(99)
->getImageData('test.jpg');

expect($imagick->getCompressionQuality())->toEqual(99);
});

class PdfTest extends TestCase
{
/** @var string */
protected $testFile;

/** @var string */
protected $multipageTestFile;

/** @var string */
protected $remoteFileUrl;

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

$this->testFile = __DIR__.'/files/test.pdf';

$this->multipageTestFile = __DIR__.'/files/multipage-test.pdf';

$this->remoteFileUrl = 'https://tcd.blackboard.com/webapps/dur-browserCheck-BBLEARN/samples/sample.pdf';
}

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

new Pdf('pdfdoesnotexists.pdf');
}

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

(new Pdf($this->testFile))->setOutputFormat('bla');
}

/**
* @test
* @dataProvider invalid_page_number_provider
*/
public function it_will_throw_an_exception_when_passed_an_invalid_page($invalidPage)
{
$this->expectException(PageDoesNotExist::class);

(new Pdf($this->testFile))->setPage($invalidPage);
}

/** @test */
public function it_will_correctly_return_the_number_of_pages_in_pdf_file()
{
$pdf = new Pdf($this->multipageTestFile);

$this->assertTrue($pdf->getNumberOfPages() === 3);
}

/** @test */
public function it_will_accept_a_custom_specified_resolution()
{
$image = (new Pdf($this->testFile))
->setResolution(150)
->getImageData('test.jpg')
->getImageResolution();

$this->assertEquals($image['x'], 150);
$this->assertEquals($image['y'], 150);
}

/** @test */
public function it_will_convert_a_specified_page()
{
$imagick = (new Pdf($this->multipageTestFile))
->setPage(2)
->getImageData('page-2.jpg');

$this->assertInstanceOf('Imagick', $imagick);
}

/** @test */
public function it_will_accept_a_specified_file_type_and_convert_to_it()
{
$imagick = (new pdf($this->testFile))
->setOutputFormat('png')
->getImageData('test.png');

$this->assertSame($imagick->getFormat(), 'png');
$this->assertNotSame($imagick->getFormat(), 'jpg');
}

/** @test */
public function it_can_accept_a_layer()
{
$image = (new Pdf($this->testFile))
->setLayerMethod(Imagick::LAYERMETHOD_FLATTEN)
->setResolution(72)
->getImageData('test.jpg')
->getImageResolution();

$this->assertEquals($image['x'], 72);
$this->assertEquals($image['y'], 72);
}

/** @test */
public function it_will_set_compression_quality()
{
$imagick = (new Pdf($this->testFile))
->setCompressionQuality(99)
->getImageData('test.jpg');

$this->assertEquals(99, $imagick->getCompressionQuality());
}

public function invalid_page_number_provider()
{
return [[5], [0], [-1]];
}
}