Skip to content

Commit

Permalink
Merge pull request #800 from LibreSign/feature/add-tests
Browse files Browse the repository at this point in the history
Improve test coverate to PdfParser
  • Loading branch information
vitormattos authored May 5, 2022
2 parents 7418adb + 0089214 commit 49464ff
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Types of changes:
- *Security* in case of vulnerabilities.

<!-- changelog-linker -->
## 6.1.2 - development

# Added
- Improve test coverate to PdfParser

## 6.1.1 - 2022-05-05

# Fixed
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Configure/Cfssl.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$configPath = $this->installService->getFullPath() . DIRECTORY_SEPARATOR . 'cfssl_config' . DIRECTORY_SEPARATOR;
$cfsslUri = 'http://127.0.0.1:8888/api/v1/cfssl/';
} else {
$output->writeln('CFSSL binary not found!');
$output->writeln('<info>CFSSL binary not found! run libresign:istall --cfssl first.</info>');
if (!$configPath = $input->getOption('config-path')) {
throw new InvalidArgumentException('Invalid config path');
}
Expand Down
9 changes: 9 additions & 0 deletions lib/Service/PdfParserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use OC\SystemConfig;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Exception\LibresignException;
use OCP\IConfig;

class PdfParserService {
Expand Down Expand Up @@ -31,10 +32,18 @@ private function getDataDir(): string {
return $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT . '/data/');
}

/**
* @param string $filePath
* @return array
* @throws LibresignException
*/
public function getMetadata(string $filePath): array {
$fullPath = $this->getDataDir() . $filePath;
$json = shell_exec($this->cliPath . ' info ' . $fullPath);
$array = json_decode($json, true);
if (!is_array($array)) {
throw new LibresignException('Impossible get metadata from this file. Check if you installed correctly the libresign-cli.');
}
$output = [
'p' => count($array['pages']),
];
Expand Down
90 changes: 90 additions & 0 deletions tests/Unit/Service/PdfParseServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace OCA\Libresign\Tests\Unit\Service;

use OC\SystemConfig;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Service\InstallService;
use OCA\Libresign\Service\PdfParserService;
use OCP\IConfig;
use PHPUnit\Framework\MockObject\MockObject;

/**
* @internal
*/
final class PdfParseServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
/** @var IConfig|MockObject */
private $config;
/** @var SystemConfig|MockObject */
private $systemConfig;
/** @var InstallService|MockObject */
private $installService;

public function setUp(): void {
$this->config = $this->createMock(IConfig::class);
$this->systemConfig = $this->createMock(SystemConfig::class);
$this->installService = $this->createMock(InstallService::class);
}

private function getService(): PdfParserService {
return new PdfParserService(
$this->config,
$this->systemConfig,
$this->installService
);
}

public function testGetMetadataWithFail(): void {
$this->expectException(LibresignException::class);
$this->config
->method('getAppValue')
->willReturnCallback(function ($appid, $key, $default) {
switch ($key) {
case 'libresign_cli_path': return '/fake_path/';
}
});
$this->systemConfig
->method('getValue')
->willReturnCallback(function ($key, $default) {
switch ($key) {
case 'datadirectory': return $default;
}
});
$path = '/fake';
$this->getService()->getMetadata($path);
}

/**
* @dataProvider providerGetMetadataWIthSuccess
*/
public function testGetMetadataWIthSuccess(string $path, array $expected): void {
$this->installService = \OC::$server->get(InstallService::class);
$this->systemConfig = \OC::$server->get(SystemConfig::class);
$this->config = \OC::$server->get(IConfig::class);
$actual = $this->getService()->getMetadata($path);
$this->assertEquals($expected, $actual);
}

public function providerGetMetadataWIthSuccess(): array {
return [
[
'/../apps/libresign/tests/fixtures/small_valid.pdf',
[
'p' => 1,
'd' => [
['w' => 595.276, 'h' => 841.89],
],
]
],
[
'/../apps/libresign/tests/fixtures/small_valid-signed.pdf',
[
'p' => 1,
'd' => [
['w' => 595.276, 'h' => 841.89],
],
]
],
];
}
}

0 comments on commit 49464ff

Please sign in to comment.