Skip to content

Commit 55884fa

Browse files
committed
chore: mock exec function
Signed-off-by: Vitor Mattos <vitor@php.rio>
1 parent e20f6ab commit 55884fa

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

src/Sign/JSignService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function sign(JSignParam $params)
2424
$this->validation($params);
2525

2626
$commandSign = $this->commandSign($params);
27-
\exec($commandSign, $output);
27+
exec($commandSign, $output);
2828

2929
$out = json_encode($output);
3030
$messageSuccess = "Finished: Signature succesfully created.";
@@ -84,7 +84,7 @@ public function getVersion(JSignParam $params)
8484
return explode('version ', $lastRow)[1];
8585
}
8686

87-
public function validation(JSignParam $params)
87+
private function validation(JSignParam $params)
8888
{
8989
$this->throwIf(empty($params->getTempPath()) || !is_writable($params->getTempPath()), 'Temp Path is invalid or has not permission to writable.');
9090
$this->throwIf(empty($params->getPdf()), 'PDF is Empty or Invalid.');

tests/JSignPDFTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
<?php
22

3+
namespace Jeidison\JSignPDF\Sign;
4+
5+
function exec(string $command, array &$output = null, int &$return_var = null) {
6+
global $mockExec;
7+
if ($mockExec) {
8+
$output = $mockExec;
9+
return $output;
10+
}
11+
return \exec($command, $output, $return_var);
12+
}
13+
314
namespace Jeidison\JSignPDF\Tests;
415

516
use Exception;
@@ -16,6 +27,8 @@ class JSignPDFTest extends TestCase
1627

1728
protected function setUp(): void
1829
{
30+
global $mockExec;
31+
$mockExec = null;
1932
$this->service = new JSignService();
2033
}
2134

@@ -57,17 +70,21 @@ public function testSignSuccess()
5770

5871
/**
5972
* @dataProvider providerSignUsingDifferentPasswords
60-
* @doesNotPerformAssertions
6173
*/
6274
public function testSignUsingDifferentPasswords(string $password)
6375
{
76+
global $mockExec;
6477
if (!class_exists('JSignPDF\JSignPDFBin\JavaCommandService')) {
6578
$this->markTestSkipped('Install jsignpdf/jsignpdf-bin');
6679
}
6780
$params = JSignParamBuilder::instance()->withDefault();
6881
$params->setCertificate($this->getNewCert($password));
6982
$params->setPassword($password);
70-
$this->service->validation($params);
83+
$mockExec = 'Finished: Signature succesfully created.';
84+
$path = $params->getTempPdfSignedPath();
85+
file_put_contents($path, 'dummy');
86+
$fileSigned = $this->service->sign($params);
87+
$this->assertNotNull($fileSigned);
7188
}
7289

7390
public function providerSignUsingDifferentPasswords()

0 commit comments

Comments
 (0)