Skip to content

Commit

Permalink
Improve test coverate to PdfParser
Browse files Browse the repository at this point in the history
  • Loading branch information
vitormattos authored and backportbot[bot] committed May 5, 2022
1 parent 24ce51d commit 3bf8ee7
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Command/Configure/Cfssl.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$configPath = $this->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 3bf8ee7

Please sign in to comment.