Skip to content

Commit

Permalink
chore(tests): update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lsnepomuceno committed Sep 7, 2024
1 parent b75ea4d commit 45845b3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
42 changes: 19 additions & 23 deletions tests/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,34 @@ public function testWhenTheSignatureCommandIsSuccessfullyCompleted()
{
$cert = new ManageCert;
list($pfxPath, $pass) = $cert->makeDebugCertificate(true);
$pdfPath = __DIR__ . '/Resources/test.pdf';
$fileName = a1TempDir(true, '.pdf');
$pdfPath = __DIR__ . '/Resources/test.pdf';
$fileName = a1TempDir(true, '.pdf');
$parameters = [
'pdfPath' => $pdfPath,
'pfxPath' => $pfxPath,
'pdfPath' => $pdfPath,
'pfxPath' => $pfxPath,
'password' => $pass,
'fileName' => $fileName
];

$this->artisan('pdf:sign', $parameters)
->assertSuccessful()
->expectsOutput('Your PDF file is being signed!')
->expectsOutput("Your file has been signed and is available at: \"{$fileName}\"");

File::delete([$pfxPath, $fileName]);
->assertSuccessful()
->expectsOutput('Your PDF file is being signed!')
->expectsOutput("Your file has been signed and is available at: \"{$fileName}\"");
}

public function testWhenTheSignatureCommandDoesNotFinishSuccessfully()
{
$parameters = [
'pdfPath' => a1TempDir(true, '.pdf'),
'pfxPath' => a1TempDir(true, '.pfx'),
'pdfPath' => a1TempDir(true, '.pdf'),
'pfxPath' => a1TempDir(true, '.pfx'),
'password' => Str::random(32),
'fileName' => a1TempDir(true, '.pdf')
];

$this->artisan('pdf:sign', $parameters)
// ->assertFailed()
->expectsOutput('Your PDF file is being signed!')
->expectsOutputToContain('Could not sign your file, error occurred:');
->expectsOutput('Your PDF file is being signed!')
->expectsOutputToContain('Could not sign your file, error occurred:');

}

Expand All @@ -72,7 +70,7 @@ public function testWhenASignedPdfIsSuccessfullyValidated()
$cert = new ManageCert;
list($pfxPath, $pass) = $cert->makeDebugCertificate(true);

$signed = signPdfFromFile($pfxPath, $pass, __DIR__ . '/Resources/test.pdf');
$signed = signPdfFromFile($pfxPath, $pass, __DIR__ . '/Resources/test.pdf');
$pdfPath = a1TempDir(true, '.pdf');

File::put($pdfPath, $signed);
Expand All @@ -85,23 +83,21 @@ public function testWhenASignedPdfIsSuccessfullyValidated()
];

$this->artisan('pdf:validate-signature', $parameters)
->assertSuccessful()
->expectsOutput('Your PDF document is being validated.')
->expectsOutput('Your PDF document is VALID');

File::delete([$pfxPath, $pdfPath]);
->assertSuccessful()
->expectsOutput('Your PDF document is being validated.')
->expectsOutput('Your PDF document is VALID');
}

public function testWhenAnUnsignedDocumentThrowsAnErrorWhenRunningAValidationCommand()
{
$pdfPath = __DIR__ . '/Resources/test.pdf';
$pdfPath = __DIR__ . '/Resources/test.pdf';
$parameters = [
'pdfPath' => $pdfPath
];

$this->artisan('pdf:validate-signature', $parameters)
->assertFailed()
->expectsOutput('Your PDF document is being validated.')
->expectsOutputToContain('Unable to validate your file signature, an error occurred:');
->assertFailed()
->expectsOutput('Your PDF document is being validated.')
->expectsOutputToContain('Unable to validate your file signature, an error occurred:');
}
}
5 changes: 0 additions & 5 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public function testWhenAFileIsSignedByTheSignPdfFromUploadHelperUsingPathEnv()
$fileExists = File::exists($pdfPath);

$this->assertTrue($fileExists);
File::delete([$pfxPath, $pdfPath]);
}

/**
Expand All @@ -143,8 +142,6 @@ public function testWhenCertificateDataIsEncrypted()
foreach (['certificate', 'password', 'hash'] as $key) {
$this->assertArrayHasKey($key, $encryptedData->toArray());
}

File::delete([$pfxPath]);
}

public function testWhenTheA1TempDirHelperCreatesTheFilesCorrectly()
Expand Down Expand Up @@ -189,7 +186,5 @@ public function testWhenASignedPdfFileIsCorrectlyValidatedByTheValidatePdfSignat

$validation = validatePdfSignature($pdfPath);
$this->assertTrue($validation->isValidated);

File::delete([$pfxPath, $pdfPath]);
}
}
10 changes: 5 additions & 5 deletions tests/SealImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LSNepomuceno\LaravelA1PdfSign\Tests;

use Illuminate\Support\Facades\File;
use Intervention\Image\Drivers\Gd\Driver as GDDriver;
use Intervention\Image\ImageManager as IMG;
use LSNepomuceno\LaravelA1PdfSign\Exceptions\CertificateOutputNotFoundException;
use LSNepomuceno\LaravelA1PdfSign\Exceptions\FileNotFoundException;
Expand Down Expand Up @@ -34,10 +35,10 @@ public function testGenerateImageFromCertFile()

$image = SealImage::fromCert($cert);

$interventionImg = new IMG(['driver' => SealImage::IMAGE_DRIVER_GD]);
$interventionImg = $interventionImg->make($image);
$interventionImg = new IMG(driver: new GDDriver);
$interventionImg = $interventionImg->read($image);

$this->assertEqualsIgnoringCase('image/png', $interventionImg->mime());
$this->assertEqualsIgnoringCase('image/png', $interventionImg->toPng()->mediaType());
$this->assertEquals(590, $interventionImg->width());
$this->assertEquals(295, $interventionImg->height());
}
Expand Down Expand Up @@ -66,8 +67,7 @@ public function testInsertSealImageOnPdfFile()
$pdfPath = a1TempDir(true, '.pdf');
try {
$pdf = new SignaturePdf(__DIR__ . '/Resources/test.pdf', $cert);
$resource = $pdf->setImage($imagePath)
->signature();
$resource = $pdf->setImage($imagePath)->signature();
File::put($pdfPath, $resource);
} catch (Throwable $e) {
throw new $e;
Expand Down
16 changes: 16 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@

namespace LSNepomuceno\LaravelA1PdfSign\Tests;

use Illuminate\Support\Facades\File;
use LSNepomuceno\LaravelA1PdfSign\LaravelA1PdfSignServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{
protected function tearDown(): void
{
$path = dirname(__DIR__) . '/src/Temp/';
if (File::exists($path)) {
$files = File::files($path);

foreach ($files as $file) {
if ($file->getFilename() !== '.gitkeep') {
File::delete($file->getPathname());
}
}
}
parent::tearDown();
}

protected function getPackageProviders($app): array
{
return [
Expand Down

0 comments on commit 45845b3

Please sign in to comment.